Dynamodb scan in sorted order

Amazon Web-ServicesAmazon Dynamodb

Amazon Web-Services Problem Overview


Hi I have a dynamodb table. I want the service to return me all the items in this table and the order is by sorting on one attribute.

Do I need to create a global secondary index for this? If that is the case, what should be the hash key, what is the range key? (Note that query on gsi must specify a "EQ" comparator on the hash key of GSI.)

Thanks a lot!

Erben

Amazon Web-Services Solutions


Solution 1 - Amazon Web-Services

If you know the HashKey, then any query will return the items sorted by Range key. From the documentation:

> Query results are always sorted by the range key. If the data type of the range key is Number, the results are returned in numeric order. Otherwise, the results are returned in order of UTF-8 bytes. By default, the sort order is ascending. To reverse the order, set the ScanIndexForward parameter set to false.

Now, if you need to return all the items, you should use a scan. You cannot order the results of a scan.

Another option is to use a GSI (example). Here, you see that the GSI contains only HashKey. The results I guess will be in sorted order of this key (I didn't check this part in a program yet!).

Solution 2 - Amazon Web-Services

Approach I followed to solve this problem is by creating a Global Secondary Index as below. Not sure if this is the best approach but posting it if it is useful to someone.

Hash Key                 | Range Key
------------------------------------
Date value of CreatedAt  | CreatedAt

Limitation imposed on the HTTP API user to specify the number of days to retrieve data, defaults to 24 hr.

This way, I can always specify the HashKey as Current date's day and RangeKey can use > and < operators while retrieving. This way the data is also spread across multiple shards.

Solution 3 - Amazon Web-Services

As of now the dynamoDB scan cannot return you sorted results.

You need to use a query with a new global secondary index (GSI) with a hashkey and range field. The trick is to use a hashkey which is assigned the same value for all data in your table.

I recommend making a new field for all data and calling it "Status" and set the value to "OK", or something similar.

Then your query to get all the results sorted would look like this:

{
    TableName: "YourTable",
    IndexName: "Status-YourRange-index",
    KeyConditions: {
        Status: {
            ComparisonOperator: "EQ", 
            AttributeValueList: [ 
                "OK"
            ]
        }
    },
    ScanIndexForward: false
}

The docs for how to write GSI queries are found here: http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/GSI.html#GSI.Querying

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionErben MoView Question on Stackoverflow
Solution 1 - Amazon Web-ServicesSony KadavanView Answer on Stackoverflow
Solution 2 - Amazon Web-ServicesGireeshView Answer on Stackoverflow
Solution 3 - Amazon Web-ServicesDeemoeView Answer on Stackoverflow