go to  ForumEasy.com   
JavaPro  
 
 
   Home  |  MyForum  |  FAQ  |  Archive    You are not logged in. [Login] or [Register]  
Forum Home » BigData » Apache Cassandra -- Access via Java
Email To Friend  |   Set Alert To This Topic Rewarding Points Availabe: 0 (What's this) New Topic  |   Post Reply
Author Topic: Apache Cassandra -- Access via Java
EricJ
member
offline   
 
posts: 50
joined: 02/22/2007
from: CA
  posted on: 11/13/2016 10:19:03 PM    Edit  |   Quote  |   Report 
Apache Cassandra -- Access via Java
There are several Java driver provider. One of them is from DataStax.com: cassandra-driver-core "3.0.0" (cassandra-driver-core-3.0.0.jar), which can be downloaded from:

https://www.versioneye.com/java/com.datastax.cassandra:cassandra-driver-core/3.0.0



 Profile | Reply Points Earned: 0
EricJ
member
offline   
 
posts: 50
joined: 02/22/2007
from: CA
  posted on: 11/13/2016 10:27:04 PM    Edit  |   Quote  |   Report 
Establish Connection

	Cluster cluster = Cluster.builder().
				addContactPoint("10.11.12.13").
				build();
	
	Session session = cluster.connect();
	
	Metadata metadata = cluster.getMetadata();
	
	System.out.printf("Connected to cluster: %s\n",	metadata.getClusterName());
	
	for (Host host : metadata.getAllHosts()) {

   	    System.out.printf("Datatacenter: %s; Host: %s; Rack: %s --> Is up? : %s\n",
			host.getDatacenter(), host.getAddress(), host.getRack(), host.isUp());
	}



OUTPUT:
Connected to cluster: Test Cluster
Datatacenter: datacenter1; Host: /10.11.12.13; Rack: rack1 --> Is up? : true
Datatacenter: datacenter1; Host: /10.11.12.123; Rack: rack1 --> Is up? : false


Note: The second instance of this cluster is not running.


 Profile | Reply Points Earned: 0
EricJ
member
offline   
 
posts: 50
joined: 02/22/2007
from: CA
  posted on: 11/13/2016 10:33:29 PM    Edit  |   Quote  |   Report 
Create Keyspace
	// keyspace: simplex
	session.execute(
               "CREATE KEYSPACE IF NOT EXISTS simplex " +
               "WITH replication " +
	       "= {'class':'SimpleStrategy', 'replication_factor':3};"  // redundancy : 3
        );


 Profile | Reply Points Earned: 0
EricJ
member
offline   
 
posts: 50
joined: 02/22/2007
from: CA
  posted on: 11/13/2016 10:57:08 PM    Edit  |   Quote  |   Report 
Create Tables/Families
	// column family: songs
	session.execute("CREATE TABLE IF NOT EXISTS simplex.songs ("
			+ "id uuid PRIMARY KEY, " 
                        + "title text, " 
                        + "album text,"
			+ "artist text, " 
                        + "tags set<text>, "
                        + "data blob" + ");"
        );
		
	// column family: playlists
	session.execute("CREATE TABLE IF NOT EXISTS simplex.playlists ("
			+ "id uuid, " 
                        + "title text, " 
                        + "album text, " 
                        + "artist text,"
			+ "song_id uuid, " 
                        + "PRIMARY KEY (id, title, album, artist)"
			+ ");"
        );


 Profile | Reply Points Earned: 0

 
Powered by ForumEasy © 2003-2005, All Rights Reserved. | Privacy Policy | Terms of Use
 
Get your own forum today. It's easy and free.