aws(学习笔记第十四课) 面向NoSQL DB的DynamoDB

aws(学习笔记第十四课)

  • 面向NoSQL DBDynamoDB

学习内容:

  • 开发一个任务TODO管理器

1. 主键,分区键和排序键

  1. DynamoDB的表定义和属性定义
    • 表定义(简单主键)
      • 表定义的命名需要系统名 + _ + 表名的形式,提前规划好前缀。因为DynamoDB直接建立表,没有建立数据库,之后在个别的数据库里面建立表的过程,所以注意提前规划。

      • 分区键 -简单的主键,由一个称为分区键 的属性组成。DynamoDB 使用分区键的值作为内部散列函数的输入。来自散列函数的输出决定了项目将存储到的分区 (DynamoDB 内部的物理存储)。在只有分区键的表中,任何两个项目都不能有相同的分区键值
        在这里插入图片描述

      • 表定义代码
        注意,建表语句区分大小写
        注意,这里建表的时候,建表的时候id作为主键,不参与主键的namegender不能定义

        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": 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值