【问题标题】:DynamoDB validation exception on lambdaLambda 上的 DynamoDB 验证异常
【发布时间】:2020-01-18 04:50:52
【问题描述】:

调用我的 lambda 技能时出现以下错误

ClientError: An error occurred (ValidationException) 
when calling the CreateTable operation: 1 validation error detected: 
Value '[com.amazonaws.dynamodb.v20120810.KeySchemaElement@2273ace6, 
com.amazonaws.dynamodb.v20120810.KeySchemaElement@4d13ab9, 
com.amazonaws.dynamodb.v20120810.KeySchemaElement@115e22b2]' at 
'keySchema' failed to satisfy constraint: Member must have length less than or equal to 2

代码如下:

def write_values_to_db(ddid, token, intent):
    pid = ...
    dynamodb_client = boto3.client('dynamodb')
    try:
        response = dynamodb_client.create_table(
            AttributeDefinitions=[
                {
                    'AttributeName': 'pid',
                    'AttributeType': 'S',
                },
                {
                    'AttributeName': 'ddid',
                    'AttributeType': 'S',
                },
                {
                    'AttributeName': 'token',
                    'AttributeType': 'S',
                },
            ],
            KeySchema=[
                {
                    'AttributeName': 'pid',
                    'KeyType': 'HASH',
                },
                {
                    'AttributeName': 'ddid',
                    'KeyType': 'RANGE',
                },
                {
                    'AttributeName': 'token',
                    'KeyType': 'RANGE',
                },
            ],
            ProvisionedThroughput={
                'ReadCapacityUnits': 5,
                'WriteCapacityUnits': 5,
            },
            TableName='Values',
        )
    except dynamodb_client.exceptions.ResourceInUseException:
        dynamodb_client.put_item(
            TableName='Values',
            Item={
                'pid': pid,
                'ddid': ddid,
                'token': token
            }
        )

根据我的仪表板,错误出现在TableName='Values' 行。我正在关注一个教程,只更改了某些内容,所以我不明白为什么这不起作用。我无法在本地环境中进行测试,因为我有区域/凭据问题。

【问题讨论】:

  • DynamoDB 需要一个哈希键,并且主键中不超过一个范围键,不是吗?

标签: lambda amazon-dynamodb boto3


【解决方案1】:

您代码中的 KeySchema 应如下所示,

AttributeDefinitions=[
            {
                'AttributeName': 'pid',
                'AttributeType': 'S',
            }
        ],
KeySchema=[
                {
                    'AttributeName': 'pid',
                    'KeyType': 'HASH'
                }
]

您只能拥有一个 Hash Key 和一个 Range Key Max。

如果你想要额外的索引,你可以用二级索引来创建它们。

http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/LSI.html

以下是全局二级索引的语法。

参考:http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB.html

GlobalSecondaryIndexes: [
    {
      IndexName: 'STRING_VALUE', /* required */
      KeySchema: [ /* required */
        {
          AttributeName: 'STRING_VALUE', /* required */
          KeyType: HASH | RANGE /* required */
        },
        /* more items */
      ],
      Projection: { /* required */
        NonKeyAttributes: [
          'STRING_VALUE',
          /* more items */
        ],
        ProjectionType: ALL | KEYS_ONLY | INCLUDE
      },
      ProvisionedThroughput: { /* required */
        ReadCapacityUnits: 0, /* required */
        WriteCapacityUnits: 0 /* required */
      }
    },
    /* more items */
  ]

【讨论】:

  • 我不确定是否需要额外的索引,我只想要额外的属性。我希望pid 成为主键,ddid and consent_token 成为属性
  • 让我修改答案。你可以只有主键没有范围键。
  • 是的,我试过了,但这会导致 KeySchema 中的属性数量与 AttributeSchema 中的数量不匹配。我能够通过在console.aws.amazon.com/dynamodb 中创建表并使用 put_item 方法来解决我的问题
  • 除主键外,不需要定义属性定义。其余的发电机将按原样接受。不需要模式定义。您可能无法添加值为 null 的属性,它们的值为空。除此之外,它将采用任何类型。
【解决方案2】:

AWS 在documentation:

中说明了这一点

对于复合主键(分区键和排序键),您必须 按以下顺序恰好提供两个元素:第一个元素必须 具有 HASH 的 KeyType,并且第二个元素的 KeyType 必须为 范围。

它们只允许两个KeySchema:一个KeyType 作为HASH,另一个KeyType 作为RANGE

...
"KeySchema": [
    {
        "AttributeName": "ForumName",
        "KeyType": "HASH"
    },
    {
        "AttributeName": "Subject",
        "KeyType": "RANGE"
    }
]
... 

【讨论】:

    猜你喜欢
    • 2020-12-05
    • 2018-11-30
    • 2016-06-17
    • 1970-01-01
    • 2018-01-17
    • 1970-01-01
    • 1970-01-01
    • 2022-01-09
    • 1970-01-01
    相关资源
    最近更新 更多