Python 如何使用Boto3将子级JSON文件加载到DynamoDB中?
我试图用Python和Boto3将JSON文件加载到AWS dynamoDB中时遇到了问题,因为该文件具有子级别JSON 例如,我有以下代码:Python 如何使用Boto3将子级JSON文件加载到DynamoDB中?,python,python-2.7,amazon-web-services,amazon-dynamodb,boto3,Python,Python 2.7,Amazon Web Services,Amazon Dynamodb,Boto3,我试图用Python和Boto3将JSON文件加载到AWS dynamoDB中时遇到了问题,因为该文件具有子级别JSON 例如,我有以下代码: from __future__ import print_function # Python 2/3 compatibility import boto3 dynamodb = boto3.resource('dynamodb', region_name='sa-east-1', aws_access_key_id='AWS ACCESS KEY',
from __future__ import print_function # Python 2/3 compatibility
import boto3
dynamodb = boto3.resource('dynamodb', region_name='sa-east-1', aws_access_key_id='AWS ACCESS KEY', aws_secret_access_key='AWS SECRET KEY')
table = dynamodb.create_table(
TableName='Movies',
KeySchema=[
{
'AttributeName': 'year',
'KeyType': 'HASH' #Partition key
},
{
'AttributeName': 'title',
'KeyType': 'RANGE' #Sort key
}
],
AttributeDefinitions=[
{
'AttributeName': 'year',
'AttributeType': 'N'
},
{
'AttributeName': 'title',
'AttributeType': 'S'
},
],
ProvisionedThroughput={
'ReadCapacityUnits': 10,
'WriteCapacityUnits': 10
}
)
print("Table status:", table.table_status)
在这个布局中,我在AWS dynamoDB中创建了一个表,但仅在一级结构上为JSON创建一个表,如:
[
{
"year": 2013,
"title": "Rush"
}
]
但是如果我想把一个JSON文件和子级别放在一起?如何使用Boto3创建此表?如何输入文件?像这样:
[
{
"year": 2013,
"title": "Rush",
"info": {
"directors": ["Ron Howard"],
"release_date": "2013-09-02T00:00:00Z",
"rating": 8.3,
"genres": [
"Action",
"Biography",
"Drama",
"Sport"
],
"image_url": "http://ia.media-imdb.com/images/M/MV5BMTQyMDE0MTY0OV5BMl5BanBnXkFtZTcwMjI2OTI0OQ@@._V1_SX400_.jpg",
"plot": "A re-creation of the merciless 1970s rivalry between Formula One rivals James Hunt and Niki Lauda.",
"rank": 2,
"running_time_secs": 7380,
"actors": [
"Daniel Bruhl",
"Chris Hemsworth",
"Olivia Wilde"
]
}
}
]
我在网上阅读了这两个文档并搜索了一些教程,但我找不到如何做到这一点。这应该很简单,我知道我必须有办法做到这一点,但我还不能得到它。有人给我一些提示吗?使用上面的示例,我认为您可以简单地使用table.update\u item()方法
实际上,我犯了一个简单的概念错误。对于DynamoDB,在创建表时,不需要声明表的每个属性。在这个阶段,您只需要说谁将是分区键和排序键(如果有)。如果输入的项具有更多属性,则可以在put_item()函数上声明,如:
from __future__ import print_function # Python 2/3 compatibility
import boto3
import json
import decimal
dynamodb = boto3.resource('dynamodb', region_name='sa-east-1', aws_access_key_id='AWS ACCESS KEY', aws_secret_access_key='AWS SECRET KEY')
table = dynamodb.Table('Movies')
title = "The Big New Movie"
year = 2015
response = table.put_item(
Item={
'year': year,
'title': title,
'info': {
'plot':"Nothing happens at all.",
'rating': decimal.Decimal(0)
}
}
)
如果以这种方式加载,它将处理嵌套的JSON结构,当您读取它时,您可以解析JSON并读取JSON中所需的属性
import boto3
import json
dynamodbclient=boto3.resource('dynamodb')
sample_table = dynamodbclient.Table('ec2metadata')
with open('/samplepath/spotec2interruptionevent.json', 'r') as myfile:
data=myfile.read()
# parse file
obj = json.loads(data)
#instance_id and cluster_id is the Key in dynamodb table
response=sample_table.put_item(
Item={
'instance_id': instanceId,
'cluster_id': clusterId,
'event':obj
}
)
import boto3
import json
dynamodbclient=boto3.resource('dynamodb')
sample_table = dynamodbclient.Table('ec2metadata')
with open('/samplepath/spotec2interruptionevent.json', 'r') as myfile:
data=myfile.read()
# parse file
obj = json.loads(data)
#instance_id and cluster_id is the Key in dynamodb table
response=sample_table.put_item(
Item={
'instance_id': instanceId,
'cluster_id': clusterId,
'event':obj
}
)