Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DynamoDB Mapper "batchLoad()" input

I was just using batchLoad function of dynamoDB. Here, the documentation of the function says, the input it takes is List<KeyPair>. But when I use a KeyPair object, it throws the error that the argument should be a dynamodb annotated class.

I can use a DynamoDB class, where I set only hashKey and rangeKey attributes of the class and pass them as an argument. But now my use case is the Class(DynamoDB annotated), I am using has @NonNull fields. If I have to pass arguments for this I have to set junk values in them, which is obviously not desirable. Any kind of help/ suggestions ? Thanks!

like image 416
Arushi Avatar asked Dec 19 '22 06:12

Arushi


1 Answers

Here is the working example.

Summary:-

  • Model class - should be the key of map
  • keyPairList - List of key pairs which you would like to retrieve

With model class:-

Map<Class<?>, List<KeyPair>> keyPairForTable = new HashMap<>();
            keyPairForTable.put(Movies.class, keyPairList);

Full code:-

public Boolean batchLoadMoviesUsingKeyPair() {

    DynamoDBMapper dynamoDBMapper = new DynamoDBMapper(dynamoDBClient);

    KeyPair keyPair1 = new KeyPair();
    keyPair1.withHashKey(1991);
    keyPair1.withRangeKey("Movie with map attribute");

    KeyPair keyPair2 = new KeyPair();
    keyPair2.withHashKey(2010);
    keyPair2.withRangeKey("The Big New Movie 2010");

    List<KeyPair> keyPairList = new ArrayList<>();
    keyPairList.add(keyPair1);
    keyPairList.add(keyPair2);

    Map<Class<?>, List<KeyPair>> keyPairForTable = new HashMap<>();
    keyPairForTable.put(Movies.class, keyPairList);

    Map<String, List<Object>> batchResults = dynamoDBMapper.batchLoad(keyPairForTable);

    for (Map.Entry<String, List<Object>> entry : batchResults.entrySet()) {
        System.out.println(entry.getKey());
        System.out.println(entry.getValue());
    }

    return true;

}
like image 124
notionquest Avatar answered Mar 12 '23 19:03

notionquest