2017-08-18 2 views
1

Je voudrais écrire le groupe de données suivant dans Dynamodb.
Il y a environ 100 données. Puisque les images ne sont pas forcément nécessaires, il y a un mélange avec et sans l'élément image_url.

Comment écrire des données JSON sur Dynamodb en ignorant les éléments vides dans boto3

(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" 
    ] 
}, 

Comme il est la phase de test d'écriture, DynamoDB à écrire à est préparé localhost: 8000 à l'aide du plug-in Serverless-DynamoDB local du cadre serverless, pas l'environnement de production.
Afin d'écrire les données JSON ci-dessus à ce Dynamodb, j'ai écrit le code suivant dans Boto 3 (AWS SDK pour 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, 
     } 
    ) 

Lorsque ce code est exécuté, l'erreur suivante se produit dans la partie de caractère nul.

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

Apparemment, il semble être causé par le caractère nul de JSON.
Si vous excluez l'image_url contenant le caractère nul de la cible d'écriture comme ci-dessous, l'écriture est terminée sans aucun problème.

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, 
     } 
    ) 

Depuis DynamoDB est NoSQL, il peut y avoir d'autres méthodes qui font bon usage des caractéristiques, mais la façon de corriger le code pour écrire les données ci-dessus en ignorant les caractères vides? Je voudrais dire "si image_url existe, écrivez-le si ce n'est pas le cas, ignorez-le."

Merci.

Répondre

0

J'ai résolu mon problème. Vous pouvez définir null comme suit.

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, 
     } 
    ) 

Afin de vérifier la situation des DynamoDB, utilisez le plugin hors du cadre serverless pour exécuter la passerelle API dans l'environnement local. Lorsque j'ai appelé l'API avec Postman, Null était correctement inséré dans la valeur.

{ 
    "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" 
    ] 
},