Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set Sort key as Global secondary index (Another Partition key) in DynamoDB..?

I used DynamoDB table with the following fields:

Primary Partition key => user_id

Primary Sort key => conversation_id

Sample table data

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

I have two separate queries in dynamodb:

  1. To fetch all conversation_id by particular user_id.
    If Input 10 => Output => aaaa, bbbb, cccc
  2. How to fetch all user_id from particular conversation_id? If Input aaaa => Output => 10,11

I can get the result for 1st query, but how to fetch the 2nd query result.?

Is it a good practice to fetch data by using Primary Sort key(conversation_id) or

How to assign or create conversation_id as Global secondary index (Another Partition key)..?

Note: I am using PHP (Codeigniter framework)

like image 684
ArunValaven Avatar asked Dec 30 '16 09:12

ArunValaven


People also ask

Can a sort key be a global secondary index?

The primary key of a global secondary index can be either simple (partition key) or composite (partition key and sort key).

Does a GSI need a sort key?

Every global secondary index must have a partition key, and can have an optional sort key. The index key schema can be different from the base table schema.

Can we have two sort keys in DynamoDB?

How many sort keys can DynamoDB have? There should only be one sort key defined per table. But, it can be composed using multiple columns.

How do I add a sort key to an existing DynamoDB table?

DynamoDB doesn't allow to add a sort key for an already existing table. Your only option is to create a new table with redefined key attributes and then copy the data from the existing table to the newly created table.


1 Answers

1) You need to use query to get all the sort keys of a partition key. Please refer the below link.

Query API

Query sample code

2) Create the GSI using AWS CLI command.

Local DynamoDB:-

You may need to delete the endpoint url and include the appropriate region --region us-east-1. Also, please change the table name accordingly.

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:-

Please change the attribute names (and type) to conversation_id and user_id

[{
    "AttributeName": "title",
    "AttributeType": "S"
},
{
    "AttributeName": "yearkey",
    "AttributeType": "N"
}]

create_gsi.json:-

Please change the key schema attribute names to conversation_id and user_id

[{
    "Create": {
        "IndexName": "Movies_Gsi",
        "KeySchema": [{
            "AttributeName": "title",
            "KeyType": "HASH"
        },
        {
            "AttributeName": "yearkey",
            "KeyType": "RANGE"
        }],
        "Projection": {
            "ProjectionType": "ALL"
        },
        "ProvisionedThroughput": {
            "ReadCapacityUnits": 100,
            "WriteCapacityUnits": 100
        }
    }
}]

EDIT:-

Command:-

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"
}]
like image 76
notionquest Avatar answered Oct 15 '22 15:10

notionquest