0

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());

前のコードは接続の一部であり、クエリを実行してデータを取得したいのですが、クエリで期待しているデータ型がわからないため、取得に使用する方法がわかりません以下に示すように、値はgetBooleanValue、、、などを使用する必要があるかどうかわかりません。getStringValuegetIntegerValue

    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 を使用することもできます。

ありがとうございました。

4

2 に答える 2

2

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)
);
于 2013-08-27T20:14:18.163 に答える
1

java-driverを使用して CQL3 クエリを発行すると、行の getColumnDefinitions 関数が型を教えてくれます。

http://www.datastax.com/drivers/java/apidocs/com/datastax/driver/core/Row.html#getColumnDefinitions()

于 2013-08-28T20:32:33.557 に答える