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.
Tip: DataStax recommends using the DataStax Java Driver for Apache Cassandra. You can also use the DataStax Enterprise (DSE) Java Driver, which exposes the same API for connecting to Cassandra databases.
- Download and install Maven.
- 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.
- Download the secure connect bundle (instructions for GCP and AWS) to obtain connection credentials for your database.
-
Edit the
pom.xmlfile at the root of your and according to this Example pom.xml file. -
Initialize the DataStax Java Driver.
a. Create a
ConnectDatabase.javafile in the/src/main/javadirectory for your Java project.$ cd javaProject/src/main/java$ touch ConnectDatabase.java
b. Programmatic configuration. Copy the following code for your DataStax Driver into the
ConnectDatabase.javafile. The following example implements aConnectDatabaseclass to connect to your Apollo database, runs a CQL query, and prints the output to the console.Note: With the
CqlSession.builder()object, make sure to set the path to the secure connect bundle for your Apollo database in thewithCloudSecureConnectBundle()method as shown in the following example. If converting from using the open source Cassandra Java Driver to the DSE Java Driver, ensure that you changeCqlSessiontoDseSession.-
DataStax Java Driver for Apache Cassandra 4.x (recommended)
import com.datastax.oss.driver.api.core.CqlSession; import com.datastax.oss.driver.api.core.cql.ResultSet; import com.datastax.oss.driver.api.core.cql.Row; import java.nio.file.Paths; public class ConnectDatabase { public static void main(String[] args) { // Create the CqlSession object: try (CqlSession session = CqlSession.builder() // make sure you change the path to the secure connect bundle below .withCloudSecureConnectBundle(Paths.get("/path/to/secure-connect-database_name.zip")) .withAuthCredentials("user_name","password") .withKeyspace("keyspace_name") .build()) { // 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."); } } } }
-
DataStax Java Driver for DataStax Enterprise (DSE) 2.x
import com.datastax.dse.driver.api.core.DseSession; import com.datastax.oss.driver.api.core.cql.ResultSet; import com.datastax.oss.driver.api.core.cql.Row; import java.nio.file.Paths; public class ConnectDatabase { public static void main(String[] args) { // Create the DseSession object: try (DseSession session = DseSession.builder() // make sure you change the path to the secure connect bundle below .withCloudSecureConnectBundle(Paths.get("/path/to/secure-connect-database_name.zip")) .withAuthCredentials("user_name","password") .withKeyspace("keyspace_name") .build()) { // 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."); } } } }
c. File-based configuration. An alternative to the programmatic configuration method detailed above is to include the information required to connect in the driver's configuration file (
application.conf). Merge the following options with any other options that you might want to include in the configuration file:basic { # change this to match the target keyspace session-keyspace = keyspace_name cloud { # change this to match bundle's location; can be either a path on the local filesystem # or a valid URL, e.g. http://acme.com/path/to/secure-connect-database_name.zip secure-connect-bundle = /path/to/secure-connect-database_name.zip } } advanced { auth-provider { class = PlainTextAuthProvider # change below to match the appropriate credentials username = user_name password = password } }
For more information about the driver configuration mechanism, refer to the driver documentation.
With the above configuration, your ConnectDatabase.java file should be simplified as shown below:
import com.datastax.oss.driver.api.core.CqlSession; import com.datastax.oss.driver.api.core.cql.ResultSet; import com.datastax.oss.driver.api.core.cql.Row; public class ConnectDatabase { public static void main(String[] args) { // Create the CqlSession object; it will read the configuration file and pick the right // values to connect to the Apollo database. try (CqlSession session = CqlSession.builder().build()) { // 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."); } } } }
d. Save and close the ConnectDatabase.java file.
-