这个实验包括:
条件是and ,不是or。
可以添加Filter
以下是 DynamoDB 中的几个 Query 示例:
###返回 Aritist = ‘No One You Know’ SongTitle=‘Call Me Today’ 的歌曲:
{
TableName: “Music”,
KeyConditionExpression: “Artist = :a and SongTitle = :t”,
ExpressionAttributeValues: {
“:a”: “No One You Know”,
“:t”: “Call Me Today”
}
}
###返回 Aitist='No One You Know’ 的所有歌曲:
{
TableName: “Music”,
KeyConditionExpression: “Artist = :a”,
ExpressionAttributeValues: {
“:a”: “No One You Know”
}
}
###返回Aritist =‘No One You Know’ 并且 SongTitle 开头为Today 并且价格小于1 的所有歌曲:
{
TableName: “Music”,
KeyConditionExpression: “Artist = :a and contains(SongTitle, :t)”,
FilterExpression: “price < :p”,
ExpressionAttributeValues: {
“:a”: “No One You Know”,
“:t”: “Today”,
“:p”: 1.00
}
}
###在 DynamoDB 中,必须使用 ExpressionAttributeValues 作为表达式参数(例如,KeyConditionExpression和 FilterExpression)中的占位符。
这类似于在关系数据库中使用绑定变量,在运行时将实际值代入 SELECT语句。 下边是query的语法:
###返回 Aritist = ‘No One You Know’ SongTitle=‘Call Me Today’ 的歌曲:
{
TableName: “Music”,
KeyConditionExpression: “Artist = :a and SongTitle = :t”,
ExpressionAttributeValues: {
“:a”: “No One You Know”,
“:t”: “Call Me Today”
}
}
###返回 Aitist='No One You Know’ 的所有歌曲:
{
TableName: “Music”,
KeyConditionExpression: “Artist = :a”,
ExpressionAttributeValues: {
“:a”: “No One You Know”
}
}
###返回Aritist =‘No One You Know’ 并且 SongTitle 开头为Today 并且价格小于1 的所有歌曲:
{
TableName: “Music”,
KeyConditionExpression: “Artist = :a and contains(SongTitle, :t)”,
FilterExpression: “price < :p”,
ExpressionAttributeValues: {
“:a”: “No One You Know”,
“:t”: “Today”,
“:p”: 1.00
}
}
###Python的查询语法:
###返回 Artist=‘No One You Know’ 并且SongTitle=‘Somewhere Down The Road’ 的所有歌曲:
response = table.query(
KeyConditionExpression=Key(‘Artist’).eq(‘No One You Know’) & Key(‘SongTitle’).eq(‘Somewhere Down The Road’)
)
items = response[‘Items’]
print(items)
##返回值,output:
[{
u’Artist’: u’No One You Know’,
u’AlbumTitle’: u’Somewhat Famous’,
u’CriticRating’: Decimal(‘8.4’),
u’Year’: Decimal(‘1984’),
u’Genre’: u’Country’,
u’SongTitle’: u’Somewhere Down The Road’
}
]
###返回Aritist =‘No One You Know’ 并且 SongTitle 开头为 Look 的所有歌曲:
response = table.query(
KeyConditionExpression=Key(‘Artist’).eq(‘The Acme Band’) & Key(‘SongTitle’).begins_with(‘Look’)
)
items = response[‘Items’]
print(items)
###output
[
{
u’Genre’: u’Rock’,
u’Price’: Decimal(‘0.99’),
u’Artist’: u’The Acme Band’,
u’SongTitle’: u’Look Out, World’,
u’AlbumTitle’: u’The Buck Starts Here’
}
]
有关DynamoDB的详细视频课程参考链接:https://edu.51cto.com/center/course/lesson/index?id=558308