简体   繁体   English

使用 boto3 从没有分区键的 dynamodb 进行扫描

[英]Scanning from dynamodb with out partition key using boto3

  • I need to extract the items from dynamodb which is matching id, name, role我需要从 dynamodb 中提取与id, name, role匹配的项目

  • Above 3 are just attributes of the table they are not part key or sort key以上 3 只是表的属性,它们不是部分键或排序键

Below is the code下面是代码

import boto3
from boto3.dynamodb.conditions import Attr
dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('mytable')

def lambda_handler(event, context):
    response_list = []
    scan_response = table.scan(
        FilterExpression=Attr('id').eq(event['id']) & Attr(
        'name').eq(event['name']) & Attr(
        'role').eq('Manager')
    )
    response_list.extend(scan_response['Items'])
    while scan_response.get('LastEvaluatedKey'):
        scan_response = table.scan(
            FilterExpression=Attr('id').eq(event['id']) & Attr(
        'name').eq(event['name']) & Attr(
        'role').eq('Manager')
         )
         response_list.extend(scan_response['Items'])

    print( response_list)

My loop is running infinitely on the first item only.我的循环仅在第一项上无限运行。 where is the issue?问题在哪里?

You aren't passing the LastEvaluatedKey into the scan() call, so it is starting the scan over from the beginning each time.您没有将LastEvaluatedKey传递给scan()调用,因此它每次都从头开始扫描。

Change it to this:将其更改为:

while scan_response.get('LastEvaluatedKey'):
    scan_response = table.scan(
        ExclusiveStartKey=scan_response.get('LastEvaluatedKey'),
        FilterExpression=Attr('id').eq(event['id']) & Attr(
    'name').eq(event['name']) & Attr(
    'role').eq('Manager')
     )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM