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

使用DynamoDBMapper.save插入数据时遭遇主键不匹配Schema错误的问题求助

DynamoDBMapper.save插入数据时遭遇主键不匹配Schema错误的问题求助

我现在碰到一个百思不得其解的问题:我有一张仅包含哈希键(id)的DynamoDB表,没有排序键,但配置了一个全局二级索引。当我用DynamoDBMapper.save()方法插入数据时,明明主键和表结构完全匹配,却一直收到如下错误:

The provided key element does not match the schema (Service: AmazonDynamoDBv2; Status Code: 400; Error Code: ValidationException

先给大家贴一下我的表结构(通过describe-table返回的完整结果):

{
  "Table": {
    "AttributeDefinitions": [
      {
        "AttributeName": "id",
        "AttributeType": "S"
      },
      {
        "AttributeName": "myfield",
        "AttributeType": "S"
      },
      {
        "AttributeName": "title",
        "AttributeType": "S"
      }
    ],
    "TableName": "myTable",
    "KeySchema": [
      {
        "AttributeName": "id",
        "KeyType": "HASH"
      }
    ],
    "TableStatus": "ACTIVE",
    "CreationDateTime": "2022-11-15T14:44:24.068000+01:00",
    "ProvisionedThroughput": {
      "NumberOfDecreasesToday": 0,
      "ReadCapacityUnits": 0,
      "WriteCapacityUnits": 0
    },
    "TableSizeBytes": 8413,
    "GlobalSecondaryIndexes": [
      {
        "IndexName": "my-index",
        "KeySchema": [
          {
            "AttributeName": "title",
            "KeyType": "HASH"
          },
          {
            "AttributeName": "myfield",
            "KeyType": "RANGE"
          }
        ],
        "Projection": {
          "ProjectionType": "KEYS_ONLY"
        },
        "IndexStatus": "ACTIVE",
        "ProvisionedThroughput": {
          "NumberOfDecreasesToday": 0,
          "ReadCapacityUnits": 0,
          "WriteCapacityUnits": 0
        }
      }
    ]
  }
}

对应的Java数据实体类是这样定义的:

public class DynamoDbData {

    @DynamoDBHashKey(attributeName = "id")
    private String id;

    @DynamoDBIndexRangeKey(globalSecondaryIndexName = "my-index", attributeName = "myfield")
    private String myField;

    @DynamoDBAttribute(attributeName = "title")
    @DynamoDBIndexHashKey(globalSecondaryIndexName = "my-index", attributeName = "title")
    private String title;
}

我调用保存方法的代码很简单:

dynamoDBMapper.save(dynamoDbData);

另外我的DynamoDBMapper Bean配置如下:

@Bean
DynamoDBMapper dynamoDbMapper(AmazonDynamoDB amazonDynamoDB) {
    DynamoDBMapperConfig config = DynamoDBMapperConfig.builder()
            .withPaginationLoadingStrategy(DynamoDBMapperConfig.PaginationLoadingStrategy.EAGER_LOADING)
            .withConsistentReads(DynamoDBMapperConfig.ConsistentReads.CONSISTENT)
            .withTableNameOverride(DynamoDBMapperConfig.TableNameOverride.withTableNamePrefix(properties.getTablePrefix()))
            .build();
    return new DynamoDBMapper(amazonDynamoDB, config);
}

更奇怪的是,我用DynamoDBLocal做本地测试的时候,完全能正常保存数据,但到了线上环境就报这个主键不匹配的错误。一般这个错误都是因为主键的字段名或者类型和表结构不一致,但我的表只有哈希键id,要保存的对象里也有对应的id属性,名字和类型都完全对得上啊。

我想请教一下,到底哪里出问题了?为什么会触发这个错误?是不是我需要把表主键和索引的映射分开,用不同的实体类来处理?

备注:内容来源于stack exchange,提问作者user1474111

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.23 07:48:06