lambda上的DynamoDB验证异常

时间:2017-09-08 22:19:36

标签: lambda amazon-dynamodb boto3

调用我的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'行。我正在按照教程进行操作,只更改了某些内容,所以我不明白为什么这样做不起作用。我无法测试本地环境,因为我有区域/凭据问题。

2 个答案:

答案 0 :(得分:2)

代码中的KeySchema应如下所示,

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

您只能有一个哈希键和一个范围键最大。

如果您想要其他索引,可以使用辅助索引创建它们。

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 */
  ]

答案 1 :(得分:2)

AWS在documentation中做了以下说明:

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

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

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