How to define composite key for a column family and then reference it using Hector?

走远了吗. 提交于 2019-12-09 06:52:32

问题


where can I find samples for this?

Most of my code using ColumnFamilyTemplate to do CRUD on the data records, see below. Once I have the composite key defined, can I still use ColumnFamilyTemplate to access my data having composite keys?

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

回答1:


It shouldn't matter which API you use to do the CRUD on the records; CompositeType (or DynamicCompositeType) is just another type (e.g. similar to UUID) which has a corresponding serializer (CompositeSerializer). So, your example might become:

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

Only extra would be to create the Composite before using template (assume composite of UUID & Long):

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

When fetching results, one way to get the components of the key:

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



回答2:


If you define a composite key, you also need to tell Cassandra (>0.8.1) to use CompositeType as the comparator. Here is a complete example starting from defining CompositeType schema to programming a range query: http://randomizedsort.blogspot.com/2011/11/cassandra-range-query-using.html



来源:https://stackoverflow.com/questions/7329211/how-to-define-composite-key-for-a-column-family-and-then-reference-it-using-hect

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!