Nghia Do
Nghia Do

Reputation: 2658

Dynamodb with list.append in Java

Currently my dynamodb has a key is batch_id and then a list 'doc_info_list'. I would like to add to the list with below code.

But it is showing an exception

com.amazonaws.services.dynamodbv2.model.AmazonDynamoDBException: ExpressionAttributeValues contains invalid value: One or more parameter values were invalid: An AttributeValue may not contain an empty string for key :val1 (Service: AmazonDynamoDBv2; Status Code: 400; Error Code: ValidationException; Request ID: 03LRKK3J3SBVFNGMKV36NVSQBJVV4KQNSO5AEMVJF66Q9ASUAAJG)

Code:

AmazonDynamoDB amazonDynamoDBClient = AmazonDynamoDBClientBuilder.defaultClient();
        DynamoDB dynamoDB = new DynamoDB(amazonDynamoDBClient);
        Table table = dynamoDB.getTable("XXXX");

        String currentDateTime = MiscHelper.getDateTimeAsISO8601();

        Map<String, Object> map = objectMapper.convertValue(doc, Map.class);

        ValueMap valueMap = new ValueMap().withList(":val1", Arrays.asList(map));
        valueMap.withList(":empty_list", new ArrayList<>());
        valueMap.withString(":val2", currentDateTime);
        UpdateItemSpec updateItemSpec = new UpdateItemSpec().withPrimaryKey("batch_id", batchId)
                .withUpdateExpression("set doc_info_list = list_append(if_not_exists(doc_info_list, :empty_list), :val1), created_date_time = :val2, updated_date_time = :val2")
                .withValueMap(valueMap)
                .withReturnValues(ReturnValue.UPDATED_NEW);

What am I doing wrong here?

Upvotes: 1

Views: 1548

Answers (1)

Nestor Sokil
Nestor Sokil

Reputation: 2272

So, you're trying to save doc into dynamodb through a Map representation. The only problem I might guess is that some of your doc fields are an empty String and then you're trying to save it as Dynamo's AttributeValue and this is not allowed: https://forums.aws.amazon.com/thread.jspa?threadID=90137

Upvotes: 1

Related Questions