lambda 上的 DynamoDB 验证异常

Jer*_*her 5 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
Run Code Online (Sandbox Code Playgroud)

这是代码:

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
            }
        )
Run Code Online (Sandbox Code Playgroud)

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

Kan*_*yan 6

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

AttributeDefinitions=[
            {
                'AttributeName': 'pid',
                'AttributeType': 'S',
            }
        ],
KeySchema=[
                {
                    'AttributeName': 'pid',
                    'KeyType': 'HASH'
                }
]
Run Code Online (Sandbox Code Playgroud)

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

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

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 */
  ]
Run Code Online (Sandbox Code Playgroud)

  • 除了主键之外,您不需要定义属性定义。其余的发电机将接受原样。不需要模式定义。您可能无法添加值为 null、为空的属性。除此之外,它将采用任何类型。 (2认同)

Jos*_*tes 5

AWS 在文档中解释说:

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

他们只允许两种KeySchema:一种KeyType是 as HASH,另一种KeyType是 as RANGE

{
  "KeySchema": [
    {
      "AttributeName": "ForumName",
      "KeyType": "HASH"
    },
    {
      "AttributeName": "Subject",
      "KeyType": "RANGE"
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)