Dynamodb Tutorial
Dynamodb Tutorial
This tutorial introduces you to key DynamoDB concepts necessary for creating and
deploying a highly-scalable and performance-focused database.
Audience
This tutorial targets IT professionals, students, and management professionals who want
a solid grasp of essential DynamoDB concepts.
After completing this tutorial, you will achieve intermediate expertise in DynamoDB, and
easily build on your knowledge to solve more challenging problems.
Prerequisites
This tutorial assumes general knowledge of database technology, programming, Java or
Java-like programming languages, and querying languages. It also assumes familiarity
with typical database operations in an application.
All the content and graphics published in this e-book are the property of Tutorials Point (I)
Pvt. Ltd. The user of this e-book is prohibited to reuse, retain, copy, distribute or republish
any contents or a part of contents of this e-book in any manner without written consent
of the publisher.
We strive to update the contents of our website and tutorials as timely and as precisely as
possible, however, the contents may contain inaccuracies or errors. Tutorials Point (I) Pvt.
Ltd. provides no guarantee regarding the accuracy, timeliness or completeness of our
website or its contents including this tutorial. If you discover any errors on our website or
in this tutorial, do notify us at contact@tutorialspoint.com
i
DynamoDB
Table of Contents
About the Tutorial .................................................................................................................................... i
Audience .................................................................................................................................................. i
Prerequisites ............................................................................................................................................ i
1. DYNAMODB – OVERVIEW.................................................................................................... 1
Partitions ................................................................................................................................................ 5
3. DYNAMODB – ENVIRONMENT............................................................................................. 6
ii
DynamoDB
Java ....................................................................................................................................................... 13
Java ....................................................................................................................................................... 16
Java ....................................................................................................................................................... 21
Java ....................................................................................................................................................... 26
iii
DynamoDB
Retrieve an Item.................................................................................................................................... 36
iv
DynamoDB
v
DynamoDB
Tables.................................................................................................................................................. 129
vi
1. DynamoDB – Overview DynamoDB
DynamoDB allows users to create databases capable of storing and retrieving any amount of
data, and serving any amount of traffic. It automatically distributes data and traffic over
servers to dynamically manage each customer's requests, and also maintains fast
performance.
Common
RDBMS DynamoDB
Tasks
Connect to It uses a persistent connection and It uses HTTP requests and API
the Source SQL commands. operations.
Get Table
All table info remains accessible. Only primary keys are revealed.
Info
Modify Table
It uses an UPDATE statement. It uses an UpdateItem operation.
Data
Delete Table
It uses a DELETE statement. It uses a DeleteItem operation.
Data
Delete a
It uses a DROP TABLE statement. It uses a DeleteTable operation.
Table
Advantages
7
DynamoDB
The two main advantages of DynamoDB are scalability and flexibility. It does not force the
use of a particular data source and structure, allowing users to work with virtually anything,
but in a uniform way.
Its design also supports a wide range of use from lighter tasks and operations to demanding
enterprise functionality. It also allows simple use of multiple languages: Ruby, Java, Python,
C#, Erlang, PHP, and Perl.
Limitations
DynamoDB does suffer from certain limitations, however, these limitations do not necessarily
create huge problems or hinder solid development.
Capacity Unit Sizes – A read capacity unit is a single consistent read per second for
items no larger than 4KB. A write capacity unit is a single write per second for items
no bigger than 1KB.
Provisioned Throughput Min/Max – All tables and global secondary indices have a
minimum of one read and one write capacity unit. Maximums depend on region. In the
US, 40K read and write remains the cap per table (80K per account), and other regions
have a cap of 10K per table with a 20K account cap.
Provisioned Throughput Increase and Decrease – You can increase this as often
as needed, but decreases remain limited to no more than four times daily per table.
Table Size and Quantity Per Account – Table sizes have no limits, but accounts
have a 256 table limit unless you request a higher cap.
Secondary Indexes Per Table – Five local and five global are permitted.
Partition Key Length and Values – Their minimum length sits at 1 byte, and
maximum at 2048 bytes, however, DynamoDB places no limit on values.
Sort Key Length and Values – Its minimum length stands at 1 byte, and maximum
at 1024 bytes, with no limit for values unless its table uses a local secondary index.
Attribute Names – One character remains the minimum, and 64KB the maximum,
with exceptions for keys and certain attributes.
Reserved Words – DynamoDB does not prevent the use of reserved words as names.
Expression Length – Expression strings have a 4KB limit. Attribute expressions have
a 255-byte limit. Substitution variables of an expression have a 2MB limit.
8
2. DynamoDB – Basic Concepts DynamoDB
Before using DynamoDB, you must familiarize yourself with its basic components and
ecosystem. In the DynamoDB ecosystem, you work with tables, attributes, and items. A table
holds sets of items, and items hold sets of attributes. An attribute is a fundamental element
of data requiring no further decomposition, i.e., a field.
Primary Key
The Primary Keys serve as the means of unique identification for table items, and secondary
indexes provide query flexibility. DynamoDB streams record events by modifying the table
data.
The Table Creation requires not only setting a name, but also the primary key; which identifies
table items. No two items share a key. DynamoDB uses two types of primary keys:
Partition Key – This simple primary key consists of a single attribute referred to as
the “partition key.” Internally, DynamoDB uses the key value as input for a hash
function to determine storage.
Partition Key and Sort Key – This key, known as the “Composite Primary Key”,
consists of two attributes:
DynamoDB applies the first attribute to a hash function, and stores items with the
same partition key together; with their order determined by the sort key. Items can
share partition keys, but not sort keys.
The Primary Key attributes only allow scalar (single) values; and string, number, or binary
data types. The non-key attributes do not have these constraints.
Secondary Indexes
These indexes allow you to query table data with an alternate key. Though DynamoDB does
not force their use, they optimize querying.
Global Secondary Index – This index possesses partition and sort keys, which can
differ from table keys.
Local Secondary Index – This index possesses a partition key identical to the table,
however, its sort key differs.
9
DynamoDB
API
The API operations offered by DynamoDB include those of the control plane, data plane (e.g.,
creation, reading, updating, and deleting), and streams. In control plane operations, you
create and manage tables with the following tools:
CreateTable
DescribeTable
ListTables
UpdateTable
DeleteTable
In the data plane, you perform CRUD operations with the following tools:
GetItem
DeleteItem
PutItem BatchGetItem
UpdateItem BatchWriteItem
BatchWriteItem Query
Scan
The stream operations control table streams. You can review the following stream tools:
ListStreams
DescribeStream
GetShardIterator
GetRecords
Provisioned Throughput
In table creation, you specify provisioned throughput, which reserves resources for reads and
writes. You use capacity units to measure and set throughput.
When applications exceed the set throughput, requests fail. The DynamoDB GUI console
allows monitoring of set and used throughput for better and dynamic provisioning.
Read Consistency
DynamoDB uses eventually consistent and strongly consistent reads to support dynamic
application needs. Eventually consistent reads do not always deliver current data.
The strongly consistent reads always deliver current data (with the exception of equipment
failure or network problems). Eventually consistent reads serve as the default setting,
requiring a setting of true in the ConsistentRead parameter to change it.
10
DynamoDB
Partitions
DynamoDB uses partitions for data storage. These storage allocations for tables have SSD
backing and automatically replicate across zones. DynamoDB manages all the partition tasks,
requiring no user involvement.
In table creation, the table enters the CREATING state, which allocates partitions. When it
reaches ACTIVE state, you can perform operations. The system alters partitions when its
capacity reaches maximum or when you change throughput.
11
3. DynamoDB – Environment DynamoDB
The DynamoDB Environment only consists of using your Amazon Web Services account to
access the DynamoDB GUI console, however, you can also perform a local install.
Click the “Get Started with Amazon DynamoDB” button, or the “Create an AWS Account”
button if you do not have an Amazon Web Services account. The simple, guided process will
inform you of all the related fees and requirements.
After performing all the necessary steps of the process, you will have the access. Simply sign
in to the AWS console, and then navigate to the DynamoDB console.
Local Install
The AWS (Amazon Web Service) provides a version of DynamoDB for local installations. It
supports creating applications without the web service or a connection. It also reduces
provisioned throughput, data storage, and transfer fees by allowing a local database. This
guide assumes a local install.
When ready for deployment, you can make a few small adjustments to your application to
convert it to AWS use.
The install file is a .jar executable. It runs in Linux, Unix, Windows, and any other OS with
Java support. Download the file by using one of the following links:
Tarball – http://dynamodb-local.s3-website-us-west-
2.amazonaws.com/dynamodb_local_latest.tar.gz
Note: Other repositories offer the file, but not necessarily the latest version. Use the links
above for up-to-date install files. Also, ensure you have Java Runtime Engine (JRE) version
6.x or a newer version. DynamoDB cannot run with older versions.
After downloading the appropriate archive, extract its directory (DynamoDBLocal.jar) and
place it in the desired location.
You can then start DynamoDB by opening a command prompt, navigating to the directory
containing DynamoDBLocal.jar, and entering the following command:
You can also stop the DynamoDB by closing the command prompt used to start it.
12
DynamoDB
Working Environment
You can use a JavaScript shell, a GUI console, and multiple languages to work with
DynamoDB. The languages available include Ruby, Java, Python, C#, Erlang, PHP, and Perl.
In this tutorial, we use Java and GUI console examples for conceptual and code clarity. Install
a Java IDE, the AWS SDK for Java, and setup AWS security credentials for the Java SDK in
order to utilize Java.
A local install differs from the web service in many ways including, but not limited to the
following key differences:
The local install creates tables immediately, but the service takes much longer.
The local install ignores throughput.
The deletion occurs immediately in a local install.
The reads/writes occur quickly in local installs due to the absence of network overhead.
13
4. DynamoDB – Operations Tools DynamoDB
DynamoDB provides three options for performing operations: a web-based GUI console, a
JavaScript shell, and a programming language of your choice.
In this tutorial, we will focus on using the GUI console and Java language for clarity and
conceptual understanding.
GUI Console
The GUI console or the AWS Management Console for Amazon DynamoDB can be found at
the following address: https://console.aws.amazon.com/dynamodb/home
CRUD
View Table Items
Perform Table Queries
Set Alarms for Table Capacity Monitoring
View Table Metrics in Real-Time
View Table Alarms
14
DynamoDB
If your DynamoDB account has no tables, on access, it guides you through creating a table.
Its main screen offers three shortcuts for performing common operations:
Create Tables
Add and Query Tables
Monitor and Manage Tables
15
DynamoDB
Access the shell by opening a web browser and entering the following address:
http://localhost:8000/shell
Use the shell by entering JavaScript in the left pane, and clicking the “Play” icon button in the
top right corner of the left pane, which runs the code. The code results display in the right
pane.
16
5. DynamoDB – Data Types DynamoDB
Data types supported by DynamoDB include those specific to attributes, actions, and your
coding language of choice.
Scalar – These types represent a single value, and include number, string, binary,
Boolean, and null.
Set – These types represent multiple scalars, and include string sets, number sets,
and binary sets.
Remember DynamoDB as a schemaless, NoSQL database that does not need attribute or data
type definitions when creating a table. It only requires a primary key attribute data types in
contrast to RDBMS, which require column data types on table creation.
Scalars
Numbers – They are limited to 38 digits, and are either positive, negative, or zero.
String – They are Unicode using UTF-8, with a minimum length of >0 and maximum
of 400KB.
Binary – They store any binary data, e.g., encrypted data, images, and compressed
text. DynamoDB views its bytes as unsigned.
Document
List – It stores ordered value collections, and uses square ([...]) brackets.
Map – It stores unordered name-value pair collections, and uses curly ({...}) braces.
17
DynamoDB
Set
Sets must contain elements of the same type whether number, string, or binary. The only
limits placed on sets consist of the 400KB item size limit, and each element being unique.
18
DynamoDB
19