【问题标题】:cassandra 1.1.x get by composite keycassandra 1.1.x 通过复合键获取
【发布时间】:2012-06-21 10:34:17
【问题描述】:

是否可以使用 Hector 或 Astyanax 通过复合键获取行(在多列中,而不是序列化为一列的行)?

在 cqlsh 中我创建了简单的列族:

CREATE COLUMNFAMILY kkvv (x int, y int, val1 varchar, val2 varchar, PRIMARY KEY (x,y));

根据Cassandra Developer Center,行由 x 作为键存储,其余存储在列中。

我不知道如何获取给定 x 和 y 的列切片。

在那个cql中执行cql

cqlQuery.setQuery("select * from kkvv")

给我行:

行(2,ColumnSlice([HColumn(x=2)]))

行(10,ColumnSlice([HColumn(x=10)]))

控制台 cqlsh 给出:

x |是 | val1 | val2

----+-----+-------+------------

2 | 1 | v1_1 | v2_1

10 | 27 | v1_4b | v2_4b

10 | 91 | v1_4a | v2_4a

有没有人在任何 cassandra 的 java 客户端中做到了这一点? 我可以为此使用 thrift,还是它是 cql 唯一的功能?

【问题讨论】:

    标签: cassandra thrift hector cql astyanax


    【解决方案1】:

    这里有两种略有不同的语法:CQL 2 和 CQL 3。默认情况下,Cassandra 连接需要 CQL 2。但是,CQL 2 不理解您在此处所做的那种复合键列族.

    所以您显然正确地将 CQL 3 与 cqlsh 一起使用,因为它以一种理智的方式显示您的列,但您没有将它与 Hector 一起使用。我不确定 Hector 或 Astyanax 是否支持这一点。最新版本的 cassandra-jdbc 驱动程序可以,因此,如果 Hector 和/或 Astyanax 使用它,那么它们也应该可以工作。

    在 Thrift 中没有(也可能不会)任何支持将复合比较器列族视为具有多组件主键的表,就像 CQL 3 所做的那样。如果需要,请使用 CQL 3。

    【讨论】:

    • 我想知道如何以及是否能够将复合键与为 hadoop 提供的可写批量加载程序一起使用!?
    • 我查过了,和你说的一样。此外,可以使用 Astyanax 或 Hector,但您必须警告源代码以强制使用 cql3。
    【解决方案2】:

    您是否尝试过 cassandra-tutorial 项目中提供的 CompositeQuery.java 示例?

    另外,你读过 DataStax 的 Introduction to Composite Columns 吗?

    【讨论】:

      【解决方案3】:

      here 很好地解释了如何在 Cassandra 中存储具有复合键的行。

      在 Astyanax 和 Hector 中,我注意到有趣的事情 - 当 a 尝试连接时 - 它使用了 CQL2。当我使用 CQL3 和 cassandra api(来自下面示例的代码)连接到 Cassandra 时,在某处存储了这个设置,之后 Astyanax 和 Hector 使用 cql3 而不是 CQL2。连接是作为单独的执行进行的,因此无法将其存储在客户端...有人对此有任何想法吗?

      CQL 版本可以通过 set_cql_version 方法在 org.apache.cassandra.thrift.Cassandra.Client 上设置。

      如果有人对使用纯 Cassandra api 的工作示例感兴趣:

      import java.io.UnsupportedEncodingException;
      import java.nio.ByteBuffer;
      import java.util.List;
      
      import org.apache.cassandra.thrift.Cassandra;
      import org.apache.cassandra.thrift.Column;
      import org.apache.cassandra.thrift.Compression;
      import org.apache.cassandra.thrift.CqlResult;
      import org.apache.cassandra.thrift.CqlRow;
      import org.apache.cassandra.thrift.InvalidRequestException;
      import org.apache.cassandra.thrift.SchemaDisagreementException;
      import org.apache.cassandra.thrift.TimedOutException;
      import org.apache.cassandra.thrift.UnavailableException;
      import org.apache.thrift.TException;
      import org.apache.thrift.protocol.TBinaryProtocol;
      import org.apache.thrift.protocol.TProtocol;
      import org.apache.thrift.transport.TFramedTransport;
      import org.apache.thrift.transport.TSocket;
      import org.apache.thrift.transport.TTransport;
      
      public class KKVVGetter {
          private static Cassandra.Client client;
          private static TTransport       transport;
      
          public static void main(String[] args) throws UnsupportedEncodingException, InvalidRequestException,
                  UnavailableException, TimedOutException, SchemaDisagreementException, TException {
      
              transport = new TFramedTransport(new TSocket("localhost", 9160));
              TProtocol protocol = new TBinaryProtocol(transport);
              client = new Cassandra.Client(protocol);        
              transport.open();
              client.set_cql_version("3.0.0");
      
              executeQuery("USE ks_test3");
      
              show("select x,y,val1,val2 from kkvv where x > 1 and x < 11 and y < 100 and y > 2");
      
              System.out.println("\n\n*****************************\n\n");
      
              show("select x,y,val1,val2 from kkvv");
      
              transport.close();
          }
      
          private static int toInt(byte[] bytes) {
              int result = 0;
              for (int i = 0; i < 4; i++) {
                  result = (result << 4) + (int) bytes[i];
              }
              return result;
          }
      
          private static CqlResult executeQuery(String query) throws UnsupportedEncodingException, InvalidRequestException,
                  UnavailableException, TimedOutException, SchemaDisagreementException, TException {
              return client.execute_cql_query(ByteBuffer.wrap(query.getBytes("UTF-8")), Compression.NONE);
          }
      
          private static void show(String query) throws UnsupportedEncodingException, InvalidRequestException,
                  UnavailableException, TimedOutException, SchemaDisagreementException, TException {
              CqlResult result = executeQuery(query);
              List<CqlRow> rows = result.getRows();
              System.out.println("rows: " + rows.size());
              for (CqlRow row : rows) {
                  System.out.println("columns: " + row.getColumnsSize());
                  for (Column c : row.getColumns()) {
                      System.out.print("  " + new String(c.getName()));
                      switch (new String(c.getName())) {
                          case "x":
                          case "y":
                              System.out.print("  " + toInt(c.getValue()));
                              break;
                          case "val1":
                          case "val2":
                              System.out.print("  " + new String(c.getValue()));
                              break;
      
                          default:
                              break;
                      }
                      System.out.println();
                  }
              }
          }
      }
      

      相关架构示例。

      【讨论】:

        猜你喜欢
        • 2012-08-31
        • 1970-01-01
        • 2021-02-16
        • 2021-05-14
        • 2013-11-01
        • 1970-01-01
        • 2014-05-24
        • 2016-11-28
        • 1970-01-01
        相关资源
        最近更新 更多