我想將以下數據組寫入Dynamodb。
約有100個數據。由於圖像不一定是必需的,因此有和沒有image_url元素的混合。
如何通過忽略boto3中的空元素將JSON數據寫入Dynamodb
(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"
]
},
既然是寫入測試階段,Dynamodb寫入在本地主機制備:使用無服務器架構的無服務器-dynamodb本地插件8000,而不是生產環境。
爲了將上述JSON數據寫入此Dynamodb,我在Boto 3(適用於Python的AWS開發工具包)中編寫了以下代碼。
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,
}
)
當執行該代碼,在空字符部分時發生錯誤。
botocore.exceptions.ClientError: An error occurred (ValidationException) when calling the PutItem operation: One or more parameter values were invalid: An AttributeValue may not contain an empty string
顯然它似乎是由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,
}
)
由於DynamoDB是NoSQL的,可能有其他的方法是充分利用的特點,但如何糾正代碼編寫以上數據忽略空字符?我想說「如果image_url存在,如果不存在,請將其忽略,然後忽略它。」
謝謝。