有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

CQL中语句的java分页状态

我试图理解PagingState如何使用Cassandra中的语句。我尝试使用一个示例,该示例将几个1000条记录插入到数据库中,并尝试在fetch size设置为10并使用分页状态的情况下从数据库中读取相同的记录。这很好用。以下是我的junit代码示例:

@Before
public void setup() {
    cassandraTemplate.executeQuery("create table if not exists pagesample(a int, b int, c int, primary key(a,b))");
    String insertQuery = "insert into pagesample(a,b,c) values(?,?,?)";
    PreparedStatement insertStmt = cassandraTemplate.getConnection().prepareStatement(insertQuery);
    for(int i=0; i < 5; i++){
        for(int j=100; j<1000; j++){
            cassandraTemplate.executeQuery(insertStmt, new Object[]{i, j, RandomUtils.nextInt()});
        }
    }
}

@Test
public void testPagination() {
    String selectQuery = "select * from pagesample where a=?";
    String pagingStateStr = null;
    for(int run=0; run<90; run++){
        ResultSet resultSet = selectRows(selectQuery, 10, pagingStateStr, 1);
        int fetchedCount = resultSet.getAvailableWithoutFetching();
        System.out.println(run+". Fetched size: "+fetchedCount);
        for(Row row : resultSet){
            System.out.print(row.getInt("b")+", ");
            if(--fetchedCount == 0){
                break;
            }
        }
        System.out.println();

        PagingState pagingState = resultSet.getExecutionInfo().getPagingState();
        pagingStateStr =  pagingState.toString();
    }
}

public ResultSet selectRows(String cql, int fetchSize, String pagingState, Object... bindings){
    SimpleStatement simpleStatement = new SimpleStatement(cql, bindings);
    statement.setFetchSize(fetchSize);
    if(StringUtils.isNotEmpty(pagingState)){
        statement.setPagingState(PagingState.fromString(pagingState));
    }
    return getSession().execute(simpleStatement);
}

当我执行这个程序时,我看到testPagination中的每个迭代都精确地打印10条记录。但文件上说:

  • 请注意,设置提取大小并不意味着Cassandra将 始终返回准确的行数,可能是 返回稍多或略少的结果

我真的无法理解为什么Cassandra返回的行数与fetch size中指定的行数不完全相同。当查询中没有提供where子句时,是否就是这种情况?当查询被限制在分区键上时,它会返回确切的记录数吗?请澄清


共 (1) 个答案

  1. # 1 楼答案

    CQL protocol specification开始:

    Clients should also not assert that no result will have more than result_page_size results. While the current implementation always respect the exact value of result_page_size, we reserve ourselves the right to return slightly smaller or bigger pages in the future for performance reasons

    因此,最好总是依赖getAvailableWithoutFetching而不是页面大小,以防Cassandra将来更改其实现