shalvi_pandita
shalvi_pandita

Reputation: 143

Connecting to remote Cassandra Database through Eclipse

Cassandra Database has been installed on a server machine with following configurations : cqlsh 4.1.1 | Cassandra 2.0.7.31 | CQL spec 3.1.1 | Thrift protocol 19.39.0

I wanted to connect to a keyspace say "X" via eclipse through java.

Following is my code :

package cassandraConnectivity; 
import java.sql.DriverManager;
import java.sql.SQLException;

public class connect{

public static java.sql.Connection con = null;

public static void main(String[] a)throws ClassNotFoundException, SQLException{
   try {
        Class.forName("org.apache.cassandra.cql.jdbc.CassandraDriver");
        con=DriverManager.getConnection("jdbc:cassandra:username/pswd@<IP>/<KS>");
        System.out.println("cassandra connection established");
        } catch (ClassNotFoundException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        } catch (SQLException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        }
       }
}

I have also added following jar files to my eclipse build path :

I have also disabled the firewall at the remote location where cassandra is installed

But despite this I am getting an error :

Exception in thread "main" org.apache.cassandra.cql.jdbc.DriverResolverException: java.net.ConnectException: Connection refused: connect at org.apache.cassandra.cql.jdbc.CassandraConnection.(CassandraConnection.java:91) at org.apache.cassandra.cql.jdbc.CassandraDriver.connect(CassandraDriver.java:86) at java.sql.DriverManager.getConnection(Unknown Source) at java.sql.DriverManager.getConnection(Unknown Source) at cassandraConnectivity.connect.main(connect.java:15)

Also it is not able to find the source for the above jar files that were added externally

Kindly let me know where i am going wrong

Upvotes: 1

Views: 2010

Answers (2)

Max Kotasek
Max Kotasek

Reputation: 21

You may want to use an established driver - something like Astyanax: https://github.com/Netflix/astyanax

The getting started page has some pretty straightforward examples:

AstyanaxContext<Keyspace> context = new AstyanaxContext.Builder()
.forCluster("ClusterName")
.forKeyspace("KeyspaceName")
.withAstyanaxConfiguration(new AstyanaxConfigurationImpl()      
    .setDiscoveryType(NodeDiscoveryType.RING_DESCRIBE)
)
.withConnectionPoolConfiguration(new ConnectionPoolConfigurationImpl("MyConnectionPool")
    .setPort(9160)
    .setMaxConnsPerHost(1)
    .setSeeds("127.0.0.1:9160")
)
.withConnectionPoolMonitor(new CountingConnectionPoolMonitor())
.buildKeyspace(ThriftFamilyFactory.getInstance());

context.start();
Keyspace keyspace = context.getClient();

Upvotes: 0

rs_atl
rs_atl

Reputation: 8985

The JDBC driver is very old. You should use the new native Java driver, which you can get here.

Upvotes: 1

Related Questions