Monday, September 1, 2014

Cassandra Connection from Java Client

Single node Cluster

Three Things to do :
i. Install Cassandra
ii. Download Eclipse and add Maven Plugin
iii.Write Client code in java  and run.

Cheers..

Install Cassandra :


The only prerequisites you’ll need for your Windows machine are:
  • Windows 7 or Windows 2008 server
  • Java
  • Either the Firefox or Chrome Web browser for DataStax OpsCenter (which doesn’t support Internet Explorer yet)
  • There is a dependency on the Visual C++ 2008 runtime (32bit), but Windows 7 and Windows 2008 Server R2 has it already installed. If you need it, see 
  • http://www.microsoft.com/download/en/details.aspx?displaylang=en&id=29
        And install the set up file of cassandra.


ii. Download Eclipse and add Maven Plugin : 

a)Download eclipse from the following url.

b) Add Maven Plugin :
    Open eclipse, click on help->Install New Software...
     in work with text box and press enter.
    


Check the check boxes in the middle panel and press next , then follow the screen instructions to install it online.
N.B. : This might take a little longer, so have patients while installing.

 Then ensure the maven is installed : click on window->preferences
you will find Maven in the left panel of preferences window.



iii.Write Client code in java  and run :

Create a maven project in eclipse :

File->New->Project->Maven->Maven Project




then press next ->next

put  groupId: com.example.cassandra
      artifactId: simple-client
  and press Finish.



Now your Maven project is ready and would have been enlisted in package explorer in eclipse.


and you will find your pom.xmll like this, if it is not then make the necessary correction as the below figure.

Now your project created.lets write java code to connect cassandra.

Create a package com.example.cassandra inside src->main folder, and make a class SimpleClient.java

Add the following code to SimpleClient.java

private Cluster cluster;
public void connect(String node) {
cluster = Cluster.builder()
        .addContactPoint(node).build();
  Metadata metadata = cluster.getMetadata();
  System.out.printf("Connected to cluster: %s\n", 
        metadata.getClusterName());
  for ( Host host : metadata.getAllHosts() ) {
     System.out.printf("Datacenter: %s; Host: %s; Rack: %s\n",
        host.getDatacenter(), host.getAddress(), host.getRack());
  }
}
public void close() {
  cluster.close();
}
public static void main(String[] args) {
  SimpleClient client = new SimpleClient();
  client.connect("127.0.0.1");
  client.close();
}


Like this you will get lots of error, because the classes like Cluster not found because we have not added cassandra java driver to it. To sort this out follow the instructions.

download cassandra java driver i.e. a jar file  from http://mvnrepository.com/artifact/com.datastax.cassandra/cassandra-driver-core/2.0.5


Now add this jar file to your project. Right click on on your project->properties->Java Build Path->           ->libraries->Add External Jars..  . locate your downloaded jar file and press ok. Then press ctrl+shift+0 in your SimpleClient.java file, your errors will be vanished.


Now right click on SimpleClient.java->Run as->Java Application ,  Then see the out put in console view
as below.




For more CQL program refer the upper link.

Thankq so muck.

1 comment:

  1. You are doing a great job by sharing useful information about Apache Spark course. It is one of the post to read and improve my knowledge in Apache Spark.You can check our Cassandra installation prerequisites, tutorial for more information about Cassandra cluster ubuntu setup.

    ReplyDelete