10

Does anyone know what has replaced AmazonDynamoDBClient? Couldn't find anything in the documentation

Package - com.amazonaws.services.dynamodbv2

    AmazonDynamoDBClient amazonDynamoDBClient = new AmazonDynamoDBClient();

2 Answers2

18

As per the API doc, the builder class (e.g. AmazonDynamoDBClientBuilder) should be used to create the instance.

Sample code using the builder class:-

I have create the client for DynamoDB local.

DynamoDB dynamoDB = new DynamoDB(AmazonDynamoDBClientBuilder.standard().withEndpointConfiguration(new EndpointConfiguration("http://localhost:8000", "us-east-1")).build());

Table table = dynamoDB.getTable("Movies");

Scan using DynamoDB table class:-

private static void findProductsForPriceLessThanZero() {

        Table table = dynamoDB.getTable(tableName);

        Map<String, Object> expressionAttributeValues = new HashMap<String, Object>();
        expressionAttributeValues.put(":pr", 100);

        ItemCollection<ScanOutcome> items = table.scan(
            "Price < :pr", //FilterExpression
            "Id, Title, ProductCategory, Price", //ProjectionExpression
            null, //ExpressionAttributeNames - not used in this example 
            expressionAttributeValues);

        System.out.println("Scan of " + tableName + " for items with a price less than 100.");
        Iterator<Item> iterator = items.iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next().toJSONPretty());
        }    
    }
mkobit
  • 43,979
  • 12
  • 156
  • 150
notionquest
  • 37,595
  • 6
  • 111
  • 105
  • Refer this link for Dynamodb endpoints http://docs.aws.amazon.com/general/latest/gr/rande.html#ddb_region ... If you are not using the local DynamoDB, the endpoint may not be required as it will be derived automatically based on region. Alternatively, you can use the endpoints as given in the above link. – notionquest Jan 27 '17 at 08:58
  • Thanks. Just wondering if this can help me get all records in a table at one go? –  Jan 30 '17 at 09:52
  • Yes, you can scan the table recursively to get all the items in the table. http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/ScanJavaDocumentAPI.html – notionquest Jan 30 '17 at 09:59
  • I am able to do that using - ScanRequest scanRequest = new ScanRequest().withTableName(tableName); ScanResult result = amazonDynamoDBClient.scan(scanRequest); //And then use result.getItems() –  Jan 30 '17 at 10:07
  • Yes, but the scan method is available in AmazonDynamoDBClient - and the answer above use DynamoDB –  Jan 30 '17 at 10:09
  • Here's a related question that I posted on getting 403 forbidden when I post a request to elastic search. This is how I am using ScanRequest class. http://stackoverflow.com/questions/41932026/why-do-i-get-403-forbidden-error-while-posting-a-json-to-elasticsearch-endpoint –  Jan 30 '17 at 10:18
  • Refer this page http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/ScanJavaDocumentAPI.html#DocumentAPIJavaScanExample .. updated the same in awswer for reference .. – notionquest Jan 30 '17 at 11:05
2

I am using spring-boot, the way I am working with Dynamo is injecting an AWSCredentialsProvider and using the variables which are in my environment in this way:

    @Bean
    public AmazonDynamoDB amazonDynamoDB(AWSCredentialsProvider awsCredentialsProvider) {
        AmazonDynamoDB amazonDynamoDB
                = AmazonDynamoDBClientBuilder.standard()
                .withCredentials(awsCredentialsProvider).build();
        return amazonDynamoDB;
    }

    @Bean
    public AWSCredentialsProvider awsCredentialsProvider() {
        return new EnvironmentVariableCredentialsProvider();
    }

The full example is available here: https://github.com/ioet/bpm-skills-api

Rene Enriquez
  • 1,418
  • 1
  • 13
  • 33