使用 Hector 查询 Cassandra 中的 CompositeType 列 [英] Querying CompositeType columns in Cassandra using Hector

查看:19
本文介绍了使用 Hector 查询 Cassandra 中的 CompositeType 列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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'

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

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 并做一个列表,我会得到这个:

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:

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

推荐答案

好的起点教程 这里.

但是,在最终需要使用复合组件并尝试针对数据编写查询之后,我想出了一些我想分享的内容.

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.

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

所以,假设 s 由 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"组件了吗?开始/结束条款中没有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.

在构建复合搜索条目时,您必须指定 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆