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.