我有以下表/列系列Cassandra来存储按时间(后代)排序的事务,每个事务都属于一个帐户:
CREATE TABLE transactions (
transaction_id uuid,
description text,
account_id uuid,
category text,
sub_category text,
date timestamp,
amount bigint,
PRIMARY KEY (account_id, date, transaction_id, amount)
) WITH CLUSTERING ORDER BY (date DESC);
我想在交易中进行分页,获取按日期排序的25个交易组,以及特定帐户。
这个定义是否可行,或者我需要其他额外的CF来帮助这个功能?你会建议什么。
提前致谢。
答案 0 :(得分:1)
所以我不确定为什么你的PK有多少,但现在不重要。
您可以针对特定帐户按日期排序获取结果25。 Datastax Native驱动程序将允许您通过分页选项执行此操作。
//Expected inputs account_id and if not the first page a state
//string from the last page of data
Select query = select().from('keyspace', 'transactions')
.where(eq('account_id', account_id))
.orderBy(desc('date'))
if (state) {
query.setPagingState(PagingState.fromString(state))
}
query.setFetchSize(25)
ResultSet resultSet = session.execute(query)
String newState = resultSet.getExecutionInfo().pagingState?.toString()
//This will get you just the 25 rows you requested first
resultSet.getAvailableWithoutFetching()
//When sending data back to the page you will need to make sure you also
//send back the paging state so it can help you go to the next 25.