我有一个本地 Cassandra 服务器(1.1.10)和一个使用 cql3 创建的列族:
CREATE TABLE test_columnfamily (
key1 text,
key2 text,
column1 text,
PRIMARY KEY (key1, key2));
我正在尝试使用 Hector Java 客户端(版本 1.1-2)插入一行
public class Test {
private static final CompositeSerializer compSerializer = new CompositeSerializer();
private static final StringSerializer strSerializer = new StringSerializer();
public static void main(String[] args) {
CassandraHostConfigurator conf = new CassandraHostConfigurator("localhost:9160");
conf.setCassandraThriftSocketTimeout(10000);
conf.setRetryDownedHostsDelayInSeconds(30);
conf.setRetryDownedHostsQueueSize(128);
conf.setMaxWaitTimeWhenExhausted(60000);
conf.setRetryDownedHosts(true);
conf.setLoadBalancingPolicy(new LeastActiveBalancingPolicy());
conf.setAutoDiscoverHosts(true);
conf.setHostTimeoutCounter(20);
Cluster cluster = HFactory.getOrCreateCluster("test", conf);
Keyspace ks = HFactory.createKeyspace("test_keyspace", cluster);
Mutator<Composite> compMutator = HFactory.createMutator(ks, compSerializer);
ColumnFamilyDefinition cfDef = HectorUtils.getColumnFamily(cluster, "test_keyspace", "test_columnfamily");
System.out.println("Found column family: " + cfDef.getName());
Composite key = new Composite();
key.addComponent("key1", strSerializer);
key.addComponent("key2", strSerializer);
compMutator.addInsertion(key, "test_columnfamily", HFactory.createColumn(
"column1", "col1 value", HFactory.createClock(), strSerializer, strSerializer));
compMutator.execute();
}
}
我得到了这个例外:
Exception in thread "main" me.prettyprint.hector.api.exceptions.HInvalidRequestException: InvalidRequestException(why:Not enough bytes to read value of component 0)
at me.prettyprint.cassandra.service.ExceptionsTranslatorImpl.translate(ExceptionsTranslatorImpl.java:45)
at me.prettyprint.cassandra.connection.HConnectionManager.operateWithFailover(HConnectionManager.java:264)
at me.prettyprint.cassandra.model.ExecutingKeyspace.doExecuteOperation(ExecutingKeyspace.java:113)
at me.prettyprint.cassandra.model.MutatorImpl.execute(MutatorImpl.java:243)
at com.bninc.job.Test.main(Test.java:46)
Caused by: InvalidRequestException(why:Not enough bytes to read value of component 0)
at org.apache.cassandra.thrift.Cassandra$batch_mutate_result.read(Cassandra.java:20350)
at org.apache.thrift.TServiceClient.receiveBase(TServiceClient.java:78)
at org.apache.cassandra.thrift.Cassandra$Client.recv_batch_mutate(Cassandra.java:926)
at org.apache.cassandra.thrift.Cassandra$Client.batch_mutate(Cassandra.java:912)
at me.prettyprint.cassandra.model.MutatorImpl$3.execute(MutatorImpl.java:246)
at me.prettyprint.cassandra.model.MutatorImpl$3.execute(MutatorImpl.java:243)
at me.prettyprint.cassandra.service.Operation.executeAndSetResult(Operation.java:104)
at me.prettyprint.cassandra.connection.HConnectionManager.operateWithFailover(HConnectionManager.java:258)
... 3 more
您能否告诉我如何使我的代码工作(使用复合键将行插入列族)?我确实在这里和那里看到过类似的问题,但找不到确切的答案。提前致谢!
编辑:
在 CREATE TABLE 语句的末尾添加 WITH COMPACT STORAGE 后,异常消失了。但表中的行是:
key1 | key2 | column1
----------------------------------+---------+------------
\x00\x04key1\x00\x00\x04key2\x00 | column1 | col1 value