Reputation: 59
I trying to execute a simple request in Java with Cassandra database but I don't understand my example it don't work :
public class Test {
public static void main( String[] args )
{
Cluster cluster = Cluster.builder()
.addContactPoints("127.0.0.1")
.build();
Session session = cluster.connect("test");
ResultSet results = session.execute("SELECT * FROM datatest");
for ( Row row : results ) {
System.out.println(row.getString("name"));
}
}
}
When I execute my code I have this error :
[main] INFO com.datastax.driver.core.policies.DCAwareRoundRobinPolicy - Using data-center name 'datacenter1' for DCAwareRoundRobinPolicy (if this is incorrect, please provide the correct datacenter name with DCAwareRoundRobinPolicy constructor)
[main] INFO com.datastax.driver.core.Cluster - New Cassandra host /127.0.0.1:9042 added
Exception in thread "main" java.lang.IllegalArgumentException: name is not a column defined in this metadata
at com.datastax.driver.core.ColumnDefinitions.getAllIdx(ColumnDefinitions.java:273)
at com.datastax.driver.core.ColumnDefinitions.getFirstIdx(ColumnDefinitions.java:279)
at com.datastax.driver.core.ArrayBackedRow.getIndexOf(ArrayBackedRow.java:69)
at com.datastax.driver.core.AbstractGettableData.getString(AbstractGettableData.java:137)
at fakemillions.Test.main(Test.java:23)
This is my database :
Upvotes: 3
Views: 4662
Reputation: 111
Try this work in my case
Cluster.Builder builder = Cluster.builder();
Cluster cluster=builder.withLoadBalancingPolicy(new TokenAwarePolicy(new DCAwareRoundRobinPolicy("us-east"))).addContactPoints("127.0.0.1").build();
Upvotes: 1
Reputation: 1931
When you create a table using thrift, which contains dynamic columns, and therefore did not give explicit column definitions, then CQL by default assign the following names: key
, column1
, value
. In your case it will have column1
contain the value "name", value
column will contain "TiTi", etc.
Think about it as this. The data is stored really as key-value pairs. CQL can't infer any column names from these key-value pairs, since there may be billions of different keys (column names) in the same row.
Here is a good guide to migrate thrift tables to cql: http://www.datastax.com/dev/blog/thrift-to-cql3
Upvotes: 2
Reputation: 494
It sounds like you haven't defined a column with the name "name" in the table datatest. I haven't used Datastax driver before, but as the documentation says the IllegalArgumentException is thrown if
name is not part of the ResultSet this row is part of, i.e. if !this.columns().names().contains(name)
It'd be better if you provided how you created the datatest table.
Upvotes: 3