Migrating Java driver
Complete the following procedure to migrate your existing DataStax Java driver to a version capable of connecting to Astra DB databases created using DataStax Astra DB.
-
In your existing DSE Java driver code, update dependencies to include the DSE Java driver version that can connect to Astra DB databases.
Replace
{version}
with the current Java driver version.
- DataStax Java driver for Apache Cassandra 4.x
-
<dependency> <groupId>com.datastax.oss</groupId> <artifactId>java-driver-core</artifactId> <!--Use the latest version from https://search.maven.org/artifact/com.datastax.oss/java-driver-core --> <version>{version}</version> </dependency>
- DataStax Java driver for Apache Cassandra 3.x
-
<dependency> <groupId>com.datastax.cassandra</groupId> <artifactId>cassandra-driver-core</artifactId> <!--Use the latest version from https://search.maven.org/artifact/com.datastax.cassandra/cassandra-driver-core --> <version>{version}</version> </dependency>
-
Optional: To use DSE QueryBuilder, which is a utility used to generate CQL queries programmatically, add the following dependency to your
pom.xml
file. Replace{version}
with the current Java driver version.
<dependency> <groupId>com.datastax.dse</groupId> <artifactId>dse-java-driver-query-builder</artifactId> <!-- Use the latest 4.x version from https://search.maven.org/artifact/com.datastax.oss/java-driver-query-builder --> <version>{version}</version> </dependency>
-
Modify the connection code to use the Astra DB API. In the
CqlSession
orSession
objects (DseCluster
orDseSession
objects for DSE), include the path to the secure connect bundle for your Cassandra database (secure-connect-database_name.zip
) in thewithCloudSecureConnectBundle()
method, as shown in the following example.-
DataStax Java driver for Apache Cassandra 4.x
-
DataStax Java driver for Apache Cassandra 3.x
CqlSession session = CqlSession.builder() .withCloudSecureConnectBundle("/path/to/secure-connect-database_name.zip") .withAuthCredentials("clientId","clientSecret") .withKeyspace("keyspace_name") .build())
Session session = Cluster.builder() .withCloudSecureConnectBundle("/path/to/secure-connect-database_name.zip") .withAuthProvider(new PlainTextAuthProvider("clientId", "clientSecret")) .build() .connect("keyspace_name"))
If converting from using the open source Cassandra Java driver to the DSE Java driver, ensure that you change
Session
toDseSession
. -
-
Build the project to test the connection.
mvn clean compile exec:java -Dexec.mainClass=YourJavaDriver.java
If successful, the code builds, compiles, and connects to your Cassandra database.