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

社会主义新天地 提交于 2019-12-03 08:57:01

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());

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

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