【问题标题】:How to set a counter with Hector / Cassandra?如何用 Hector / Cassandra 设置计数器?
【发布时间】:2012-06-02 19:14:10
【问题描述】:

我应该想用带赫克托的计数器来装卡桑德拉。我从赫克托复制/粘贴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 hector


    【解决方案1】:

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

    【讨论】:

    • 感谢 phatfingers,但我应该用代码和 hector 来制作我所有的东西。是否可以使用 hector 创建列族?
    • 另外,您可能会快速了解第三种选择——从您的应用程序运行 CQL:github.com/jsevellec/cassandra-unit/blob/master/src/test/java/…
    • 致phatfingers:谢谢;但如果我这样做,我会在cluster.addColumnFamily(cfDef); 线上得到me.prettyprint.hector.api.exceptions.HInvalidRequestException: InvalidRequestException(why:CounterColumnType is not a valid comparator)
    • 我先测试了,然后添加了新版本。
    • 好的,谢谢!但是对于初学者来说有点难以理解。
    猜你喜欢
    • 2012-10-08
    • 2012-12-02
    • 2015-06-03
    • 1970-01-01
    • 2014-09-11
    • 2013-03-22
    • 2012-03-12
    • 2013-08-27
    • 1970-01-01
    相关资源
    最近更新 更多