Question

I'm running a sample java program to query a dynamodb table, the table has about 90000 items but when i get the scan count from java it shows only 1994 items

 ScanRequest scanRequest = new ScanRequest().withTableName(tableName);
 ScanResult result = client.scan(scanRequest);
 System.out.println("#items:" + result.getScannedCount());

the program outputs #items:1994 but the detail from amazon aws console shows:

Item Count*: 89249

any idea? thanks

Was it helpful?

Solution 2

Set your book object with correct hash key value, and use DynamoDBMapper to get the count.

DynamoDBQueryExpression<Book> queryExpression = new DynamoDBQueryExpression<Book>()
                .withHashKeyValues(book);
dynamoDbMapper.count(Book.class, queryExpression);

OTHER TIPS

scanning or querying dynamodb only returns maximum of 1MB of data. the count is the number of return items fit in 1MB. in order to get the whole table, you should aggressively scan the database until the value LastEvaluatedKey is null

This should help . Worked for me

        AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard()
                                .withRegion("your region").build();
        DynamoDB dynamoDB = new DynamoDB(client);
        TableDescription tableDescription = dynamoDB.getTable("table name").describe();
        tableDescription.getItemCount();

Based on answer from nightograph

private ArrayList<String> fetchItems() {
    ArrayList<String> ids = new ArrayList<>();
    ScanResult result = null;
    do {
        ScanRequest req = new ScanRequest();
        req.setTableName("table_name");
        if (result != null) {
            req.setExclusiveStartKey(result.getLastEvaluatedKey());
        }
        result = amazonDynamoDBClient.scan(req);
        List<Map<String, AttributeValue>> rows = result.getItems();
        for (Map<String, AttributeValue> map : rows) {
            AttributeValue v = map.get("rangeKey");
            String id = v.getS();
            ids.add(id);
        }
    } while (result.getLastEvaluatedKey() != null);
    System.out.println("Result size: " + ids.size());
    return ids;
}

I agreed with nightograph. I thinks this link is useful.

http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/QueryAndScan.html

I just tested with this example. Anyway this is the Dyanamodb v2.

 final ScanRequest scanRequest = new ScanRequest()
                    .withTableName("table_name");
 final ScanResult result = dynamoDB.scan(scanRequest);
 return result.getCount();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top