从春天获取Cassandra表中的行

时间:2016-02-24 18:33:14

标签: java spring cassandra cassandra-2.0 datastax

你好我试图从Cassandra表中获取10行数据。但是在每个请求中,相同的10行正在返回。请在这里查看我的逻辑。并建议我在这里做错了什么 -

 public class CustomerRequestDaoImpl implements CustomerRequestDao
 {
  private static Cluster cluster;

 @Resource
  private CassandraSessionFactory cassandraSessionFactory;

   /** The ProductsByTagDaoImpl session. */
    private Session session;

  @Override
   public List<CustomerRequest> getCustomerRequestData(final String productId, final String receiptPeriod)
  {

   final int RESULTS_PER_PAGE = 10;

   session = cassandraSessionFactory.getSession();
   final List<CustomerRequest> customerRequestdata = new ArrayList<CustomerRequest>();
   try
   {
     final PreparedStatement statement =
      session.prepare("select * from customer_request where product_id = :id and receipt_period = :receiptPeriod");

     final BoundStatement boundStatement = new BoundStatement(statement);
     boundStatement.setFetchSize(RESULTS_PER_PAGE);
     boundStatement.setString("id", productId);
     boundStatement.setString("receiptPeriod", receiptPeriod);
     final ResultSet resultSet = session.execute(boundStatement);
     final Iterator<Row> iter = resultSet.iterator();
     final PagingState nextPage = resultSet.getExecutionInfo().getPagingState();

     int remaining = resultSet.getAvailableWithoutFetching();
     for (final Row rowdt : resultSet)
     {
      customerRequestdata.add(constructCustomerReq(rowdt));
      if (--remaining == 0)
       {
         break;
       }
      }

    }
   catch (final Exception e)
    {
      e.printStackTrace();
    }
   return customerRequestdata;
  }

 @PostConstruct
  public void init()
  {
    session = cassandraSessionFactory.getSession();
    cluster = session.getCluster();

  }

}

我的表 -        我的表结构: -

 CREATE TABLE customer_request (
  product_id varchar PRIMARY KEY,
  id varchar,
  receipt_period varchar,
  delivery_method_status varchar,
  first_name varchar
  );

返回回复 -

  <e>
  <deliveryMethodStatus null="true"/>
  <firstName null="true"/>
  <id>0b0352f6b3904</id>
  <lastName Adkin="true"/>
  <orderId>FORMS8a04e</orderId>
  <orderItemId>FORMS8a04e-1</orderItemId>
  <productId>PI_NAME_CHANGE</productId>
  <receiptPeriod>2016-02-06</receiptPeriod>
  <receivedDate null="true"/>
  <requestData null="true"/>

1 个答案:

答案 0 :(得分:0)

几条评论:

  • 不应该每次重新准备查询,它是一个 的反模式即可。只准备一次语句并为每次方法调用重复使用

  • 您显示的源代码始终返回第一页 数据,因为您在remaining之后突破了for循环 变量倒计数到0. PagingState对象未使用
    任何地方......

  • 您的问题不明确Hello am trying to fetch 10 rows of data from Cassandra table. But on each request same 10 row is returning。你想要10行? 第一次 10行?该 阈值后10行?

分页示例代码:

注意:仅准备以下查询一次select * from customer_request where product_id = :id and receipt_period = :receiptPeriod LIMIT :lim并将其与方法一起传递

 @Override
   public List<Tuple2<String,CustomerRequest>> getCustomerRequestData(final String productId, PreparedStatement ps, final String receiptPeriod, String pagingState)
  {

   final int PAGE_SIZE = 10;

   session = cassandraSessionFactory.getSession();
   final List<CustomerRequest> customerRequestdata = new ArrayList<CustomerRequest>();
   try
   {

     final BoundStatement boundStatement = ps.bind(productId, receiptPeriod, PAGE_SIZE);
     boundStatement.setPagingState(PagingState.fromString(pagingState));
     final ResultSet resultSet = session.execute(boundStatement);
     final Iterator<Row> iter = resultSet.iterator();
     final PagingState nextPage = resultSet.getExecutionInfo().getPagingState();

     int remaining = resultSet.getAvailableWithoutFetching();
     for (final Row rowdt : resultSet)
     {
      customerRequestdata.add(constructCustomerReq(rowdt));
      if (--remaining == 0)
       {
         break;
       }
      }

    }
   catch (final Exception e)
    {
      e.printStackTrace();
    }
   return new Tuple2<>(nextPage.toString(), customerRe);

请注意使用Tuple2类返回结果列表以及分页状态,序列化为字符串以便轻松传递到前端