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

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. Use version 3.7.1 for the following steps.

For a detailed video demonstration, watch our DataStax Distribution of Apache Cassandra™ on Microsoft Azure 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 Azure portal, click Resources, select your DDAC cluster, and choose the Outputs tab.
  4. Copy the nodePubIp, which is 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("public_ip_address").build();

    Replace the IP address shown in the addContactPoint() method with the nodePubIp copied in the previous step.

  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 videostable.
      Note: DataStax recommends creating the data model outside of the application. For more, see KillrVideo, a reference application for developers learning how to build applications with Apache Cassandra.
      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