DynamoDb putItem ConditionExpression在c ++中 - 添加或更新

时间:2016-05-18 03:41:05

标签: c++ amazon-web-services amazon-dynamodb

我的putItem正在运行。现在我想确保只更新现有项目的新信息,或将其添加为新项目:

ConditionlExpression:

  • 如果分区:排序不存在则创建新项目
  • 更新if属性生成< :newTimestamp

所以我在代码中添加了一行:

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

这应该创建一个新项目,但它似乎试图在新项目不存在的情况下评估“生成”属性。

putItem返回错误:

  

无效的ConditionExpression:未定义表达式中使用的表达式属性值;属性值:: 1461782160

从调试器中,conditionExpression看起来像:

  

m_conditionExpression =“attribute_not_exists(机场)OR(attribute_exists(机场)AND(生成&lt;:1461782160))”

我试图避免:

  • 查找分区:sort
  • 如果它不存在,putItem
  • else检查生成的属性
  • 然后如果生成&lt; newTimestamp更新项目

有没有办法构造conditionExpression以满足我的期望?

编辑:使用updateItem

时出现同样的问题

代码:

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);

错误:

  

无效的UpdateExpression:未定义表达式中使用的表达式属性值;属性值:: 1461781980

该属性值是时间戳。它没有定义,因为此项目不存在且应该创建。

以下是我当前的工作

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;
    }
}

1 个答案:

答案 0 :(得分:0)

服务的错误消息表明您需要将:1461782160放在attributeMap中。 UpdateExpression应为"SET " + timestampName + " = :timestamp, " + attributeName + " = :a" 你的地图应该定义如下。

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