cassandra-Hector:如何使用复合键获取列族

时间:2013-10-23 10:17:08

标签: java cassandra hector cassandra-cli

我是 cassandra 的新手,我的数据存储结构是

list test
RowKey: key1
=> (column=colkey1:colkey2, value=amitdubey, timestamp=1381832571947000)
=> (column=colkey1:colkey3, value=amitdubey, timestamp=1381832571947000)
=> (column=colkey1:colkey4, value=amitdubey, timestamp=1381832571947000)
-------------------
RowKey: key2
=> (column=colkey1:colkey2, value=amitdubey, timestamp=1381832571947000)
=> (column=colkey1:colkey3, value=amitdubey, timestamp=1381832571947000)
=> (column=colkey1:colkey4, value=amitdubey, timestamp=1381832571947000)
-------------------

使用列定义是

create column family test
with column_type = 'Standard'
and comparator=
'CompositeType(org.apache.cassandra.db.marshal.UTF8Type,
org.apache.cassandra.db.marshal.UTF8Type)'
and default_validation_class = 'UTF8Type'
and key_validation_class = 'UTF8Type';

但是当我获取 keyRow 时:

SliceQuery<String,Composite,String> sliceQuery=HFactory.createSliceQuery(keyspace, se, ce, se);
sliceQuery.setColumnFamily("test");
sliceQuery.setKey("jax");
sliceQuery.setRange(null,null, false, Integer.MAX_VALUE);
QueryResult<ColumnSlice<Composite,String>>result=sliceQuery.execute();
System.out.println(orderedRows.getColumns());

输出是:

[HColumn([java.nio.HeapByteBuffer[pos=0 lim=1 cap=1], java.nio.HeapByteBuffer[pos=0 lim=1 cap=1]]=5), HColumn([java.nio.HeapByteBuffer[pos=0 lim=1 cap=1], java.nio.HeapByteBuffer[pos=0 lim=1 cap=1]]=5), HColumn([java.nio.HeapByteBuffer[pos=0 lim=1 cap=1], java.nio.HeapByteBuffer[pos=0 lim=1 cap=1]]=5), HColumn([java.nio.HeapByteBuffer[pos=0 lim=1 cap=1], java.nio.HeapByteBuffer[pos=0 lim=1 cap=1]]=5), HColumn([java.nio.HeapByteBuffer[pos=0 lim=1 cap=1], java.nio.HeapByteBuffer[pos=0 lim=1 cap=1]]=5)]       

1 个答案:

答案 0 :(得分:3)

你快到了,你有数据 - 你需要解压缩列,见下文:

SliceQuery<String, Composite, String> sliceQuery = HFactory.createSliceQuery(keyspace, StringSerializer.get(), CompositeSerializer.get(), StringSerializer.get());
sliceQuery.setColumnFamily("test");
sliceQuery.setKey("key1");
sliceQuery.setRange(null, null, false, Integer.MAX_VALUE);
QueryResult<ColumnSlice<Composite, String>> result = sliceQuery.execute();
ColumnSlice<Composite, String> slice = result.get();

List<HColumn<Composite, String>> columns = slice.getColumns();

System.out.println("Packed");
System.out.println(columns);

System.out.println();
System.out.println("Unpacked");
for (HColumn<Composite, String> column : columns) {
  String first = column.getName().get(0, StringSerializer.get());
  String second = column.getName().get(1, StringSerializer.get());
  System.out.println(first + ":" + second + "=" + column.getValue());
}

这应该给你这样的输出:

Packed
[HColumn([java.nio.HeapByteBuffer[pos=0 lim=7 cap=7], java.nio.HeapByteBuffer[pos=0 lim=7 cap=7]]=amitdubey), HColumn([java.nio.HeapByteBuffer[pos=0 lim=7 cap=7], java.nio.HeapByteBuffer[pos=0 lim=7 cap=7]]=amitdubey), HColumn([java.nio.HeapByteBuffer[pos=0 lim=7 cap=7], java.nio.HeapByteBuffer[pos=0 lim=7 cap=7]]=amitdubey)]

Unpacked
colkey1:colkey2=amitdubey
colkey1:colkey3=amitdubey
colkey1:colkey4=amitdubey

(与hector 1.1-4一起测试。)