创建 dynamo 数据库时出错 table 未在属性定义中指定哈希键

Error creating dynamo db table Hash Key not specified in Attribute Definitions

我在使用适用于 Java 2.0 的 AWS SDK 创建 dynamo 数据库 table 时遇到错误。已指定散列键,但仍然出现错误

    @Bean
  ApplicationRunner applicationRunner(DynamoDbClient dynamoDbClient) {
    return args -> {
      dynamoDbClient.listTables().tableNames().forEach(System.out::println);

  CreateTableRequest createTableRequest =
      CreateTableRequest.builder()
          .keySchema(
              KeySchemaElement.builder().keyType(KeyType.HASH).attributeName("id").build())
          .attributeDefinitions(
              AttributeDefinition.builder()
                  .attributeName("campaign_name")
                  .attributeType(ScalarAttributeType.S)
                  .build())
          .provisionedThroughput(
              ProvisionedThroughput.builder()
                  .readCapacityUnits(5L)
                  .writeCapacityUnits(5L)
                  .build())
          .tableName("campaign")
          .build();
  try {
    dynamoDbClient.createTable(createTableRequest);
    dynamoDbClient.listTables().tableNames().forEach(System.out::println);
  } catch (DynamoDbException e) {
    System.err.println(e.getMessage());
  }
};

}

错误:

Hash Key not specified in Attribute Definitions. Type unknown. (Service: DynamoDb, Status Code: 400, Request ID: bed4b8c3-f312-4a9d-8d7b-a2184dacce54)

我正在尝试创建一个 table 具有键和非键列

@Bean
ApplicationRunner applicationRunner(DynamoDbClient dynamoDbClient) {
    return args -> {
      dynamoDbClient.listTables().tableNames().forEach(System.out::println);

      CreateTableRequest createTableRequest =
          CreateTableRequest.builder()
              .attributeDefinitions(
                  AttributeDefinition.builder()
                      .attributeName("id")
                      .attributeType(ScalarAttributeType.S)
                      .build(),
                  AttributeDefinition.builder()
                      .attributeName("name")
                      .attributeType(ScalarAttributeType.S)
                      .build())
              .keySchema(
                  KeySchemaElement.builder()
                      .keyType(KeyType.HASH)
                      .attributeName("campaign_id")
                      .build())
              .provisionedThroughput(
                  ProvisionedThroughput.builder()
                      .readCapacityUnits(5L)
                      .writeCapacityUnits(5L)
                      .build())
              .tableName("campaign")
              .build();
      try {
        dynamoDbClient.createTable(createTableRequest);
        dynamoDbClient.listTables().tableNames().forEach(System.out::println);
      } catch (DynamoDbException e) {
        System.err.println(e.getMessage());
      }
    };
  }

错误 - 键架构中的属性数必须与属性定义中定义的属性数相匹配。

我认为您还需要为 KeySchemeElement 添加一个 AttributeDefinition

尝试将以下内容添加到您的 attributeDefinitions 调用中

AttributeDefinition.builder()
              .attributeName("campaign_id")
              .attributeType(ScalarAttributeType.S)
              .build()

我认为 campaign_id 是 HASH 键,这是需要在 AttributeDefinitions 中的键。

              AttributeDefinition.builder()
                  .attributeName("campaign_id")
                  .attributeType(ScalarAttributeType.S)
                  .build(),