我正在使用 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)
但我已经创建了“教程”键空间,并在修改器中使用。
最佳答案
HFactory 中的 createKeyspace() 调用旨在创建一个 hector Keyspace 对象供本地使用,但它实际上并没有在 Cassandra 中创建一个键空间。为此,您想在实际的集群对象上使用“addKeyspace()”和“addColumnFamily”方法。
关于java - 在 Apache Cassandra + Hector + Java 的键空间上插入行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9168538/