Skip to content

Latest commit

 

History

History
 
 

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 

README.md

Connecting to Apollo (Cloud)

Using the DataStax Java Driver to connect to a DataStax Apollo database is almost identical to using the driver to connect to any normal Apache Cassandra® database. The only differences are in how the driver is configured in an application and that you will need to obtain a secure connect bundle.

The following is a Quick Start guide to writing a simple application that can connect to an Apollo database.

Prerequisites

  1. Download and install Maven.
  2. Create an Apollo database on GCP or AWS; alternatively, have a team member provide access to their Apollo database (instructions for GCP and AWS) to obtain database connection details.
  3. Download the secure connect bundle (instructions for GCP and AWS) to obtain connection credentials for your database.

Procedure

  1. Include the driver artifacts in your pom.xml file according to this pom.xml dependency.

  2. Initialize the DataStax Java Driver.

    a. Create a ConnectDatabase.java file in the /src/main/java directory for your Java project.

    $ cd javaProject/src/main/java
    $ touch ConnectDatabase.java

    b. Copy the following code for your DataStax Driver into the ConnectDatabase.java file.
    The following example implements a ConnectDatabase class to connect to your Apollo database, runs a CQL query, and prints the output to the console.

    Note: With the Cluster.builder() object, make sure to set the path to the secure connect bundle for your Apollo database ("/path/to/secure-connect-database_name.zip") in the withCloudSecureConnectBundle() method as shown in the following example.

    • DataStax Java Driver for Apache Cassandra 3.x

      import com.datastax.driver.core.Cluster;
      import com.datastax.driver.core.ResultSet;
      import com.datastax.driver.core.Row;
      import com.datastax.driver.core.Session;
      import java.io.File;
      
      public class ConnectDatabase {
      
        public static void main(String[] args) {
          // Create the Cluster object:
          Cluster cluster = null;
          try {
            cluster = Cluster.builder()
              // make sure you change the path to the secure connect bundle below
              .withCloudSecureConnectBundle(new File("/path/to/secure-connect-database_name.zip"))
              .withCredentials("user_name", "password")
              .build();
            Session session = cluster.connect();
            // Select the release_version from the system.local table:
            ResultSet rs = session.execute("select release_version from system.local");
            Row row = rs.one();
            //Print the results of the CQL query to the console:
            if (row != null) {
              System.out.println(row.getString("release_version"));
            } else {
              System.out.println("An error occurred.");
            }
          } finally {
            if (cluster != null) cluster.close();
          }
        }
      }

    c. Save and close the ConnectDatabase.java file.