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?
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;...
jdbc:Cassandra:URL=http://myCassandra.com;User=myUser;Password=myPassword; Click Test Connection.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With