2011-08-04 185 views
8

我正在使用Cassandra-0.8.2。 我与最新版本的赫克托&工作 我的Java版本为1.6.0_26Hector&Cassandra的基础知识

我很新的卡桑德拉&赫克托。

我在做什么: 1.连接到一个不同的服务器上运行的cassandra的实例&。我知道它正在运行b/c我可以通过我的终端ssh进入运行该Cassandra实例的服务器并运行具有完整功能的CLI。 2.然后我想连接到一个keyspace &创建一个列族,然后通过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 

预先感谢您的帮助。

+0

您是否注意到'Keyspace苹果不存在? – Mat

回答

8

你所得到的例外是,

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); 
+0

谢谢你的回应。我的问题是,当我尝试运行此代码时,replicationFactor似乎未定义。我完全复制了这里提供的内容:https://github.com/rantav/hector/wiki/Getting-started-%285-minutes%29,并更改了适当的名称变量。任何想法为什么“replicationFactor”是未定义的?我没有别的东西可以导入这门课。 – Henry

+0

您正在收到编译器错误?你可以添加行int replicationfactor = 1; – sbridges

+0

谢谢,我想出了我的问题! – Henry