【问题标题】:Retrieve a row by key of cassandra using Hector使用 Hector 通过 cassandra 的键检索一行
【发布时间】:2011-11-15 18:11:53
【问题描述】:

我正在插入 CF 并且一切正常,我还可以通过简单的方式从 cassandra-cli 读取整行

get columnFamily[lexicaluuid(de80a290-dd71-11e0-8f9d-f81edfd6bd91)];

每一列的key是一个timeUUID:

key = TimeUUIDUtils.getUniqueTimeUUIDinMillis();

CF的部分描述:

ColumnFamily: columnFamily
       Key Validation Class: org.apache.cassandra.db.marshal.TimeUUIDType
       Default column value validator: org.apache.cassandra.db.marshal.BytesType
       Columns sorted by: org.apache.cassandra.db.marshal.UTF8Type

我们还更改了密钥验证类以使用 BytesType UTF8Type 和 TimeUUIDType 进行测试。

作为 Java 的 cassandra 客户端,我使用的是 Hector。

当我在知道它的 id 的情况下尝试获取一行时,我无法检索任何内容。

例子:

String rowKey = "de80a290-dd71-11e0-8f9d-f81edfd6bd91";
SliceQuery<UUID, String, String> result =HFactory.createSliceQuery(CassandraManager.getKeyspace(), UUIDSerializer.get(), StringSerializer.get(), StringSerializer.get());
result.setColumnFamily("columnFamily");
result.setKey(UUIDSerializer.get().fromByteBuffer(StringSerializer.get().toByteBuffer(rowKey)));

result.setColumnNames(COLUMNS); //COLUMNS ins a predefined list of colums
QueryResult <ColumnSlice<String, String>> columnSlice = result.execute();
System.out.println(columnSlice.get().getColumnByName(name).getValue());

结果是以下异常:

me.prettyprint.hector.api.exceptions.HInvalidRequestException: InvalidRequestException(why:Invalid version for TimeUUID type.)
    at me.prettyprint.cassandra.service.ExceptionsTranslatorImpl.translate(ExceptionsTranslatorImpl.java:50)
    at me.prettyprint.cassandra.service.KeyspaceServiceImpl$7.execute(KeyspaceServiceImpl.java:285)
    at me.prettyprint.cassandra.service.KeyspaceServiceImpl$7.execute(KeyspaceServiceImpl.java:268)
    at me.prettyprint.cassandra.service.Operation.executeAndSetResult(Operation.java:101)
    at me.prettyprint.cassandra.connection.HConnectionManager.operateWithFailover(HConnectionManager.java:232)
    at me.prettyprint.cassandra.service.KeyspaceServiceImpl.operateWithFailover(KeyspaceServiceImpl.java:131)
    at me.prettyprint.cassandra.service.KeyspaceServiceImpl.getSlice(KeyspaceServiceImpl.java:289)
    at me.prettyprint.cassandra.model.thrift.ThriftSliceQuery$1.doInKeyspace(ThriftSliceQuery.java:53)
    at me.prettyprint.cassandra.model.thrift.ThriftSliceQuery$1.doInKeyspace(ThriftSliceQuery.java:49)...

我还尝试在 sliceQuery 定义中使用 StringSerializer 作为 keySerializer,但没有得到异常但没有结果。

我想我的问题基本上是“如何通过键检索行查询?” :)

这似乎是一个愚蠢的问题,但让我发疯了好几天。

【问题讨论】:

    标签: cassandra hector


    【解决方案1】:

    这一行是错的,

    result.setKey(UUIDSerializer.get().fromByteBuffer(StringSerializer.get().toByteBuffer(rowKey)));
    

    相反,你想做一些类似的事情,

    result.setKey(java.util.UUID.fromString(rowKey));
    

    StringSerializer.get().toByteBuffer(rowKey)) 为您提供一个字节缓冲区,其中包含 36 个 utf-8 字节的 rowKey。这与 uuid 的序列化形式不同,它是 16 个字节(一个长 msb,然后一个长 lsb)。

    在这种情况下,可能根本不需要使用字符串。只需在代码中的任何地方使用 TimeUUIDSerializer 和 com.eaio.uuid.UUID。

    【讨论】:

      猜你喜欢
      • 2012-03-12
      • 2012-04-23
      • 1970-01-01
      • 2012-01-15
      • 2015-03-15
      • 2012-09-06
      • 2013-05-23
      • 2012-07-04
      • 2012-01-16
      相关资源
      最近更新 更多