Search code examples
javaamazon-web-servicesaws-lambdaamazon-auroraaws-aurora-serverless

Using AWS Aurora data api for java with AWS Sdk for Java 2.x


I have been trying to use the Aurora aws data api from my lambda to do some operations like insert and update on the tables. The issue that I am facing is the official doc only provides the details about using AWS SDK for Java version 1.x. The latest version of the aws java sdk 2.x has several changes compared to version 1.0 which prevent me from porting the code over to the latest sdk. I was unable to find the updated documentation for data api for java aws sdk 2.0. It would be great if someone could provide me a sample code which performs the same operations as below on the latest sdk :

public void insert() {
    AWSRDSData rdsData = AWSRDSDataClient.builder().build();

    BatchExecuteStatementRequest request = new BatchExecuteStatementRequest()
            .withDatabase("test")
            .withResourceArn(RESOURCE_ARN)
            .withSecretArn(SECRET_ARN)
            .withSql("INSERT INTO test_table2 VALUES (:string, :number)")
            .withParameterSets(Arrays.asList(
                    Arrays.asList(
                            new SqlParameter().withName("string").withValue(new Field().withStringValue("Hello")),
                            new SqlParameter().withName("number").withValue(new Field().withLongValue(1L))
                    ),
                    Arrays.asList(
                            new SqlParameter().withName("string").withValue(new Field().withStringValue("World")),
                            new SqlParameter().withName("number").withValue(new Field().withLongValue(2L))
                    )
            ));

    rdsData.batchExecuteStatement(request);
}

Solution

  • So I went through the aws sdk version 2 and found that the following code works for me :

    public ExecuteStatementResponse insert() {
        RdsDataClient rdsData = RdsDataClient
                .builder()
                .build();
        try {
            ExecuteStatementRequest executeStatementRequest = ExecuteStatementRequest
                    .builder()
                    .resourceArn(resourceArn)
                    .secretArn(secretArn)
                    .database(database)
                    .sql("INSERT INTO test_table2 VALUES (:name1, :name2)")
                    .parameters(Arrays.asList(
                            SqlParameter.builder().name("name1").value(Field.builder().stringValue("Hello").build()).build(),
                            SqlParameter.builder().name("name2").value(Field.builder().longValue(20L).build()).build()
                    ))
                    .continueAfterTimeout(true)
                    .build();
            return rdsData.executeStatement(executeStatementRequest);
        } catch (BadRequestException | StatementTimeoutException | InternalServerErrorException | ForbiddenException
                | ServiceUnavailableErrorException dataException) {
            LOG.error("Execute statement failed: Error Message: {}, Cause: {}",
                    dataException.getMessage(), dataException.getCause());
        }
        return null;
    }