php 如何在不指定主键的情况下从DynamoDB表中获取所有项?

cld4siwp  于 2023-06-04  发布在  PHP
关注(0)|答案(9)|浏览(469)

我有一个名为products的表,主键为Id。我想选择表格中的所有项目。这是我使用的代码:

$batch_get_response = $dynamodb->batch_get_item(array(
    'RequestItems' => array(

        'products' => array(
            'Keys' => array(
                array( // Key #1
                    'HashKeyElement'  => array( AmazonDynamoDB::TYPE_NUMBER => '1'),
                    'RangeKeyElement' => array( AmazonDynamoDB::TYPE_NUMBER => $current_time),
                ),
                array( // Key #2
                    'HashKeyElement'  => array( AmazonDynamoDB::TYPE_NUMBER => '2'),
                    'RangeKeyElement' => array( AmazonDynamoDB::TYPE_NUMBER => $current_time),
                ),
            )
        )
    )   
));

是否可以在不指定主键的情况下选择所有项目?我正在使用AWS SDK for PHP。

w6lpcovy

w6lpcovy1#

Amazon DynamoDB为此目的提供了Scan操作,该操作 * 通过执行表的全扫描 * 返回一个或多个项及其属性。请注意以下两个限制:

  • 根据表的大小,您可能需要使用分页来检索整个结果集:
    备注

如果扫描的项目总数超过1MB限制,则扫描停止,并将结果返回给用户,并提供LastEvaluatedKey,以便在后续操作中继续扫描。结果还包括超出限制的项目数。扫描可能导致没有表数据满足筛选条件。
结果集最终是一致的。

[...]此外,随着表的增长,扫描操作也会变慢。扫描操作会检查每个项是否有请求的值,可能会在一次操作中用完大型表的预配吞吐量。为了缩短响应时间,请以可以使用Query、Get或BatchGetItem API的方式设计表。或者,将应用程序设计为以最小化对表请求率影响的方式使用扫描操作。有关详细信息,请参阅Provisioned Throughput Guidelines in Amazon DynamoDB[强调我的]
您可以在Scanning Tables Using the AWS SDK for PHP Low-Level API for Amazon DynamoDB中找到有关此操作的更多详细信息和一些示例片段,其中最简单的示例说明了该操作:

$dynamodb = new AmazonDynamoDB();

$scan_response = $dynamodb->scan(array(
    'TableName' => 'ProductCatalog' 
));

foreach ($scan_response->body->Items as $item)
{
    echo "<p><strong>Item Number:</strong>"
         . (string) $item->Id->{AmazonDynamoDB::TYPE_NUMBER};
    echo "<br><strong>Item Name: </strong>"
         . (string) $item->Title->{AmazonDynamoDB::TYPE_STRING} ."</p>";
}
iaqfqrcu

iaqfqrcu2#

你可以使用boto3下载。在python中

import boto3
from boto3.dynamodb.conditions import Key, Attr

dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('Table')
response = table.scan()
items = response['Items']
while 'LastEvaluatedKey' in response:
    print(response['LastEvaluatedKey'])
    response = table.scan(ExclusiveStartKey=response['LastEvaluatedKey'])
    items.extend(response['Items'])
gc0ot86w

gc0ot86w3#

我想你正在使用PHP,但没有提到(编辑)。我通过搜索互联网发现了这个问题,因为我得到了解决方案,对于那些使用nodejs的人这里有一个简单的解决方案,使用扫描:

var dynamoClient = new AWS.DynamoDB.DocumentClient();
  var params = {
    TableName: config.dynamoClient.tableName, // give it your table name 
    Select: "ALL_ATTRIBUTES"
  };

  dynamoClient.scan(params, function(err, data) {
    if (err) {
       console.error("Unable to read item. Error JSON:", JSON.stringify(err, null, 2));
    } else {
       console.log("GetItem succeeded:", JSON.stringify(data, null, 2));
    }
  });

我假设相同的代码也可以*使用不同的AWS SDK转换为PHP

fykwrbwg

fykwrbwg4#

我使用以下查询从dynamodb中获取所有项。它工作正常。我在zend framework中创建了这些通用函数,并通过项目访问这些函数。

public function getQuerydata($tablename, $filterKey, $filterValue){
            return $this->getQuerydataWithOp($tablename, $filterKey, $filterValue, 'EQ');
        }

        public function getQuerydataWithOp($tablename, $filterKey, $filterValue, $compOperator){
        $result = $this->getClientdb()->query(array(
                'TableName'     => $tablename,
                'IndexName'     => $filterKey,
                'Select'        => 'ALL_ATTRIBUTES',
                'KeyConditions' => array(
                    $filterKey => array(
                        'AttributeValueList' => array(
                            array('S' => $filterValue)
                        ),
                'ComparisonOperator' => $compOperator
            )
            )
        ));
            return $result['Items'];
        }

       //Below i Access these functions and get data.
       $accountsimg = $this->getQuerydataWithPrimary('accounts', 'accountID',$msgdata[0]['accountID']['S']);
zqry0prt

zqry0prt5#

通过指定AWS服务的区域列出DynamoDB表中所有项目的简单代码。

import boto3

dynamodb = boto3.resource('dynamodb', region_name='ap-south-1')
table = dynamodb.Table('puppy_store')
response = table.scan()
items = response['Items']

# Prints All the Items at once
print(items)

# Prints Items line by line
for i, j in enumerate(items):
    print(f"Num: {i} --> {j}")
balp4ylt

balp4ylt6#

这里有一个Java的例子。在withAttributesToGet中,您可以指定要读取的确切内容。在运行之前,您必须将凭据文件放置到.aws文件夹中。

public static final String TABLE_NAME = "table_name";

    public static final AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard()
            .withRegion(Regions.CA_CENTRAL_1)
            .build();

    public static void main(String[] args) throws IOException, InterruptedException {
        downloadAllRecords();
    }

    public static void downloadAllRecords() throws InterruptedException, IOException {
        final Object[] FILE_HEADER = {"key", "some_param"};
        CSVFormat csvFormat = CSVFormat.DEFAULT.withRecordSeparator("\n");
        CSVPrinter csvPrinter = new CSVPrinter(new FileWriter(TABLE_NAME + ".csv"), csvFormat);
        csvPrinter.printRecord(FILE_HEADER);

        ScanRequest scanRequest = new ScanRequest()
                .withTableName(TABLE_NAME)
                .withConsistentRead(false)
                .withLimit(100)
                .withAttributesToGet("key", "some_param");
        int counter = 0;
        do {
            ScanResult result = client.scan(scanRequest);
            Map<String, AttributeValue> lastEvaluatedKey = result.getLastEvaluatedKey();
            for (Map<String, AttributeValue> item : result.getItems()) {
                AttributeValue keyIdAttribute = item.getOrDefault("key", new AttributeValue());
                AttributeValue createdDateAttribute = item.getOrDefault("some_param", new AttributeValue());

                    counter++;
                    List record = new ArrayList();
                    record.add(keyIdAttribute.getS());
                    record.add(createdDateAttribute.getS());
                    csvPrinter.printRecord(record);
                    TimeUnit.MILLISECONDS.sleep(50);

            }
            scanRequest.setExclusiveStartKey(lastEvaluatedKey);
        } while (scanRequest.getExclusiveStartKey() != null);
        csvPrinter.flush();
        csvPrinter.close();
        System.out.println("CSV file generated successfully.");
    }

还指定必要的依赖项。

<dependencies>
   <dependency>
       <groupId>com.sparkjava</groupId>
       <artifactId>spark-core</artifactId>
       <version>2.5.4</version>
   </dependency>
   <!-- https://mvnrepository.com/artifact/com.sparkjava/spark-template-velocity -->
   <dependency>
       <groupId>com.sparkjava</groupId>
       <artifactId>spark-template-velocity</artifactId>
       <version>2.7.1</version>
   </dependency>
   <!-- https://mvnrepository.com/artifact/com.amazonaws/aws-java-sdk-logs -->
   <dependency>
       <groupId>com.amazonaws</groupId>
       <artifactId>aws-java-sdk-logs</artifactId>
       <version>1.12.132</version>
   </dependency>
   <dependency>
       <groupId>com.google.code.gson</groupId>
       <artifactId>gson</artifactId>
       <version>2.8.9</version>
   </dependency>
   <dependency>
       <groupId>com.google.guava</groupId>
       <artifactId>guava</artifactId>
       <version>31.0.1-jre</version>
   </dependency>
   <dependency>
       <groupId>org.apache.commons</groupId>
       <artifactId>commons-collections4</artifactId>
       <version>4.4</version>
   </dependency>
   <dependency>
       <groupId>com.opencsv</groupId>
       <artifactId>opencsv</artifactId>
       <version>5.3</version>
   </dependency>
   <!-- https://mvnrepository.com/artifact/com.amazonaws/aws-java-sdk-dynamodb -->
   <dependency>
       <groupId>com.amazonaws</groupId>
       <artifactId>aws-java-sdk-dynamodb</artifactId>
       <version>1.12.161</version>
   </dependency>
   <dependency>
       <groupId>org.apache.commons</groupId>
       <artifactId>commons-csv</artifactId>
       <version>1.1</version>
   </dependency>

</dependencies>

凭据文件示例

[default]
aws_access_key_id = AAAAAAA
aws_secret_access_key = AAAAAAAA
aws_session_token = AAAAAAA
w46czmvw

w46czmvw7#

我没有在下面的代码中指定pk:

client = boto3.client('dynamodb')
table = 'table_name'
response = client.scan(
    TableName=table,
    AttributesToGet=['second_field_in_order', 'first_field_in_order']
)
50few1ms

50few1ms8#

不使用主键和不运行扫描来获取所有行的方法是在表上定义/添加GSI(全局二级索引),并使用可以指定为GSI键的附加属性。在每一行中将此值设置为相同的值(除非您希望将其用于多种用途)
然后对GSI执行查询,指定添加的属性的值。
您可能必须对返回值进行分页。
使用GSI将增加表的大小,并且可能增加成本。

kuarbcqp

kuarbcqp9#

此C#代码用于使用BatchGet或CreateBatchGet从dynamodb表中获取所有项

string tablename = "AnyTableName"; //table whose data you want to fetch

        var BatchRead = ABCContext.Context.CreateBatchGet<ABCTable>(  

            new DynamoDBOperationConfig
            {
                OverrideTableName = tablename; 
            });

        foreach(string Id in IdList) // in case you are taking string from input
        {
            Guid objGuid = Guid.Parse(Id); //parsing string to guid
            BatchRead.AddKey(objGuid);
        }

        await BatchRead.ExecuteAsync();
        var result = BatchRead.Results;

// ABCTable是用于在dynamodb中创建的表模型&您想要获取的数据

相关问题