python - 预期关键数据类型不匹配 : S actual: L Dynamodb insert error with boto3

标签 python json amazon-web-services amazon-dynamodb boto3

我有以下脚本用于将 json 文件插入 DynamoDb 表:

import os
import boto3
import json

region = '<>'
image = 'ami-<>'
ubuntu_image = 'ami-<>'
keyname = '<>'
AWS_ACCESS_KEY_ID = '<>'
AWS_SECRET_ACCESS_KEY = '<>'


def dynamo(path):
    session = boto3.Session(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY)
    dynamo = session.resource('dynamodb')
    table = dynamo.Table('<>')

    for filename in os.listdir(path):
        with open(path + '/' + filename) as data_file:
            data = json.loads(data_file.read())
            print data
            table.put_item(
                           Item={
                                'filename': filename,
                                'data': data,
                            }
                        )      

        print filename



print dynamo('<>')

尝试这样做时出现以下错误:

botocore.exceptions.ClientError: An error occurred (ValidationException) when calling the PutItem operation: One or more parameter values were invalid: Type mismatch for key data expected: S actual: L

其中一个 json 文件是:

[{"text": "Vasovagal syncope", "type": "DX_LSTM", "end": 103, "assertion": 1, "start": 86}, {"text": "Traumatic arthritis", "type": "DX_LSTM", "end": 145, "assertion": 1, "start": 126}, {"text": "right knee", "type": "DX_LSTM", "end": 157, "assertion": 1, "start": 147}, {"text": "Hypertension", "type": "DX_LSTM", "end": 174, "assertion": 1, "start": 162}, {"text": "urinary tract infection", "type": "DX_LSTM", "end": 223, "assertion": 1, "start": 200}, {"text": "renal carcinoma", "type": "DX_LSTM", "end": 254, "assertion": 1, "start": 239}, {"text": "obstructive pulmonary disease", "type": "DX_LSTM", "end": 315, "assertion": 1, "start": 286}, {"text": "stroke", "type": "DX_LSTM", "end": 442, "assertion": 1, "start": 436}, {"text": "hypertension", "type": "DX_LSTM", "end": 456, "assertion": 1, "start": 444}, {"text": "COPD", "type": "DX_LSTM", "end": 462, "assertion": 1, "start": 458}, {"text": "renal carcinoma", "type": "DX_LSTM", "end": 487, "assertion": 1, "start": 472}, {"text": "syncope", "type": "DX_LSTM", "end": 533, "assertion": 1, "start": 526}, {"text": "fell to her knees", "type": "DX_LSTM", "end": 584, "assertion": 1, "start": 567}, {"text": "hit her head on the ground, near", "type": "DX_LSTM", "end": 625, "assertion": 1, "start": 593}, {"text": "loss of consciousness", "type": "DX_LSTM", "end": 725, "assertion": 0, "start": 704}, {"text": "falls", "type": "DX_LSTM", "end": 804, "assertion": 1, "start": 799}, {"text": "hip fracture", "type": "DX_LSTM", "end": 845, "assertion": 1, "start": 833}, {"text": "bruising around the left eye", "type": "DX_LSTM", "end": 967, "assertion": 1, "start": 939}, {"text": "decreased mobility of", "type": "DX_LSTM", "end": 1085, "assertion": 1, "start": 1064}, {"text": "left", "type": "DX_LSTM", "end": 1094, "assertion": 1, "start": 1090}, {"text": "syncope", "type": "DX_LSTM", "end": 1175, "assertion": 0, "start": 1168}, {"text": "stroke", "type": "DX_LSTM", "end": 1195, "assertion": 0, "start": 1189}, {"text": "fractures", "type": "DX_LSTM", "end": 1348, "assertion": 0, "start": 1339}, {"text": "left humeral head", "type": "DX_LSTM", "end": 1405, "assertion": 0, "start": 1388}, {"text": "neck fracture", "type": "DX_LSTM", "end": 1423, "assertion": 0, "start": 1410}, {"text": "baseline anterior dislocation", "type": "DX_LSTM", "end": 1458, "assertion": 1, "start": 1429}, {"text": "changes", "type": "DX_LSTM", "end": 1500, "assertion": 0, "start": 1493}, {"text": "left periorbital soft tissue swelling", "type": "DX_LSTM", "end": 1539, "assertion": 0, "start": 1502}, {"text": "soft tissue", "type": "DX_BLME", "end": 1530, "assertion": 0, "start": 1519}, {"text": "facial bone fracture", "type": "DX_LSTM", "end": 1600, "assertion": 0, "start": 1580}, {"text": "ventricular function", "type": "DX_LSTM", "end": 1656, "assertion": 1, "start": 1636}, {"start": 1774, "end": 1782, "text": "syncopal", "type": "DX_LSTM", "assertion": 0}, {"text": "orthostatic", "type": "DX_LSTM", "end": 1865, "assertion": 1, "start": 1854}, {"text": "walk", "type": "DX_LSTM", "end": 2021, "assertion": 1, "start": 2017}, {"text": "traumatic injury of her knee", "type": "DX_LSTM", "end": 2072, "assertion": 1, "start": 2044}, {"text": "injury", "type": "DX_Exact", "end": 2060, "assertion": 1, "start": 2054}, {"text": "pain", "type": "DX_LSTM", "end": 2098, "assertion": 1, "start": 2094}, {"text": "swelling", "type": "DX_LSTM", "end": 2111, "assertion": 1, "start": 2103}, {"text": "fractures", "type": "DX_LSTM", "end": 2154, "assertion": 0, "start": 2145}, {"text": "frail", "type": "DX_LSTM", "end": 2175, "assertion": 0, "start": 2170}]

最佳答案

首先,您似乎正在尝试将 List 对象持久化为 DynamoDB 的 String 数据类型。

第二,字符串属性data - 我认为它被定义为键属性。 键属性不能是List数据类型。因此,您不能将键属性更改为List。

我不确定您想要实现什么目标。但是,我猜您希望将文件中存在的整个 JSON 存储在其中一个属性上。

如果您尝试将数据存储在某些非键属性中,上面的代码应该可以工作。

输出将是一个 List 属性,其中所有对象(即单个事件)都存储为 Map。

参见下面的示例:-

enter image description here

关于python - 预期关键数据类型不匹配 : S actual: L Dynamodb insert error with boto3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43062461/

相关文章:

python - 使用 pandas 根据日期范围返回多列

python - 使用 Python 将 Unicode 字符串转换为十六进制转义序列

json - 使用 Grails Spring Security Plugin Core 和 REST 从用户响应中省略密码字段

json - 在 Azure 数据工厂中发送 JSON 文件

amazon-web-services - 如何在 CloudFormation 模板中获取 Glue 开发端点 URL?

amazon-web-services - LaunchConfiguration 和 cfn-hup 可以一起玩吗?

python - 通过 Tor 进行多线程 Python 请求

python - python-C++扩展能否获取C++对象并调用其成员函数?

Javascript - 使用映射函数而不是嵌套 for 循环对 JSON 数组进行非规范化

bash - AWS CloudFormation 模板不接受 bash 脚本