access AWS Keyspace from springboot application

106 Views Asked by At

I am trying to doa POC to see how we can connect AWS Keyspace from a springboot application. I running it from my local desktop.

I am using IAM user accesskey/secret and using datastax and aws java sdk dependency.

application.properties

aws.accessKey=ACCESS_KEY
aws.secretKey=SECRET_KEY
aws.region=us-east-2
aws.keyspace.endPoint=cassandra.us-east-2.amazonaws.com
aws.keyspace.name=test_keyspace

I have also created a bean of AmazonKeyspace using BasicAWSCredentials and AmazonKeyspaceClientBuilder using the above properties. I have doubt about this endpoint, if this is correct or not

but when I am running the application, it's failing with AllNodesFailedException. Can you please let me know how to resolve this issue ? I am not sure if I missing something for this.

1

There are 1 best solutions below

10
smac2020 On

To work with Keyspace in a Java project, it's recommended that you use software.amazon.awssdk.services.keyspaces.KeyspacesClient, which is AWS SDK for V2. AWS Java V1 is on the path to deprecation:

https://aws.amazon.com/blogs/developer/announcing-end-of-support-for-aws-sdk-for-java-v1-x-on-december-31-2025/

You need to do a few steps:

  1. Create an application.conf file that references the JKS file. See below.

  2. You must create a Cassandra Java client driver (JKS) file. This file is a secure file format used to hold certificate information for Java applications. This is required to make a connection to Amazon Keyspaces. For more information, see the following documentation topic: https://docs.aws.amazon.com/keyspaces/latest/devguide/using_java_driver.html.

You will need that application.conf file. Here is the contents. Notice it references the JKS file.

datastax-java-driver {
            basic.contact-points = ["cassandra.us-east-1.amazonaws.com:9142"]
            basic.load-balancing-policy {
                class = DefaultLoadBalancingPolicy
                local-datacenter = us-east-1
                slow-replica-avoidance = false
            }
            advanced {
                auth-provider = {
                    class = software.aws.mcs.auth.SigV4AuthProvider
                    aws-region = us-east-1
                }
                ssl-engine-factory {
                    class = DefaultSslEngineFactory
                    truststore-path = "C:\\Users\\scmacdon\\KeyspaceCert\\cassandra_truststore.jks"
                    truststore-password = "11xxxxxxxx"
                    hostname-validation = false
                }
            }
        }

To work with KeyspacesClient and the required CqlSession object, use the code below.

 String configFilePath = "C:\\AWS\\application.conf";
    Region region = Region.US_EAST_1;
    KeyspacesClient keyClient = KeyspacesClient.builder()
        .region(region)
        .build();
    
   DriverConfigLoader loader = DriverConfigLoader.fromFile(new File(configFilePath));
   CqlSession session = CqlSession.builder()
       .withConfigLoader(loader)
       .build();

To handle creds, see the Java V2 dev guide here: Default credentials provider chain.

You will also need a movies.json file for the full example. You can find that here:

https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/resources/sample_files

To run the full example in Java V2, run this code. This will get your familiar with the V2 KeyspacesClient.

See:

https://github.com/awsdocs/aws-doc-sdk-examples/blob/main/javav2/example_code/keyspaces/src/main/java/com/example/keyspace/ScenarioKeyspaces.java

Note this example is too big to post here.

I just ran this and it works...

enter image description here