gpt4 book ai didi

java - 无法使用 Hector 插入带有复合键的行

转载 作者:行者123 更新时间:2023-12-01 14:49:53 25 4
gpt4 key购买 nike

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

您能否告诉我如何使我的代码正常工作(使用组合键将行插入到列族中)?我确实看到到处有人问类似的问题,但找不到确切的答案。提前致谢!

<小时/>

编辑:

将WITH COMPACT STORAGE 添加到CREATE TABLE 语句的末尾后,异常消失了。但表中的行是:

 key1                             | key2    | column1
----------------------------------+---------+------------
\x00\x04key1\x00\x00\x04key2\x00 | column1 | col1 value

最佳答案

您需要使用不同的行键和列名称。对于此示例,行键应为 key1,列名称应由 key2 和“column1”这两个值组成(注意“column1”是文字字符串常量)。

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

进一步说明此示例,这意味着如果您存储的三行碰巧具有相同的 key1 值,那么您最终将只存储一个 Cassandra 行。该行将包含三列,每三行中的非键字段“column1”的每个值一列。

关于java - 无法使用 Hector 插入带有复合键的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14991443/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com