Nodejs课堂笔记-第六课 在DynamoDB中如何创建表,nodejsdynamodb


在上一节课中,我们在nodejs中通过代码访问了DynamoDB Local服务器,并且创建了一张表。但那段代码只能用来做demo,因为里面有很多参数并没有讲解应该如何使用。(这个不能怪我啦~~ amazon官网上面资料太多,一时半会没找到应该算是正常情况吧~~)

这节课,我们补上那段缺失的历史,讲解一下create table api中各项参数的使用。

OK,话不多说,看代码:

{
    "AttributeDefinitions": [
        {
            "AttributeName": "string",
            "AttributeType": "string"
        }
    ],
    "GlobalSecondaryIndexes": [
        {
            "IndexName": "string",
            "KeySchema": [
                {
                    "AttributeName": "string",
                    "KeyType": "string"
                }
            ],
            "Projection": {
                "NonKeyAttributes": [
                    "string"
                ],
                "ProjectionType": "string"
            },
            "ProvisionedThroughput": {
                "ReadCapacityUnits": number,
                "WriteCapacityUnits": number
            }
        }
    ],
    "KeySchema": [
        {
            "AttributeName": "string",
            "KeyType": "string"
        }
    ],
    "LocalSecondaryIndexes": [
        {
            "IndexName": "string",
            "KeySchema": [
                {
                    "AttributeName": "string",
                    "KeyType": "string"
                }
            ],
            "Projection": {
                "NonKeyAttributes": [
                    "string"
                ],
                "ProjectionType": "string"
            }
        }
    ],
    "ProvisionedThroughput": {
        "ReadCapacityUnits": number,
        "WriteCapacityUnits": number
    },
    "StreamSpecification": {
        "StreamEnabled": boolean,
        "StreamViewType": "string"
    },
    "TableName": "string"
}

这是DynamoDB中创建表的API参数。待会,我们需要讲解的就是其中各项应该如何赋值。

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

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

(关于DynamoDB中的二级索引和Oralce中的索引概念还不完全一样,我们留到后面的课程中再讲解)

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

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

下面是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"
    }
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

相关内容