Consistency Levels

ConsistencyLevels.java

vim /home/hadoop/AdithyaJ/Cassandra/java/Cassandra-Java/src/main/java/com/admatic/ConsistencyLevels.java
package com.admatic;

import com.datastax.driver.core.*;

public class ConsistencyLevels {
    static String[] CONTACT_POINTS = {"159.65.156.47"};
    static int PORT = 9042;

    public static void main(String[] args) {

        ConsistencyLevels client = new ConsistencyLevels();

        try {
            client.connect(CONTACT_POINTS, PORT);
            client.createSchema();
            client.loadData();
            client.querySchema(ConsistencyLevel.ALL);
            client.querySchema(ConsistencyLevel.EACH_QUORUM);
            client.querySchema(ConsistencyLevel.QUORUM);
            client.querySchema(ConsistencyLevel.LOCAL_QUORUM);
            client.querySchema(ConsistencyLevel.ONE);
            client.querySchema(ConsistencyLevel.TWO);
            client.querySchema(ConsistencyLevel.THREE);
            client.querySchema(ConsistencyLevel.LOCAL_ONE);
            client.querySchema(ConsistencyLevel.ANY);
            client.querySchema(ConsistencyLevel.SERIAL);
            client.querySchema(ConsistencyLevel.LOCAL_SERIAL);
        } finally {
            client.close();
        }
    }

    private Cluster cluster;

    private Session session;

    /**
     * Initiates a connection to the cluster
     * specified by the given contact point.
     *
     * @param contactPoints the contact points to use.
     * @param port          the port to use.
     */
    public void connect(String[] contactPoints, int port) {

        cluster = Cluster.builder()
                .addContactPoints(contactPoints).withPort(port)
                .build();

        System.out.printf("Connected to cluster: %s%n", cluster.getMetadata().getClusterName());

        session = cluster.connect();
    }

    /**
     * Creates the schema (keyspace) and tables
     * for this example.
     */
    public void createSchema() {

        session.execute("CREATE KEYSPACE IF NOT EXISTS simplex WITH replication = {'class': 'NetworkTopologyStrategy', 'dc1': '2', 'dc2': '2'}");

        session.execute(
                "CREATE TABLE IF NOT EXISTS simplex.songs (" +
                        "id uuid PRIMARY KEY," +
                        "title text," +
                        "album text," +
                        "artist text," +
                        "tags set<text>," +
                        "data blob" +
                        ");");

        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)" +
                        ");");
    }

    /**
     * Inserts data into the tables.
     */
    public void loadData() {
        session.execute(
                "INSERT INTO simplex.songs (id, title, album, artist, tags) " +
                        "VALUES (" +
                        "756716f7-2e54-4715-9f00-91dcbea6cf50," +
                        "'La Petite Tonkinoise'," +
                        "'Bye Bye Blackbird'," +
                        "'Joséphine Baker'," +
                        "{'jazz', '2013'})" +
                        ";");

        session.execute(
                "INSERT INTO simplex.playlists (id, song_id, title, album, artist) " +
                        "VALUES (" +
                        "2cc9ccb7-6221-4ccb-8387-f22b6a1b354d," +
                        "756716f7-2e54-4715-9f00-91dcbea6cf50," +
                        "'La Petite Tonkinoise'," +
                        "'Bye Bye Blackbird'," +
                        "'Joséphine Baker'" +
                        ");");
    }

    /**
     * Queries and displays data.
     */
    public void querySchema(ConsistencyLevel consistencyLevel) {
        long startTime = System.nanoTime();

        SimpleStatement statement = new SimpleStatement(
                "SELECT * FROM simplex.playlists");
        statement.setConsistencyLevel(ConsistencyLevel.ONE);
        ResultSet results = session.execute(statement);

        System.out.printf("%-30s\t%-20s\t%-20s%n", "title", "album", "artist");
        for (Row row : results) {
            System.out.printf("%-30s\t%-20s\t%-20s%n",
                    row.getString("title"),
                    row.getString("album"),
                    row.getString("artist"));
        }

        long endTime = System.nanoTime();

        long duration = (endTime - startTime) / 1000000;
        System.out.println("Time taken is " + duration + " milliseconds for "+ consistencyLevel.toString() + " Consistency Level\n");
    }

    /**
     * Closes the session and the cluster.
     */
    public void close() {
        session.close();
        cluster.close();
    }
}

Compile and Run

mvn compile

mvn exec:java -Dexec.mainClass="com.admatic.ConsistencyLevels"

Connected to cluster: AdmaticCassandraCluster
title                           album                   artist
La Petite Tonkinoise            Bye Bye Blackbird       Joséphine Baker
Time taken is 36 milliseconds for ALL Consistency Level

title                           album                   artist
La Petite Tonkinoise            Bye Bye Blackbird       Joséphine Baker
Time taken is 21 milliseconds for EACH_QUORUM Consistency Level

title                           album                   artist
La Petite Tonkinoise            Bye Bye Blackbird       Joséphine Baker
Time taken is 32 milliseconds for QUORUM Consistency Level

title                           album                   artist
La Petite Tonkinoise            Bye Bye Blackbird       Joséphine Baker
Time taken is 12 milliseconds for LOCAL_QUORUM Consistency Level

title                           album                   artist
La Petite Tonkinoise            Bye Bye Blackbird       Joséphine Baker
Time taken is 13 milliseconds for ONE Consistency Level

title                           album                   artist
La Petite Tonkinoise            Bye Bye Blackbird       Joséphine Baker
Time taken is 15 milliseconds for TWO Consistency Level

title                           album                   artist
La Petite Tonkinoise            Bye Bye Blackbird       Joséphine Baker
Time taken is 13 milliseconds for THREE Consistency Level

title                           album                   artist
La Petite Tonkinoise            Bye Bye Blackbird       Joséphine Baker
Time taken is 15 milliseconds for LOCAL_ONE Consistency Level

title                           album                   artist
La Petite Tonkinoise            Bye Bye Blackbird       Joséphine Baker
Time taken is 15 milliseconds for ANY Consistency Level

title                           album                   artist
La Petite Tonkinoise            Bye Bye Blackbird       Joséphine Baker
Time taken is 13 milliseconds for SERIAL Consistency Level

title                           album                   artist
La Petite Tonkinoise            Bye Bye Blackbird       Joséphine Baker
Time taken is 17 milliseconds for LOCAL_SERIAL Consistency Level

results matching ""

    No results matching ""