DataStax Java driver
Use the unified DataStax Java driver to connect to your DataStax Astra database and begin building your own application.
You add a repository and dependencies to the pom.xml
file for your project to download the appropriate .jar files for the Java driver and make them available to your code. Additionally, you implement a ConnectDatabase
class to initialize the DSE Java driver.
DataStax recommends using the unified DataStax Java driver. If you have an existing Apache Cassandra or DataStax Enterprise (DSE) Java driver, migrate the driver to a version that is capable of connecting to Astra databases. |
Prerequisites
-
Download the secure connect bundle to obtain connection credentials for your DataStax Astra database.
-
Client ID and Client Secret by creating your application token for your username and password.
Alternatively, have a teammate provide access to their Astra database. |
Procedure
-
Navigate to the
pom.xml
file at the root of your Java project and open it for editing.
For a complete |
-
Add the DataStax Java driver dependency to your
pom.xml
file, ensuring that the name of the dependency corresponds to the installed version: This dependency causes Maven to automatically download the appropriate .jar files found at theurl
specified in therepository
for the DSE Java driver and make them available to your code.
<dependency> <groupId>com.datastax.oss</groupId> <artifactId>java-driver-core</artifactId> <version>4.6.1</version> </dependency>
-
Save and close your
pom.xml
file. -
Initialize the DataStax Java driver.
-
Create a
ConnectDatabase.java
file in the/src/main/java
directory for your Java project.
cd **javaProject**/src/main/java
touch ConnectDatabase.java
-
Copy the following code for your DataStax driver into the
ConnectDatabase.java
file. The following example implements aConnectDatabase
class to connect to your Astra database, runs a CQL query, and prints the output to the console.
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:
try (CqlSession session = CqlSession.builder()
.withCloudSecureConnectBundle(Paths.get("/path/to/secure-connect-database_name.zip"))
.withAuthCredentials("clientId","clientSecret")
.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.");
}
}
System.exit(0);
}
}
-
Make the following changes:
-
Use the
withCloudSecureConnectBundle()
method to specify the path to the secure connect bundle for your Astra database. -
Use the
withAuthCredentials()
method to specify the username and password for your database. -
Use the
withKeyspace()
method to specify the [glossary:keyspace] name for your database.
-
-
Save and close the
ConnectDatabase.java
file.
Example pom.xml file
You can use the following pom.xml
file in your Java project to connect to your Astra database. If you already have a pom.xml
file for your Java project, copy only the repository and dependencies as indicated in the previous steps.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>net.techne.web</groupId>
<artifactId>cloudTest</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<!-- START-javaDriverDependencyCore -->
<dependency>
<groupId>com.datastax.dse</groupId>
<artifactId>dse-java-driver-core</artifactId>
<version>4.6.1</version>
</dependency>
<!-- END-javaDriverDependencyCore -->
<!-- START-javaDriverDependencyQuery -->
<dependency>