我是AWS DynamoDB和nosql的新手,我在創建表時遇到問題。使用Global Secondary Index創建DynamoDB表
我試圖創建一個名爲User
具有以下屬性表:
- 用戶ID(HASH)
- OSTYPE(RANGE)
- MSISDN
- IMSI
- 的DeviceID
我需要查詢表格不是LY由UserId
,而且還由以下字段:
- MSISDN
- IMSI
- 的DeviceID
我的邏輯如下:由UserId
- 查詢表領域。
- 如果查詢返回任何結果創建一個新的,但檢查沒有其他用戶使用相同的
MSISDN
申請或相同IMSI
場或相同DeviceID
領域。
在閱讀manual about LSI/GSI之後,我有困難低估如何創建表和定義這些索引。
這是我我特林執行使用PHP + AWS SDK到創世表的代碼:
$client->createTable(array(
'TableName' => 'User',
'AttributeDefinitions' => array(
array('AttributeName' => 'UserId', 'AttributeType' => 'S'),
array('AttributeName' => 'OSType', 'AttributeType' => 'S'),
array('AttributeName' => 'MSISDN', 'AttributeType' => 'S'),
array('AttributeName' => 'IMSI', 'AttributeType' => 'S'),
array('AttributeName' => 'DeviceID', 'AttributeType' => 'S'),
),
'KeySchema' => array(
array('AttributeName' => 'UserId', 'KeyType' => 'HASH'),
array('AttributeName' => 'OSType', 'KeyType' => 'RANGE')
),
'GlobalSecondaryIndexes' => array(
array(
'IndexName' => 'IMSIIndex',
'KeySchema' => array(
array('AttributeName' => 'IMSI', 'KeyType' => 'HASH')
),
'Projection' => array(
'ProjectionType' => 'KEYS_ONLY',
),
'ProvisionedThroughput' => array(
'ReadCapacityUnits' => 10,
'WriteCapacityUnits' => 10
)
),
array(
'IndexName' => 'MSISDNIndex',
'KeySchema' => array(
array('AttributeName' => 'MSISDN', 'KeyType' => 'HASH')
),
'Projection' => array(
'ProjectionType' => 'KEYS_ONLY',
),
'ProvisionedThroughput' => array(
'ReadCapacityUnits' => 10,
'WriteCapacityUnits' => 10
)
),
array(
'IndexName' => 'DeviceIDIndex',
'KeySchema' => array(
array('AttributeName' => 'DeviceID', 'KeyType' => 'HASH')
),
'Projection' => array(
'ProjectionType' => 'KEYS_ONLY',
),
'ProvisionedThroughput' => array(
'ReadCapacityUnits' => 10,
'WriteCapacityUnits' => 10
)
),
),
'ProvisionedThroughput' => array(
'ReadCapacityUnits' => 50,
'WriteCapacityUnits' => 50
)
));
我得到這個錯誤:
PHP Fatal error: Uncaught Aws\DynamoDb\Exception\ValidationException: AWS Error Code: ValidationException, Status Code: 400, AWS Request ID: 70LGIARTTQF90S8P0HVRUKSJ27VV4KQNSO5AEMVJF66Q9ASUAAJG, AWS Error Type: client, AWS Error Message: One or more parameter values were invalid: Number of attributes in KeySchema does not exactly match number of attributes defined in AttributeDefinitions, User-Agent: aws-sdk-php2/2.4.11 Guzzle/3.7.4 curl/7.29.0 PHP/5.4.14
請幫助我瞭解什麼我做錯了嗎?我想創建GSI的表,但我只是無法理解次級指數在DynamoDB本質:(
我還沒有使用PHP SDK,但是如果你可以使用AWS控制檯創建表並設置GSI只是爲了開始 –
它看起來就像PHP代碼是OK和S一樣DK正在將數據正確地發送到服務。 'Aws \ DynamoDb \ Exception \ ValidationException'指示服務拒絕您的配置。來自該服務的錯誤信息有點神祕,但我認爲您在GSI中設置的密鑰不正確。您可能還需要一個範圍鍵,因爲您的主表有一個散列和範圍鍵。嘗試詢問[Amazon DynamoDB論壇](https://forums.aws。amazon.com/forum.jspa?forumID=131) –
我遇到了同樣的問題,我通過從AttributeDefinitions中刪除非關鍵屬性來解決問題,並且您是否在查詢DynamoDB Local? –