使用Boto3更新DynamoDb 更新姓氏和名字

问题描述

我正在编写一个lambda函数来更新dynamodb项目。但是问题是,当我给

 {
  "employee_id": "1","last_name": "jane","first_name": "anderson"
}

它可以正常工作,但是如果您删除last_name或first_name,程序将崩溃。我想使其动态化,就像我提供last_name和employee_id一样,它仅更改了last_name,并且与first_name相同。如果我同时提供first_name和last_name,则两者都会更改。 Note: employee_id is used here to fetch the data

 {
  "employee_id": "1","last_name": "jane"
}

给出错误提示

{
  "errorMessage": "'first_name'","errorType": "KeyError","stackTrace": [
    "  File \"/var/task/lambda_function.py\",line 10,in lambda_handler\n    first_name= event['first_name']\n"
  ]
}

Lambda函数

import boto3
import json

dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('Employee')

def lambda_handler(event,context):
    employee_id = event['employee_id']
    last_name= event['last_name']
    first_name= event['first_name']
    update = table.update_item(
        Key={
            'employee_id': employee_id
        },ConditionExpression= 'attribute_exists(employee_id)',UpdateExpression='SET first_name = :val1,last_name = :val2',ExpressionAttributeValues={
            ':val1': last_name,':val2': first_name
        }
    )

解决方法

几种可能性,您可以解决此问题。如果不存在first_name,则可以跳过DynamoDB中的文件,也可以提供一些默认值/空值。这是特定于用例的,并且取决于您要如何处理丢失的first_name。如果不允许这种情况,也可能会引发错误。

无论哪种方式,如果first_name中存在event,都会执行检查

以下是第一个选项的示例

import boto3
import json

dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('Employee-wahaj')

def lambda_handler(event,context):

    employee_id = event['employee_id']
    last_name = event['last_name']

    UpdateExpression = 'SET last_name = :val1'
    ExpressionAttributeValues = {':val1': last_name }


    if 'first_name' in event:
        
        first_name= event['first_name']
        UpdateExpression = 'SET last_name = :val1,first_name = :val2'
        ExpressionAttributeValues = {
                ':val1': last_name,':val2': first_name
            }

    update = table.update_item(
        Key={
            'employee_id': employee_id
        },ConditionExpression= 'attribute_exists(employee_id)',UpdateExpression=UpdateExpression,ExpressionAttributeValues=ExpressionAttributeValues
    )

更新姓氏和名字

它是一个基本的解决方案(只有很少的if-else-if),因为我不想让示例过于复杂。

import boto3
import json

dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('Employee-wahaj')

def lambda_handler(event,context):

    employee_id = event['employee_id']

    if 'first_name' in event and 'last_name' not in event:

        first_name = event['first_name']
        UpdateExpression = 'SET first_name = :val1'
        ExpressionAttributeValues = {':val1': first_name }

    elif 'last_name' in event and 'first_name' not in event:

        last_name = event['last_name']
        UpdateExpression = 'SET last_name = :val1'
        ExpressionAttributeValues = {':val1': last_name}

    elif 'first_name' in event and 'last_name' in event:

        last_name = event['last_name']
        first_name= event['first_name']
        UpdateExpression = 'SET last_name = :val1,':val2': first_name
            }

    else:
        raise ValueError("first_name and last_name not given")


    update = table.update_item(
        Key={
            'employee_id': employee_id
        },ExpressionAttributeValues=ExpressionAttributeValues
    )