【问题标题】:DynamoDB and Boto3 error on BatchGetItem operation: "The provided key element does not match the schema"BatchGetItem 操作上的 DynamoDB 和 Boto3 错误:“提供的关键元素与架构不匹配”
【发布时间】:2018-07-31 01:08:48
【问题描述】:

我在使用 Boto3/DynamoDB BatchGetItem 操作时遇到了困难。我将不胜感激任何帮助或指导!如果这是一个新手问题,我对 python/aws 很陌生,所以很抱歉。

当我执行操作时,我得到这个错误:

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

这是我的代码:

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',
        }
    }
)

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'

这是完整的错误信息:

Traceback (most recent call last):
File "C:/Users/Henry Miller/PycharmProjects/bioinformatics_webapp/get_items.py", line 18, in <module>
'ProjectionExpression': 'item_ID, color',
File "C:\Users\Henry Miller\PycharmProjects\bioinformatics_webapp\venv\lib\site-packages\boto3\resources\factory.py", line 520, in do_action
response = action(self, *args, **kwargs)
File "C:\Users\Henry Miller\PycharmProjects\bioinformatics_webapp\venv\lib\site-packages\boto3\resources\action.py", line 83, in __call__
response = getattr(parent.meta.client, operation_name)(**params)
File "C:\Users\Henry Miller\PycharmProjects\bioinformatics_webapp\venv\lib\site-packages\botocore\client.py", line 314, in _api_call
return self._make_api_call(operation_name, kwargs)
File "C:\Users\Henry Miller\PycharmProjects\bioinformatics_webapp\venv\lib\site-packages\botocore\client.py", line 612, in _make_api_call
raise error_class(parsed_response, operation_name)
botocore.exceptions.ClientError: An error occurred (ValidationException) when calling the BatchGetItem operation: The provided key element does not match the schema

【问题讨论】:

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


    【解决方案1】:

    在这里回答我自己的问题...但事实证明您需要使用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
    

    【讨论】:

    • 我可以知道为什么会发生这种变化吗?是版本问题吗?
    【解决方案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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-09-14
      • 2021-08-26
      • 2019-07-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多