BatchGetItem操作上的DynamoDB和Boto3错误:“提供的键元素与架构不匹配”

时间:2018-07-31 01:08:49

标签: python-3.x nosql amazon-dynamodb boto3 dynamodb-queries

我在Boto3 / DynamoDB BatchGetItem 操作上遇到困难。我将不胜感激任何帮助或指导!我是python / aws的新手,如果这是一个新手问题,敬请原谅。

执行该操作时,出现此错误:

[[''] + lst[1:] for lst in listOfLists if lst[0] in patterns ]

这是我的代码:

botocore.exceptions.ClientError: An error occurred (ValidationException) when calling the BatchGetItem operation: The provided key element does not match the schema

This is a screen-cap of the items in the table.

This is a screen-cap of the table details, showing the partition key is 'item_ID' and that it's a 'string'

这是完整的错误消息:

import boto3
dynamodb = boto3.resource('dynamodb', region_name='us-west-2')
response = dynamodb.batch_get_item(
    RequestItems={
        'test': {
            'Keys': [
                {
                    'item_ID': {
                        'S': '1'
                    }
                },
                {
                    'item_ID': {
                        'S': '2'
                    }
                }
            ],
            'ProjectionExpression': 'item_ID, color',
        }
    }
)

2 个答案:

答案 0 :(得分:2)

您也可以使用Boto3.resource版本,但在这种情况下,请不要传递密钥类型。您的代码如下所示:

import boto3
dynamodb = boto3.resource('dynamodb')
response = dynamodb.batch_get_item(
    RequestItems={
        'test': {
            'Keys': [
                {'item_ID': 'ID1_value'},
                {'item_ID': 'ID2_value'}
            ]
        }
    }
)

在GitHub上有一个完整的工作示例:https://github.com/awsdocs/aws-doc-sdk-examples/blob/master/python/example_code/dynamodb/batching/dynamo_batching.py

答案 1 :(得分:0)

在这里回答我自己的问题...但是事实证明,您需要使用boto3.client而不是boto3.resouce。这是有效的更新代码:

import boto3
dynamodb = boto3.resource('dynamodb', region_name='us-west-2')
client = boto3.client('dynamodb', region_name='us-west-2')
response = client.batch_get_item(
    RequestItems={
        'test': {
            'Keys': [
                {
                    'item_ID': {
                        'S': '1'
                    }
                },
                {
                    'item_ID': {
                        'S': '2'
                    }
                }
            ],
            'ProjectionExpression': 'item_ID, color',
        }
    }
)

这是回应:

"C:\Users\Henry Miller\PycharmProjects\bioinformatics_webapp\venv\Scripts\python.exe" "C:/Users/Henry Miller/PycharmProjects/bioinformatics_webapp/get_items.py"
{'Responses': {'test': [{'item_ID': {'S': '1'}, 'color': {'S': 'red'}}, {'item_ID': {'S': '2'}, 'color': {'S': 'blue'}}]}, 'UnprocessedKeys': {}, 'ResponseMetadata': {'RequestId': 'BAH1SHCBHOMRJMJ5AHE7VRTON3VV4KQNSO5AEMVJF66Q9ASUAAJG', 'HTTPStatusCode': 200, 'HTTPHeaders': {'server': 'Server', 'date': 'Tue, 31 Jul 2018 04:15:13 GMT', 'content-type': 'application/x-amz-json-1.0', 'content-length': '130', 'connection': 'keep-alive', 'x-amzn-requestid': 'BAH1SHCBHOMRJMJ5AHE7VRTON3VV4KQNSO5AEMVJF66Q9ASUAAJG', 'x-amz-crc32': '1917096114'}, 'RetryAttempts': 0}}

Process finished with exit code 0