我目前正在使用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());
2条答案
按热度按时间yacmzcpb1#
如果您查看hector serializer层次结构,您会发现objectserializer和stringserializer都是同级的,都实现了serializer接口并扩展了abstractserializer类。
stringserializer中的frombytebuffer方法需要一个字符串,而objectserializer中的frombytebuffer方法需要一个对象。在您的代码中,您已经将列值定义为string类型,因此您必须使用stringserializer,因为您的代码集需要string类型。
gkn4icbw2#
首先感谢阿比的回答:)你提供了一些重要的线索。
我所做的是使用适当的序列化程序插入bytebuffers。例如
例如检索它们
谢谢大家!