How to get item count from DynamoDB?

前端 未结 12 2312
故里飘歌
故里飘歌 2020-12-04 17:15

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(*

12条回答
  •  我在风中等你
    2020-12-04 17:48

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

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

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

提交回复
热议问题