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