发电机根据条件插入或更新boto3
dynamo insert or update based on condition boto3
我有一个发电机 table 结构为:
partKey sortKey thirdAttribute
1 sort1 x
2 sort2 y
1 sort2 w
2 sort3 z
这是我想要插入或更新的方式:
如果partition+sortKey组合不存在则插入
仅在以下情况下更新:
partition+sortKey组合存在但thirdAttribute=x或y如果新项如下:
item:{
"partKey":1,
"sortKey":"sort1",
"thirdAttribute":"k"
}
或
item:{
"partKey":2,
"sortKey":"sort2",
"thirdAttribute":"l"
}
而新的 table 应如下所示:
partKey sortKey thirdAttribute
1 sort1 k(updated)
2 sort2 l(updated)
1 sort2 w
2 sort3 z
2 sort4 k (inserted)
尝试了以下但不符合预期:
table.put_item(
Item=item,
ConditionExpression='partKey <> :v_partKey AND sortKey <>:v_sortKey and (attribute_not_exists(thirdAttribute) '
'or thirdAttribute <> :v_thirdAttribute_x or thirdAttribute <> :v_thirdAttribute_y)',
ExpressionAttributeValues={':v_partKey': item['partKey'], ':v_apId': item['sortKey'], ':v_thirdAttribute_x': 'x',':v_thirdAttribute_y':'y'}
这应该有效:
table.put_item(
Item=json_data,
ConditionExpression='(attribute_not_exists(partKey) and attribute_not_exists(sortKey)) or (thirdAttribute <> :v_thirdAttribute_x or thirdAttribute <> :v_thirdAttribute_y)',
ExpressionAttributeValues={':v_thirdAttribute_x': 'x',
':v_thirdAttribute_y': 'y'}
)
我有一个发电机 table 结构为:
partKey sortKey thirdAttribute
1 sort1 x
2 sort2 y
1 sort2 w
2 sort3 z
这是我想要插入或更新的方式:
如果partition+sortKey组合不存在则插入
仅在以下情况下更新:
partition+sortKey组合存在但thirdAttribute=x或y如果新项如下:
item:{
"partKey":1,
"sortKey":"sort1",
"thirdAttribute":"k"
}
或
item:{
"partKey":2,
"sortKey":"sort2",
"thirdAttribute":"l"
}
而新的 table 应如下所示:
partKey sortKey thirdAttribute
1 sort1 k(updated)
2 sort2 l(updated)
1 sort2 w
2 sort3 z
2 sort4 k (inserted)
尝试了以下但不符合预期:
table.put_item(
Item=item,
ConditionExpression='partKey <> :v_partKey AND sortKey <>:v_sortKey and (attribute_not_exists(thirdAttribute) '
'or thirdAttribute <> :v_thirdAttribute_x or thirdAttribute <> :v_thirdAttribute_y)',
ExpressionAttributeValues={':v_partKey': item['partKey'], ':v_apId': item['sortKey'], ':v_thirdAttribute_x': 'x',':v_thirdAttribute_y':'y'}
这应该有效:
table.put_item(
Item=json_data,
ConditionExpression='(attribute_not_exists(partKey) and attribute_not_exists(sortKey)) or (thirdAttribute <> :v_thirdAttribute_x or thirdAttribute <> :v_thirdAttribute_y)',
ExpressionAttributeValues={':v_thirdAttribute_x': 'x',
':v_thirdAttribute_y': 'y'}
)