Arithmetic operation in condition expression DynamoDb Using the AWS SDK for Java

Viewed 1104

I have a dynamodb Table and I want to do a conditional update operation on that using aws sdk java library. I have the table with hashkey named as "Id" and sortkey named as "Sk" . I have 5 other fields in the table and i have to update 2 fields based on the condition on the remaining fields. The condition is "total_record_count = record_passed + records_failed" , where total_record_count, record_passed and records_failed are the fields in the table. Below is the code

DynamoDB dynamoDB = new DynamoDB(amazonDynamoDB);
        Table table = dynamoDB.getTable("ciw_ocrResponse_upload");

        Map<String, String> expressionAttributeNames = new HashMap<String, String>();
        expressionAttributeNames.put("#updatedAt", "updatedAt");
        expressionAttributeNames.put("#statusOfTracker","statusOfTracker");

        Timestamp timestamp = new Timestamp(System.currentTimeMillis());
        Map<String, Object> eav = new HashMap<>();
        eav.put(":val1", value);
        eav.put(":val2", timestamp.getTime());


        UpdateItemSpec updateItemSpec = new UpdateItemSpec().withPrimaryKey("Id", requestId, "Sk", sortKey)
                .withUpdateExpression("set #updatedAt = :val2, #statusOfTracker = :val1")
                .withValueMap(eav)
                .withNameMap(expressionAttributeNames)
                .withConditionExpression(" total_record_count = record_passed + records_failed")
                .withReturnValues(ReturnValue.UPDATED_NEW);

        table.updateItem(updateItemSpec);
I am getting the error as Invalid ConditionExpression: Syntax error; token: "+", near: "record_passed + records_failed"
1 Answers

According to the docs this arithmetic operation is not supported.

Workaround is to create a total_record_count field which you need to calculate and add on every new item created. Also to add it to all existing items.

Related