Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cassandra Java request

Tags:

java

cassandra

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 :

My database screenshot

like image 247
John Doee Avatar asked Nov 25 '25 18:11

John Doee


1 Answers

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.

like image 137
Ruba Avatar answered Nov 27 '25 08:11

Ruba



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!