简体   繁体   中英

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 :

  • apache-cassandra-0.8.4.jar
  • apache-cassandra-cql-1.0.3.jar
  • apache-cassandra-thrift-0.8.4.jar
  • casssandra-clientutil-1.2.1.jar
  • cassandra-jdbc-1.2.5.jar
  • commons-lang-2.4.jar
  • guava-r08.jar
  • libthrift-0.6.jar
  • log4j-1.2.16.jar
  • slf4j-log4j12-1.6.1.jar
  • slf4j.api-1.6.1.jar

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

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

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM