如何从 DynamoDB 获取项目

问题描述 投票:0回答:1

我尝试从 Dynamo DB 表获取项目,但最终收到错误消息。尝试使用 boto3 资源和客户端,但没有成功。任何帮助将不胜感激。

表中accountId为PK,comState为SK。

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

response = table.get_item(Key={'accountId': 'ACC123',
                          'comState':'STARTED'})
items = response.get('Item', None)

我收到以下错误消息。

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

  Captured logging:
  INFO:botocore.tokens:Loading cached SSO token for default
python-3.x amazon-dynamodb
1个回答
0
投票

我认为您需要编辑 Key 字典的格式,以包含键的值类型及其值,格式如下

{
  '<key 1>': {
    '<value type 1>': <value 1>
  },
  '<key 2>': {
    '<value type 2>': <value 2>
  },
}

根据您的代码,我认为两个键的正确值类型是字符串(

S
)(您可以了解有关其他值类型的更多信息here

response = table.get_item(Key={
  'accountId': {'S': 'ACC123'},
  'comState':{'S': 'STARTED'}
})
© www.soinside.com 2019 - 2024. All rights reserved.