Amazon Keyspaces can use the 3.x drivers pretty simply. While there are benefits to the 4.x driver, like externalized configuration, there is no need to upgrade to move the Amazon Keyspaces.
You can use traditional user/name password authentication or the sigv4 plugin.
<!-- https://mvnrepository.com/artifact/software.aws.mcs/aws-sigv4-auth-cassandra-java-driver-plugin -->
<dependency>
<groupId>software.aws.mcs</groupId>
<artifactId>aws-sigv4-auth-cassandra-java-driver-plugin_3</artifactId>
<version>3.0.3</version>
</dependency>
Code sample to connect with java 3.x and write to keyspaces.
import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.Session;
import com.datastax.driver.core.PreparedStatement;
import com.datastax.driver.core.ResultSet;
import com.datastax.driver.core.Row;
import software.aws.mcs.auth.SigV4AuthProvider;
import java.net.InetSocketAddress;
import java.util.Collections;
import java.util.List;
public class OrderFetcher {
public final static String TABLE_FORMAT = "%-25s%s\n";
public final static int KEYSPACES_PORT = 9142;
public static Cluster connectToCluster(String region, List<InetSocketAddress> contactPoints) {
SigV4AuthProvider provider = new SigV4AuthProvider(region);
return Cluster.builder()
.addContactPointsWithPorts(contactPoints)
.withPort(KEYSPACES_PORT)
.withAuthProvider(provider)
.withSSL()
.build();
}
public static void main(String[] args) {
if (args.length != 3) {
System.err.println("Usage: OrderFetcher <region> <endpoint> <customer ID>");
System.exit(1);
}
String region = args[0];
List<InetSocketAddress> contactPoints = Collections.singletonList(new InetSocketAddress(args[1], KEYSPACES_PORT));
try (Cluster cluster = connectToCluster(region, contactPoints)) {
Session session = cluster.connect();
// Use a prepared query for quoting
PreparedStatement prepared = session.prepare("select * from acme.orders where customer_id = ?");
// We use execute to send a query to Cassandra. This returns a ResultSet, which is essentially a collection
// of Row objects.
ResultSet rs = session.execute(prepared.bind(args[2]));
// Print the header
System.out.printf(TABLE_FORMAT, "Date", "Order Id");
for (Row row : rs) {
System.out.printf(TABLE_FORMAT, row.getTimestamp("order_timestamp"), row.getUUID("order_id"));
}
}
}
}
Here is a link to example on github
https://github.com/aws-samples/amazon-keyspaces-examples/blob/main/java/datastax-v3/connection-sigv4/src/main/java/software/aws/mcs/example/OrderFetcher.java