无法使用 python 从 AWS dynamodb 获取 get_item?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37589254/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 19:35:39  来源:igfitidea点击:

Not able to get_item from AWS dynamodb using python?

pythonamazon-dynamodbboto3

提问by Narendra M

I am new to dynamodb trying to get data from dynamodb.

我是 dynamodb 的新手,试图从 dynamodb 获取数据。

This is my table with "topic" as a primary hash key

这是我以“主题”作为主哈希键的表

my python code

我的蟒蛇代码

import boto3 
from boto3 import dynamodb 

from boto3.session import Session

from boto3.dynamodb.conditions import Key, Attr


dynamodb_session = Session(aws_access_key_id='XXXXXXXXXXXXXXX',
          aws_secret_access_key='XXXXXXXXXXXXXXXXXXXXXXXXXXXX',
          region_name='us-east-1')

dynamodb = dynamodb_session.resource('dynamodb')

table=dynamodb.Table('Garbage_collector_table')

my_topic = "$aws/things/garbage_collector_thing/shadow/update/accepted"

response = table.get_item(TableName='Garbage_collector_table', Key={'topic':my_topic})

for res in response: 
    print "result ",res

I am getting the following error

我收到以下错误

Traceback (most recent call last):
 File "get-data-dynamodb-boto3.py", line 19, in <module>
    response = table.get_item(TableName='Garbage_collector_table', Key={'topic': my_topic})   File
 "/usr/local/lib/python2.7/dist-packages/boto3/resources/factory.py",
 line 518, in do_action
     response = action(self, *args, **kwargs)   File "/usr/local/lib/python2.7/dist-packages/boto3/resources/action.py",
 line 83, in __call__
     response = getattr(parent.meta.client, operation_name)(**params)   File "/usr/local/lib/python2.7/dist-packages/botocore/client.py", line
 258, in _api_call
     return self._make_api_call(operation_name, kwargs)   File /usr/local/lib/python2.7/dist-packages/botocore/client.py", line 548,
 in _make_api_call
     raise ClientError(parsed_response, operation_name)

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

botocore.exceptions.ClientError: 调用 GetItem 操作时发生错误 (ValidationException):提供的关键元素与架构不匹配

am I missing anything in my code?

我的代码中是否缺少任何内容?

回答by Eyal Ch

You are mixing resource and client objects which have different methods. More info here.

您正在混合具有不同方法的资源和客户端对象。更多信息在这里

The correct syntax for a resource is:

资源的正确语法是:

response = table.get_item(Key={'topic': my_topic})

but personally I recommend to use boto client:

但我个人建议使用 boto 客户端:

client = boto3.client('dynamodb')

response = client.get_item(TableName='Garbage_collector_table', Key={'topic':{'S':str(my_topic)}})

http://boto3.readthedocs.io/en/latest/reference/services/dynamodb.html

http://boto3.readthedocs.io/en/latest/reference/services/dynamodb.html

回答by Tomiwa

You can also query the database:

您还可以查询数据库:

from boto3.dynamodb.conditions import Key

table = dynamodb.Table(table_name)
response = table.query(
    KeyConditionExpression=Key('topic').eq(my_topic)
)
items = response['Items']
if items:
    return items[0]
else:
    return []

Source: https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/GettingStarted.Python.04.html

来源:https: //docs.aws.amazon.com/amazondynamodb/latest/developerguide/GettingStarted.Python.04.html

回答by kartik

Assuming you only have partition key (aka hash key) in the table.

假设您在表中只有分区键(又名散列键)。

import boto3
dynamodb = boto3.resource('dynamodb',region_name='ap-southeast-2')
table = dynamodb.Table('my-table')
key = {}
key['key'] = 'my-key'
print(key)
response = table.get_item(Key=key)
print(response['Item'])

回答by mprivat

There are actual examples here: https://boto3.amazonaws.com/v1/documentation/api/latest/guide/dynamodb.html

这里有实际例子:https: //boto3.amazonaws.com/v1/documentation/api/latest/guide/dynamodb.html

In your case, you need to do this:

在你的情况下,你需要这样做:

response = table.get_item(TableName='Garbage_collector_table', Key={'topic': my_topic})