读取DynamoDB表的所有项目

时间:2019-03-18 10:27:17

标签: python json aws-lambda amazon-dynamodb boto3

我想读取DynamoDb表的所有项目。我正在使用table.scan()函数。它给了我回应。但是,输出是随意排列的。 我应该得到这样的输出-

{
"Items": [
{
  "Name": "ABC",
  "Location": "sdkjc",
  "id": "abc"
},

{
  "Name": "DEF",
  "Location": "jfyef",
  "id": "def"
}
]
}

但是我得到-

{
"Items": [
{
  "Name": "ABC",
  },
  {
  "Location": "sdkjc",
  "id": "abc"
  },

  {
  "Name": "DEF",
  "Location": "jfyef",
  },
  {
  "id": "def"
}
]
}

代码-

 import boto3
 dynamodb = boto3.resource("dynamodb")
 table = dynamodb.Table(event['tableName'])
 response = table.scan()
 print (response)

输出中没有图案。可能是什么问题?

1 个答案:

答案 0 :(得分:0)

try something like

def read():
    dynamodb = boto3.resource('dynamodb', region_name='us-west-2')
    table = dynamodb.Table("test")
    items = table.scan()['Items']
    for item in items:
        print (item)