【发布时间】:2016-06-07 05:16:51
【问题描述】:
您好,我正在尝试从 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"/>
【问题讨论】:
-
您在receipt_period 列上有二级索引吗?因为如果没有,Cassandra 不允许您查询“where product_id = :id andreceipt_period = :receiptPeriod”
-
o 是的.. 我们在receipt_period 上有二级索引
-
@doanduyhai-先生有什么更新吗?
-
感谢您的帮助。我唯一的疑问是如何从 UI 端第一次传递 (String pagingState) 请求?当我第一次作为 null 传递时,我在设置分页状态时遇到错误。 boundStatement.setPagingState(PagingState.fromString(pagingState));
-
对第一次查询的 pagingState 添加一个空检查,并且不要将它注入到 boundStatement 中
标签: java spring cassandra cassandra-2.0 datastax