预期关键数据的类型不匹配:S actual:L Dynamodb插入错误与boto3

时间:2017-03-28 06:47:01

标签: 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}]

2 个答案:

答案 0 :(得分:1)

首先,您似乎试图将List对象持久保存到String数据类型的DynamoDB中。

其次,String属性data - 我认为它被定义为Key Attribute。 关键属性不能是列表数据类型。因此,您无法将关键属性更改为列表。

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

如果您尝试将数据存储在某些非关键属性中,则上述代码应该有效。

输出将是List属性,所有对象(即单个出现)都存储为Map。

请参阅以下示例: -

enter image description here

答案 1 :(得分:0)

值得一提的是,在我的例子中,我有一个重复的属性 fruitID

当我构建要插入 DynamoDB 的对象时,我使用 fruitID

创建了一个模板
const template = {
  fruitID: ulib()
}

// Where fruitData is an incoming payload
const newFruit = Object.assign(template, fruitData)

传入的负载还包含意外的 fruitID 属性。

传入的有效载荷 fruitData.fruitID 道具实际上是 L 而不是 S,因此出现了上述问题。

即使我这里的代码是 JS,我的管道也使用 boto3。