java - 赫克托与 Cassandra 的基础知识

标签 java cassandra hector

我正在使用 Cassandra-0.8.2。 我正在使用最新版本的 Hector & 我的java版本是1.6.0_26

我对 Cassandra 和 Hector 还很陌生。

我正在尝试做的事情: 1. 连接到另一台服务器上正在运行的 cassandra 实例。我知道它正在运行 b/c 我可以通过我的终端 ssh 进入运行这个 Cassandra 实例的服务器并运行具有完整功能的 CLI。 2. 然后我想连接到一个键空间并创建一个列族,然后通过 Hector 向该列族添加一个值。

我认为我的问题是此服务器上运行的 Cassandra 实例可能未配置为获取非本地命令。我想我的下一步是在我正在使用的 cpu 上添加一个 Cassandra 的本地实例,并尝试在本地执行此操作。你怎么看?

这是我的 Java 代码:

import me.prettyprint.cassandra.serializers.StringSerializer;
import me.prettyprint.cassandra.service.CassandraHostConfigurator;
import me.prettyprint.hector.api.Cluster;
import me.prettyprint.hector.api.Keyspace;
import me.prettyprint.hector.api.ddl.ColumnFamilyDefinition;
import me.prettyprint.hector.api.ddl.ComparatorType;
import me.prettyprint.hector.api.factory.HFactory;
import me.prettyprint.hector.api.mutation.Mutator;

    public class MySample {


        public static void main(String[] args) {


            Cluster cluster = HFactory.getOrCreateCluster("Test Cluster", "xxx.xxx.x.41:9160");
            Keyspace keyspace =  HFactory.createKeyspace("apples", cluster);
            ColumnFamilyDefinition cf = HFactory.createColumnFamilyDefinition("apples","ColumnFamily2",ComparatorType.UTF8TYPE);
            StringSerializer stringSerializer = StringSerializer.get();
            Mutator<String> mutator = HFactory.createMutator(keyspace, stringSerializer);
            mutator.insert("jsmith", "Standard1", HFactory.createStringColumn("first", "John"));
}
}

我的错误是:

16:22:19,852  INFO CassandraHostRetryService:37 - Downed Host Retry service started with queue size -1 and retry delay 10s
16:22:20,136  INFO JmxMonitor:54 - Registering JMX me.prettyprint.cassandra.service_Test Cluster:ServiceType=hector,MonitorType=hector
Exception in thread "main" me.prettyprint.hector.api.exceptions.HInvalidRequestException: InvalidRequestException(why:Keyspace apples does not exist)
    at me.prettyprint.cassandra.connection.HThriftClient.getCassandra(HThriftClient.java:70)
    at me.prettyprint.cassandra.connection.HConnectionManager.operateWithFailover(HConnectionManager.java:226)
    at me.prettyprint.cassandra.service.KeyspaceServiceImpl.operateWithFailover(KeyspaceServiceImpl.java:131)
    at me.prettyprint.cassandra.service.KeyspaceServiceImpl.batchMutate(KeyspaceServiceImpl.java:102)
    at me.prettyprint.cassandra.service.KeyspaceServiceImpl.batchMutate(KeyspaceServiceImpl.java:108)
    at me.prettyprint.cassandra.model.MutatorImpl$3.doInKeyspace(MutatorImpl.java:222)
    at me.prettyprint.cassandra.model.MutatorImpl$3.doInKeyspace(MutatorImpl.java:219)
    at me.prettyprint.cassandra.model.KeyspaceOperationCallback.doInKeyspaceAndMeasure(KeyspaceOperationCallback.java:20)
    at me.prettyprint.cassandra.model.ExecutingKeyspace.doExecute(ExecutingKeyspace.java:85)
    at me.prettyprint.cassandra.model.MutatorImpl.execute(MutatorImpl.java:219)
    at me.prettyprint.cassandra.model.MutatorImpl.insert(MutatorImpl.java:59)
    at org.cassandra.examples.MySample.main(MySample.java:25)
Caused by: InvalidRequestException(why:Keyspace apples does not exist)
    at org.apache.cassandra.thrift.Cassandra$set_keyspace_result.read(Cassandra.java:5302)
    at org.apache.cassandra.thrift.Cassandra$Client.recv_set_keyspace(Cassandra.java:481)
    at org.apache.cassandra.thrift.Cassandra$Client.set_keyspace(Cassandra.java:456)
    at me.prettyprint.cassandra.connection.HThriftClient.getCassandra(HThriftClient.java:68)
    ... 11 more

预先感谢您的帮助。

最佳答案

你得到的异常(exception)是,

why:Keyspace apples does not exist

在你的代码中,这一行实际上并没有创建键空间,

Keyspace keyspace =  HFactory.createKeyspace("apples", cluster);

如所述here ,这是您定义键空间所需的代码,

ColumnFamilyDefinition cfDef = HFactory.createColumnFamilyDefinition("MyKeyspace", "ColumnFamilyName", ComparatorType.BYTESTYPE);

KeyspaceDefinition newKeyspace = HFactory.createKeyspaceDefinition("MyKeyspace", ThriftKsDef.DEF_STRATEGY_CLASS,  replicationFactor, Arrays.asList(cfDef));

// Add the schema to the cluster.
// "true" as the second param means that Hector will block until all nodes see the change.
cluster.addKeyspace(newKeyspace, true);

关于java - 赫克托与 Cassandra 的基础知识,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6948893/

相关文章:

java - Hector 或 CQL,我应该使用哪个

java - 如何根据用户输入的值循环/重复程序?

java - Intellij-Idea - Tomcat 远程更新

java - 在运行时更改 Web 应用程序的状态

postgresql - Cassandra 如何处理磁盘 IO

cassandra - 如何使用 Hector/Cassandra 设置计数器?

cassandra - Cassandra DB 的 Hector API 中的 SliceQuery 抛出 HInvalidRequestException : InvalidRequestException(why:Key may not be empty)

java - 如何在我的代码中添加 try 、 catch 语句以仅接受整数值?

merge - 当 KEY 已作为批处理存在时,Cassandra 更新或插入

cassandra - 将 Joda 时间映射到 Cassandra 的 Date 类型