4-12学习记录
参考链接:What Is Amazon DynamoDB?
使用
核心组件
由table,items,attributes组成。
items具有nested attribute (嵌套属性)。
使用唯一的primary key来区分items,使用secondary indexes来提供更灵活的查询。除了primary key之外,表的结构无需提前定义,每个item可以有自己独特的attributes。
primary key
dynamoDB提供两种不同类型的primary key:
Partition key和Partition key and sort key,后者是具有两种attributes的 composite primary key,第二个attribute是sort key。相同的partition key会存储到一起,partition key可以相同但是sort key必须唯一。
更灵活的查询:根据partition key查到一堆数据(例:同一歌手的所有歌曲)
partition key也被称为hash attribute
sort key也被称为range attribute,将相同partition key的item存放到相邻的物理地址。
(primary key只能有一个值,类型必须是string,number,binary)
secondary index
可以创建一个或多个,可以通过secondary index查询。
dynamoDB支持两种index:
Global secondary index:partition key&sort key与原有都不相同
Local secondary index:partition key相同但sort key不同
(table里最多有20个global[默认]和5个local)
其中:
index属于table,对应的叫base table
table发生变更的话dynamoDB会自动更新index
可以选择index里面的attribute,key默认在index里面
dynamoDB stream
记录table变更事件。
每个事件叫做stream record,记录如下:item添加/更新/删除
保留24小时,过期删除
可以通过dynamoDB事件触发lambda(例:为每个新添加的item发送邮件)
dynamoDB API
Naming Rules and Data Types
命名规则
utf-8编码;大小写敏感;字符长度1-255;包括a-z A-Z 0-9 _-. ;
Reserved Words:参考Reserved Words
其中包括#和:
数据类型
Scalar Types ------ 只包含一个值
Document Types ------ 复杂结构具有nested attribute (嵌套属性)包括list 和map
Set Types ------ Scalar Types的集合
Read Consistency 读一致性
Eventually Consistent Reads
响应可能包含某些陈旧数据
通过短时间内重复请求获取最新结果
Strongly Consistent Reads
会返回具有最新数据的响应
可能会返回500错误;可能会延迟高;
不支持全局secondary index;使用更高的吞吐量
除非指定其他读取方式,否则 dynamoDB 将使用最终一致性读取。读取操作 (例如 GetItem,Query 和 Scan) 提供了一个 ConsistentRead 参数。如果将此参数设置为 true,DynamoDB 将在操作过程中使用强一致性读取。
Read/Write Capacity Mode 读写容量
On-demand&Provisioned
Global secondary indexes会继承来自base table的模式
On-demand
按每次读写计价
Provisioned
限制读写
Partitions and Data Distribution 数据分配
Partition Key
对于partition key,计算其hash找到对应的partition存储
Partition Key and Sort Key
按sort key有序地将具有相同partition的项目存储在互相紧邻的物理位置。
NoSQL操作
创建table
使用 CreateTable 操作
{
TableName : "Music",
KeySchema: [
{
AttributeName: "Artist",
KeyType: "HASH", //Partition key
},
{
AttributeName: "SongTitle",
KeyType: "RANGE" //Sort key
}
],
AttributeDefinitions: [
{
AttributeName: "Artist",
AttributeType: "S"
},
{
AttributeName: "SongTitle",
AttributeType: "S"
}
],
ProvisionedThroughput: { // Only specified if using provisioned mode
ReadCapacityUnits: 1,
WriteCapacityUnits: 1
}
}
删除table
使用DeleteTable操作,传入table name即可
写入item
使用 PutItem 操作
{
TableName: "Music",
Item: {
"Artist": "The Acme Band",
"SongTitle": "Look Out, World",
//上面的primary key必须要有
"AlbumTitle":"The Buck Starts Here",
"Price": 0.99,
"Genre": "Rock"
}
}
读取item
根据primary key查询
使用GetItem操作
{
TableName: "Music",
Key: {
"Artist": "No One You Know",
"SongTitle": "Call Me Today"
}
}
Query 操作
{
TableName: "Music",
KeyConditionExpression: "Artist = :a",
ExpressionAttributeValues: {
":a": "No One You Know"
}
}
Scan操作
// Return all of the values for Artist and Title
{
TableName: "Music",
ProjectionExpression: "Artist, Title"
}
修改item
使用 UpdateItem 操作
{
TableName: "Music",
Key: {
"Artist":"No One You Know",
"SongTitle":"Call Me Today"
},
UpdateExpression: "SET RecordLabel = :label",
ExpressionAttributeValues: {
":label": "Global Records"
}
}
UpdateItem 支持条件写入,条件满足时更新操作
删除item
使用 DeleteItem 操作,需要指定primary key
index操作
创建index
使用UpdateTable操作
{
TableName: "Music",
AttributeDefinitions:[
{AttributeName: "Genre", AttributeType: "S"},
{AttributeName: "Price", AttributeType: "N"}
],
GlobalSecondaryIndexUpdates: [
{
Create: {
IndexName: "GenreAndPriceIndex",
KeySchema: [
{AttributeName: "Genre", KeyType: "HASH"}, //Partition key
{AttributeName: "Price", KeyType: "RANGE"}, //Sort key
],
Projection: {
"ProjectionType": "ALL"
},
ProvisionedThroughput: { // Only specified if using provisioned mode
"ReadCapacityUnits": 1,"WriteCapacityUnits": 1
}
}
}
]
}
AttributeDefinitions – index中对应key的数据类型
Projection – index中需要的attribute
index查询
执行 Query 操作
// All of the rock songs
{
TableName: "Music",
IndexName: "GenreAndPriceIndex",
KeyConditionExpression: "Genre = :genre",
ExpressionAttributeValues: {
":genre": "Rock"
},
};