首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Hector & Cassandra基础

Hector & Cassandra基础
EN

Stack Overflow用户
提问于 2011-08-05 05:42:15
回答 2查看 10.9K关注 0票数 8

我正在使用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代码:

代码语言:javascript
复制
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"));
}
}

我的错误是:

代码语言:javascript
复制
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

提前感谢您的帮助。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-08-05 10:57:09

你得到的例外是,

代码语言:javascript
复制
why:Keyspace apples does not exist

在您的代码中,这一行实际上并不创建keyspace,

代码语言:javascript
复制
Keyspace keyspace =  HFactory.createKeyspace("apples", cluster);

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

代码语言:javascript
复制
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);
票数 8
EN

Stack Overflow用户

发布于 2011-08-06 00:46:26

我们在维基上也有a getting started guide,这可能会有一些帮助。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6948893

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档