提高Cassandra和Java集合的性能 [英] Improve performance in Cassandra and java collections

查看:250
本文介绍了提高Cassandra和Java集合的性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们在项目中使用NoSQL(Cassandra)。我们有一个表A(5000条记录),这是一个主表。我们有另一张表B(2000条记录)。表B有4列,表A有25列。我们公开了一个REST服务来获取B的所有记录;喜欢/ service / getB。该服务将返回6列作为响应 -

We are using NoSQL (Cassandra) in our project. We have a Table A (5000 records) which is a master table. We have another Table B (2000 records). Table B have 4 columns and Table A have 25 columns. We exposed a REST service to get all records from B; like /service/getB. This service will return 6 columns in response as –

{
    "result": [
        {
            "col1FromB": "1B",
            "col2FromB": "2B",
            "col3FromB": "3B",
            "col4FromB": "4B",
            "col1FromA": "1A",
            "col2FromA": "2A"
        },
        {
            "col1FromB": "11B",
            "col2FromB": "12B",
            "col3FromB": "13B",
            "col4FromB": "14B",
            "col1FromA": "11A",
            "col2FromA": "12A"
        }
    ]
}

所以,有查看表A中表B中每个项目的查询。这就是我的工作方式 -

So, there is a look up query to Table A for each item in Table B. This is how I am doing it –

    //Get all from Table B (took 90 ms in Local and 30 ms in Test)
    Select select = QueryBuilder.select().from("B");
    List<B> bList = cassandraOperations.select(select, B.class);

    //Loop through bList and do a lookup using id in Table A (took 46000 ms (46 sec) in Local (horrible) and 6000 ms (6 sec) in Test)
    For(B b: bList) {
    Select select = QueryBuilder.select("col1FromA", "col2FromA").from("A");
    select.where(QueryBuilder.eq("id", b.getId()));
    A a = cassandraOperations.selectOne(select, A.class);

    ----
    ----
    //Prepare final Pojo with a and b objects and add into a List<finalPjo> and return
}

因此,本地环境中的查找时间非常长,而且相当在测试环境中不好。我所使用的只是Java集合。

So, the lookup time is very high in Local environment and also quite not good in Test environment. All I am using is Java collections only.

有没有办法让它更好,以便我们在更短的时间内获得记录。

Is there any way to make it better so that we get records in lesser time.

推荐答案

For(B b: bList) {
 Select select = QueryBuilder.select("col1FromA", "col2FromA").from("A");
 select.where(QueryBuilder.eq("id", b.getId()));
 A a = cassandraOperations.selectOne(select, A.class);

此代码执行阻止请求 cassandraOperations.selectOne 在每次迭代中,这意味着每次下一次迭代都必须等待前一次迭代。所有2000个请求将一个接一个地执行很长一段时间。

This code performs blocking request cassandraOperations.selectOnein each iteration, it means that each next iteration have to wait the previous one. All 2000 requests will be executed one by one and for a long time.

为避免这种情况,请使用异步方式获取循环中的记录(正如我所见,您使用的是Spring,selectOne可以替换为 selectOneAsyncchronously 返回ResultSetFuture,将这些期货保存在某个列表中,并在发送所有请求时使用它来检索记录) 。

To avoid this, use asynchronous way to get records in the loop (as I see, you are using Spring and selectOne can be replaced by selectOneAsynchronously which returns ResultSetFuture, save these futures in some list, and use it to retrieve records when all requests are sent).

这篇关于提高Cassandra和Java集合的性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆