cassandra:hector,serializationexception无效的stram头

332nm8kg  于 2021-06-14  发布在  Cassandra
关注(0)|答案(2)|浏览(435)

我目前正在使用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());
yacmzcpb

yacmzcpb1#

如果您查看hector serializer层次结构,您会发现objectserializer和stringserializer都是同级的,都实现了serializer接口并扩展了abstractserializer类。
stringserializer中的frombytebuffer方法需要一个字符串,而objectserializer中的frombytebuffer方法需要一个对象。在您的代码中,您已经将列值定义为string类型,因此您必须使用stringserializer,因为您的代码集需要string类型。

gkn4icbw

gkn4icbw2#

首先感谢阿比的回答:)你提供了一些重要的线索。
我所做的是使用适当的序列化程序插入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())

谢谢大家!

相关问题