Akib Deraiya
Akib Deraiya

Reputation: 99

How to define sorting key in serverless.yaml

I am new to serverless, AWS and lambda function.
I want to know how to define a sorting key into serverless.yaml file and how we can sort data ASC or DEC order when we fetch the data from DynamoDB table?

I also want to know that suppose I have user table fields like

ID(Primary key)
Fullname(String)
Email(String)
City(String)
State(String)
PhoneNumber(String)

I want to sort in ASC or DEC order on Fullname and email column. So, as per my understanding, I need to define Fullname and email column as sorting key.

Here is my serverless.yaml file

resources:
Resources:
UserDynamoDbTable:
  Type: 'AWS::DynamoDB::Table'
  DeletionPolicy: Retain
  Properties:
    AttributeDefinitions:
      -
        AttributeName: id
        AttributeType: S
    KeySchema:
      -
        AttributeName: id
        KeyType: HASH
    ProvisionedThroughput:
      ReadCapacityUnits: 1
      WriteCapacityUnits: 1
    TableName: 'user'

So, how can i define multiple sorting keys(Here is Fullname and email) in a table?

In above .yaml file how can i define Fullname and email as sorting key?

Please help me here. Thanks in advance

Upvotes: 4

Views: 4108

Answers (2)

Richard
Richard

Reputation: 2635

Example defining the primary sort key in your serverless.yml file:

Is this example I added a timestamp sort key to my table.

This key needs to be defined under AttributeDefinitions and KeySchema.

resources:
  Resources:
    MyTable:
      Type: 'AWS::DynamoDB::Table'
      DeletionPolicy: Retain
      Properties:
        TableName: your_table_name
        AttributeDefinitions:
          -
            AttributeName: id
            AttributeType: S
          -
            AttributeName: timestamp
            AttributeType: N
        KeySchema:
          -
            AttributeName: id
            KeyType: HASH
          -
            AttributeName: timestamp
            KeyType: RANGE

I looked into the definition and it follows Cloud Formations docs.

AttributeDefinitions - https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-dynamodb-keyschema.html

KeySchema - https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_AttributeDefinition.html

Upvotes: 2

Can Sahin
Can Sahin

Reputation: 1166

Your understanding of sort keys looks wrong. Sort Keys work totally DEPENDENT to HASH Keys. You can think of it as a sub category of a certain HASH Key. Dynamodb stores everything in the same HASH Key together. Sort Keys are to tell how they are ordered while they are being stored together.

From AWS Document Local Secondary Indexes

enter image description here

DynamoDB stores all of the items with the same partition key value contiguously. In this example, given a particular ForumName, a Query operation could immediately locate all of the threads for that forum. Within a group of items with the same partition key value, the items are sorted by sort key value. If the sort key (Subject) is also provided in the query, DynamoDB can narrow down the results that are returned—for example, returning all of the threads in the "S3" forum that have a Subject beginning with the letter "a"

I would suggest studying on how DynamoDB works would help you designing your application data models.

From what I understand, you want to sort all of the items in that table by FullName and by Email. This means they all need to be stored together, which means they need to have the SAME HASH Key, which means they will be written to the same 'Partition'. Its certainly not a good design. There is a pattern like Time-Series Data which uses the same logic but in a different way. Take a look. But its not your case exactly. DynamoDB is not good for such operations you want. You should either change you data model design or look for another database options for that sorting requirement.

Upvotes: 0

Related Questions