Database design 如何在DynamoDB中将排序键设置为全局辅助索引(另一个分区键)。。?

Database design 如何在DynamoDB中将排序键设置为全局辅助索引(另一个分区键)。。?,database-design,primary-key,amazon-dynamodb,Database Design,Primary Key,Amazon Dynamodb,我使用了带有以下字段的DynamoDB表: 主分区键=>用户id 主排序键=>对话id 样本表数据 +---------+--------------------+ |用户|会话| id| +---------+--------------------+ |10 | aaaa | |10 | bbbb | |10 |中交| |11 | aaaa| |11 | bbbb | |11 |中交| +---------+--------------------+ 我在dynamodb中有两个单独的查询:

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

主分区键=>
用户id

主排序键=>
对话id

样本表数据
+---------+--------------------+
|用户|会话| id|
+---------+--------------------+
|10 | aaaa |
|10 | bbbb |
|10 |中交|
|11 | aaaa| |11 | bbbb | |11 |中交| +---------+--------------------+

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

  • 按特定的
    user\u id
    获取所有
    conversation\u id
    如果输入10=>输出=>aaaa、bbbb、cccc
  • 如何从特定的
    会话\u id
    获取所有
    用户\u id
    如果输入aaaa=>输出=>10,11
  • 我可以得到第一次查询的结果,但是如何获取第二次查询的结果呢

    使用主排序键(
    conversation\u id
    )或

    如何将会话id分配或创建为全局辅助索引(另一个分区键)

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

    1)您需要使用
    query
    来获取分区键的所有排序键。请参考下面的链接

    2) 使用AWS CLI命令创建GSI

    本地发电机B:-

    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": {
            "IndexName": "message_participants_tbl_gsi",
            "KeySchema": [{
                "AttributeName": "conversation_id",
                "KeyType": "HASH"
            },
            {
                "AttributeName": "user_id",
                "KeyType": "RANGE"
            }],
            "Projection": {
                "ProjectionType": "ALL"
            },
            "ProvisionedThroughput": {
                "ReadCapacityUnits": 100,
                "WriteCapacityUnits": 100
            }
        }
    }]
    
    [{
        "AttributeName": "user_id",
        "AttributeType": "S"
    },
    {
        "AttributeName": "conversation_id",
        "AttributeType": "S"
    }]
    
    您可能需要删除端点url并包括相应的地区
    ——地区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
    
    创建\u gsi\u属性。json:-

    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": {
            "IndexName": "message_participants_tbl_gsi",
            "KeySchema": [{
                "AttributeName": "conversation_id",
                "KeyType": "HASH"
            },
            {
                "AttributeName": "user_id",
                "KeyType": "RANGE"
            }],
            "Projection": {
                "ProjectionType": "ALL"
            },
            "ProvisionedThroughput": {
                "ReadCapacityUnits": 100,
                "WriteCapacityUnits": 100
            }
        }
    }]
    
    [{
        "AttributeName": "user_id",
        "AttributeType": "S"
    },
    {
        "AttributeName": "conversation_id",
        "AttributeType": "S"
    }]
    
    请将属性名称(和类型)更改为
    conversation\u id
    user\u id

    [{
        "AttributeName": "title",
        "AttributeType": "S"
    },
    {
        "AttributeName": "yearkey",
        "AttributeType": "N"
    }]
    
    [{
        "Create": {
            "IndexName": "Movies_Gsi",
            "KeySchema": [{
                "AttributeName": "title",
                "KeyType": "HASH"
            },
            {
                "AttributeName": "yearkey",
                "KeyType": "RANGE"
            }],
            "Projection": {
                "ProjectionType": "ALL"
            },
            "ProvisionedThroughput": {
                "ReadCapacityUnits": 100,
                "WriteCapacityUnits": 100
            }
        }
    }]
    
    创建\u gsi.json:-

    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": {
            "IndexName": "message_participants_tbl_gsi",
            "KeySchema": [{
                "AttributeName": "conversation_id",
                "KeyType": "HASH"
            },
            {
                "AttributeName": "user_id",
                "KeyType": "RANGE"
            }],
            "Projection": {
                "ProjectionType": "ALL"
            },
            "ProvisionedThroughput": {
                "ReadCapacityUnits": 100,
                "WriteCapacityUnits": 100
            }
        }
    }]
    
    [{
        "AttributeName": "user_id",
        "AttributeType": "S"
    },
    {
        "AttributeName": "conversation_id",
        "AttributeType": "S"
    }]
    
    请将密钥架构属性名称更改为
    conversation\u id
    user\u id

    [{
        "AttributeName": "title",
        "AttributeType": "S"
    },
    {
        "AttributeName": "yearkey",
        "AttributeType": "N"
    }]
    
    [{
        "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": {
            "IndexName": "message_participants_tbl_gsi",
            "KeySchema": [{
                "AttributeName": "conversation_id",
                "KeyType": "HASH"
            },
            {
                "AttributeName": "user_id",
                "KeyType": "RANGE"
            }],
            "Projection": {
                "ProjectionType": "ALL"
            },
            "ProvisionedThroughput": {
                "ReadCapacityUnits": 100,
                "WriteCapacityUnits": 100
            }
        }
    }]
    
    [{
        "AttributeName": "user_id",
        "AttributeType": "S"
    },
    {
        "AttributeName": "conversation_id",
        "AttributeType": "S"
    }]
    
    命令:-

    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": {
            "IndexName": "message_participants_tbl_gsi",
            "KeySchema": [{
                "AttributeName": "conversation_id",
                "KeyType": "HASH"
            },
            {
                "AttributeName": "user_id",
                "KeyType": "RANGE"
            }],
            "Projection": {
                "ProjectionType": "ALL"
            },
            "ProvisionedThroughput": {
                "ReadCapacityUnits": 100,
                "WriteCapacityUnits": 100
            }
        }
    }]
    
    [{
        "AttributeName": "user_id",
        "AttributeType": "S"
    },
    {
        "AttributeName": "conversation_id",
        "AttributeType": "S"
    }]
    
    创建\u gsi\u对话。json:-

    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": {
            "IndexName": "message_participants_tbl_gsi",
            "KeySchema": [{
                "AttributeName": "conversation_id",
                "KeyType": "HASH"
            },
            {
                "AttributeName": "user_id",
                "KeyType": "RANGE"
            }],
            "Projection": {
                "ProjectionType": "ALL"
            },
            "ProvisionedThroughput": {
                "ReadCapacityUnits": 100,
                "WriteCapacityUnits": 100
            }
        }
    }]
    
    [{
        "AttributeName": "user_id",
        "AttributeType": "S"
    },
    {
        "AttributeName": "conversation_id",
        "AttributeType": "S"
    }]
    
    create\u gsi\u attributes\u conversation.json:-

    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": {
            "IndexName": "message_participants_tbl_gsi",
            "KeySchema": [{
                "AttributeName": "conversation_id",
                "KeyType": "HASH"
            },
            {
                "AttributeName": "user_id",
                "KeyType": "RANGE"
            }],
            "Projection": {
                "ProjectionType": "ALL"
            },
            "ProvisionedThroughput": {
                "ReadCapacityUnits": 100,
                "WriteCapacityUnits": 100
            }
        }
    }]
    
    [{
        "AttributeName": "user_id",
        "AttributeType": "S"
    },
    {
        "AttributeName": "conversation_id",
        "AttributeType": "S"
    }]
    

    您能否在
    create\u gsi\u attributes.json
    create\u gsi.json
    中使用名称
    conversation\u id
    user\u id
    。。。?我无法理解需要为
    conversation\u id
    user\u id
    分配哪个键类型。用户id和conversation id的数据类型是什么?更新了特定于表的命令和json。请试一试。