【问题标题】:How to set Sort key as Global secondary index (Another Partition key) in DynamoDB..?如何在 DynamoDB 中将排序键设置为全局二级索引(另一个分区键)..?
【发布时间】:2016-12-30 09:20:42
【问题描述】:

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

主分区键 => user_id

主排序键 => conversation_id

示例表数据

+---------+--------------------+ | user_id | conversation_id | +---------+--------------------+ | 10 | aaaa | | 10 | bbbb | | 10 | cccc |
| 11 | aaaa | | 11 | bbbb | | 11 | cccc | +---------+--------------------+

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

  1. 通过特定的user_id 获取所有conversation_id
    如果输入 10 => 输出 => aaaa、bbbb、cccc
  2. 如何从特定的conversation_id 获取所有user_id如果输入 aaaa => 输出 => 10,11

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

使用主排序键(conversation_id) 或

获取数据是一种好习惯吗?

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

注意:我使用的是 PHP(Codeigniter 框架)

【问题讨论】:

    标签: database-design primary-key amazon-dynamodb


    【解决方案1】:

    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"
    }]
    

    【讨论】:

    • 你能在create_gsi_attributes.jsoncreate_gsi.json中使用名称conversation_iduser_id吗?我无法理解需要为 conversation_iduser_id 分配哪个 keyType。
    • 用户id和对话id的数据类型是什么?
    • 更新了特定于您的表的命令和 json。请尝试。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-03-30
    • 2021-03-14
    • 2014-08-03
    • 1970-01-01
    • 1970-01-01
    • 2016-02-07
    • 2020-07-13
    相关资源
    最近更新 更多