1

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

JavaBeigner
  • 608
  • 9
  • 28
shalvi_pandita
  • 143
  • 1
  • 9
  • 2
    And can you connect with a regular csql client? Beside, I would probably upgrade your cassandra jars to match the server more closerly. – Niels Bech Nielsen Sep 02 '14 at 12:55
  • 1
    Hi, thanks for the reply. I have not tried with a regular csql client yet. Also, I had tested a lot of combinations of cassandra jars but unfortunately could not connect. – shalvi_pandita Sep 02 '14 at 18:08

2 Answers2

1

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

rs_atl
  • 8,935
  • 1
  • 23
  • 28
  • Thanks for the reply, I will try with the new native java driver. Also I wanted to ask, will CQL hector java client for cassandra be a better option to explore ? – shalvi_pandita Sep 02 '14 at 18:20
  • 2
    No, use the native driver from DataStax. Hector is an older Thrift-based client, whereas the native driver uses the new binary protocol and supports all the newer features. – rs_atl Sep 02 '14 at 18:23
  • Thanks ! Will use the native driver then ! – shalvi_pandita Sep 02 '14 at 18:28
  • Hi , I followed the [link](http://www.datastax.com/documentation/developer/java-driver/1.0/java-driver/quick_start/qsSimpleClientCreate_t.html) but I am getting an error - Exception in thread "main" com.datastax.driver.core.exceptions.NoHostAvailableException: All host(s) tried for query failed ... For the error I also checked this [link](http://stackoverflow.com/questions/19538585/unable-to-get-cassandra-to-work-in-basic-applcation).. All configurations are correct. Any Suggestions / Advice ? – shalvi_pandita Sep 03 '14 at 09:58
  • Then your issue is either bad configuration, or something preventing your client machine from talking to your Cassandra instance. – rs_atl Sep 03 '14 at 12:38
0

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