Ayush Goyal
Ayush Goyal

Reputation: 392

Cassandra jdbc connection with Authentication

I am trying to connect to the Cassandra using cdata jdbc connector. However, I cannot find how to provide username and password in connection url. Here are my java code.

package com.dremio.exec.store.jdbc.conf;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class TestConnection {

    public static void main(String[] args) throws Exception {

        Connection con = null;
        Class.forName("org.apache.cassandra.cql.jdbc.CassandraDriver");
        con = DriverManager.getConnection("jdbc:cassandra://localhost:9160");

        String query = "SELECT * FROM emp";

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

        while (result.next()) {
            System.out.println(result.getString("emp_id"));
            System.out.println(result.getString("emp_name"));
            System.out.println(result.getString("emp_city"));
        }
        con.close();
    }
}

Currently, I am providing url as without Authentication. How can i provide username and password there.

Upvotes: 1

Views: 1161

Answers (1)

Aaron
Aaron

Reputation: 57843

CData's (poor) documentation lists User and Password as available properties to add to a JDBC connection string. You can add these with your username and password to authenticate:

con = DriverManager.getConnection(
        "jdbc:cassandra://localhost:9160;User=ayush;Password=yourpwd");

You could also add them as properties, as detailed in their Connecting From Code examples:

Properties prop = new Properties();
prop.setProperty("Port","9042");
prop.setProperty("Server","127.0.0.1");
prop.setProperty("User","ayush");
prop.setProperty("Password","yourpwd");
 
Connection conn = DriverManager.getConnection("jdbc:cassandra:",prop);

Also, unless you're using an old Cassandra cluster or 3.x cluster with Thrift enabled, 9160 isn't going to work. In fact, the Thrift project hasn't had a pull request in a few years now, so I would suggest moving away from it and using the native binary protocol on 9042.

Upvotes: 1

Related Questions