Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What should be the connection string while using CQL jdbc driver

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?

like image 432
Tamil Avatar asked Oct 07 '11 14:10

Tamil


People also ask

What is the JDBC connection string?

A JDBC connection string is used to load the driver and to indicate the settings that are required to establish a connection to the data source. These settings are referred to as connection properties . The connection string is a URL with the following format: jdbc:rs:dv://host:port;Key=Value;Key=value;...

What is the JDBC URL for Cassandra?

jdbc:Cassandra:URL=http://myCassandra.com;User=myUser;Password=myPassword; Click Test Connection.

How to connect to Cassandra database in Java?

In order to connect to Cassandra from Java, we need to build a Cluster object. An address of a node needs to be provided as a contact point. If we don't provide a port number, the default port (9042) will be used. These settings allow the driver to discover the current topology of a cluster.


1 Answers

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

like image 74
libjack Avatar answered Sep 20 '22 18:09

libjack