Domanda

What should be the connection string while using CQL jdbc driver?

Will I be able to find a proper/complete example for CQL using CQL JDBC driver in Java online?

È stato utile?

Soluzione

You'll need the cql jar from the apache site.

Here's the basic test I used after entering data via CLI (using sample from wiki):

public class CqlJdbcTestBasic {
public static void main(String[] args) {
    Connection con = null;
    try {
        Class.forName("org.apache.cassandra.cql.jdbc.CassandraDriver");
        con = DriverManager.getConnection("jdbc:cassandra:root/root@localhost:9160/MyKeyspace");

        String query = "SELECT KEY, 'first', last FROM User WHERE age=42";

        Statement stmt = con.createStatement();
        ResultSet result = stmt.executeQuery(query);

        while (result.next()) {
            System.out.println(result.getString("KEY"));
            System.out.println(result.getString("first"));
            System.out.println(result.getString("last"));
        }

    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (SQLException e) {
        e.printStackTrace();
    } finally {
        if (con != null) {
            try {
                con.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            con = null;
        }
    }
}
}

The user/password (root/root) seems arbitrary, just be sure to specify the Keyspace (MyKeyspace)

Note, 'first' is quoted in the query string because it is an CQL keyword

Altri suggerimenti

You may also try using the cassandra-jdbc driver from http://code.google.com/a/apache-extras.org/p/cassandra-jdbc/.

Alternatively using the following maven dependency:

<dependency>
    <groupId>org.apache-extras.cassandra-jdbc</groupId>
    <artifactId>cassandra-jdbc</artifactId>
    <version>1.2.1</version>
</dependency>
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top