首页 文章

如何通过忽略boto3中的空元素将JSON数据写入Dynamodb

提问于
浏览
2

我想将以下数据组写入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"
  ]
},

由于它是写入测试阶段,因此要使用无服务器框架的serverless-dynamodb-local插件而不是 生产环境 环境在localhost:8000中编写要写入的Dynamodb .
为了将上述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,
        }
    )

执行此代码时,空字符部分中会发生以下错误 .

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

由于DynamoDB是NoSQL,可能还有其他方法可以很好地利用这些特性,但是如何更正代码来编写忽略空字符的上述数据呢?我想说"if image_url exists, write it if it does not, ignore it."

谢谢 .

1 回答

  • 2

    我解决了我的问题 . 您可以按如下方式设置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,
            }
        )
    

    要检查Dynamodb的情况,请使用无服务器框架的脱机插件在本地环境中运行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"
      ]
    },
    

相关问题