| Author | 
              Topic: Apache Cassandra -- Access via Java  |  
           
         |  
        
          
            
              
                
                	
                  
                    
                      EricJ member offline     |  
                    
                      |   |  
                    
                      
                        
                          
                            | posts: | 
                            50 |  
                          
                            | joined: | 
                            02/22/2007 |  
                          
                            | from: | 
                            CA |  
                         
                       |  
                    | 
                  | 
                
                  
                    
                       |  
                    
                       |  
                    
                      
                        
                          | 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
 
 
 
  |  
                        
                           |  
                        |  
                    
                       |  
                    
                       |  
                    |  
                |  
        
          
            
              
                
                	
                  
                    
                      EricJ member offline     |  
                    
                      |   |  
                    
                      
                        
                          
                            | posts: | 
                            50 |  
                          
                            | joined: | 
                            02/22/2007 |  
                          
                            | from: | 
                            CA |  
                         
                       |  
                    | 
                  | 
                
                  
                    
                       |  
                    
                       |  
                    
                      
                        
                          | 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.
 
 
  |  
                        
                           |  
                        |  
                    
                       |  
                    
                       |  
                    |  
                |  
        
          
            
              
                
                	
                  
                    
                      EricJ member offline     |  
                    
                      |   |  
                    
                      
                        
                          
                            | posts: | 
                            50 |  
                          
                            | joined: | 
                            02/22/2007 |  
                          
                            | from: | 
                            CA |  
                         
                       |  
                    | 
                  | 
                
                  
                    
                       |  
                    
                       |  
                    
                      
                        
                          | Create Keyspace |  
                        
                          
	// keyspace: simplex
	session.execute(
               "CREATE KEYSPACE IF NOT EXISTS simplex " +
               "WITH replication " +
	       "= {'class':'SimpleStrategy', 'replication_factor':3};"  // redundancy : 3
        );
 
  |  
                        
                           |  
                        |  
                    
                       |  
                    
                       |  
                    |  
                |  
        
          
            
              
                
                	
                  
                    
                      EricJ member offline     |  
                    
                      |   |  
                    
                      
                        
                          
                            | posts: | 
                            50 |  
                          
                            | joined: | 
                            02/22/2007 |  
                          
                            | from: | 
                            CA |  
                         
                       |  
                    | 
                  | 
                
                  
                    
                       |  
                    
                       |  
                    
                      
                        
                          | 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)"
			+ ");"
        );
 
  |  
                        
                           |  
                        |  
                    
                       |  
                    
                       |  
                    |  
                |  
      |