gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 13:16:11 26 4
gpt4 key购买 nike

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

条件表达式:

  • 如果 partition:sort 不存在则创建新项
  • 如果属性生成则更新 <:newTimestamp

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

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

这应该创建一个新项目,但它看起来是在尝试评估新项目不存在的“生成”属性。

putItem返回错误:

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

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

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

我正在努力避免:

  • 查找分区:排序
  • 如果不存在,则putItem
  • 否则检查生成的属性
  • 然后如果生成

有没有办法构造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);

错误:

Invalid UpdateExpression: An expression attribute value used in expression is not defined; attribute value: :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;
}
}

最佳答案

服务的错误消息说您需要将 :1461782160 放入 attributeMap 中。 UpdateExpression 应该是 "SET "+ timestampName + "= :timestamp, "+ attributeName + "= :a"并且您的 map 应定义如下。

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

关于c++ - C++ 中的 DynamoDb putItem ConditionExpression - 添加或更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37289868/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com