2017-09-08 51 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方法,我能夠解決我的問題。 –