由于页面对象序列化不正确,Cassandra分页不起作用

时间:2018-10-29 20:09:18

标签: cassandra

我正在尝试实现对cassandra查询结果集的简单分页。我正在尝试关注本文http://niels.nu/blog/2016/cassandra-resultset-paging.html。这是我的Web服务的样子:

@GET
@Path("products")
@Produces(MediaType.APPLICATION_JSON)

public Response resources(@QueryParam(value = "next") String next,@QueryParam(value = "size") String size) {

    Cluster cluster = null;
    Session session = null;

    System.out.println("Next is "+ next);
    System.out.println("Size is "+ size);

    int nextValue =Integer.parseInt(next);
    int sizeValue =Integer.parseInt(size);


    final Cluster.Builder clusterBuilder = Cluster.builder()
            .addContactPoint("127.0.0.1").withPort(9042)
            .withCredentials("cassandra", "cassandra");
    cluster = clusterBuilder.build();
    session = cluster.connect("paging");

    CassandraPaging cassandraPaging = new CassandraPaging(session);


    return Response.status(Status.OK.getStatusCode()).entity(cassandraPaging.getProductsWithArgs(next, size))
            .type(MediaType.APPLICATION_JSON).build();

}

以下是我在getProductsWithArgs(next,size)方法中所拥有的:

public ResultSet getResourcesWithArgs(String next, String size){

    ResultSet result =null;
    String savingPageState =null;
    boolean isEnd = false;

    Select selectAll =QueryBuilder.select().all().from("paging", "users");
    selectAll.setFetchSize(3);

    List<Row> pageList = new ArrayList<Row>();

    if (next != null) {
        selectAll.setPagingState(
            PagingState.fromString(savingPageState));
    } 
    result =session.execute(selectAll);

    PagingState pagingState = result.getExecutionInfo()
            .getPagingState();
    if (null != pagingState) {
        savingPageState = result.getExecutionInfo().getPagingState()
                .toString();
    }

    System.out.println("Paging state "+ pagingState);
    if (result.isFullyFetched() && null == pagingState) {
        // if hit the end more than once, then nothing to return,
        // otherwise, mark the isEnd to 'true'
        if (true == isEnd) {
            return null;
        } else {
            isEnd = true;
        }
    }

    List<Row> rowsList =getRows(result, 4, 3);
    printUser(rowsList);
    return result;


}

当我尝试访问Web服务时:

http://localhost:8080/PagingService/cass/app/resources?next=4&size=3

我不断收到以下异常:

com.datastax.driver.core.exceptions.PagingStateException: Cannot deserialize 
paging state, invalid format. The serialized form was corrupted, or not 
initially generated from a PagingState object.
com.datastax.driver.core.PagingState.fromString(PagingState.java:148)
com.devjavasource.cassandra.PagingExample.CassandraPaging.getResourcesWithAr gs(CassandraPaging.java:58)
com.devjavasource.cassandra.PagingExample.PagingWebService.resources(PagingWebService.java:44)

尽管我已注释掉setPagingState的代码,然后

savingPageState = result.getExecutionInfo().getPagingState()
            .toString();

确实获得了寻呼状态的有效值。有人可以帮我解决我在这里做错什么吗?非常感谢。谢谢。

1 个答案:

答案 0 :(得分:0)

看起来像这行 PagingState.fromString(savingPageState)); 正在尝试从空字符串创建PagingState对象。那是你打算的吗?