2

Astyanax 通过其AnnotatedCompositeSerializer. 我有一个具有 3 字段复合列名称的列族,类似于此示例,改编自Astyanax 的文档(这些列实际上不是sessionIdand token,假装它们是为了论证):

// Annotated composite class
public class SessionEvent {
  @Component(ordinal=0) UUID sessionId;
  @Component(ordinal=1) UUID token;
  @Component(ordinal=2) UUID timestamp;

  public SessionEvent() { }
}

static AnnotatedCompositeSerializer<SessionEvent> eventSerializer
      = new AnnotatedCompositeSerializer<>(SessionEvent.class);
static ColumnFamily<String, SessionEvent> CF_SESSION_EVENTS
      = new ColumnFamily<>("SessionEvents",
                           StringSerializer.get(), eventSerializer);

// Querying cassandra for a column slice on one prefix, but we want two!
OperationResult<ColumnList<SessionEvent>> result = keyspace.prepareQuery(CF_SESSION_EVENTS)
    .getKey("UserId1")
    .withColumnRange(eventSerializer.buildRange()
        .withPrefix("SessionId1")
        .build())
    .execute();

问题是:我想查询前缀中有两个复合列(在本例中为sessionIdtoken)的所有列,而不仅仅是一个,以及所有时间戳,而不仅仅是在一个范围内。这显然可以使用 CQL3 轻松实现,但我被困在 Cassandra 1.0.x 上,找不到 Astyanax 接受的方法(通过调用withPrefix(UUID)两次或传递一个复合数据结构)。

有没有办法用 Astyanax 做到这一点?也许我可以以某种方式使用基本RangeBuilder并手动序列化开始和结束吗?

4

1 回答 1

1

Cassandra 中的列是按顺序存储的。在您的示例中, sessionId 是最重要的,其次是 token,timestamp 是最不重要的。换句话说,列按 sessionId 排序,然后是令牌,然后是时间戳。

如果我理解正确,您正在尝试查询具有指定 sessionId 以及令牌的列以及任何时间。由于上述原因,具有匹配 sessionId 和 token 的列是连续存储的。您可以通过以下方式使用最小/最大日期查询任何日期:

startTime = new Date(0);
endTime = new Date(Long.MAX_VALUE);

SessionEvent from = new SessionEvent(sessionId, token, startTime);
SessionEvent to = new SessionEvent(sessionId, token, endTime);

RangeBuilder rb = new RangeBuilder().setStart(from).setEnd(to);

OperationResult<ColumnList<SessionEvent>> result = keyspace
        .prepareQuery(CF_SESSION_EVENTS)
        .getKey("UserId1")
        .withColumnRange(rb.build())
        .execute();
于 2013-10-10T00:54:41.487 回答