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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31378347/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 09:53:26  来源:igfitidea点击:

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

pythonamazon-dynamodbboto

提问by kxxoling

I'm using boto.dynamodb2, and it seems I can use Table.query_count(). However it had raised an exception when no query filter is applied.

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

What can I do to fix this?

我能做些什么来解决这个问题?

BTW, where is the document of filters that boto.dynamodb2.table.Table.Querycan use? I tried searching for it but found nothing.

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

采纳答案by JaredHatfield

There are two ways you can get a row count in DynamoDB.

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

The first is performing a full table scan and counting the rows as you go. For a table of any reasonable size this is generally a horrible idea as it will consume all of your provisioned read throughput.

第一个是执行全表扫描并随时计算行数。对于任何合理大小的表,这通常是一个可怕的想法,因为它会消耗您所有预配的读取吞吐量。

The other way is to use the Describe Tablerequest to get an estimate of the number of rows in the table. This will return instantly, but will only be updated periodically per the AWS documentation.

另一种方法是使用Describe Table请求来估计表中的行数。这将立即返回,但只会根据 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.

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

回答by theBuzzyCoder

You can use this, to get count of entire table items

您可以使用它来获取整个表格项目的计数

from boto.dynamodb2.table import Table

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

Refer here: http://boto.cloudhackers.com/en/latest/ref/dynamodb2.html#module-boto.dynamodb2.table

请参阅此处:http: //boto.cloudhackers.com/en/latest/ref/dynamodb2.html#module-boto.dynamodb2.table

query_count method will return the item count based on the indexes you provide.

query_count 方法将根据您提供的索引返回项目计数。

For example,

例如,

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
)

You can add the primary index or global secondary indexes along with range keys. possible comparison operators

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

__eq for equal

__eq 等于

__lt for less than

__lt 少于

__gt for greater than

__gt 大于

__gte for greater than or equal

__gte 大于或等于

__lte for less than or equal

__lte 小于或等于

__between for between

__between 之间

__beginswith for begins with

__beginswith for 开始于

Example for between

之间的示例

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
)

回答by Strabek

As per documentation boto3

根据文档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."

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

import boto3

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

or you can use DescribeTable:

或者您可以使用DescribeTable

import boto3

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

回答by Hari K

If you want to count the number of items:

如果要计算物品的数量:

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.

Ref: Boto3Documentation (under ItemCount in describe_table())

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