Kent
Kent

Reputation: 1374

DynamoDb putItem ConditionExpression in c++ - add or update

My putItem is working. Now I want to make sure I update only newer information to an existing item, or add this as a new item:

ConditionlExpression:

So I added one line to code:

    putItemRequest.SetConditionExpression(" attribute_not_exists(" + partitionName + ") OR (attribute_exists(" + partitionName + ") AND (" + timestampName + " < :" + timestamp + "))");

This should create a new item, but it looks to be trying to evaluate the attribute 'generated' when it does not exist for a new item.

The error on putItem return:

Invalid ConditionExpression: An expression attribute value used in expression is not defined; attribute value: :1461782160

From the debugger the conditionExpression look like:

m_conditionExpression = " attribute_not_exists(airport) OR (attribute_exists(airport) AND (generated < :1461782160))"

I am trying to avoid:

Is there a way to construct the conditionExpression to meet my expectation?

Edit: Same problem when using updateItem

Code:

UpdateItemRequest updateItemRequest;
updateItemRequest.WithTableName(dynamoDbTableName);

AttributeValue hashPartition;
hashPartition.SetS(partition);
updateItemRequest.AddKey(partitionName, hashPartition);

AttributeValue hashSort;
hashSort.SetS(sort);
updateItemRequest.AddKey(sortName, hashSort);

AttributeValue hashAttribute;
hashAttribute.SetS(attribute);
Aws::Map<Aws::String, AttributeValue> attributeMap;
attributeMap[":a"] = hashAttribute;

updateItemRequest.SetUpdateExpression("SET " + timestampName + " = :" + timestamp + ", " + attributeName + " = :a");

updateItemRequest.SetExpressionAttributeValues(attributeMap);

// Allow only older items to be updated
updateItemRequest.SetConditionExpression("(" + timestampName + " < :" + timestamp + ")");

auto updateItemOutcome = dynamoDbClient.UpdateItem(updateItemRequest);

Error:

Invalid UpdateExpression: An expression attribute value used in expression is not defined; attribute value: :1461781980

That attribute value is the timestamp. It's not defined because this item doesn't exist and should be created.

Here is my current work around:

ClientConfiguration config;
config.region = Aws::Region::US_WEST_2;
Aws::DynamoDB::DynamoDBClient dynamoDbClient(config);

Aws::Map<Aws::String, AttributeValue> aMap;

PutItemRequest putItemRequest;
putItemRequest.WithTableName(dynamoDbTableName);

AttributeValue hashPartition;
hashPartition.SetS(partition);
putItemRequest.AddItem(partitionName, hashPartition);
aMap[":p"] = hashPartition;

AttributeValue hashSort;
hashSort.SetS(sort);
putItemRequest.AddItem(sortName, hashSort);
aMap[":s"] = hashSort;

AttributeValue hashTimestamp;
hashTimestamp.SetN(timestamp);
putItemRequest.AddItem(timestampName, hashTimestamp);

AttributeValue hashAttribute;
hashAttribute.SetS(attribute);
putItemRequest.AddItem(attributeName, hashAttribute);

// Do not update existing items
putItemRequest.SetConditionExpression("NOT((" + partitionName + " = :p) AND (" + sortName + " = :s))");
putItemRequest.SetExpressionAttributeValues(aMap);

auto putItemOutcome = dynamoDbClient.PutItem(putItemRequest);

if(putItemOutcome.IsSuccess())
{
    poco_information(logger, "writeDb PutItem Success: " + partition + ":" + sort);
    status = SWIMPROCESSOR_OK;
}
else
{
    if(putItemOutcome.GetError().GetErrorType() == DynamoDBErrors::CONDITIONAL_CHECK_FAILED) {
        // item exists, try to update
        Aws::Map<Aws::String, AttributeValue> uMap;
        uMap[":t"] = hashTimestamp;
        uMap[":a"] = hashAttribute;

        UpdateItemRequest updateItemRequest;
        updateItemRequest.WithTableName(dynamoDbTableName);
        updateItemRequest.AddKey(partitionName, hashPartition);
        updateItemRequest.AddKey(sortName, hashSort);
        updateItemRequest.SetUpdateExpression("SET " + timestampName + " = :t, " + attributeName + " = :a");
        updateItemRequest.SetExpressionAttributeValues(uMap);

        // Allow only older items to be updated
        updateItemRequest.SetConditionExpression(timestampName + " < :t");

        auto updateItemOutcome = dynamoDbClient.UpdateItem(updateItemRequest);

        if(updateItemOutcome.IsSuccess())
        {
            poco_information(logger, "writeDb UpdateItem Success: " + partition + ":" + sort);
            status = SWIMPROCESSOR_OK;
        }
        else
        {
            if(putItemOutcome.GetError().GetErrorType() == DynamoDBErrors::CONDITIONAL_CHECK_FAILED) {
                poco_information(logger, "writeDB UpdateItem new timestamp is older then current timestamp");
                status = SWIMPROCESSOR_OK;
            } else {
                std::string msg(updateItemOutcome.GetError().GetMessage());
                poco_error(logger, "writeDb UpdateItem Failure: " + msg);
                status = SWIMPROCESSOR_DBWRITEERROR;
            }
        }

    } else {
        std::string msg(putItemOutcome.GetError().GetMessage());
        poco_error(logger, "writeDb PutItem Failure: " + msg);
        status = SWIMPROCESSOR_DBWRITEERROR;
    }
}

Upvotes: 1

Views: 1075

Answers (1)

Alexander Patrikalakis
Alexander Patrikalakis

Reputation: 5205

The service's error message says that you need to put :1461782160 in the attributeMap. UpdateExpression should be "SET " + timestampName + " = :timestamp, " + attributeName + " = :a" and your map should be defined as follows.

AttributeValue hashAttributeA;
hashAttributeA.SetS(attribute)
AttributeValue hashAttributeTimestamp;
hashAttributeTimestamp.SetN(timestamp)
Aws::Map<Aws::String, AttributeValue> attributeMap;
attributeMap[":a"] = hashAttributeA;
attributeMap[":timestamp"] = hashAttributeTimestamp;

Upvotes: 0

Related Questions