cassandra - 如何使用 Hector/Cassandra 设置计数器?

标签 cassandra hector

我应该为 cassandra 使用带有 hector 的计数器。我复制/粘贴 this test case from test code来自赫克托:

    Mutator<String> m = createMutator(keyspace, se);
    MutationResult mr = m.insertCounter( // exception here.
     "k", "Counter1", createCounterColumn("name", 5)); 
    assertTrue("Execution time on single counter insert should be > 0", mr.getExecutionTimeMicro() > 0);
    assertTrue("Should have operated on a host", mr.getHostUsed() != null);
    CounterQuery<String, String> counter = new ThriftCounterColumnQuery<String,String>(keyspace, se, se);
    counter.setColumnFamily("Counter1").setKey("k").setName("name");
    assertEquals(new Long(5), counter.execute().get().getValue());

但是我在 insertCounter 行上遇到了这个异常,因为 Counter1 未配置,他说:
me.prettyprint.hector.api.exceptions.HInvalidRequestException: InvalidRequestException(why:unconfigured columnfamily Counter1)
    at me.prettyprint.cassandra.service.ExceptionsTranslatorImpl.translate(ExceptionsTranslatorImpl.java:45) ~[hector-core-1.0-5.jar:na]
    at me.prettyprint.cassandra.connection.HConnectionManager.operateWithFailover(HConnectionManager.java:264) ~[hector-core-1.0-5.jar:na]
    at me.prettyprint.cassandra.model.ExecutingKeyspace.doExecuteOperation(ExecutingKeyspace.java:97) ~[hector-core-1.0-5.jar:na]
    at me.prettyprint.cassandra.model.MutatorImpl.execute(MutatorImpl.java:243) ~[hector-core-1.0-5.jar:na]
    at me.prettyprint.cassandra.model.MutatorImpl.insertCounter(MutatorImpl.java:285) ~[hector-core-1.0-5.jar:na]

好的,但是测试用例没有配置 Counter1 ?我如何配置?

谢谢。

最佳答案

您在 Cassandra 服务器上创建列族。一种常用方法是使用 cqlsh 工具。

cqlsh

USE Keyspace1;
CREATE COLUMNFAMILY counter1 (example text PRIMARY KEY) 
  WITH comparator=text and default_validation=counter;

在 Hector 中,你可以使用 ColumnFamilyDefinition 类——像这样:
    ColumnFamilyDefinition cfDef = HFactory.createColumnFamilyDefinition(KEYSPACE, "counter1");
    cfDef.setKeyValidationClass(ComparatorType.UTF8TYPE.getClassName());
    cfDef.setComparatorType(ComparatorType.UTF8TYPE);
    cfDef.setDefaultValidationClass(ComparatorType.COUNTERTYPE.getClassName());
    cfDef.setColumnType(ColumnType.STANDARD);
    List<ColumnFamilyDefinition> columnFamilies = new ArrayList<ColumnFamilyDefinition>();
    columnFamilies.add(cfDef);
    KeyspaceDefinition ksdef = HFactory.createKeyspaceDefinition(KEYSPACE, "org.apache.cassandra.locator.SimpleStrategy", 1,
            columnFamilies);
    cluster.addKeyspace(ksdef);

关于cassandra - 如何使用 Hector/Cassandra 设置计数器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10784322/

相关文章:

json - Cassandra + 数据插入 + set<FROZEN<map<text,text>>>

组合键的 Cassandra 分区

dynamic - 如何以编程方式在 Cassandra Hector API 中创建二级索引

hadoop - Hector 的批处理 Mutation 与使用 Hadoop 作业将数据加载到 Cassandra 中?

java - cassandra 1.1.2 中的数据丢失

java - 用于时间序列数据的 Cassandra Map Reduce

nosql - Cassandra 数据库中的 commitLog 和 SSTables

postgresql - Postgres 是否将 bytea 数据以十六进制形式存储在服务器上?

cassandra - 使用 Cassandra 进行 OLAP

运行 hector 示例代码时出现 java.lang.ClassNotFoundException : org. apache.xbean.finder.archive.Archive 错误