【问题标题】:Client Error while updating the DynamoDB?更新 DynamoDB 时出现客户端错误?
【发布时间】:2020-11-10 17:05:33
【问题描述】:

代码如下

import boto3
dynamodb = boto3.resource ('dynamodb')
table =dynamodb.Table('test')
def lambda_handler(event, context):
    response = table.update_item(
        Key={
            'id': "100",
            'name': "David"
            })

我创建了一个 DynamoDB 表 test 我的主键是 id 这是字符串。

在 DynamoDB 中,id 100 的表值为 John,我需要更新为 David。以上是代码。为什么错误抛出元模式

下面是完整的错误

"errorMessage": "调用UpdateItem操作时发生错误(ValidationException):更新表达式中提供的文档路径对更新无效", "errorType": "ClientError",

试过下面的代码

import boto3
dynamodb = boto3.resource ('dynamodb')
table =dynamodb.Table('test')
def lambda_handler(event, context):
    response = table.update_item(
    Key={
        'id': '100'
        },
    UpdateExpression='SET name = :val1',
    ExpressionAttributeValues={
        ':val1': 'David'
    })

再添加一张表来复制案例

放表:输出>>成功

首先在 DynamoDB 中创建表 newTable

import boto3

def lambda_handler(event, context):
    dynamodb = boto3.resource ('dynamodb')
    table =dynamodb.Table('newTable')
    response = table.put_item(
    Item={
        'username': 'Ac',
        'first_name': 'DEF',
        'last_name': 'FHI',
        'age': 10,
        'account': 'GOld'
    })

如何获得物品?输出>>错误

import boto3

def lambda_handler(event, context):
    dynamodb = boto3.resource ('dynamodb')
    table =dynamodb.Table('newTable')
    response = table.get_item(
        Key={
            'username':'Ac'
        }
        )
    print (response)

错误>>响应: "errorMessage": "调用 GetItem 操作时发生错误 (ValidationException):提供的关键元素与架构不匹配", "errorType": "ClientError",

【问题讨论】:

  • 你能发布错误信息吗?
  • @Marcin "调用 UpdateItem 操作时发生错误 (ValidationException):提供的关键元素与架构不匹配",客户端错误
  • @Marcin 没有快捷键
  • 架构是什么?
  • @Marcin 都是strings

标签: python amazon-web-services aws-lambda amazon-dynamodb


【解决方案1】:

第二个答案

get和update需要准确的item更新而不是批量更新,所以还需要提供对应的排序键

礼貌@Sairsreenivas

import boto3

def lambda_handler(event, context):
    dynamodb = boto3.resource ('dynamodb')
    table =dynamodb.Table('newTable')
    # response = table.put_item(
    # Item={
    #     'username': 'Ac',
    #     'first_name': 'DEF',
    #     'last_name': 'GH',
    #     'age': 10,
    #     'account': 'GOld'
    # })
    # try:
    #     response = table.get_item(Key={'username':'Mak'})
    # except Exception as e:
    #     print(e.response['Error']['Message'])
    # else:
    #     return response['Item']
    # item = response['Item']
    # print (item)
    #Get Item
    response = table.get_item(Key={'username':'Ac', 'last_name':'GH'})
    print (response['Item'])
    table.update_item(
        Key ={
            'username':'Ac', 'last_name':'GH'
        },
        UpdateExpression = 'SET age = :value1',
        ExpressionAttributeValues={
            ':value1':20
        }
        )
    print ("After update \n")
    response = table.get_item(Key={'username':'Ac', 'last_name':'GH'})
    print (response['Item'])

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-12-28
    • 1970-01-01
    • 1970-01-01
    • 2022-07-10
    • 2010-11-23
    • 1970-01-01
    • 2019-06-09
    相关资源
    最近更新 更多