2012-05-28 65 views
4

我應該使用一個計數器與卡桑德拉hector。我複製/粘貼this test case from test code從赫克託:如何設置Hector/Cassandra的計數器?

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線,因爲C1的是未配置,他說:

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] 

OK,但測試用例沒有按」 t配置Counter1?我如何配置?

謝謝。

回答

4

您可以在Cassandra服務器上創建列族。一種常用的方法是使用cqlsh工具。

cqlsh 

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

在赫克託,你可以使用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); 
+0

感謝phatfingers,但我想應該讓我所有的東西與代碼,赫克託。是否有可能用hector創建一個列家族? – Istao

+0

我添加了在Hector中執行相同任務的代碼。 – phatfingers

+0

另外,您可以快速瀏覽第三個選擇 - 從您的應用程序運行CQL:https://github.com/jsevellec/cassandra-unit/blob/master/src/test/java/org/cassandraunit/DataLoaderAndCQLExecutionTest .java – phatfingers