Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DynamoDB Descending Order fetch records

i have 100 records in collection,

collection name:'users'

{
   "name":'senthilkumar',  
   "email":'[email protected]',  //HashKey
   "age":21,
   "created":1465733486137,         //RangeKey-timestamp
}

i need to fetch records the following sql query wise

select * from users order by created desc limit 10

How i can get above query format records from DynamoDB

like image 849
user1837631 Avatar asked Jun 12 '16 12:06

user1837631


People also ask

How do I sort DynamoDB query results?

You can use the sort-key and apply the ScanIndexForward parameter in a query to sort in either ascending or descending order.

Does DynamoDB support sorting?

In an Amazon DynamoDB table, the primary key that uniquely identifies each item in the table can be composed not only of a partition key, but also of a sort key. Well-designed sort keys have two key benefits: They gather related information together in one place where it can be queried efficiently.

Can you query on sort key DynamoDB?

You can Query any table or secondary index, provided that it has a composite primary key (partition key and sort key). Query operations consume read capacity units, as follows. The table's provisioned read capacity. The index's provisioned read capacity.

What order does DynamoDB Scan return?

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.


2 Answers

Dynamodb sorts the results by the range key attribute. You can set the ScanIndexForward boolean parameter to true for ascending or false for descending.

resource: http://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_Query.html

Use the KeyConditionExpression parameter to provide a specific value for the partition key. The Query operation will return all of the items from the table or index with that partition key value. You can optionally narrow the scope of the Query operation by specifying a sort key value and a comparison operator in KeyConditionExpression. You can use the ScanIndexForward parameter to get results in forward or reverse order, by sort key.

like image 107
Eyal Ch Avatar answered Oct 20 '22 11:10

Eyal Ch


To Save Json Data to DynamoDB us put()

var Newparams = {
    TableName: this.SuffleTableName,
       Item: {
        "userId": /* YOUR PRIMARY KEY */,
        "addedAt": /* YOUR SORT KEY */,
        "status": /* Additional Datas */,
       }
}

Fetch Data From DynamoDB using Query()

  QueryParam = {
       TableName: 'YOUR TABLE NAME HERE',
       IndexName: 'YOUR INDEX NAME HERE', //IF YOUR CREATED NEW INDEX
       KeyConditionExpression: "UserId = :UserId  ", //YOUR PRIMARY KEY
       ExpressionAttributeValues: {
          ":UserId": UserId,
       },
       ScanIndexForward: false, //DESC ORDER, Set 'true' if u want asc order 
       ExclusiveStartKey: LastEvalVal, //Pagination - LastEvaluatedKeyPair
       Limit: 10 //DataPerReq
    }
like image 5
Anandan K Avatar answered Oct 20 '22 11:10

Anandan K