如何设置Hector / Cassandra的计数器?

时间:2012-05-28 12:00:24

标签: cassandra hector

我应该用一个带有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?我如何配置?

感谢。

1 个答案:

答案 0 :(得分:4)

您在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);