使用 Hector 查询 Cassandra 中的 CompositeType 列

发布于 2024-12-27 17:47:17 字数 1335 浏览 5 评论 0原文

这是我面临的场景的示例。假设我有这个列族:

    create column family CompositeTypeCF 
    with comparator = 'CompositeType(IntegerType,UTF8Type)' 
    and key_validation_class = 'UTF8Type' 
    and default_validation_class = 'UTF8Type'

这是一些使用 Hector 的示例 Java 代码,说明我如何将一些数据插入到这个列族中:

 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 并执行一个列表,我会得到以下结果:

$ list CompositeTypeCF;

Using default limit of 100
-------------------
RowKey: rowkey1
=> (column=1:test1, value=Some Data, timestamp=1326916937547000)

我现在的问题是:我如何在 Hector 中查询这些数据?基本上我需要以几种方式查询它:

  1. 给我行键 =“rowkey1” 的整行
  2. 给我列数据,其中列名称的第一部分 = 某个整数值
  3. 给我第一部分的所有列列名在一定范围内

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'

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

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)

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:

  1. Give me the whole row where Row Key = "rowkey1"
  2. Give me the column data where the first part of the column name = some integer value
  3. Give me all the columns where the first part of the column name is within a certain range

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

像你 2025-01-03 17:47:17

很好的起点教程此处

但是,在最终需要使用复合组件并尝试针对数据编写查询之后,我发现了一些我想分享的事情。

搜索复合列时,结果将是连续的列块。

因此,假设为 3 个字符串的组合,我的列如下所示:

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,结果将是

A:A:A
A:B:B
A:B:C
A:C:B
B:A:A
B:B:A
B:B:B

注意“C”组件吗?开始/结束项中没有“C”成分!什么给了?这些是 A:A:A 和 B:B:B 列之间的所有结果。 复合搜索词不会给出像处理嵌套循环一样的结果(这是我最初的想法),而是,由于列已排序,您指定连续列块的开始和结束术语

构建复合搜索条目时,必须指定 ComponentEquality

只有最后一项应为 GREATER_THAN_EQUAL,所有其他项应为 EQUAL。例如上面的

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

Good starting point tutorial here.

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.

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

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

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.

When building the Composite search entries, you must specify the ComponentEquality

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()) ....
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文