我試圖使用batchGetItem
從表中返回多個項目的屬性,但似乎只適用於分區鍵和範圍鍵的組合,但如果我想識別請求的項目只能通過主鍵?唯一的辦法是創建沒有範圍鍵的表格?dyanmodb batchGetItem和分區鍵和排序鍵
// Adding items
$client->putItem(array(
'TableName' => $table,
'Item' => array(
'id' => array('S' => '2a49ab04b1534574e578a08b8f9d7441'),
'name' => array('S' => 'test1'),
'user_name' => array('S' => 'aaa.bbb')
)
));
// Adding items
$client->putItem(array(
'TableName' => $table,
'Item' => array(
'id' => array('S' => '4fd70b72cc21fab4f745a6073326234d'),
'name' => array('S' => 'test2'),
'user_name' => array('S' => 'aaaa.bbbb'),
'user_name1' => array('S' => 'aaaaa.bbbbb')
)
));
$client->batchGetItem(array(
"RequestItems" => array(
$table => array(
"Keys" => array(
// hash key
array(
"id" => array('S' => "2a49ab04b1534574e578a08b8f9d7441"),
// range key
"name" => array('S' => "test1"),
),
array(
// hash key
"id" => array('S' => "4fd70b72cc21fab4f745a6073326234d"),
// range key
"name" => array('S' => "test2"),
),
)
)
)
));
按照官方文檔:
http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/HowItWorks.Partitions.html
如果表中有一個複合主鍵(分區鍵和分類鍵), DynamoDB計算分區鍵的哈希值以與數據分配:分區密鑰中所述相同的方式,但它將所有具有相同分區密鑰值的項目的所有 存儲在一起,物理地關閉 在一起,順序按排序鍵值排序。
使用Partition Key and Sort Key
旁邊的優點是什麼使用相同的分區鍵值存儲物理上靠得很近的所有項目?
按照官方文檔:
單一操作可以檢索多達16個MB的數據,它可以包含 多達100個項目。 BatchGetItem將返回如果 超出響應的大小限制,超過該表的置備 吞吐量,或者發生內部處理失敗的局部結果。
如何處理請求,如果我需要更多的則100個項目?剛剛通過的所有項目從每一次的代碼,並要求100倍或有另一種方式通過AWS SDK DynamoDB來實現它循環?創建表的
例子:
$client->createTable(array(
'TableName' => $table,
'AttributeDefinitions' => array(
array(
'AttributeName' => 'id',
'AttributeType' => 'N'
),
array(
'AttributeName' => 'name',
'AttributeType' => 'S'
)
),
'KeySchema' => array(
array(
'AttributeName' => 'id',
'KeyType' => 'HASH'
),
array(
'AttributeName' => 'name',
'KeyType' => 'RANGE'
)
),
'ProvisionedThroughput' => array(
'ReadCapacityUnits' => 5,
'WriteCapacityUnits' => 5
)
));
感謝
更新 - 問題標記B答案:
是的,你可以創建一個沒有範圍的關鍵指標。範圍關鍵是 完全可選的。但是,即使您定義了範圍鍵,也可以使用 將其包含在查詢中。您可以簡單地在查詢中指定密鑰 以獲取所有具有散列密鑰的項目,這將根據範圍密鑰按順序返回 。
如果我指定與哈希鍵和範圍鍵,我得到下面的錯誤,一表我的查詢只哈希鍵如果我指定我的查詢表上沒有範圍的關鍵它只能哈希鍵。 請注意表沒有索引。
An uncaught Exception was encountered
Type: Aws\DynamoDb\Exception\DynamoDbException
Message: Error executing "BatchGetItem" on "https://dynamodb.eu-central-1.amazonaws.com"; AWS HTTP error: Client error: `POST https://dynamodb.eu-central-1.amazonaws.com` resulted in a `400 Bad Request` response:
{"__type":"com.amazon.coral.validate#ValidationException","message":"The provided key element does not match the schema" (truncated...)
ValidationException (client): The provided key element does not match the schema - {"__type":"com.amazon.coral.validate#ValidationException","message":"The provided key element does not match the schema"}
Filename: /var/app/vendor/aws/aws-sdk-php/src/WrappedHttpHandler.php
我對您的回答發表了評論,我更新了我的問題,請您檢查一下,謝謝。 – Berlin
@Berlin我認爲你需要發出一個'Query'而不是'BatchGetItem'請求。 –
與'查詢'工作,但我更喜歡使用'BatchGetItem'的性能。 – Berlin