skip to Main Content

When I create a dynamodb table, I try to use waiters to wait AWS service to complete its action.

CreateTableRequest createRequest = new CreateTableRequest()
            .withTableName(tableName);
    
CreateTableResult createResult = dynamoDbClient.createTable(createRequest);
dynamoDbClient.waiters().tableExists().run(() -> new DescribeTableRequest(tableName));
return dynamoDB.getTable(tableName);

But I get:

incompatible types: com.amazonaws.waiters.WaiterParameters is not a functional interface

I’m new to Java, what’s a functional interface? How can I get around this problem?

2

Answers


  1. Chosen as BEST ANSWER

    Dividing into 2 rows seems to solve the problem:

    Waiter<DescribeTableRequest> waiter = dynamoDbClient.waiters().tableExists();
    waiter.run(new WaiterParameters<>(new DescribeTableRequest(tableName)));
    

  2. Use AWS SDK for Java V2, not the AWS SDK for Java V1 – which is no longer recommended to use as specified on this AWS page.

    You can use Waiters easily using the Amazon Dynamo V2 API. See this code example.

    //snippet-sourcedescription:[CreateTable.java demonstrates how to create an Amazon DynamoDB table by using a waiter.]
    //snippet-keyword:[SDK for Java v2]
    //snippet-service:[Amazon DynamoDB]
    
    /*
       Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
       SPDX-License-Identifier: Apache-2.0
    */
    
    package com.example.dynamodb;
    
    // snippet-start:[dynamodb.java2.create_table.import]
    import software.amazon.awssdk.auth.credentials.ProfileCredentialsProvider;
    import software.amazon.awssdk.core.waiters.WaiterResponse;
    import software.amazon.awssdk.regions.Region;
    import software.amazon.awssdk.services.dynamodb.DynamoDbClient;
    import software.amazon.awssdk.services.dynamodb.model.AttributeDefinition;
    import software.amazon.awssdk.services.dynamodb.model.CreateTableRequest;
    import software.amazon.awssdk.services.dynamodb.model.CreateTableResponse;
    import software.amazon.awssdk.services.dynamodb.model.DescribeTableRequest;
    import software.amazon.awssdk.services.dynamodb.model.DescribeTableResponse;
    import software.amazon.awssdk.services.dynamodb.model.DynamoDbException;
    import software.amazon.awssdk.services.dynamodb.model.KeySchemaElement;
    import software.amazon.awssdk.services.dynamodb.model.KeyType;
    import software.amazon.awssdk.services.dynamodb.model.ProvisionedThroughput;
    import software.amazon.awssdk.services.dynamodb.model.ScalarAttributeType;
    import software.amazon.awssdk.services.dynamodb.waiters.DynamoDbWaiter;
    // snippet-end:[dynamodb.java2.create_table.import]
    
    /**
     * Before running this Java V2 code example, set up your development environment, including your credentials.
     *
     * For more information, see the following documentation topic:
     *
     * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html
     */
    public class CreateTable {
    
        public static void main(String[] args) {
    
            final String usage = "n" +
                "Usage:n" +
                "    <tableName> <key>nn" +
                "Where:n" +
                "    tableName - The Amazon DynamoDB table to create (for example, Music3).nn" +
                "    key - The key for the Amazon DynamoDB table (for example, Artist).n" ;
    
           if (args.length != 2) {
               System.out.println(usage);
               System.exit(1);
           }
    
           String tableName = args[0];
           String key = args[1];
           System.out.println("Creating an Amazon DynamoDB table "+tableName +" with a simple primary key: " +key );
    
           ProfileCredentialsProvider credentialsProvider = ProfileCredentialsProvider.create();
           Region region = Region.US_EAST_1;
           DynamoDbClient ddb = DynamoDbClient.builder()
               .credentialsProvider(credentialsProvider)
               .region(region)
               .build();
    
           String result = createTable(ddb, tableName, key);
           System.out.println("New table is "+result);
           ddb.close();
        }
    
        // snippet-start:[dynamodb.java2.create_table.main]
        public static String createTable(DynamoDbClient ddb, String tableName, String key) {
            DynamoDbWaiter dbWaiter = ddb.waiter();
            CreateTableRequest request = CreateTableRequest.builder()
                .attributeDefinitions(AttributeDefinition.builder()
                    .attributeName(key)
                    .attributeType(ScalarAttributeType.S)
                    .build())
                .keySchema(KeySchemaElement.builder()
                    .attributeName(key)
                    .keyType(KeyType.HASH)
                    .build())
                .provisionedThroughput(ProvisionedThroughput.builder()
                    .readCapacityUnits(new Long(10))
                    .writeCapacityUnits(new Long(10))
                    .build())
                .tableName(tableName)
                .build();
    
            String newTable ="";
            try {
                CreateTableResponse response = ddb.createTable(request);
                DescribeTableRequest tableRequest = DescribeTableRequest.builder()
                    .tableName(tableName)
                    .build();
    
                // Wait until the Amazon DynamoDB table is created.
                WaiterResponse<DescribeTableResponse> waiterResponse = dbWaiter.waitUntilTableExists(tableRequest);
                waiterResponse.matched().response().ifPresent(System.out::println);
                newTable = response.tableDescription().tableName();
                return newTable;
    
            } catch (DynamoDbException e) {
                System.err.println(e.getMessage());
                System.exit(1);
            }
           return "";
        }
        // snippet-end:[dynamodb.java2.create_table.main]
    }
    

    To read more about waiter and the AWS SDK for Java V2, see:

    Using waiters in the AWS SDK for Java 2.x

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search