当前位置: 首页 > 知识库问答 >
问题:

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

金秦斩
2023-03-14

我正在使用Astyanax使用CQL3查询来查询Cassandra,它工作得很好。

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。

谢谢你。

共有1个答案

仇征
2023-03-14

我认为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)
);
 类似资料:
  • 好吧,我的问题是这段代码没有返回任何内容,至少它没有打印任何内容。唯一有效的方法是$user_email,它可以打印用户的电子邮件。 代码: “用户”表包含7个不同的键: ID(bigint,主键,auto_increment),组(int),电子邮件(varchar),密码(varchar),用户名(varchar),名称(varchar),验证(int)。 "group"表包含3个不同的键:

  • 问题内容: 嘿,我只是学习了如何将SQL语句放入VBA(或至少将它们写出),但是我不知道如何获取返回的数据? 我有一些基于查询的表格(图表形式),这些查询针对的是我定期运行的常规参数,只是更改了时间范围(例如,当月交易量排名前10位的商品)。然后,我有一些将图表对象自动传输到PowerPoint演示文稿中的过程。因此,我已经预先构建了所有这些查询(例如63),并且要匹配图表格式(嗯,是的....

  • 问题内容: 我想知道什么是从hibernate查询中获取地图数组的最佳方法。Google表示要迭代query.list(),然后将对象创建/放入空地图数组。 我想会有一些优雅而有效的方法来做到这一点。有人可以给我个主意吗? 问题答案: 参见Hibernate文档-15.6。select子句: 您可以使用以下方式为选定的表达式分配别名: 与选择新地图一起使用时,这非常有用: 该查询返回一个从别名到所

  • 问题内容: 我的代码有问题。 像这样的情况: 我有一个下拉列表,如果选择“个人”,则会出现新的下拉列表,其中包含从数据库查询中检索到的数据;如果选择“公开”,则该下拉列表将消失。 这样的HTML代码: 查询如下: 像这样的JavaScript代码: 我不知道如何将值/结果发送到javascript代码(选择选项中的值和名称)。 问题答案: 在javascript中,您必须对您的php文件进行操作:

  • 我正在开发Spring Boot应用程序与Spring Data JPA和H2数据库。我使用的是sping-data-jpa。当我使用ManyTo很多映射器类来获取另一个类的数据时。但是我发现它是NULL。 代码在github上 书班 著者班 测试中的测试代码捕捉器。班 我的代码有错误吗?或者其他方式? 非常感谢。

  • 问题内容: 如果我的查询包含一个类,例如: 然后我迭代它,那里是一个类的对象。 那么如何从包含多个类的查询中获取结果呢?例如: 问题答案: for (Object[] result : query.list()) { User user = (User) result[0]; Group group = (Group) result[1]; }