AdSenseHeader

How to connect to cassandra  using java

  • Cassandra is Nosql database.First cassandra is developed by Facebook for the inbox search future.
  • First you need to download DatastaxDriver jar for establishing the connection to cassandra database using java.
Sample Java program for establishing connection to cassandra

CassandraConnection.java

import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.Session;

public  class CassandraConnection  {

 public static void main(String[] args)  throws Exception{
  
   String serverIp ="127.0.0.1";
   String keyspace = "mykeyspace";
   Cluster cluster = Cluster.builder()
              .addContactPoints(serverIp)
              .build();
   Session session = cluster.connect(keyspace);
   System.out.println("Connection Established");
   
 }

}
In the above java program defining the server ip address.This Ip is getting where the cassandra is installed. Keyspace is nothing but database.By using keyspace we can create tables or columnfamilies in cassandra. 


0 comments:

Post a Comment

 
Top