2012-06-20 105 views
2

我正在尝试为Cassandra和Java一起使用Astyanax。我尝试了https://github.com/Netflix/astyanax/wiki/Getting-Started的例子。我有我刚才从这个链接复制代码:Cassandra Astyanax文档

package def; 

import com.netflix.astyanax.AstyanaxContext; 
import com.netflix.astyanax.Keyspace; 
import com.netflix.astyanax.MutationBatch; 
import com.netflix.astyanax.connectionpool.NodeDiscoveryType; 
import com.netflix.astyanax.connectionpool.OperationResult; 
import com.netflix.astyanax.connectionpool.exceptions.ConnectionException; 
import com.netflix.astyanax.connectionpool.impl.ConnectionPoolConfigurationImpl; 
import com.netflix.astyanax.connectionpool.impl.CountingConnectionPoolMonitor; 
import com.netflix.astyanax.impl.AstyanaxConfigurationImpl; 
import com.netflix.astyanax.model.Column; 
import com.netflix.astyanax.model.ColumnFamily; 
import com.netflix.astyanax.model.ColumnList; 
import com.netflix.astyanax.serializers.StringSerializer; 
import com.netflix.astyanax.thrift.ThriftFamilyFactory; 

public class sample { 

    public static void main(String[] args) throws Exception{ 
    AstyanaxContext<Keyspace> context = new AstyanaxContext.Builder() 
    .forCluster("Test Cluster") 
    .forKeyspace("KeyspaceName") 
    .withAstyanaxConfiguration(new AstyanaxConfigurationImpl()  
     .setDiscoveryType(NodeDiscoveryType.NONE) 
    ) 
    .withConnectionPoolConfiguration(new ConnectionPoolConfigurationImpl("MyConnectionPool") 
     .setPort(9160) 
     .setMaxConnsPerHost(10) 
     .setSeeds("127.0.0.1:9160") 
    ) 
    .withConnectionPoolMonitor(new CountingConnectionPoolMonitor()) 
    .buildKeyspace(ThriftFamilyFactory.getInstance()); 

    context.start(); 
    Keyspace keyspace = context.getEntity(); 

    ColumnFamily<String, String> CF_USER_INFO = 
       new ColumnFamily<String, String>(
       "Standard1",    // Column Family Name 
       StringSerializer.get(), // Key Serializer 
       StringSerializer.get()); // Column Serializer 

    // Inserting data 
    MutationBatch m = keyspace.prepareMutationBatch(); 

    m.withRow(CF_USER_INFO, "acct1234") 
     .putColumn("firstname", "john", null) 
     .putColumn("lastname", "smith", null) 
     .putColumn("address", "555 Elm St", null) 
     .putColumn("age", 30, null); 

    m.withRow(CF_USER_INFO, "acct1234") 
     .incrementCounterColumn("loginCount", 1); 

    try { 
     OperationResult<Void> result = m.execute(); 
    } catch (ConnectionException e) { 
    } 

    System.out.println("completed the task!!!"); 

    OperationResult<ColumnList<String>> result = 
       keyspace.prepareQuery(CF_USER_INFO) 
       .getKey("Key1") 
       .execute(); 
      ColumnList<String> columns = result.getResult(); 

      // Lookup columns in response by name 
      int age  = columns.getColumnByName("age").getIntegerValue(); 
      long counter = columns.getColumnByName("loginCount").getLongValue(); 
      String address = columns.getColumnByName("address").getStringValue(); 

      // Or, iterate through the columns 
      for (Column<String> c : result.getResult()) { 
       System.out.println(c.getName()); 
      } 
    } 

} 

但是当我运行此我得到一个异常:

log4j:WARN No appenders could be found for logger (com.netflix.astyanax.connectionpool.impl.ConnectionPoolMBeanManager). 
log4j:WARN Please initialize the log4j system properly. 
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info. 
completed the task!!! 
Exception in thread "main" com.netflix.astyanax.connectionpool.exceptions.BadRequestException: BadRequestException: [host=127.0.0.1(127.0.0.1):9160, latency=0(0), attempts=1] InvalidRequestException(why:Keyspace KeyspaceName does not exist) 
    at com.netflix.astyanax.thrift.ThriftConverter.ToConnectionPoolException(ThriftConverter.java:159) 
    at com.netflix.astyanax.thrift.ThriftSyncConnectionFactoryImpl$1.execute(ThriftSyncConnectionFactoryImpl.java:119) 
    at com.netflix.astyanax.connectionpool.impl.AbstractExecuteWithFailoverImpl.tryOperation(AbstractExecuteWithFailoverImpl.java:52) 
    at com.netflix.astyanax.connectionpool.impl.AbstractHostPartitionConnectionPool.executeWithFailover(AbstractHostPartitionConnectionPool.java:229) 
    at com.netflix.astyanax.thrift.ThriftColumnFamilyQueryImpl$1.execute(ThriftColumnFamilyQueryImpl.java:180) 
    at def.sample.main(sample.java:68) 
Caused by: InvalidRequestException(why:Keyspace KeyspaceName does not exist) 
    at org.apache.cassandra.thrift.Cassandra$set_keyspace_result.read(Cassandra.java:4874) 
    at org.apache.thrift.TServiceClient.receiveBase(TServiceClient.java:78) 
    at org.apache.cassandra.thrift.Cassandra$Client.recv_set_keyspace(Cassandra.java:489) 
    at org.apache.cassandra.thrift.Cassandra$Client.set_keyspace(Cassandra.java:476) 
    at com.netflix.astyanax.thrift.ThriftSyncConnectionFactoryImpl$1.execute(ThriftSyncConnectionFactoryImpl.java:109) 
    ... 4 more 

谁能告诉我有什么错呢?没有适当的文件可用于此。所以,你能帮我一把吗?甚至给我一些链接,我可以从中获得更多的例子。

回答

3
why:Keyspace KeyspaceName does not exist 

上面的错误是相当不言自明的。当应用程序连接到本地主机时,密钥空间不存在。因此,请确保您创建密钥空间,然后重新运行您的应用程序。我想你想看看this。摘自线程,

该Keyspace作为客户端只,并不创建密钥空间 或cassandra列家庭。您可以使用AsytanaxContext.Builder 构建一个集群接口,通过它您可以实际创建 密钥空间和列族。

这个link的单元测试应该为您提供关于如何在集群中创建密钥空间的足够信息。

+0

好..有没有什么办法可以在代码本身中创建密钥空间?我没有办法做到这一点。这将工作在我已经创建了键空间和列族时。但我以前不想克里特。我怎样才能通过代码做到这一点? – shalki

+1

@shalki我对astyanax并不熟悉,但我认为你可以创建密钥空间。查看更新后的答案。 – Jasonw

+0

好的..谢谢你的所有帮助...... – shalki

0

您的代码示例被编写为在127.0.0.1上与localhost上的Cassandra服务器的运行实例进行通信。如果你有Cassandra在其他地方运行,或者根本没有运行,那么你需要在执行你的代码之前安装和设置该服务器环境。

相关问题