[AWS][Nosql]DynamoDB动手实验101
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[AWS][Nosql]DynamoDB动手实验101相关的知识,希望对你有一定的参考价值。
这个实验包括:
- 创建一个DynamoDB 的Table(表)
- 添加数据到Table中
- 查询Table中的数据
- 删除数据库
- 补充,JSON和 Python 的query
Task1:创建一个DynamoDB 的Table(表)
Task2:添加数据到Table中
Task3:查询Table中的数据
条件是and ,不是or。
可以添加Filter
Task4:删除数据库
那么应用程序查询的方式呢?
以下是 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
以上是关于[AWS][Nosql]DynamoDB动手实验101的主要内容,如果未能解决你的问题,请参考以下文章