Amazon web services 有没有一种方法可以有效地从中型DynamoDB获取所有结果?
我将boto3与python结合使用,但我相信问题和逻辑应该在所有语言中都是通用的 我知道Amazon web services 有没有一种方法可以有效地从中型DynamoDB获取所有结果?,amazon-web-services,aws-lambda,amazon-dynamodb,boto3,dynamodb-queries,Amazon Web Services,Aws Lambda,Amazon Dynamodb,Boto3,Dynamodb Queries,我将boto3与python结合使用,但我相信问题和逻辑应该在所有语言中都是通用的 我知道table.scan()理论上应该返回所有记录,但事实上,它们的scan()结果大小限制为1MB。建议基于LastEvaluatedKey创建一个while循环,但这也不能给出所有结果(15200而不是16000),代码如下: dynamodb = boto3.resource('dynamodb', region_name='eu-west-2') table = dynamodb.Table(dBTab
table.scan()
理论上应该返回所有记录,但事实上,它们的scan()结果大小限制为1MB。建议基于LastEvaluatedKey
创建一个while循环,但这也不能给出所有结果(15200而不是16000),代码如下:
dynamodb = boto3.resource('dynamodb', region_name='eu-west-2')
table = dynamodb.Table(dBTable)
response = table.scan()
print("item_count:", table.item_count)
print("response1:", response["Count"])
items=[]
while 'LastEvaluatedKey' in response and response['LastEvaluatedKey'] != "":
print("response:", response["Count"])
items+=response["Items"]
response = table.scan(ExclusiveStartKey=response['LastEvaluatedKey'])
如何可靠地获取所有记录
建议基于LastEvaluatedKey创建一个while循环,但这也不能给出所有结果(15200而不是16000)
你确定吗?我猜你还有别的事。我在每天运行的生产代码中的循环中使用boto3和LastEvaludatedKey,并且从未遇到过不是所有行都返回的情况——不是说这是不可能的,但我首先要确保您的代码是正确的
编辑时,此代码可以工作:
import boto3
from boto3.dynamodb.conditions import Key, Attr
dynamodb = boto3.resource('dynamodb', region_name='us-east-1')
table = dynamodb.Table('DeadLetterQueue')
response = table.scan()
print("item_count:", table.item_count)
items=response["Items"]
while 'LastEvaluatedKey' in response and response['LastEvaluatedKey'] != "":
response = table.scan(ExclusiveStartKey=response['LastEvaluatedKey'])
items.extend(response["Items"])
print (len(items))
您面临的问题与DynamoDB扫描操作无关。这与你的代码有关。上次扫描操作未附加到items数组 下面是经过轻微修改的代码-
dynamodb = boto3.resource('dynamodb', region_name='eu-west-2')
table = dynamodb.Table(dBTable)
response = table.scan()
print("item_count:", table.item_count)
print("response1:", response["Count"])
items=response["Items"] // Changed HERE.
while 'LastEvaluatedKey' in response and response['LastEvaluatedKey'] != "":
response = table.scan(ExclusiveStartKey=response['LastEvaluatedKey'])
print("response:", response["Count"])
items+=response["Items"] // Shifted this line HERE
我在问题中添加了代码,您介意看一下吗?我在lambda上运行这个-如果它重要的话,我已经为您添加了工作代码,与您的略有不同,但是您应该能够调整它。谢谢,这很有效!这都是关于行的顺序