Domanda

I am using Astyanax to query Cassandra using CQL3 queries, and it is working fine.

I only want to make queries (SELECT ...), and I am using the following code for example:

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

The previous code is the part of the connection and now, I want to execute queries and get the data, but I don't know what Data Type I am expecting on the queries, so I don't know what method to use to get the values, like you can see below, I don't know if I need to use getBooleanValue,getStringValue, getIntegerValue, etc.

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

So is there a way that I can know this? using this API, or maybe with a different one.

Thank you.

È stato utile?

Soluzione

I think Astyanax client can not provide you all the data types info, rather using CQLSH you can get most of them.

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

Also for any other meta field related information you can see the structure

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

Altri suggerimenti

If you use the java-driver to issue your CQL3 queries, then the getColumnDefinitions function on a row will tell you the types.

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

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top