DynamoDB 补充

在DynamoDB之中,同一个用户可以在不同地区创建多张同名表。只要在一个地区中保持表名唯一即可。在DynamoDB执行createTable是一个异步操作,当接收到CreateTable请求时,DynamoDB实时返回一个处理响应,同时将其标记为CREATING状态。只有当表真正建完之后,DynamoDB才会将表状态更改为ACTIVE状态。用户只能在ACTIVE状态的表中进行读写操作。

用户可以在创建表时,同时在表中创建二级索引。如果用户想在多张表中都创建二级索引,那么目前来说只能顺序创建,DynamoDB引擎还无法做到并行创建。在一个时间点,只能有一张表和其二级索引处于CREATING状态。在同一张表中,最多只能创建5个二级索引。(索引太多,和没有索引效果一样。这点和Oracle的处理机制很类似。因此最多只能创建5个索引)

因为createTable是一个异步操作,所以只能通过查询才能得知目前表的具体状态。查询API是DescribleTable。

OK,CreateTable的注意事项,我们交代完了。 下面我们开始讲解各项参数:

AttributeDefinitions 必填项

表示表或者索引的Key属性数组。允许最大的长度是255.其允许的值为:

S 表示Key类型为String
N 表示Key类型为Number
B 表示Key类型为Binary
KeySchema 必填项

指定表或者索引的主键。在KeySchema中的Key必须首先在AttributeDefinitions中存在。 每一个KeySchema都包括以下两项:

AttributeName: Key名称
KeyType: 主键类型,HASH或者RANGE(二选一)。

因为在DynamoDB之中,必须存在一个HASH类型的主键,所以在KeySchema中,必须有指定一个HASH类型的主键。

如果需要创建一个包含hash和range类型的索引或者主键时,必须按照先指定hash键,然后在指定range键的顺序。

因此在KeySchema属性中,至少要有一组定义。

刚才关系型数据库转到NOSQL数据库的同学,可能会进入到一个误区。就是在KeySchema中将表所有字段都定义好。这在DynamoDB中是没有必要的。

在DynamoDB之中,除了主键和索引需要在KeySchema中定义好字段。其它所有字段都是可以在运行时,自行添加的。比如说,现在有两个字段。当执行代码时,又多了个字段。dynamodb会自动添加上这个字段。因为在DynamoDB之中,都是用K,V键值对进行存储的。所以只要Key指定好了,Value可以随时添加删除。

ProvisionedThroughput 必填项

设置此表的预计数据吞吐量。这个值,可以等表创建成功之后通过UpdateTable API进行修改。

一个读取容量单位=对大小为4KB的项目每秒执行一次强一致性读取,或者每秒执行两次最终一致性读取。
一个写入容量单位=对大小为1KB的项目,每秒进行一次写入。

ReadCapacityUnits最小单位为1.
WriteCapacityUnits最小单位也为1.

TableName 必填项

这个属性,呵呵想必都能看懂。 必填项…. 长度不得超过255个字符,最小不能少于3个字符。只允许使用[a-zA-Z0-9_.-]+这里面的有效合法字符。

GlobalSecondaryIndexes 可选项

每张表可以最多创建5个全局二级索引。在每个全局二级索引定义中包括以下三项:

IndexName: 索引名称。
KeySchema: 定义索引使用的Key
Projection:指定投影类型。DynamoDB会自动创建主键或者索引中定义Key(组合)的投影。投影类型一共分为三类:
KEYS_ONLY。 只拷贝主键和索引中的字段到投影中
INCLUDE。只将指定的字段拷贝到投影中,指定的KEY由NonKeyAttributes指定。
ALL。 所有的字段都拷贝到投影。

NonKeyAttributes :用来指定需要拷贝到投影中的字段数组。此属性中指定的字段总数不得超过20个。如果在不同投影中指定了相同的字段,那么计算总数时会被计算两次。

ProvisionedThroughput :设定全局二级索引的读写吞吐总量。最小值都为1.

LocalSecondaryIndexes 可选项

在一张表当中,最多创建5个本地二级索引。每个二级索引将会对应一个字段的Hash集合。每张表对于hash集合10GB的总容量上限,但对于每个本地二级索引则没有容量上限限制。

此属性包括:IndexName KeySchema Projection ,含义同全局二级索引相同。

StreamSpecification 可选项

此属性用来设置此表的DynamoDB 数据流。设定项为:

StreamEnabled:设定Stream是否开启,参数值为true或者fasle。
StreamViewType: 当表中数据被修改时,DynamoDB决定何种数据可以被同步到此表的stream流中。有效值为:

KEYS_ONLY,KEY发生变化时,将KEYS写入数据流。
NEW_IMAGE,当数据发生变化后,将变化数据写入数据流。
OLD_IMAGE,当数据发生变化前,将变化数据写入数据流。
NEW_AND_OLD_IMAGES,新旧数据都写入数据流。

下面是CreateTable所返回的响应schema:

{
“TableDescription”: {
“AttributeDefinitions”: [
{
“AttributeName”: “string”,
“AttributeType”: “string”
}
],
“CreationDateTime”: number,
“GlobalSecondaryIndexes”: [
{
“Backfilling”: boolean,
“IndexArn”: “string”,
“IndexName”: “string”,
“IndexSizeBytes”: number,
“IndexStatus”: “string”,
“ItemCount”: number,
“KeySchema”: [
{
“AttributeName”: “string”,
“KeyType”: “string”
}
],
“Projection”: {
“NonKeyAttributes”: [
“string”
],
“ProjectionType”: “string”
},
“ProvisionedThroughput”: {
“LastDecreaseDateTime”: number,
“LastIncreaseDateTime”: number,
“NumberOfDecreasesToday”: number,
“ReadCapacityUnits”: number,
“WriteCapacityUnits”: number
}
}
],
“ItemCount”: number,
“KeySchema”: [
{
“AttributeName”: “string”,
“KeyType”: “string”
}
],
“LatestStreamArn”: “string”,
“LatestStreamLabel”: “string”,
“LocalSecondaryIndexes”: [
{
“IndexArn”: “string”,
“IndexName”: “string”,
“IndexSizeBytes”: number,
“ItemCount”: number,
“KeySchema”: [
{
“AttributeName”: “string”,
“KeyType”: “string”
}
],
“Projection”: {
“NonKeyAttributes”: [
“string”
],
“ProjectionType”: “string”
}
}
],
“ProvisionedThroughput”: {
“LastDecreaseDateTime”: number,
“LastIncreaseDateTime”: number,
“NumberOfDecreasesToday”: number,
“ReadCapacityUnits”: number,
“WriteCapacityUnits”: number
},
“StreamSpecification”: {
“StreamEnabled”: boolean,
“StreamViewType”: “string”
},
“TableArn”: “string”,
“TableName”: “string”,
“TableSizeBytes”: number,
“TableStatus”: “string”
}
}

增强大家对createTable参数的理解,下面提供一个示例,请看:

{
“AttributeDefinitions”: [
{
“AttributeName”: “ForumName”,
“AttributeType”: “S”
},
{
“AttributeName”: “Subject”,
“AttributeType”: “S”
},
{
“AttributeName”: “LastPostDateTime”,
“AttributeType”: “S”
}
],
“TableName”: “Thread”,
“KeySchema”: [
{
“AttributeName”: “ForumName”,
“KeyType”: “HASH”
},
{
“AttributeName”: “Subject”,
“KeyType”: “RANGE”
}
],
“LocalSecondaryIndexes”: [
{
“IndexName”: “LastPostIndex”,
“KeySchema”: [
{
“AttributeName”: “ForumName”,
“KeyType”: “HASH”
},
{
“AttributeName”: “LastPostDateTime”,
“KeyType”: “RANGE”
}
],
“Projection”: {
“ProjectionType”: “KEYS_ONLY”
}
}
],
“ProvisionedThroughput”: {
“ReadCapacityUnits”: 5,
“WriteCapacityUnits”: 5
}
}

返回值如下:

{
“TableDescription”: {
“TableArn”: “arn:aws:dynamodb:us-west-2:123456789012:table/Thread”,
“AttributeDefinitions”: [
{
“AttributeName”: “ForumName”,
“AttributeType”: “S”
},
{
“AttributeName”: “LastPostDateTime”,
“AttributeType”: “S”
},
{
“AttributeName”: “Subject”,
“AttributeType”: “S”
}
],
“CreationDateTime”: 1.36372808007E9,
“ItemCount”: 0,
“KeySchema”: [
{
“AttributeName”: “ForumName”,
“KeyType”: “HASH”
},
{
“AttributeName”: “Subject”,
“KeyType”: “RANGE”
}
],
“LocalSecondaryIndexes”: [
{
“IndexArn”: “arn:aws:dynamodb:us-west-2:123456789012:table/Thread/index/LastPostIndex”,
“IndexName”: “LastPostIndex”,
“IndexSizeBytes”: 0,
“ItemCount”: 0,
“KeySchema”: [
{
“AttributeName”: “ForumName”,
“KeyType”: “HASH”
},
{
“AttributeName”: “LastPostDateTime”,
“KeyType”: “RANGE”
}
],
“Projection”: {
“ProjectionType”: “KEYS_ONLY”
}
}
],
“ProvisionedThroughput”: {
“NumberOfDecreasesToday”: 0,
“ReadCapacityUnits”: 5,
“WriteCapacityUnits”: 5
},
“TableName”: “Thread”,
“TableSizeBytes”: 0,
“TableStatus”: “CREATING”
}
}

https://blog.csdn.net/vikings_1001/article/details/48434689

猜你喜欢

转载自blog.csdn.net/weixin_43102784/article/details/83898299
今日推荐