如何为列族定义复合键,然后使用 Hector 引用它?

发布于 2024-12-03 10:28:59 字数 471 浏览 0 评论 0原文

我在哪里可以找到这方面的样本?

我的大部分代码使用 ColumnFamilyTemplate 对数据记录进行 CRUD,请参见下文。定义复合键后,我是否仍然可以使用 ColumnFamilyTemplate 来访问具有复合键的数据?

private static final ColumnFamilyTemplate<UUID, String> template = 
    new ThriftColumnFamilyTemplate<UUID, String>(
        Bootstrap.keyspace, 
        "User", 
        UUIDSerializer.get(), 
        StringSerializer.get(),
        HFactory.createMutator(Bootstrap.keyspace, UUIDSerializer.get()));

where can I find samples for this?

Most of my code using ColumnFamilyTemplate to do CRUD on the data records, see below. Once I have the composite key defined, can I still use ColumnFamilyTemplate to access my data having composite keys?

private static final ColumnFamilyTemplate<UUID, String> template = 
    new ThriftColumnFamilyTemplate<UUID, String>(
        Bootstrap.keyspace, 
        "User", 
        UUIDSerializer.get(), 
        StringSerializer.get(),
        HFactory.createMutator(Bootstrap.keyspace, UUIDSerializer.get()));

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

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

发布评论

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

评论(2

﹎☆浅夏丿初晴 2024-12-10 10:28:59

使用哪个 API 对记录执行 CRUD 并不重要; CompositeType(或DynamicCompositeType)只是另一种类型(例如类似于UUID),它具有相应的序列化器(CompositeSerializer)。因此,您的示例可能会变成:

private static final ColumnFamilyTemplate<Composite, String> template = 
new ThriftColumnFamilyTemplate<Composite, String>(
    Bootstrap.keyspace, 
    "User", 
    CompositeSerializer.get(), 
    StringSerializer.get(),
    HFactory.createMutator(Bootstrap.keyspace, CompositeSerializer.get()));

唯一的额外方法是在使用模板之前创建复合体(假设 UUID 和长整型的复合体):

Composite key = new Composite();
key.addComponent(someUUID, UUIDSerializer.get());
key.addComponent(someLong, LongSerializer,get());
ColumnFamilyResult<Composite,String> result = template.queryColumns(key);

在获取结果时,获取密钥组件的一种方法是:

myUUID = result.getKey().get(0, UUIDSerializer.get());
myLong = result.getKey().get(1, LongSerializer,get());

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());
-黛色若梦 2024-12-10 10:28:59

如果定义复合键,还需要告诉 Cassandra (>0.8.1) 使用 CompositeType 作为比较器。下面是一个完整的示例,从定义 CompositeType 架构开始到对范围查询进行编程:
http://randomizedsort.blogspot.com/2011/11/cassandra -range-query-using.html

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

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