日期范围的卡桑德拉建模

时间:2017-07-25 01:33:02

标签: cassandra cassandra-3.0

卡桑德拉新手在这里。 Cassandra v 3.9。

我正在为旅行者航班登机数据建模。

我的主要查询标准是搜索具有日期范围(最多7天窗口)的旅行者。

以下是我对Cassandra的有限接触所得出的结果。

create table IF NOT EXISTS travellers_checkin (checkinDay text, checkinTimestamp bigint, travellerName text, travellerPassportNo text, flightNumber text, from text, to text, bookingClass text, PRIMARY KEY (checkinDay, checkinTimestamp)) WITH CLUSTERING ORDER BY (checkinTimestamp DESC)

每天,我预计会有多达一百万条记录 - 导致分区有一百万条记录。

现在我的用户希望搜索日期窗口是必需的(最多一周的窗口)。在这种情况下,我应该使用跨越多个分区的IN子句吗?这是正确的方法还是应该考虑重新建模数据?或者,我也想知道发出7个查询(每天)并合并回复是否有效。

1 个答案:

答案 0 :(得分:2)

您的数据模型似乎很好。但是如果您可以向分区键添加更多字段,它将很好地扩展。您应该使用带有executeAsync

的单独查询

如果您正在使用in子句,这意味着您正在等待这个单个协调器节点给您一个响应,它将所有这些查询及其响应保留在堆中,如果其中一个查询失败,或者协调员失败,你必须重试整个事情

enter image description here

来源:https://lostechies.com/ryansvihla/2014/09/22/cassandra-query-patterns-not-using-the-in-query-for-multiple-partitions/

不使用IN子句,而是使用每天的单独查询并使用executeAsync执行它。

Java示例:

PreparedStatement statement = session.prepare("SELECT * FROM travellers_checkin where checkinDay = ? and checkinTimestamp >= ? and checkinTimestamp <= ?");

List<ResultSetFuture> futures = new ArrayList<>();
for (int i = 1; i < 4; i++) {
    ResultSetFuture resultSetFuture = session.executeAsync(statement.bind(i, i));
    futures.add(resultSetFuture);
}

for (ResultSetFuture future : futures){
     ResultSet rows = future.getUninterruptibly();
     //You get the result set of each query, merge them here
}
相关问题