0

我有一个本地 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
4

1 回答 1

1

您需要使用不同的行键和列名。对于这个例子,你的行键应该是 key1,你的列名应该由两个值 key2 和 "column1" 组成(注意 "column1" 是一个文字字符串常量)。

这完全是因为使用 CQL3 进行的映射。第一个主键用作行(或分区)键,其余主键值用作复合列名称的一部分,该列名称还包含所存储的非键字段的名称(不是值)。

进一步看这个例子,这意味着如果你存储三行恰好具有相同的 key1 值,你最终将只存储一个 Cassandra 行。该行将有三列,一列用于三行中每一行中非关键字段“column1”的每个值。

于 2013-02-25T18:06:41.710 回答