יום ראשון, 27 באפריל 2014

Simple CQL Query

The following is a simple project that demonstrates Cassandra CQL .

package playwithcassandralocal;
import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.Host;
import com.datastax.driver.core.Metadata;
import com.datastax.driver.core.ResultSet;
import com.datastax.driver.core.Row;
import com.datastax.driver.core.Session;
public class CqlAccessor {
    
    private Cluster cluster;
     private Session session;
     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("Datatacenter: %s; Host: %s; Rack: %s\n",
                 host.getDatacenter(), host.getAddress(), host.getRack());
        }
        session = cluster.connect();
     }
     public void createSchema() 
     {
             session.execute("CREATE KEYSPACE GSelfDrivingCarModel WITH replication " + 
                                "= {'class':'SimpleStrategy', 'replication_factor':1};");
             session.execute(
                                "CREATE TABLE GSelfDrivingCarModel.Images (" +
                                      "id uuid PRIMARY KEY," + 
                                      "title text," + 
                                      "Datecreated text," + 
                                      "Location text," +                                    
                                      "Image blob" + 
                                      ");");
     }
     public void loadData() 
     {
             session.execute(
                                "INSERT INTO GSelfDrivingCarModel.Images (id, title, Datecreated, Location) " +
                                "VALUES (" +
                                    "756716f7-2e54-4715-7f00-91dcbea6cf50," +
                                    "'This is my image.'," +
                                    "'25/10/2014'," +
                                    "'34.16234434 32.0245345'" +
                                    ");" );
                        	   
     }
     public void QueryData ()
     {
             ResultSet results = session.execute("SELECT * FROM GSelfDrivingCarModel.Images " +
                          "WHERE id = 756716f7-2e54-4715-7f00-91dcbea6cf50;");
                  System.out.println(String.format("%-30s\t%-20s\t%-20s\n%s", "title", "Datecreated","Location",
                         "-------------------------------+-----------------------+--------------------"));
                  for (Row row : results) {
                      System.out.println(String.format("%-30s\t%-20s\t%-20s", row.getString("title"),
                      row.getString("Datecreated"),  row.getString("Location")));
                  }
                  System.out.println();
     }
     public void close() {
        cluster.close();
     }
     public static void main(String[] args) {
        CqlAccessor theCqlAccessor = new CqlAccessor();
        
        theCqlAccessor.connect("127.0.0.1");
        theCqlAccessor.createSchema();
        theCqlAccessor.loadData() ;
        theCqlAccessor.QueryData ();
        theCqlAccessor.close();
     }
}

The project :
Capture9


When using Maven the following pom the following dependency retrieves the relevant Jars


	<dependency>
  		<groupId>com.datastax.cassandra</groupId>
  		<artifactId>cassandra-driver-core</artifactId>
  		<version>2.0.1</version>
  		<type>bundle</type>
  	</dependency>

And the Result :
Connected to cluster: Test Cluster
Datatacenter: datacenter1; Host: /127.0.0.1; Rack: rack1
title                          Datecreated          Location           
-------------------------------+-----------------------+--------------------
This is my image.              25/10/2014           34.16234434 32.0245345

Note that there is no update operation in Cassandra when doing insert operation with id that is already exists then the old data will be replaced with the new data .
Resources:
Cassandra documentation .

אין תגובות:

הוסף רשומת תגובה