【问题标题】:How to define composite key for a column family and then reference it using Hector?如何为列族定义复合键,然后使用 Hector 引用它?
【发布时间】:2011-11-11 20:29:44
【问题描述】:

我在哪里可以找到这方面的样本?

我的大部分代码使用 ColumnFamilyTemplate 对数据记录进行 CRUD,见下文。定义复合键后,我还可以使用 ColumnFamilyTemplate 访问具有复合键的数据吗?

private static final ColumnFamilyTemplate<UUID, String> template = 
    new ThriftColumnFamilyTemplate<UUID, String>(
        Bootstrap.keyspace, 
        "User", 
        UUIDSerializer.get(), 
        StringSerializer.get(),
        HFactory.createMutator(Bootstrap.keyspace, UUIDSerializer.get()));

【问题讨论】:

  • 我四处搜索,没有发现任何有用的东西,有人可以帮忙吗?

标签: cassandra composite-key hector


【解决方案1】:

使用哪个 API 对记录进行 CRUD 并不重要; CompositeType(或 DynamicCompositeType)只是另一种类型(例如,类似于 UUID),它具有相应的序列化器(CompositeSerializer)。所以,你的例子可能变成:

private static final ColumnFamilyTemplate<Composite, String> template = 
new ThriftColumnFamilyTemplate<Composite, String>(
    Bootstrap.keyspace, 
    "User", 
    CompositeSerializer.get(), 
    StringSerializer.get(),
    HFactory.createMutator(Bootstrap.keyspace, CompositeSerializer.get()));

唯一额外的就是在使用模板之前创建复合(假设 UUID 和 Long 的复合):

Composite key = new Composite();
key.addComponent(someUUID, UUIDSerializer.get());
key.addComponent(someLong, LongSerializer,get());
ColumnFamilyResult<Composite,String> result = template.queryColumns(key);

获取结果时,获取key的组成部分的一种方式:

myUUID = result.getKey().get(0, UUIDSerializer.get());
myLong = result.getKey().get(1, LongSerializer,get());

【讨论】:

    【解决方案2】:

    如果定义复合键,还需要告诉 Cassandra (>0.8.1) 使用 CompositeType 作为比较器。这是一个从定义 CompositeType 模式到编写范围查询的完整示例: http://randomizedsort.blogspot.com/2011/11/cassandra-range-query-using.html

    【讨论】:

      猜你喜欢
      • 2013-11-01
      • 2013-10-28
      • 2015-03-15
      • 2013-07-08
      • 2012-06-26
      • 2012-08-03
      • 2013-02-09
      • 2013-11-17
      • 2014-01-06
      相关资源
      最近更新 更多