dynamo-db
  1. dynamo-db-items

DynamoDB Data Model

Syntax

{
  "TableName": "table_name",
  "KeySchema": [
    {
      "AttributeName": "primary_key_1",
      "KeyType": "HASH"
    },
    {
      "AttributeName": "sort_key_1",
      "KeyType": "RANGE"
    }
  ],
  "AttributeDefinitions": [
    {
      "AttributeName": "primary_key_1",
      "AttributeType": "string"
    },
    {
      "AttributeName": "sort_key_1",
      "AttributeType": "string"
    }
  ],
  "BillingMode": "PAY_PER_REQUEST",
  "GlobalSecondaryIndexes": [
    {
      "IndexName": "index_name",
      "KeySchema": [
        {
          "AttributeName": "global_secondary_key",
          "KeyType": "HASH"
        }
      ],
      "Projection": {
        "ProjectionType": "ALL"
      },
      "BillingMode": "PAY_PER_REQUEST"
    }
  ]
}

Example

{
  "TableName": "users",
  "KeySchema": [
    {
      "AttributeName": "user_id",
      "KeyType": "HASH"
    },
    {
      "AttributeName": "timestamp",
      "KeyType": "RANGE"
    }
  ],
  "AttributeDefinitions": [
    {
      "AttributeName": "user_id",
      "AttributeType": "S"
    },
    {
      "AttributeName": "timestamp",
      "AttributeType": "N"
    }
  ],
  "BillingMode": "PAY_PER_REQUEST",
  "GlobalSecondaryIndexes": [
    {
      "IndexName": "username_index",
      "KeySchema": [
        {
          "AttributeName": "username",
          "KeyType": "HASH"
        }
      ],
      "Projection": {
        "ProjectionType": "ALL"
      },
      "BillingMode": "PAY_PER_REQUEST"
    }
  ]
}

Output

The above code creates a DynamoDB table called "users" with a primary key of "user_id" and a sort key of "timestamp". It also creates a global secondary index called "username_index" with a hash key of "username". The table is set to use pay-per-request billing mode.

Explanation

DynamoDB is a NoSQL database, which means that the data model is different than traditional relational databases. Instead of having tables with pre-defined columns and rows, DynamoDB uses a schema-less design that allows the structure of the database to be more flexible.

In DynamoDB, tables have a primary key (which can be partition key or a combination of partition key and sort key) that is used to uniquely identify each item in the table. In the example above, the primary key is a combination of "user_id" and "timestamp".

Global Secondary indexes in DynamoDB is used to provide alternate query patterns. To add flexibility in querying the data it creates an index.

The DynamoDB data model is important to understand because it determines how your data will be stored and accessed in the database. Choosing the right data model is crucial for the performance and scalability of your application.

Use

The DynamoDB data model is used to define the structure of a DynamoDB database table. It includes information about the table's primary key, attribute definitions, and optional global secondary indexes. The data model is crucial for determining how data is stored and accessed in the database.

Important Points

  • DynamoDB uses a schema-less design that is more flexible than traditional relational databases
  • Tables have a primary key that uniquely identifies each item in the table
  • Global Secondary Indexes are used to provide alternate querying patterns
  • The data model is important for the performance and scalability of your application

Summary

The DynamoDB data model is used to define the structure of a DynamoDB database table. It includes information about the table's primary key, attribute definitions, and optional global secondary indexes. The data model is crucial for determining how data is stored and accessed in the database, and choosing the right data model is important for the performance and scalability of your application.

Published on: