在Apache Cassandra + Hector + Java中的键空间上插入行

时间:2012-02-06 22:51:45

标签: java nosql cassandra hector

我正在使用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”键空间,并在mutator中使用。

1 个答案:

答案 0 :(得分:6)

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