首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Cassandra Astyanax文档

Cassandra Astyanax文档
EN

Stack Overflow用户
提问于 2012-06-20 17:36:59
回答 2查看 8.1K关注 0票数 2

我正在尝试使用Astyanax for Cassandra with Java。我在https://github.com/Netflix/astyanax/wiki/Getting-Started上试过这个例子。我有我刚刚从这个链接复制的代码:

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

}

但是当我运行这段代码时,我得到了一个异常:

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

有人能告诉我这是怎么回事吗?也没有合适的文档来说明这一点。所以你能帮帮我吗。甚至给我一些链接,在那里我可以获得更多的例子。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-06-21 12:25:58

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

上面的错误不言而喻。当应用程序连接到localhost时,密钥空间不存在。因此,请确保创建了密钥空间,然后重新运行应用程序。

从评论中,我认为你想研究一下this。摘录自帖子,

键空间仅用作客户端,并不在cassandra上创建键空间或列族。您可以使用AsytanaxContext.Builder构造一个集群接口,通过该接口可以实际创建键空间和列族。

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

票数 3
EN

Stack Overflow用户

发布于 2012-06-20 21:04:58

您的代码示例旨在与位于127.0.0.1的本地主机上正在运行的Cassandra服务器实例对话。如果在其他地方运行Cassandra,或者根本没有运行Cassandra,那么在执行代码之前需要安装和设置服务器环境。

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

https://stackoverflow.com/questions/11116714

复制
相关文章

相似问题

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