2017-09-08 53 views
0

叫我拉姆达技能DynamoDB验证异常的拉姆达

ClientError: An error occurred (ValidationException) 
when calling the CreateTable operation: 1 validation error detected: 
Value '[[email protected], 
[email protected], 
[email protected]]' 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'线。我正在跟着一个教程,只改变了某些东西,所以我不明白为什么这不起作用。我无法在本地环境中测试,因为我有区域/凭证问题。

+0

DynamoDB需要一个散列密钥,并在主键不超过一个范围键,不是吗? –

回答

0

的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 */ 
    ] 
+0

我不确定是否需要额外的索引,我只想要其他属性。我希望'pid'是主键,'ddid和consent_token'是属性 –

+0

让我修改答案。只有主键没有范围键。 – Kannaiyan

+0

是的,我尝试过,但是会导致KeySchema中的属性数量与AttributeSchema中的数量不匹配。通过在https://console.aws.amazon.com/dynamodb中创建表并使用put_item方法,我能够解决我的问题。 –