C++ DynamoDb putItem条件表达式在c++;-添加或更新
我的电脑正在工作。现在,我要确保仅更新现有项目的更新信息,或将其添加为新项目: 条件表达:C++ DynamoDb putItem条件表达式在c++;-添加或更新,c++,amazon-web-services,amazon-dynamodb,C++,Amazon Web Services,Amazon Dynamodb,我的电脑正在工作。现在,我要确保仅更新现有项目的更新信息,或将其添加为新项目: 条件表达: 如果分区:排序不存在,则创建新项 如果生成的属性
- 如果分区:排序不存在,则创建新项
- 如果生成的属性<:newTimestamp,则更新
putItemRequest.SetConditionExpression(" attribute_not_exists(" + partitionName + ") OR (attribute_exists(" + partitionName + ") AND (" + timestampName + " < :" + timestamp + "))");
putItemRequest.SetConditionExpression(“属性_不存在(“+partitionName+”)或(属性_存在(“+partitionName+”)和(“+timestamp+”)”);
这将创建一个新项,但它似乎正在尝试计算属性“generated”,而新项不存在该属性
putItem返回时出错:
ConditionExpression无效:未定义表达式中使用的表达式属性值;属性值::1461782160
从调试器中,条件表达式如下所示:
m_conditionExpression=“属性_不存在(机场)或(属性_存在(机场)和(生成<:1461782160))”
我试图避免:
- 查找分区:排序
- 如果它不存在,则输入项目
- 否则请检查生成的属性
- 然后,如果生成
UpdateItemRequest UpdateItemRequest;
updateItemRequest.WithTableName(dynamoDbTableName);
属性值hashPartition;
hashPartition.set(分区);
updateItemRequest.AddKey(partitionName,hashPartition);
属性值哈希排序;
hashSort.set(排序);
AddKey(sortName,hashSort);
属性值hashAttribute;
hashAttribute.set(属性);
Aws::地图属性映射;
属性映射[“:a”]=hashAttribute;
SetUpdateExpression(“SET”+timestamp name+”=:“+timestamp+”,“+attributeName+”=:a”);
updateItemRequest.SetExpressionAttributeValue(attributeMap);
//只允许更新较旧的项目
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;
}
}
ClientConfiguration配置;
config.region=Aws::region::US_WEST_2;
Aws::DynamoDB::DynamoDB客户端DynamoDB客户端(配置);
Aws::Map aMap;
PutItemRequest PutItemRequest;
putItemRequest.WithTableName(dynamoDbTableName);
属性值hashPartition;
hashPartition.set(分区);
AddItem(partitionName,hashPartition);
aMap[“:p”]=hashPartition;
属性值哈希排序;
hashSort.set(排序);
AddItem(sortName,hashSort);
aMap[“:s”]=hashSort;
属性值哈希时间戳;
hashTimestamp.SetN(timestamp);
AddItem(timestamp名称,hashTimestamp);
属性值hashAttribute;
hashAttribute.set(属性);
AddItem(attributeName,hashAttribute);
//不要更新现有项目
SetConditionExpression(“非(“+partitionName+”=:p)和(“+sortName+”=:s));
putItemRequest.SetExpressionAttributeValue(aMap);
auto-PutItemOutput=dynamoDbClient.PutItem(putItemRequest);
if(putItemOutput.IsSuccess())
{
poco_信息(记录器,“writeDb PutItem成功:“+partition+”:“+sort”);
状态=SwimpProcessor\u正常;
}
其他的
{
if(PutItemOutput.GetError().GetErrorType()==DynamoDBErrors::条件检查失败){
//项目已存在,请尝试更新
Aws::Map uMap;
uMap[“:t”]=哈希时间戳;
uMap[“:a”]=hashAttribute;
UpdateItemRequest UpdateItemRequest;
updateItemRequest.WithTableName(dynamoDbTableName);
updateItemRequest.AddKey(partitionName,hashPartition);
AddKey(sortName,hashSort);
updateItemRequest.SetUpdateExpression(“SET”+timestampName+”=:t,“+attributeName+”=:a”);
updateItemRequest.SetExpressionAttributeValue(uMap);
//只允许更新较旧的项目
updateItemRequest.SetConditionExpression(timestampName+“<:t”);
auto updateItemOutcome=dynamoDbClient.UpdateItem(updateItemRequest);
if(updateItemOutcome.IsSuccess())
{
poco_信息(记录器,“writeDb UpdateItem成功:“+partition+”:“+sort”);
状态=SwimpProcessor\u正常;
}
其他的
{
if(PutItemOutput.GetError().GetErrorType()==DynamoDBErrors::条件检查失败){
poco_信息(记录器,“writeDB UpdateItem新时间戳比当前时间戳旧”);
状态=SwimpProcessor\u正常;
}否则{
std::string msg(updateItemOutcome.GetError().GetMessage());
poco_错误(记录器,“writeDb UpdateItem失败:”+msg);
状态=SwimpProcessor\u DBWRITEERROR;
}
}
}否则{
std::string msg(putItemOutput.GetError().GetMessage());
poco_错误(记录器,“writeDb PutItem故障:“+msg”);
状态=SwimpProcessor\u DBWRITEERROR;
}
}
服务的错误消息说您需要将:1461782160
放入属性映射中。UpdateExpression应该是“SET”+timestamp name+“=: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;
AttributeValue hashAttributeA;
hashAttributeA.set(属性)
AttributeValue hashAttributeTimestamp;
hashAttributeTimestamp.SetN(时间戳)
Aws::地图属性映射;
attributeMap[“:a”]=hashAttributeA;
attributeMap[“:timestamp”]=hashAttributeTimestamp;
该服务的
AttributeValue hashAttributeA;
hashAttributeA.SetS(attribute)
AttributeValue hashAttributeTimestamp;
hashAttributeTimestamp.SetN(timestamp)
Aws::Map<Aws::String, AttributeValue> attributeMap;
attributeMap[":a"] = hashAttributeA;
attributeMap[":timestamp"] = hashAttributeTimestamp;