gpt4 book ai didi

php - DynamoDb batchGetItem 和分区键和排序键

转载 作者:行者123 更新时间:2023-12-03 11:22:50 25 4
gpt4 key购买 nike

我尝试使用 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

If the table has a composite primary key (partition key and sort key), DynamoDB calculates the hash value of the partition key in the same way as described in Data Distribution: Partition Key—but it stores all of the items with the same partition key value physically close together, ordered by sort key value.



使用 Partition Key and Sort Key有什么好处在它旁边存储物理上靠近在一起的具有相同分区键值的所有项目?

根据官方文档:

A single operation can retrieve up to 16 MB of data, which can contain as many as 100 items. BatchGetItem will return a partial result if the response size limit is exceeded, the table's provisioned throughput is exceeded, or an internal processing failure occurs.



如果我需要超过 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 的问题答案:

Yes you can create an index without a range key. The range key is entirely optional. However, even if you have a range key defined it is optional to include it in your query. You can simply specify the hash key in your query to get all items with the hash key, which will be returned in an order based on the range key.



如果我在具有散列键和范围键的表上的查询中仅指定散列键,则会出现以下错误,如果我在没有范围键的表上仅在查询中指定散列键,则它可以工作。
请注意没有索引的表格。
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

最佳答案

你问了很多问题,所以我会试着把它分解。 (对不起,我无法用 php 代码片段回答这个问题)

I tried to use batchGetItem to return attributes of more then one item from a table but seems it works only with the combination of the partition key and range key, but what if I want to identify the requested items only by primary key ? is the only way is to create the table without the range key ?



BatchGetItem 与多个 GetItem 调用相同。本质上,使用每个 GetItem 调用检索零个或一个项目。
您为其提供要检索的项目的唯一键(主键)。如果您的表只有分区键,那么这就是您指定的全部,否则分区和范围键。
BatchGetItem 在对 DynamoDB 的一个请求中批量调用 GetItem。

如果您想查询给定分区键的多个项目,您需要查看 Query API .

What are the advantages using Partition Key and Sort Key beside it stores all of the items with the same partition key value physically close together ?



这是一个很难回答的问题,因为它在很大程度上取决于您的数据模型的唯一键。

想到的一些优点是:
1. 排序键使您能够对该属性的数据进行排序(按升序或降序)
2. 排序键有更多的比较操作(即:大于、小于、介于、开始于等)。见 docs

How to handle the request if I need more then 100 items ? just loop through all the items from the code and request each time 100 times or there is another way to achieve it via the AWS SDK DynamoDB?



如果您请求超过 100 个项目,BatchGetItem 将返回带有消息“BatchGetItem 调用请求的项目太多”的 ValidationException。您将需要循环遍历这些项目,一次 100 个以获取您需要的所有项目。请记住,还有 16MB 的大小限制,这意味着如果有任何项目未处理,它们将在“UnprocessedItems”下的响应中返回。

如果 DynamoDB 返回任何未处理的项目,您应该对这些项目重试批处理操作。但是,我们强烈建议您使用指数退避算法。如果您立即重试批处理操作,底层读取或写入请求仍可能因单个表的限制而失败。如果您使用指数退避延迟批处理操作,批处理中的单个请求更有可能成功。

documentation解释如何使用它。

关于php - DynamoDb batchGetItem 和分区键和排序键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46175521/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com