database-design - 如何在 DynamoDB 中将排序键设置为全局二级索引(另一个分区键)?

标签 database-design primary-key amazon-dynamodb

我使用了带有以下字段的 DynamoDB 表:

主分区键 => user_id
主排序键 => conversation_id
示例表数据
+---------+--------------------+ | user_id | conversation_id | +---------+--------------------+ | 10 | aaaa | | 10 | bbbb | | 10 | cccc |
| 11 | aaaa | | 11 | bbbb | | 11 | cccc | +---------+--------------------+

我在 dynamodb 中有两个单独的查询:

  • 获取所有 conversation_id由特别 user_id .
    如果输入 10 => 输出 => aaaa, bbbb, cccc
  • 如何获取所有 user_id来自特定 conversation_id ?
    如果输入 aaaa => 输出 => 10,11

  • 我可以获得第一个查询的结果,但是如何获取第二个查询结果。?

    使用 获取数据是一个好习惯吗?主排序键 ( conversation_id ) 或

    如何分配或创建 conversation_id全局二级索引(另一个分区键) ..?

    备注 :我正在使用 PHP(Codeigniter 框架)

    最佳答案

    1) 您需要使用 query获取分区键的所有排序键。请引用以下链接。

    Query API

    Query sample code

    2) 使用 AWS CLI 命令创建 GSI。

    本地 DynamoDB:-

    您可能需要删除端点 url 并包含适当的区域 --region us-east-1 .另外,请相应地更改表名。

    aws dynamodb update-table --table-name Movies --attribute-definitions file://create_gsi_attributes.json --global-secondary-index-updates file://create_gsi.json --endpoint-url http://localhost:8000
    

    create_gsi_attributes.json:-

    请将属性名称(和类型)更改为 conversation_iduser_id
    [{
        "AttributeName": "title",
        "AttributeType": "S"
    },
    {
        "AttributeName": "yearkey",
        "AttributeType": "N"
    }]
    

    create_gsi.json:-

    请将关键架构属性名称更改为 conversation_iduser_id
    [{
        "Create": {
            "IndexName": "Movies_Gsi",
            "KeySchema": [{
                "AttributeName": "title",
                "KeyType": "HASH"
            },
            {
                "AttributeName": "yearkey",
                "KeyType": "RANGE"
            }],
            "Projection": {
                "ProjectionType": "ALL"
            },
            "ProvisionedThroughput": {
                "ReadCapacityUnits": 100,
                "WriteCapacityUnits": 100
            }
        }
    }]
    

    编辑:-

    命令:-
    aws dynamodb update-table --table-name message_participants_tbl --attribute-definitions file://create_gsi_attributes_conversation.json --global-secondary-index-updates file://create_gsi_conversation.json --endpoint-url http://localhost:8000
    

    create_gsi_conversation.json:-
    [{
        "Create": {
            "IndexName": "message_participants_tbl_gsi",
            "KeySchema": [{
                "AttributeName": "conversation_id",
                "KeyType": "HASH"
            },
            {
                "AttributeName": "user_id",
                "KeyType": "RANGE"
            }],
            "Projection": {
                "ProjectionType": "ALL"
            },
            "ProvisionedThroughput": {
                "ReadCapacityUnits": 100,
                "WriteCapacityUnits": 100
            }
        }
    }]
    

    create_gsi_attributes_conversation.json:-
    [{
        "AttributeName": "user_id",
        "AttributeType": "S"
    },
    {
        "AttributeName": "conversation_id",
        "AttributeType": "S"
    }]
    

    关于database-design - 如何在 DynamoDB 中将排序键设置为全局二级索引(另一个分区键)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41393672/

    相关文章:

    sql - 如何检查外键是否设置为不存在的主键?

    .net-core - .Net Core 自动生成字符串主键

    javascript - Amazon Lambda 不会写入 DynamoDB

    amazon-dynamodb - 授予一个 IAM 角色对大量 DynamoDB 表的访问权限

    mysql - 2张表的主键

    amazon-s3 - 使用 LAMBDA 函数从 S3 到 Dynamodb 的 csv 文件

    database - 每个表都应该有一个主键吗?

    sql-server - 主键的 Sql 数据类型 - SQL Server?

    database - 如何为未知数量的 'meta' 数据设计数据库

    mysql - 一次填充MySQL记录一对多关联表