如何在 DynamoDB 中立即获取 table 的行数?

How to get the row count of a table instantly in DynamoDB?

我正在使用boto.dynamodb2,看来我可以使用Table.query_count()。但是,当没有应用查询过滤器时它引发了异常。

我该怎么做才能解决这个问题?

顺便说一句,boto.dynamodb2.table.Table.Query可以使用的过滤器的文档在哪里?我尝试搜索它,但一无所获。

您可以通过两种方式获取 DynamoDB 中的行数。

第一个是执行完整的 table 扫描并在扫描过程中计算行数。对于任何合理大小的 table,这通常是一个可怕的想法,因为它会消耗所有预配置的读取吞吐量。

另一种方法是使用 Describe Table 请求来估计 table 中的行数。这将立即 return,但只会根据 AWS 文档定期更新。

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

您可以使用它来计算整个 table 项的数量

from boto.dynamodb2.table import Table

dynamodb_table = Table('Users')
dynamodb_table.count()  # updated roughly 6 hours

参考这里:http://boto.cloudhackers.com/en/latest/ref/dynamodb2.html#module-boto.dynamodb2.table

query_count 方法将 return 项目计数基于您提供的索引。

例如,

from boto.dynamodb2.table import Table

dynamodb_table = Table('Users')
print dynamodb_table.query_count(
    index='first_name-last_name-index',  # Get indexes from indexes tab in dynamodb console
    first_name__eq='John',  # add __eq to your index name for specific search
    last_name__eq='Smith'  # This is your range key
)

您可以添加主索引或全局二级索引以及范围键。 可能的比较运算符

__eq 等于

__lt 少于

__gt 大于

__gte 表示大于或等于

__lte 小于等于

__between

__beginswith 以

开头

之间的示例
print dynamodb_table.query_count(
    index='first_name-last_name-index',  # Get indexes from indexes tab in dynamodb console
    first_name__eq='John',  # add __eq to your index name for specific search
    age__between=[30, 50]  # This is your range key
)

根据文档boto3

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

import boto3

dynamoDBResource = boto3.resource('dynamodb')
table = dynamoDBResource.Table('tableName')
print(table.item_count)

或者您可以使用 DescribeTable:

import boto3

dynamoDBClient = boto3.client('dynamodb')
table = dynamoDBClient.describe_table(
    TableName='tableName'
)
print(table)

如果要统计物品的数量:

import boto3

client = boto3.client('dynamodb','us-east-1')
response = client.describe_table(TableName='test')
print(response['Table']['ItemCount'])

#ItemCount (integer) --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.

参考:Boto3 文档(在 describe_table() 中的 ItemCount 下)