Building an application for the DataStax Distribution of Apache Cassandra on GCP

Building an application for DDAC.

You can build an application to connect to your DataStax Distribution of Apache Cassandra™ (DDAC) cluster using any of the drivers for Apache Cassandra™, including Java, Python, C++, C#, Node.js, Ruby, and PHP.

These steps demonstrate how to create a simple Java application using version 3.7.1 of the DataStax Java Driver for Apache Cassandra™.
Note: There are API changes for newer versions of the Java driver (4.0+), which impact this process. To follow these steps, use version 3.7.1.

For a detailed video demonstration, watch our DataStax Distribution of Apache Cassandra on Microsoft GCP video series.

Procedure

  1. Set your deployment to use public IPs for your nodes.
  2. Download the Java driver from Github and add to your CLASSPATH.
    Alternatively, add the following dependency to your Maven POM file:
    <dependencies>
      <dependency>
        <groupId>com.datastax.cassandra</groupId>
        <artifactId>cassandra-driver-core</artifactId>
        <version>3.7.1</version>
      <dependency>
    <dependencies>
    
  3. In the GCP portal, click Compute Engine / VM instances and then enter your deployment name in Filter VM instances to locate the DDAC nodes.
  4. Locate the External IP address for one of the DDAC nodes. Use the public IP address for your DDAC cluster.
  5. Create a Cluster object using the Cluster.builder() helper class. The Cluster object is the starting point to connect to a DDAC cluster.
    Cluster cluster = Cluster.builder().addContactPoint("External_IP_Address").build();

    where External_IP_Address is the IP address obtained in 4.

  6. Create a Session object.
    Session session = cluster.connect();
    The driver makes connections to the cluster nodes.
  7. Using the Session object, execute statements with execute() to run CQL statements.
    1. Create the data model with the killrvideo keyspace and a videos table.
      Note: DataStax recommends creating the data model outside of the application:
      session.execute("CREATE KEYSPACE IF NOT EXISTS killrvideo WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 1};");
      
      session.execute("CREATE TABLE IF NOT EXISTS killrvideo.videos (name TEXT, description TEXT, PRIMARY KEY(name))");
    2. Insert a row into the videos table.
      session.execute("INSERT INTO killrvideo.videos (name, description) VALUES (?, ?);", "Avengers: Endgame", "No spoilers");
      
    3. Run a query, which returns a ResultSet object.
      ResultSet rs = session.execute("SELECT * FROM killrvideo.videos WHERE name = ?;", "Avengers: Endgame");
      
      for (Row row : rs) {
        System.out.println("Name:" + row.getString("name"));
        System.out.println("Description: " + row.getString("description"));
      }
      Note: The ResultSet is an Iterable object, which can be processed using a for-loop to iterate through each of the rows in the query results.
  8. Run the application.
    Code example