我正在研究Java和Hector的Apache Cassandra版本0.7.6,并尝试创建一个集群,一个键空间并在创建的该键空间中插入一列。

通过查看示例,我了解到键空间等效于SQL数据库中的数据库,而列族等效于表。知道了这一点,我试图创建简单的示例结构。

 Cluster tutorialCluster = HFactory.getOrCreateCluster("TutorialCluster","127.0.0.1:9160");
 ConfigurableConsistencyLevel ccl = new ConfigurableConsistencyLevel();

 ccl.setDefaultReadConsistencyLevel(HConsistencyLevel.ONE);

 Keyspace tutorialKeyspace = HFactory.createKeyspace("Tutorial", tutorialCluster, ccl);
 Mutator<String> mutator = HFactory.createMutator(tutorialKeyspace, stringSerializer);

 mutator.addInsertion("CA Burlingame", "StateCity", HFactory.createColumn(650L, "37.57x122.34", longSerializer, stringSerializer));

 MutationResult mr = mutator.execute();

但是当我尝试使用cassandra启动它来运行它时,却返回了一个异常。
Exception in thread "main" me.prettyprint.hector.api.exceptions.HInvalidRequestException: InvalidRequestException(why:Keyspace Tutorial does not exist)
at me.prettyprint.cassandra.connection.HThriftClient.getCassandra(HThriftClient.java:70)
at me.prettyprint.cassandra.connection.HConnectionManager.operateWithFailover(HConnectionManager.java:226)

但是我已经创建了“Tutorial”键空间,并在增变器中使用了它。

最佳答案

HFactory中的createKeyspace()调用旨在创建供本地使用的hector Keyspace对象,但实际上并未在Cassandra中创建键空间。为此,您想在实际的群集对象上使用'addKeyspace()'和'addColumnFamily'方法。

https://github.com/rantav/hector/blob/master/core/src/main/java/me/prettyprint/hector/api/Cluster.java#L117

09-27 13:01