Is this how I use DynamoDBMapper.BatchGetItemException's method getUnprocessedKeys() to get a list of the unprocessed keys?

merputation :

Say I have the following POJO with the necessary methods and stuff to successfully load and save items into a DDB table:

@DynamoDBTable(tableName = "my_ddb_table")
public class MyDDBTableItem {

    @DynamoDBHashKey(attributeName="id") 
    private String id;    // primary key
    private String a_value;
    private Set<String> some_values;
}

I am using dynamoDBMapper.batchload(), so I need to take action if the exception is thrown. I have never worked with DDB before, so I am unsure of what some of their terms mean (e.g. for getKeys(): "The primary key attribute values that define the items and the attributes associated with the items.").

What I currently want to do is just get some collection (list, set, etc.) of the unprocessed primary keys. Is this the right way to do it (I don't know how I'd test it)?

try {
    dynamoDBMapper.batchload(itemsToGet)...
} catch (BatchGetItemException e) {
    // I could get them as strings or as the instances of the class MyDDBTableItem, but I'll use String here.
    List<String> unprocessedKeys = e.getUnprocessedKeys()
        .get("my_ddb_table")
        .getKeys()    // after this is where I am unclear.
        .stream()
        .map(map -> map.get("id").getS())
        .collect(Collectors.toList());
    ...
}
albogdano :

Since you are using the higher level DynamoDB mapper, you don't actually need to catch that exception. You can specify how unprocessed keys are handled by using a strategy class implementing DynamoDBMapperConfig.BatchLoadRetryStrategy. By default, the DynamoDB client will use the DefaultBatchLoadRetryStrategy.

// TODO: implement the strategy interface
BatchLoadRetryStrategy myCustomStrategy = ...;

dynamoDBMapper.batchLoad(itemsToGet, new DynamoDBMapperConfig.Builder().
     withBatchLoadRetryStrategy(myCustomStrategy))

The KeysAndAttributes class is used to construct a list of items (by specifying their primary keys) and attributes (which columns to fetch) from a DynamoDB table. You can construct an instance of that class like this:

KeysAndAttributes kna = new KeysAndAttributes().withKeys(keysToLoad);

Basically, you would only want to call getUnprocessedKeys() manually if you are working with the lower level API for loading items in batches.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=357787&siteId=1