aws(学习笔记第十四课)
- 面向
NoSQL DB
的DynamoDB
学习内容:
- 开发一个任务
TODO
管理器
1. 主键,分区键和排序键
DynamoDB
的表定义和属性定义- 表定义(简单主键)
-
表定义的命名需要
系统名
+_
+表名
的形式,提前规划好前缀。因为DynamoDB
直接建立表,没有建立数据库,之后在个别的数据库里面建立表的过程,所以注意提前规划。 -
分区键 -简单的主键,由一个称为分区键 的属性组成。DynamoDB 使用分区键的值作为内部散列函数的输入。来自散列函数的输出决定了项目将存储到的分区 (DynamoDB 内部的物理存储)。在只有分区键的表中,任何两个项目都不能有相同的分区键值
-
表定义代码
注意,建表语句区分大小写
注意,这里建表的时候,建表的时候id
作为主键,不参与主键的name
和gender
不能定义aws dynamodb create-table --table-name stu_system_student \ --attribute-definitions AttributeName=id,AttributeType=S \ --key-schema AttributeName=id,KeyType=HASH \ --provisioned-throughput ReadCapacityUnits=5,WriteCapacityUnits=5
定义之后如下所示:
$ aws dynamodb create-table --table-name stu_system_student \ --attribute-definitions AttributeName=id,AttributeType=S \ --key-schema AttributeName=id,KeyType=HASH \ --provisioned-throughput ReadCapacityUnits=5,WriteCapacityUnits=5 { "TableDescription": { "AttributeDefinitions": [ { "AttributeName": "id", "AttributeType": "S" } ], "TableName": "stu_system_student", "KeySchema": [ { "AttributeName": "id", "KeyType": "HASH" } ], "TableStatus": "CREATING", "CreationDateTime": "2024-11-16T16:37:57.967000+08:00", "ProvisionedThroughput": { "NumberOfDecreasesToday": 0, "ReadCapacityUnits": 5, "WriteCapacityUnits": 5 }, "TableSizeBytes": 0, "ItemCount": 0, "TableArn": "arn:aws:dynamodb:ap-northeast-1:081353481087:table/stu_system_student", "TableId": "316d0a81-801b-451a-8fe1-57959f1906c6", "DeletionProtectionEnabled": false } }
执行
aws cli
查看表建立的状态$ aws dynamodb describe-table --table-name stu_system_student { "Table": { "AttributeDefinitions": [ { "AttributeName": "id", "AttributeType": "S" } ], "TableName": "stu_system_student", "KeySchema": [ { "AttributeName": "id", "KeyType": "HASH" } ], "TableStatus": "ACTIVE", "CreationDateTime": "2024-11-16T16:37:57.967000+08:00", "ProvisionedThroughput": { "NumberOfDecreasesToday": 0, "ReadCapacityUnits": 5, "WriteCapacityUnits": 5 }, "TableSizeBytes": 0, "ItemCount": 0, "TableArn": "arn:aws:dynamodb:ap-northeast-1:081353481087:table/stu_system_student", "TableId": "316d0a81-801b-451a-8fe1-57959f1906c6", "DeletionProtectionEnabled": false } }
-
- 表定义(复合主键)
- 分区键和排序键 - 称为复合主键,此类型的键由两个属性组成。第一个属性是分区键,第二个属性是排序键。
DynamoDB
使用分区键值作为对内部散列函数的输入。来自散列函数的输出决定了项目将存储到的分区 (DynamoDB
内部的物理存储)。具有相同分区键的所有项目按排序键值的排序顺序存储在一起。- 在具有分区键和排序键的表中,两个项目可以具有相同的分区键值,但是,这两个项目必须具有不同的排序键值。
- 定义复合主键表
aws dynamodb create-table --table-name stu_system_score \ --attribute-definitions AttributeName=id,AttributeType=S \ AttributeName=lesson,AttributeType=S \ --key-schema AttributeName=id,KeyType=HASH \ AttributeName=lesson,KeyType=RANGE \ --provisioned-throughput ReadCapacityUnits=5,WriteCapacityUnits=5
- 复合建表结果
$ aws dynamodb create-table --table-name stu_system_score \ --attribute-definitions AttributeName=id,AttributeType=S \ AttributeName=lesson,AttributeType=S \ --key-schema AttributeName=id,KeyType=HASH \ AttributeName=lesson,KeyType=RANGE \ --provisioned-throughput ReadCapacityUnits=5,WriteCapacityUnits=5 { "TableDescription": { "AttributeDefinitions": [ { "AttributeName": "id", "AttributeType": "S" }, { "AttributeName": "lesson", "AttributeType": "S" } ], "TableName": "stu_system_score", "KeySchema":
- 表定义(简单主键)