过滤dynamodb语句

问题描述 投票:0回答:1

它是一个小查询我只想获取那些事件名称为''newevent'的记录

如何在文档中应用此过滤器所有查询都是这样的复合键

face = dynamodb.get_item(
                                TableName='athlete_collection',
                                Key={'RekognitionId': {'S': match['Face']['FaceId']}
                                    ,'EventName': {'S' : 'celeb'}
                                     }
                            )

但就我而言,它不是复合键。

amazon-web-services amazon-dynamodb
1个回答
0
投票

您可以将Dynamodb扫描API与FilterExpression一起使用。请参阅以下示例:

AWSCLI:

aws dynamodb scan --table-name sample --filter-expression "eventname = :val" --expression-attribute-values '{":val":{"S":"newevent"}}' --region us-west-1

输出:

{
    "Count": 1,
    "Items": [
        {
            "eventname": {
                "S": "myname"
            },
            "id": {
                "N": "4"
            },
            "value": {
                "S": "value4"
            }
        }
    ],
    "ScannedCount": 5,
    "ConsumedCapacity": null
}

Boto3 SDK:

import boto3
import json

client = boto3.client('dynamodb', region_name='us-west-1')

response = client.scan(
    TableName="sample",
    FilterExpression="eventname = :val",
    ExpressionAttributeValues={
        ":val":{
            "S":"myname"
        }
    }
)
print(json.dumps(response, indent=True))

上面提供的所有示例都将返回所有属性。如果只需要特定属性,请使用--projection-expression(在aws cli中)或ProjectionExpression(在SDK中)。

我建议您浏览以下文档链接以获得更清晰和自定义:

DynamoDB Scan API document

Working with DynamoDB Queries

© www.soinside.com 2019 - 2024. All rights reserved.