使用Hector在Cassandra中查询CompositeType列 [英] Querying CompositeType columns in Cassandra using Hector
问题描述
这里是我面对的场景的示例。说明我有此列系列:
Here's a sample of the scenario I'm facing. Say I have this column family:
create column family CompositeTypeCF
with comparator = 'CompositeType(IntegerType,UTF8Type)'
and key_validation_class = 'UTF8Type'
and default_validation_class = 'UTF8Type'
一些示例Java代码使用Hector来介绍如何将一些数据插入此列系列:
Here's some sample Java code using Hector as to how I'd go about inserting some data into this column family:
Cluster cluster = HFactory.getOrCreateCluster("Test Cluster", "192.168.1.6:9160");
Keyspace keyspaceOperator = HFactory.createKeyspace("CompositeTesting", cluster);
Composite colKey1 = new Composite();
colKey1.addComponent(1, IntegerSerializer.get());
colKey1.addComponent("test1", StringSerializer.get());
Mutator<String> mutator = HFactory.createMutator(keyspaceOperator, StringSerializer.get());
Mutator<String> addInsertion = mutator.addInsertion("rowkey1", "CompositeTypeCF",
HFactory.createColumn(colKey1, "Some Data", new CompositeSerializer(), StringSerializer.get()));
mutator.execute();
这样工作,如果我去cassandra-cli并做一个列表, / p>
This works, and if I go to the cassandra-cli and do a list I get this:
$ list CompositeTypeCF;
Using default limit of 100
-------------------
RowKey: rowkey1
=> (column=1:test1, value=Some Data, timestamp=1326916937547000)
:如何在Hector中查询此数据?基本上,我需要以几种方式查询:
My question now is this: How do I go about querying this data in Hector? Basically I would need to query it in a few ways:
- 给我整行,其中Row Key =rowkey1
- 向我提供列数据的第一部分=某个整数值的列数据
- 向我提供列名称的第一部分在内的所有列一定范围
推荐答案
良好的起点教程这里。
但是,要使用复合组件并尝试对数据进行查询,我想出了一些我想分享的东西。
But, after finally having the need to use a composite component and attempting to write queries against the data, I figured out a few things that I wanted to share.
在搜索复合列时,
When searching Composite columns, the results will be a contiguous block of columns.
因此,假设作为3个字符串的复合,我的列如下所示:
So, assuming a s composite of 3 Strings, and my columns look like:
A:A:A
A:B:B
A:B:C
A:C:B
B:A:A
B:B:A
B:B:B
C:A:B
对于从A:A:A到B:B:B的搜索,结果将是
For a search from A:A:A to B:B:B, the results will be
A:A:A
A:B:B
A:B:C
A:C:B
B:A:A
B:B:A
B:B:B
组件?开始/结束术语中没有C组件!是什么赋予了?这些是A:A:A和B:B:B列之间的所有结果。 复合搜索字词不会给出结果,就像处理嵌套循环(这是我原先想的),而是,因为列被排序,
Notice the "C" Components? There are no "C" components in the start/end terms! what gives? These are all the results between A:A:A and B:B:B columns. The Composite search terms do not give the results as if processing nested loops (this is what I originally thought), but rather, since the columns are sorted, you are specifying the start and end terms for a contiguous block of columns.
在构建Composite搜索条目时,必须指定ComponentEquality
When building the Composite search entries, you must specify the ComponentEquality
只有最后一个项应该是GREATER_THAN_EQUAL,所有其他都应该是EQUAL。例如
Only the last term should be GREATER_THAN_EQUAL, all the others should be EQUAL. e.g. for above
Composite start = new Composite();
start.addComponent(0, "A", Composite.ComponentEquality.EQUAL);
start.addComponent(1, "A", Composite.ComponentEquality.EQUAL);
start.addComponent(2, "A", Composite.ComponentEquality.EQUAL);
Composite end = new Composite();
end.addComponent(0, "B", Composite.ComponentEquality.EQUAL);
end.addComponent(1, "B", Composite.ComponentEquality.EQUAL);
end.addComponent(2, "B", Composite.ComponentEquality.GREATER_THAN_EQUAL);
SliceQuery<String, Composite, String> sliceQuery = HFactory.createSliceQuery(keyspace, se, ce, se);
sliceQuery.setColumnFamily("CF").setKey(myKey);
ColumnSliceIterator<String, Composite, String> csIterator = new ColumnSliceIterator<String, Composite, String>(sliceQuery, start, end, false);
while (csIterator.hasNext()) ....
这篇关于使用Hector在Cassandra中查询CompositeType列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!