You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

插入数据库表时提示Sensor键缺失的错误排查请求

插入DynamoDB数据时提示缺少主键Sensor的问题

错误信息

One or more parameter values were invalid: Missing the key Sensor in the item

错误截图:
DynamoDB插入错误提示

待插入的objects.json内容

{    
    "Sensors":{
        "L": [
            { "M": {
                "Sensor": { "S": "Sensor1" },
                "SensorDescription": { "S": "This is a description of a Sensor"},
                "ImageFile": {"S": "/Sensors/image/Sensor1.txt"},
                "SampleRate": {"N": "1"},
                "Locations": {"S": "Orlando, FL"}
                }
            },
            { "M": {
                "Sensor": { "S": "Sensor2" },
                "SensorDescription": { "S": "This is a description of a Sensor"},
                "ImageFile": {"S": "/Sensors/image/Sensor2.txt"},
                "SampleRate": {"N": "2"},
                "Locations": {"S": "Annapolis, MD"}
                }
            },
            { "M": {
                "Sensor": { "S": "Sensor3" },
                "SensorDescription": { "S": "This is a description of a Sensor"},
                "ImageFile": {"S": "/Sensors/image/Sensor3.txt"},
                "SampleRate": {"N": "3"},
                "Locations": {"S": "Jacksonville, FL"}
                }
            },
            { "M": {
                "Sensor": { "S": "Sensor4" },
                "SensorDescription": { "S": "This is a description of a Sensor"},
                "ImageFile": {"S": "/Sensors/image/Sensor4.txt"},
                "SampleRate": {"N": "4"},
                "Locations": {"S": "Balitimore, MD"}
                }
            },
            { "M": {
                "Sensor": { "S": "Sensor5" },
                "SensorDescription": { "S": "This is a description of a Sensor"},
                "ImageFile": {"S": "/Sensors/image/Sensor5.txt"},
                "SampleRate": {"N": "5"},
                "Locations": {"S": "Washington DC"}
                }
            },
            { "M": {
                "Sensor": { "S": "Sensor6" },
                "SensorDescription": { "S": "This is a description of a Sensor"}
                }
            },
            { "M": {
                "Sensor": { "S": "Sensor7" },
                "SensorDescription": { "S": "This is a description of a Sensor" }
                }
            },
            { "M": {
                "Sensor": { "S": "Sensor8" },
                "SensorDescription": { "S": "This is a description of a Sensor"}
                }
            },
            { "M": {
                "Sensor": { "S": "Sensor9" },
                "SensorDescription": { "S": "This is a description of a Sensor"}
                }
            },
            { "M": {
                "Sensor": { "S": "Sensor10" },
                "SensorDescription": { "S": "This is a description of a Sensor"}
                }
            },
            { "M": {
                "Sensor": { "S": "Sensor11" },
                "SampleRate": {"N": "11"},
                "Locations": {"S": "New York, NY"}
                }
            },
            { "M": {
                "Sensor": { "S": "Sensor12" },
                "SampleRate": {"N": "12"},
                "Locations": {"S": "Buffalo, NY"}
                }
            },
            { "M": {
                "Sensor": { "S": "Sensor13" },
                "SampleRate": {"N": "13"},
                "Locations": {"S": "Chicago, IL"}
                }
            },
            { "M": {
                "Sensor": { "S": "Sensor14" },
                "SampleRate": {"N": "14"},
                "Locations": {"S": "Trenton, NJ"}
                }
            },
            { "M": {
                "Sensor": { "S": "Sensor15" },
                "SampleRate": {"N": "15"},
                "Locations": {"S": "Los Angeles, CA"}
                }
            },
            { "M": {
                "Sensor": { "S": "Sensor16" },
                "SampleRate": {"N": "16"},
                "SensorDescription": { "S": "This is a description of a Sensor"},
                "AltName": {"S": "Sensor-16"}
                }
            },
            { "M": {
                "Sensor": { "S": "Sensor17" },
                "SampleRate": {"N": "17"},
                "SensorDescription": { "S": "This is a description of a Sensor"},
                "Date": {"S": "2022-09-11 15:00:00.000"}
                }
            },
            { "M": {
                "Sensor": { "S": "Sensor18" },
                "SampleRate": {"N": "18"},
                "SensorDescription": { "S": "This is a description of a Sensor"},
                "Working": {"BOOL": false }
                }
            },
            { "M": {
                "Sensor": { "S": "Sensor19" },
                "SampleRate": {"N": "19"},
                "SensorDescription": { "S": "This is a description of a Sensor"},
                "color": {"S": "Green" }
                }
            },
            { "M": {
                "Sensor": { "S": "Sensor20" },
                "SampleRate": {"N": "20"},
                "SensorDescription": { "S": "This is a description of a Sensor"},
                "Senor_type": {"S": "Type 2" }
                }
            }
        ]
    }   
}

数据库表结构

DynamoDB表结构

无法定位问题,请求技术帮助。


问题原因与解决方法

核心问题

你当前的JSON结构是把所有Sensor数据嵌套在Sensors.L数组里作为单条记录插入,但你的表主键是Sensor,这意味着每条记录必须直接包含Sensor字段作为主键,而不是把所有Sensor打包成一个列表字段插入。

DynamoDB中每个顶级对象对应表中的一条记录,你现在的插入方式相当于试图插入一条没有Sensor主键的记录(Sensor字段在嵌套的数组元素里),所以触发了缺失主键的错误。

解决步骤

  1. 调整JSON结构:把Sensors.L数组里的每个M对象单独作为一条记录,格式示例:
{
    "Sensor": { "S": "Sensor1" },
    "SensorDescription": { "S": "This is a description of a Sensor"},
    "ImageFile": {"S": "/Sensors/image/Sensor1.txt"},
    "SampleRate": {"N": "1"},
    "Locations": {"S": "Orlando, FL"}
}
  1. 批量插入格式调整:如果使用DynamoDB的批量插入接口(如BatchWriteItem),需要将这些单独的记录整理为PutRequest结构,示例:
{
  "RequestItems": {
    "你的表名": [
      {
        "PutRequest": {
          "Item": {
            "Sensor": { "S": "Sensor1" },
            "SensorDescription": { "S": "This is a description of a Sensor"},
            "ImageFile": {"S": "/Sensors/image/Sensor1.txt"},
            "SampleRate": {"N": "1"},
            "Locations": {"S": "Orlando, FL"}
          }
        }
      },
      // 其余19条记录的PutRequest依次排列
    ]
  }
}
  1. 修正插入逻辑:确保插入代码是遍历数组中的每个Sensor对象,单独插入每条记录,而非直接插入整个Sensors父对象。

内容的提问来源于stack exchange,提问作者cozzymotto

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 04:15:42