我正在关注DynamoDB
python教程.此步骤显示如何基于特定密钥查询表:
http://docs.aws.amazon.com/amazondynamodb/latest/gettingstartedguide/GettingStarted.Python.04.html.
以下是此查询的代码:
from __future__ import print_function # Python 2/3 compatibility
import boto3
import json
import decimal
from boto3.dynamodb.conditions import Key, Attr
# Helper class to convert a DynamoDB item to JSON.
class DecimalEncoder(json.JSONEncoder):
def default(self, o):
if isinstance(o, decimal.Decimal):
return str(o)
return super(DecimalEncoder, self).default(o)
dynamodb = boto3.resource('dynamodb', region_name='us-west-2', endpoint_url="http://localhost:8000")
table = dynamodb.Table('Movies')
print("Movies from 1992 - titles A-L, with genres and lead actor")
response = table.query(
ProjectionExpression="#yr, title, info.genres, info.actors[0]",
ExpressionAttributeNames={ "#yr": "year" }, # Expression Attribute Names for Projection Expression only.
KeyConditionExpression=Key('year').eq(1992) & Key('title').between('A', 'L')
)
for i in response[u'Items']:
print(json.dumps(i, cls=DecimalEncoder))
示例响应项是
{
"title": "Juice",
"year": "1992",
"info": {
"actors": [
"Omar Epps"
],
"genres": [
"Crime",
"Drama",
"Thriller"
]
}
}
该表有两个关键属性’title’和’year’以及嵌套属性’info’.我想要做的是查询数据库并按流派过滤电影,例如获取所有戏剧电影.我不知道如何做到这一点,因为类型键嵌套在内部信息.
我试图从1992年开始拍摄所有的戏剧电影,但它却一片空白.
response = table.query(
KeyConditionExpression=Key('year').eq(1992),
FilterExpression=Attr('info.genres').eq('Drama')
)
如何使用嵌套的info属性正确过滤此查询?
最佳答案 您可以使用contains来过滤List数据类型中的数据.
genres -attribute存储为List inside info属性,这是一种Map数据类型
FilterExpression=Attr('info.genres').contains('Drama')