我正在使用 Java 和 Hector 研究 Apache Cassandra 0.7.6 版,我尝试创建一个集群、一个键空间并在创建的这个键空间中插入一列。
通过看例子,我了解到keyspace相当于SQL数据库中的数据库,Column Family相当于表。知道这一点后,我尝试创建我的简单示例结构。
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)
但我已经创建了“教程”键空间,并在 mutator 中使用。