【问题标题】:How to persist a new customer row in Cassandra using Hector?如何使用 Hector 在 Cassandra 中保留新的客户行?
【发布时间】:2013-02-22 11:34:51
【问题描述】:

我花了一整天的时间尝试将数据插入一个简单的 cassandra 数据库,用于存储用户名、密码、电子邮件和电话。 我所做的所有研究都将我带回http://hector-client.github.com/hector/build/html/content/getting_started.html#update。我已经完全按照示例进行操作,但是当我尝试执行我的代码时:

        ConfigurableConsistencyLevel ccl = new ConfigurableConsistencyLevel();
    Map<String, HConsistencyLevel> clm = new HashMap<String, HConsistencyLevel>();

    clm.put("customers", HConsistencyLevel.ONE);
    ccl.setReadCfConsistencyLevels(clm);
    ccl.setWriteCfConsistencyLevels(clm);
    Cluster cluster = HFactory.getOrCreateCluster("test-cluster", "localhost:9160");     

    Keyspace ksp = HFactory.createKeyspace("qualebs", cluster);
    ksp.setConsistencyLevelPolicy(ccl);

    ColumnFamilyTemplate<String, String> template = new ThriftColumnFamilyTemplate<String, String>(ksp, "customers", StringSerializer.get(), StringSerializer.get());

    ColumnFamilyUpdater<String, String> updater = template.createUpdater("KeyIsUsername");
    updater.setString("name", "the name");
    updater.setString("email", "email@email.com");
    template.update(updater);

尝试运行我的应用程序时出现以下异常:

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:20833)
at org.apache.thrift.TServiceClient.receiveBase(TServiceClient.java:78)
at org.apache.cassandra.thrift.Cassandra$Client.recv_batch_mutate(Cassandra.java:964)
at org.apache.cassandra.thrift.Cassandra$Client.batch_mutate(Cassandra.java:950)
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)
... 57 more

我需要帮助来了解此错误的原因。

我从 cqlsh 终端创建的架构看起来像

 CREATE TABLE customers(username text PRIMARY KEY, name text, email text);

【问题讨论】:

  • 您使用的是哪个 Cassandra 版本?看起来您的键或列类型是 CompositeType,但您的节俭代码使用的是 UTF8Type。您是否进行了任何其他架构更改?您在哪个 CQL 版本中创建了表?
  • 先生,非常感谢您的关注。我使用的是 cassandra 版本 1.2.2,我使用了 tar 中包含的 CQL VERSION 3.0。我没有改变桌子。从 Hector 进行更新时如何指定这些复合类型和 utfType 键
  • 它不应该是具有该定义的 CompositeType。使用复合主键将生成键 CompositeType 并可能导致此问题 - 请参阅 cassandra-user-incubator-apache-org.3065146.n2.nabble.com/…。如果您不打算使用 CQL 查询,可以尝试通过 cassandra-cli 创建列族。
  • 我使用以下命令创建了 columnFamily。想用用户名作为主键我该怎么做create column family customers with comparator = UTF8Type and key_validation_class = UTF8Type and column_metadata = [ {column_name: username, validation_class: UTF8Type} {column_name: name, validation_class: UTF8Type} {column_name: email, validation_class: UTF8Type} {column_name: phone, validation_class: UTF8Type} {column_name: sex, validation_class: UTF8Type} {column_name: password, validation_class: UTF8Type} {column_name: balance, validation_class: DecimalType} ];
  • cassandra-cli 术语中的主键是行键而不是列。因此,从 column_metadata 中删除用户名并尝试您的代码。

标签: java jsp nosql cassandra hector


【解决方案1】:

另一种选择可能是尝试 playOrm,它会为您正确转换并为您创建 CF,因此您不必担心创建错误 ;)。只是一种选择。

通过PlayOrm主页或GitHub获取框架

【讨论】:

    猜你喜欢
    • 2012-03-12
    • 2014-02-23
    • 2012-04-23
    • 1970-01-01
    • 2013-05-23
    • 2011-12-23
    • 2012-12-07
    • 2013-10-15
    • 2013-10-22
    相关资源
    最近更新 更多