2

我目前正在使用 Apache Cassandra(Cassandra 版本:1.2.3,Thrift API 版本:19.35.0,CQL 支持的版本:2.0.0,3.0.1(默认值:3.0.1))和 Hector 1.0.5。

我使用以下代码插入和检索对象。

String cf="User";
key="myKey";
String colName="colName";
String colValue="colvalue";

Mutator<String> mutator = HFactory.createMutator(keyspace, StringSerializer.get());
mutator.insert(key, cf, HFactory.createStringColumn(colName, colValue));



ColumnQuery<String, String, Object> columnQuery = HFactory.createColumnQuery(
    keyspace,
    StringSerializer.get(),
    StringSerializer.get(),
    ObjectSerializer.get());

    columnQuery.setColumnFamily(cf).setKey(key).setName(colName);
    QueryResult<HColumn<String, Object>> r = columnQuery.execute();

    System.out.println("Read HColumn from cassandra: " + r.get());

但我得到以下异常:

Exception in thread "main" me.prettyprint.hector.api.exceptions.HectorSerializationException: java.io.StreamCorruptedException: invalid stream header: 636F6C76
    at me.prettyprint.cassandra.serializers.ObjectSerializer.fromByteBuffer(ObjectSerializer.java:79)
    at me.prettyprint.cassandra.model.HColumnImpl.getValue(HColumnImpl.java:124)
    at me.prettyprint.cassandra.model.HColumnImpl.toString(HColumnImpl.java:202)
    at java.lang.String.valueOf(String.java:2854)
    at java.lang.StringBuilder.append(StringBuilder.java:128)
    at org.dargamenteria.tfg.texu.logic.UserDAOImpl.create(UserDAOImpl.java:111)

但是下面的代码有效,只是将 ObjectSerializer 更改为 StringSerializer:

Mutator<String> mutator = HFactory.createMutator(keyspace, StringSerializer.get());
mutator.insert(key, cf, HFactory.createStringColumn(colName, colValue));

ColumnQuery<String, String, String> columnQuery = HFactory.createColumnQuery(
    keyspace,
    StringSerializer.get(),
    StringSerializer.get(),
    StringSerializer.get());

    columnQuery.setColumnFamily(cf).setKey(key).setName(colName);
    QueryResult<HColumn<String, String>> r = columnQuery.execute();

    System.out.println("Read HColumn from cassandra: " + r.get());
4

2 回答 2

1

如果您查看 Hector Serializer 层次结构,您会发现 ObjectSerializer 和 StringSerializer 都是兄弟姐妹,都实现了 Serializer 接口并扩展了 AbstractSerializer 类。

StringSerializer 中的fromByteBuffer方法需要一个字符串,而在 ObjectSerializer 中它需要一个对象。并且在您的代码中,您已将列值定义为字符串类型,因此您必须使用 StringSerializer,因为您的代码集需要字符串类型。

于 2013-05-03T10:57:46.360 回答
0

首先感谢 Abhi 的回答 :) 你提供了一些重要的线索。

我所做的是使用正确的序列化程序插入 ByteBuffers。例如

mutator.addInsertion(key, columnFamilyName, 
HFactory.createColumn("name",                   
    ByteBufferSerializer.get().fromByteBuffer(StringSerializer.get().toByteBuffer(uh.getName())), 
    StringSerializer.get(),
    ByteBufferSerializer.get())).

addInsertion(key, columnFamilyName, 
HFactory.createColumn("email",                  
    ByteBufferSerializer.get().fromByteBuffer(ObjectSerializer.get().toByteBuffer(uh.getEmail())), 
    StringSerializer.get(),
    ByteBufferSerializer.get())).

例如检索它们

 StringSerializer.get().fromByteBuffer(
                r.get().getColumnByName("name").getValue())

谢谢你们!

于 2013-05-04T00:49:19.527 回答