java - 如何从 Astyanax CQL 查询获取数据类型

标签 java cassandra cql astyanax cql3

我正在使用 Astyanax 使用 CQL3 查询来查询 Cassandra,并且运行良好。

我只想进行查询(SELECT ...),我使用以下代码作为示例:

AstyanaxContext<Keyspace> context = new AstyanaxContext.Builder()
    .forCluster("anyCluster") // Not using clusters
    .forKeyspace("default") // Name of my keyspace
    .withAstyanaxConfiguration(new AstyanaxConfigurationImpl()
    .setDiscoveryType(NodeDiscoveryType.RING_DESCRIBE)
    .setCqlVersion("3.0.0")
    .setTargetCassandraVersion("1.2")
    )
    .withConnectionPoolConfiguration(new ConnectionPoolConfigurationImpl("MyConnectionPool")
    .setPort(9160)
    .setMaxConnsPerHost(1)
    .setSeeds("localhost:9160")
    )
    .withConnectionPoolMonitor(new CountingConnectionPoolMonitor())
    .buildKeyspace(ThriftFamilyFactory.getInstance());

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

    // Defining any columnfamily
    ColumnFamily<String, String> cf =
            new ColumnFamily<String, String>(
              ".",              // It works without passing here the name.
              StringSerializer.get(),   // Key Serializer
              StringSerializer.get());

前面的代码是连接的一部分,现在我想执行查询并获取数据,但我不知道查询中期望什么数据类型,所以我不知道该用什么方法用于获取值,如下所示,我不知道是否需要使用 getBooleanValuegetStringValuegetIntegerValue 等.

    try {
        OperationResult<CqlResult<String, String>> result
        = keyspace.prepareQuery(emp2).withCql("Select * from table_test;").execute();

        for (Row<String, String> row : result.getResult().getRows()) {

            ColumnList<String> cols = row.getColumns();
            System.out.println(cols.getColumnNames());

            for(String col : cols.getColumnNames()){

                try{
                    boolean value = cols.getBooleanValue(col, null);
                    System.out.println(value);
                }catch(Exception e){
                    System.out.println(col + " isn't boolean");
                }
                try{
                    Date value = cols.getDateValue(col, null);
                    System.out.println(value);
                }catch(Exception e){
                    System.out.println(col + " isn't Date");
                }
                try{
                    Integer value = cols.getIntegerValue(col, null);
                    System.out.println(value);
                }catch(Exception e){
                    System.out.println(col + " isn't Integer");
                }
                try{
                    Double value = cols.getDoubleValue(col, null);
                    System.out.println(value);
                }catch(Exception e){
                    System.out.println(col + " isn't Double");
                }
                try{
                    String value = cols.getStringValue(col, null);
                    System.out.println(value);
                }catch(Exception e){
                    System.out.println(col + " isn't string");
                }
            }
        }
    } catch (ConnectionException e) {
        e.printStackTrace();
    }

有什么方法可以让我知道这一点吗?使用此 API,或者可能使用不同的 API。

谢谢。

最佳答案

我认为 Astyanax 客户端无法为您提供所有数据类型信息,而使用 CQLSH 您可以获得其中的大部分信息。

select column_name, comparator, column_aliases,key_alias,key_validator from system.schema_columns where keyspace_name='#KS' AND columnfamily_name='#CF';

对于任何其他元字段相关信息,您可以查看结构

CREATE TABLE schema_keyspaces (
  keyspace_name text PRIMARY KEY,
  durable_writes boolean,
  strategy_class text,
  strategy_options text
);


CREATE TABLE schema_columnfamilies (
  keyspace_name text,
  columnfamily_name text,
  bloom_filter_fp_chance double,
  caching text,
  column_aliases text,
  comment text,
  compaction_strategy_class text,
  compaction_strategy_options text,
  comparator text,
  compression_parameters text,
  default_read_consistency text,
  default_validator text,
  default_write_consistency text,
  gc_grace_seconds int,
  id int,
  key_alias text,
  key_aliases text,
  key_validator text,
  local_read_repair_chance double,
  max_compaction_threshold int,
  min_compaction_threshold int,
  read_repair_chance double,
  replicate_on_write boolean,
  subcomparator text,
  type text,
  value_alias text,
  PRIMARY KEY (keyspace_name, columnfamily_name)
);

CREATE TABLE schema_columns (
  keyspace_name text,
  columnfamily_name text,
  column_name text,
  component_index int,
  index_name text,
  index_options text,
  index_type text,
  validator text,
  PRIMARY KEY (keyspace_name, columnfamily_name, column_name)
);

关于java - 如何从 Astyanax CQL 查询获取数据类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18474026/

相关文章:

java - 尝试连接 cassandra 时出错

node.js - 如何安全使用 Apache Cassandra?

java - 为什么使用十六进制文字的 Cassandra cql 查询有效,但 textAsBinary 无效?

cassandra - 如何获取 cql 查询的墓碑计数?

java - 如何为JUnit测试用例禁用EhCache

java - 将字符串转换为 double - Java

java - Cassandra 断言错误

cassandra - 如果添加新数据中心,是否需要运行 'repair'

cassandra - 无法使用 CQL 连接到 Cassandra 2.0.10

java - 突然错误: No static method setWebDialogTheme(I)V