问题描述
我正在尝试使用我的API方法来实现更新操作,以更新表的内容.
I am trying to achieve an update operation using my API method to update the content of the table.
我设置了以下lambda代码:
I have following lambda code set up:
def lambda_handler(event, context):
param = event['queryStringParameters']['employeID']
name = event['queryStringParameters']['employeName']
dynamodb = boto3.resource('dynamodb', region_name="us-east-1")
table = dynamodb.Table('api_demo_employe')
response = table.update_item(
Key = {
'employeID' : param
}
)
我需要弄清楚如何使用我的更新变量设置 UpdateExpression,ConditionExpression,ExpressionAttributeValues .
I need to figure out how can i set the UpdateExpression,ConditionExpression, ExpressionAttributeValues using my update variable.
有人可以帮我吗?
推荐答案
会是这样的:
response = table.update_item(TableName=table,
ReturnValues='UPDATED_NEW',
Key={'employeID': {'S': param}},
ExpressionAttributeValues={':f': {'S': update['some_param']},
':limit': {'N': '500'}},
UpdateExpression='SET some_field = :f',
ConditionExpression='some_other_field > :limit'
)
当然,这只是一个例子-您的特定表达式将取决于您要执行的操作.我正在猜测您的数据类型.'S'
用于字符串,但是其他数据类型还有其他选项.您可以在 ExpressionAttributeValues
中定义替换变量,然后根据需要在 UpdateExpression
和 ConditionExpression
中引用它们.
Of course this is just an example--your specific expressions will depend on what you are trying to do. And I am guessing at your data types. The 'S'
is for a string, but there are other options for other data types. You define the substitution variables in ExpressionAttributeValues
and then reference those in the UpdateExpression
and ConditionExpression
as needed.
有关所有选项的详细信息,请参见 https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/dynamodb.html#DynamoDB.Client.update_item 您将要查看这些选项并看看那里的好例子,了解如何做不同的事情.
Full details of all the options are available at https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/dynamodb.html#DynamoDB.Client.update_item You'll want to review those options and look at the good examples there for how to do different things.
这篇关于使用Boto3实现的Lambda函数更新DynamoDB表的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!