uhi*_*ama 5 python json amazon-web-services amazon-dynamodb boto3
我想将以下数据组写入 Dynamodb。
大约有100条数据。由于不一定需要图像,因此可以混合使用和不使用 image_url 元素。
(questionsList.json)
{
"q_id" : "001",
"q_body" : "Where is the capital of the United States?",
"q_answer" : "Washington, D.C.",
"image_url" : "/Washington.jpg",
"keywords" : [
"UnitedStates",
"Washington"
]
},
{
"q_id" : "002",
"q_body" : "Where is the capital city of the UK?",
"q_answer" : "London",
"image_url" : "",
"keywords" : [
"UK",
"London"
]
},
Run Code Online (Sandbox Code Playgroud)
由于是写测试阶段,要写的Dynamodb是使用serverless框架的serverless-dynamodb-local插件在localhost:8000准备的,不是生产环境。
为了将上面的 JSON 数据写入这个 Dynamodb,我在 Boto 3 (AWS SDK for Python) 中编写了以下代码。
from __future__ import print_function
import boto3
import codecs
import json
dynamodb = boto3.resource('dynamodb', region_name='us-east-1', endpoint_url="http://localhost:8000")
table = dynamodb.Table('questionListTable')
with open("questionList.json", "r", encoding='utf-8') as json_file:
items = json.load(json_file)
for item in items:
q_id = item['q_id']
q_body = item['q_body']
q_answer = item['q_answer']
image_url = item['image_url']
keywords = item['keywords']
print("Adding detail:", q_id, q_body)
table.put_item(
Item={
'q_id': q_id,
'q_body': q_body,
'q_answer': q_answer,
'image_url': image_url,
'keywords': keywords,
}
)
Run Code Online (Sandbox Code Playgroud)
执行此代码时,空字符部分出现以下错误。
botocore.exceptions.ClientError:调用 PutItem 操作时发生错误 (ValidationException):一个或多个参数值无效:一个 AttributeValue 可能不包含空字符串
显然它似乎是由 JSON 的空字符引起的。
如果将包含空字符的 image_url 从写入目标中排除,如下所示,写入完成没有任何问题。
from __future__ import print_function
import boto3
import codecs
import json
dynamodb = boto3.resource('dynamodb', region_name='us-east-1', endpoint_url="http://localhost:8000")
table = dynamodb.Table('questionListTable')
with open("questionList.json", "r", encoding='utf-8') as json_file:
items = json.load(json_file)
for item in items:
q_id = item['q_id']
q_body = item['q_body']
q_answer = item['q_answer']
#image_url = item['image_url']
keywords = item['keywords']
print("Adding detail:", q_id, q_body)
table.put_item(
Item={
'q_id': q_id,
'q_body': q_body,
'q_answer': q_answer,
#'image_url': image_url,
'keywords': keywords,
}
)
Run Code Online (Sandbox Code Playgroud)
由于DynamoDB是NoSQL,可能还有其他的方法可以很好的利用这个特性,但是如何正确的写出上面的数据忽略空字符的代码呢?我想说“如果 image_url 存在,则写它,如果它不存在,忽略它。”
谢谢你。
我解决了我的问题。您可以按如下方式设置 null。
from __future__ import print_function
import boto3
import codecs
import json
dynamodb = boto3.resource('dynamodb', region_name='ap-northeast-1', endpoint_url="http://localhost:8000")
table = dynamodb.Table('questionListTable')
with open("questionList.json", "r", encoding='utf-8_sig') as json_file:
items = json.load(json_file)
for item in items:
q_id = item['q_id']
q_body = item['q_body']
q_answer = item['q_answer']
image_url = item['image_url'] if item['image_url'] else None
keywords = item['keywords'] if item['keywords'] else None
print("Adding detail:", q_id, q_body)
table.put_item(
Item={
'q_id': q_id,
'q_body': q_body,
'q_answer': q_answer,
'image_url': image_url,
'keywords': keywords,
}
)
Run Code Online (Sandbox Code Playgroud)
为了检查Dynamodb的情况,使用serverless框架的离线插件在本地环境运行API网关。当我实际使用 Postman 调用 API 时,Null 正确插入到值中。
{
"q_id" : "001",
"q_body" : "Where is the capital of the United States?",
"q_answer" : "Washington, D.C.",
"image_url" : "/Washington.jpg",
"keywords" : [
"UnitedStates",
"Washington"
]
},
{
"q_id" : "002",
"q_body" : "Where is the capital city of the UK?",
"q_answer" : "London",
"image_url" : "null",
"keywords" : [
"UK",
"London"
]
},
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
8706 次 |
最近记录: |