dynamo-db
  1. dynamo-db-index-types

DynamoDB Data Types and Indexes

Introduction

Amazon DynamoDB is a popularly known NoSQL database service that is fully administered. It provides flexible and scalable table structure, and offers support for both simple data types and composite data types. DynamoDB is a document-based database with the primary unit of data being the document, a unique record on its own. The data in DynamoDB is stored in tables with a primary key that uniquely identifies each item.

DynamoDB Data Types

DynamoDB supports some frequently used data types, and makes it quite easy to specify attribute types. The general data types supported by DynamoDB are:

  • String
  • Number
  • Binary
  • Boolean
  • Null

Furthermore, it has advanced data types that are powerful enough to store hierarchical data. These data types include:

  • List
  • Map
  • Set

Whenever possible, it’s important to maintain the number of attributes in tables to the minimum required. This way, you can reduce the amount of storage required for each record.

Syntax

Here are the syntax for the most commonly used data types:

  • String: "name", "email", etc.
  • Number: 233, 435.3, -254
  • Binary : {"B": "bytes"}, HEX-ENCODED format
  • Boolean: true, false
  • Null : null

To specify composite attribute types, you can use the advanced data types.

List Syntax

{"L": [{"S": "Amazon S3"}, {"S": "Amazon EC2"}]}

Map Syntax

{"M": {"first":{
          "S" : "Jane"
       },
       "last":{
          "S" : "Doe"
       }
}}

Set Syntax

{"SS": ["Red","Blue","Green"]}, {"NS": ["292","23","88"]}

Example

Here's an example of a simple DynamoDB table written in JSON format. Here we have defined two attributes, name and age that represent strings and numeric data types respectively.

{
   "TableName": "example-table",
   "AttributeDefinitions": [
      {
          "AttributeName": "name",
          "AttributeType": "S"
      },
      {
          "AttributeName": "age",
          "AttributeType": "N"
      }
   ],
   "KeySchema": [
      {
         "AttributeName": "name",
         "KeyType": "HASH"
      },
      {
         "AttributeName": "age",
         "KeyType": "RANGE"
      }
   ],
   "ProvisionedThroughput": {
      "ReadCapacityUnits": 1,
      "WriteCapacityUnits": 1
   } 
}

Output

{name:S, age: N, postcode:S}

Exaplanation

  • S: This is a 'String' data type
  • N: This is a 'Number' data type The Attribute is used to specify the attribute name and the AttributeType specifies the data type of the attribute.

Use

Amazon DynamoDB provides performance and scalability advantages to customers without creating a hole in their pocket. It provides the ability to dynamically grow or shrink the throughput and storage to respond automatically to traffic. DynamoDB is beneficial for applications with simple access patterns. The primary use cases are Mobile, Web & Gaming applications, and some of the traditional database operations such as maintaining session data, maintaining transaction logs, and storing application settings.

Important Points

Here are some important points to keep in mind:

  • DynamoDB provides a flexible and scalable table structure, and offers support for both simple and composite data types.
  • DynamoDB has advanced data types that are powerful enough to store hierarchical data.
  • You can reduce the amount of storage required for each record by maintaining the number of attributes in tables to the minimum required.

Summary

In conclusion, DynamoDB supports the frequently used and advanced data types that come in handy for various applications. It provides scalable table structures with simplified attribute types and composite data types. This enables any developer with even a minimal knowledge of programming to easily define tables and maintain them according to their application’s needs. The flexibility and scalability of DynamoDB make it a highly popular database service.

Published on: