如何从 DynamoDB 获取项目计数? [英] How to get item count from DynamoDB?

查看:40
本文介绍了如何从 DynamoDB 获取项目计数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道使用 DynamoDB 查询的项目数.

I want to know item count with DynamoDB querying.

我可以查询 DynamoDB,但我只想知道项目总数".

I can querying for DynamoDB, but I only want to know 'total count of item'.

例如,'SELECT COUNT(*) FROM ... WHERE ...' 在 MySQL 中

For example, 'SELECT COUNT(*) FROM ... WHERE ...' in MySQL

$result = $aws->query(array(
 'TableName' => 'game_table',
 'IndexName' => 'week-point-index',
 'KeyConditions' => array(
    'week' => array(
        'ComparisonOperator' => 'EQ',
        'AttributeValueList' => array(
            array(Type::STRING => $week)
        )
    ),
    'point' => array(
        'ComparisonOperator' => 'GE',
        'AttributeValueList' => array(
            array(Type::NUMBER => $my_point)
        )
    )
 ),
));
echo Count($result['Items']);

此代码使所有用户数据高于我的观点.

this code gets the all users data higher than my point.

如果 $result 的计数为 100,000,则 $result 太大了.并且会超出查询大小的限制.

If count of $result is 100,000, $result is too much big. And it would exceed the limits of the query size.

我需要帮助.

推荐答案

您可以使用 Select 参数并在请求中使用 COUNT .它返回匹配项的数量,而不是匹配项本身".重要,正如由 Saumitra R. Bhave 在评论中提出"如果查询结果集的大小大于 1 MB,则 ScannedCount 和 Count 将仅表示总项目的部分计数.您将需要执行多个 Query 操作才能检索所有结果".

You can use the Select parameter and use COUNT in the request. It "returns the number of matching items, rather than the matching items themselves". Important, as brought up by Saumitra R. Bhave in a comment, "If the size of the Query result set is larger than 1 MB, then ScannedCount and Count will represent only a partial count of the total items. You will need to perform multiple Query operations in order to retrieve all of the results".

我不熟悉 PHP,但这里是您如何在 Java 中使用它的方法.然后,您可以使用 Count 响应值 - $result['Count']:

I'm Not familiar with PHP but here is how you could use it with Java. And then instead of using Count (which I am guessing is a function in PHP) on the 'Items' you can use the Count value from the response - $result['Count']:

final String week = "whatever";
final Integer myPoint = 1337;
Condition weekCondition = new Condition()
        .withComparisonOperator(ComparisonOperator.EQ)
        .withAttributeValueList(new AttributeValue().withS(week));
Condition myPointCondition = new Condition()
        .withComparisonOperator(ComparisonOperator.GE)
        .withAttributeValueList(new AttributeValue().withN(myPoint.toString()))

Map<String, Condition> keyConditions = new HashMap<>();
keyConditions.put("week", weekCondition);
keyConditions.put("point", myPointCondition);

QueryRequest request = new QueryRequest("game_table");
request.setIndexName("week-point-index");
request.setSelect(Select.COUNT);
request.setKeyConditions(keyConditions);

QueryResult result = dynamoDBClient.query(request);
Integer count = result.getCount();

如果您不需要模拟 WHERE 子句,您可以使用 DescribeTable 请求并使用 得到估计的项目计数.

If you don't need to emulate the WHERE clause, you can use a DescribeTable request and use the resulting item count to get an estimate.

指定表中的项目数.DynamoDB 大约每六小时更新一次该值.最近的更改可能不会反映在此值中.

The number of items in the specified table. DynamoDB updates this value approximately every six hours. Recent changes might not be reflected in this value.

另外,文档 中的一个重要说明如所述由 Saumitra R. Bhave 对此答案的评论:

Also, an important note from the documentation as noted by Saumitra R. Bhave in the comments on this answer:

如果 Query 结果集的大小大于 1 MB,ScannedCountCount 仅代表总项目数的一部分.您需要执行多个 Query 操作来检索所有结果(请参阅 分页表查询结果).

If the size of the Query result set is larger than 1 MB, ScannedCount and Count represent only a partial count of the total items. You need to perform multiple Query operations to retrieve all the results (see Paginating Table Query Results).

这篇关于如何从 DynamoDB 获取项目计数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆