0

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.

eLRuLL
  • 18,488
  • 9
  • 73
  • 99

2 Answers2

2

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)
);
abhi
  • 4,762
  • 4
  • 29
  • 49
  • I ended up using `select comparator, column_aliases,key_alias,key_validator from system.schema_columnfamilies where "columnfamily"='#CF';` for the `composite PRIMARY KEY` and now I want to use your query to get the ones that aren't `PRIMARY KEY`, but it isn't showing some tables (these tables aren't created with `COMPACT STORAGE`, do you know why could this be happening? and what to do? – eLRuLL Aug 28 '13 at 21:30
  • the tables not displayed by your query are the ones created WITH `COMPACT STORAGE` (on the previous comment I said the opposite) – eLRuLL Aug 28 '13 at 21:39
  • I am little bit confused.. do you really need a CF with compact storage, knowing the fact that they support one and only one column outside of the ones part of the PRIMARY KEY. – abhi Aug 29 '13 at 06:01
  • Yes, and the data for that column is adding `alue_alias, default_validator` to the query. Now it works perfectly. Could you update your answer will all the info added here to accept it please? – eLRuLL Aug 29 '13 at 06:15
1

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

Zanson
  • 3,991
  • 25
  • 31