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

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

问题描述

我想通过DynamoDB查询来了解项目计数。



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



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

  $ 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']);

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



如果$ result的计数为100,000,则$ result太大。
,它将超出查询大小的限制。



我需要帮助。

解决方案

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



我对PHP并不熟悉,但是您可以在Java中使用它。然后不用在'Items'上使用 Count (我猜这是PHP中的一个函数),您可以使用 Count 来自响应的值- $ result ['Count']



< pre class = lang-java prettyprint-override> final String week = whatever;
final Integer myPoint = 1337;
Condition weekCondition = new Condition()
.withComparisonOperator(ComparisonOperator.EQ)
.withAttributeValueList(new AttributeValue()。withS(week));
条件myPointCondition =新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结果= dynamoDBClient.query(request);
整数count = result.getCount();

如果您不需要模拟 WHERE 子句,则可以使用 DescribeTable 请求并使用得出项目数量以获取估算值。


指定表。 DynamoDB大约每六个小时更新一次该值。此值可能不会反映最近的更改。



I want to know item count with DynamoDB querying.

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

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.

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

I need help.

解决方案

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".

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();

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.

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.

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

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