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.
<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>4.14.1</version> </dependency>
<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>3.11.2</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:<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>4.14.1</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.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.