【问题标题】:How to do Cassandra CQL queries with Astyanax?如何使用 Astyanax 进行 Cassandra CQL 查询?
【发布时间】:2013-10-02 05:07:15
【问题描述】:

我正在使用 Astyanax 1.56.26 版和 Cassandra 1.2.2 版

好的,一些情况概述:

  1. 我使用 cqlsh 创建了一个非常简单的列族,如下所示:

    CREATE TABLE users ( name text PRIMARY KEY, age int, weight int );

  2. 我填充了列族(没有空列)

  3. 通过 cqlsh 查询 users 产生预期结果

  4. 现在我想以编程方式查询users,所以我尝试类似:

ColumnFamily<String, String> users =  
  new ColumnFamily<String, String>("users", StringSerializer.get(), StringSerializer.get());
OperationResult<ColumnList<String>> result = ks.prepareQuery(users)
  .getRow("bobbydigital") // valid rowkey
  .execute();
ColumnList<String> columns = result.getResult();
int weight = columns.getColumnByName("weight").getIntegerValue();
  1. 在分配weight 期间,抛出了一个NPE! :(

我的理解是result 应该包含与包含"bobbydigital" 作为其行键的行关联的所有列。然后我尝试将名为"weight" 的列中的值分配给整数变量weight。我知道变量 columns 正在被分配,因为当我在分配后立即添加一些调试代码时,如下所示:

System.out.println("Column names = " + columns.getColumnNames());

我得到以下输出:

Column names = [, age, weight]

那么为什么是空指针呢?有人能告诉我哪里出错了吗?还有,为什么会有空白的列名?

更新:
另外,如果我尝试以不同的方式进行查询,例如:

Column<String> result = ks.prepareQuery(users)
  .getKey("bobbydigital")
  .getColumn("weight")
  .execute().getResult();
int x = result.getIntegerValue();

我得到以下异常:
InvalidRequestException(why:Not enough bytes to read value of component 0)

提前感谢您提供的任何帮助!

【问题讨论】:

    标签: java nosql cassandra cql astyanax


    【解决方案1】:

    我发现我做错了什么。我尝试的查询方式不适用于 CQL 表。要使用 Astyanax 查询 CQL 表,您需要将 .withCQL 方法链接到您的 prepareQuery 方法;将 CQL 语句作为参数传递。

    可以在here 找到特定于将 CQL 与 Astyanax 结合使用的信息。

    【讨论】:

    • 应该注意,Astyanax 的 CQL 处理当前存在一个错误,如问题 here 的答案中所述。出于这个原因,正如该答案所暗示的那样,我现在正在使用来自 Datastax 的Java Driver
    【解决方案2】:

    我通过添加 setCqlVersion 解决了这个问题

    this.astyanaxContext = new AstyanaxContext.Builder() .forCluster("ClusterName") .forKeyspace(keyspace) .withAstyanaxConfiguration( new AstyanaxConfigurationImpl().setCqlVersion( "3.0.0").setDiscoveryType(

    并在创建表时添加WITH COMPACT STORAGE

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-11-15
      • 1970-01-01
      • 2014-02-04
      • 2015-11-09
      • 2019-05-18
      • 2015-03-10
      • 1970-01-01
      相关资源
      最近更新 更多