在文献资料之后,我尝试创建一个update语句,如果不存在,它将更新或添加dynamodb表中的一个属性。
我在试这个
response = table.update_item(
Key={'ReleaseNumber': '1.0.179'},
UpdateExpression='SET',
ConditionExpression='Attr(\'ReleaseNumber\').eq(\'1.0.179\')',
ExpressionAttributeNames={'attr1': 'val1'},
ExpressionAttributeValues={'val1': 'false'}
)我得到的错误是:
botocore.exceptions.ClientError: An error occurred (ValidationException) when calling the UpdateItem operation: ExpressionAttributeNames contains invalid key: Syntax error; key: "attr1"
如果有人做了任何类似我正在努力实现的事情,请分享例子。
发布于 2015-12-24 04:50:03
找到工作示例这里,非常重要的是要将表的所有索引作为键列出,这将需要在更新之前进行额外的查询,但它可以工作。
response = table.update_item(
Key={
'ReleaseNumber': releaseNumber,
'Timestamp': result[0]['Timestamp']
},
UpdateExpression="set Sanity = :r",
ExpressionAttributeValues={
':r': 'false',
},
ReturnValues="UPDATED_NEW"
)发布于 2018-04-04 17:23:06
关于使用boto3的dynamodb更新的详细信息似乎在网上非常稀少,所以我希望这些替代解决方案是有用的。
得到/放置
import boto3
table = boto3.resource('dynamodb').Table('my_table')
# get item
response = table.get_item(Key={'pkey': 'asdf12345'})
item = response['Item']
# update
item['status'] = 'complete'
# put (idempotent)
table.put_item(Item=item)实际更新
import boto3
table = boto3.resource('dynamodb').Table('my_table')
table.update_item(
Key={'pkey': 'asdf12345'},
AttributeUpdates={
'status': 'complete',
},
)发布于 2020-05-26 19:59:03
如果您不想检查更新的参数,我编写了一个很酷的函数,它将返回所需的参数,以便使用update_item执行boto3方法。
def get_update_params(body):
"""Given a dictionary we generate an update expression and a dict of values
to update a dynamodb table.
Params:
body (dict): Parameters to use for formatting.
Returns:
update expression, dict of values.
"""
update_expression = ["set "]
update_values = dict()
for key, val in body.items():
update_expression.append(f" {key} = :{key},")
update_values[f":{key}"] = val
return "".join(update_expression)[:-1], update_values下面是一个简单的例子:
def update(body):
a, v = get_update_params(body)
response = table.update_item(
Key={'uuid':str(uuid)},
UpdateExpression=a,
ExpressionAttributeValues=dict(v)
)
return responsehttps://stackoverflow.com/questions/34447304
复制相似问题