Dynamodb DG
Dynamodb DG
Developer Guide
API Version 2012-08-10
                                             Amazon DynamoDB Developer Guide
Table of Contents
  What Is Amazon DynamoDB? ............................................................................................................... 1
        How It Works ............................................................................................................................ 2
              Core Components .............................................................................................................. 2
              The DynamoDB API ............................................................................................................ 9
              Naming Rules and Data Types ........................................................................................... 11
              Read Consistency ............................................................................................................. 15
              Throughput Capacity ........................................................................................................ 15
              Partitions and Data Distribution ......................................................................................... 18
        From SQL to NoSQL ................................................................................................................. 20
              SQL or NoSQL? ................................................................................................................ 21
              Accessing the Database ..................................................................................................... 22
              Creating a Table ............................................................................................................... 24
              Getting Information About a Table ..................................................................................... 26
              Writing Data To a Table .................................................................................................... 27
              Reading Data From a Table ............................................................................................... 29
              Managing Indexes ............................................................................................................. 34
              Modifying Data in a Table ................................................................................................. 37
              Deleting Data from a Table ............................................................................................... 38
              Removing a Table ............................................................................................................. 39
  Setting Up DynamoDB ...................................................................................................................... 41
        Setting Up DynamoDB Local (Downloadable Version) .................................................................... 41
              Downloading and Running DynamoDB on Your Computer ..................................................... 41
              Setting the Local Endpoint ................................................................................................ 44
              Usage Notes .................................................................................................................... 44
        Setting Up DynamoDB (Web Service) .......................................................................................... 45
              Signing Up for AWS .......................................................................................................... 46
              Getting an AWS Access Key ............................................................................................... 46
              Configuring Your Credentials ............................................................................................. 47
  Accessing DynamoDB ........................................................................................................................ 48
        Using the Console .................................................................................................................... 48
        Using the CLI ........................................................................................................................... 49
              Downloading and Configuring the AWS CLI ......................................................................... 49
              Using the AWS CLI with DynamoDB .................................................................................... 49
              Using the AWS CLI with Downloadable DynamoDB ............................................................... 50
        Using the API .......................................................................................................................... 50
  Getting Started with DynamoDB ........................................................................................................ 52
        Java and DynamoDB ................................................................................................................. 52
              Tutorial Prerequisites ........................................................................................................ 52
              Step 1: Create a Table ...................................................................................................... 53
              Step 2: Load Sample Data ................................................................................................. 54
              Step 3: Create, Read, Update, and Delete an Item ................................................................. 56
              Step 4: Query and Scan the Data ....................................................................................... 64
              Step 5: (Optional) Delete the Table .................................................................................... 68
              Summary ........................................................................................................................ 68
        JavaScript and DynamoDB ......................................................................................................... 69
              Tutorial Prerequisites ........................................................................................................ 69
              Step 1: Create a Table ...................................................................................................... 70
              Step 2: Load Sample Data ................................................................................................. 72
              Step 3: Create, Read, Update, and Delete an Item ................................................................. 74
              Step 4: Query and Scan the Data ....................................................................................... 83
              Step 5: (Optional): Delete the Table .................................................................................... 87
              Summary ........................................................................................................................ 88
        Node.js and DynamoDB ............................................................................................................. 89
              Tutorial Prerequisites ........................................................................................................ 89
           Maximum Write Capacity for a Table With a Stream Enabled ................................................                               774
    DynamoDB Accelerator (DAX) ...................................................................................................                 775
           AWS Region Availability ..................................................................................................              775
           Nodes ...........................................................................................................................       775
           Parameter Groups ...........................................................................................................            775
           Subnet Groups ...............................................................................................................           775
    API-Specific Limits ..................................................................................................................          775
Appendix .......................................................................................................................................   777
    Troubleshooting SSL/TLS connection establishment issues ...........................................................                            777
           Testing your application or service ....................................................................................                777
           Testing your client browser ..............................................................................................              778
           Updating your software application client .........................................................................                     778
           Updating your client browser ...........................................................................................                778
           Manually updating your certificate bundle .........................................................................                      778
    Example Tables and Data ........................................................................................................               779
           Sample Data Files ...........................................................................................................           779
    Creating Example Tables and Uploading Data ............................................................................                        788
           Creating Example Tables and Uploading Data - Java ...........................................................                           788
           Creating Example Tables and Uploading Data - .NET ...........................................................                           795
    Example Application Using AWS SDK for Python (Boto) ...............................................................                            803
           Step 1: Deploy and Test Locally .......................................................................................                 804
           Step 2: Examine the Data Model and Implementation Details ...............................................                               807
           Step 3: Deploy in Production ...........................................................................................                814
           Step 4: Clean Up Resources .............................................................................................                820
    Amazon DynamoDB Storage Backend for Titan ..........................................................................                           820
    Reserved Words in DynamoDB .................................................................................................                   820
    Legacy Conditional Parameters ................................................................................................                 829
           AttributesToGet ..............................................................................................................          830
           AttributeUpdates ............................................................................................................           831
           ConditionalOperator .......................................................................................................             832
           Expected .......................................................................................................................        833
           KeyConditions ................................................................................................................          836
           QueryFilter ....................................................................................................................        838
           ScanFilter ......................................................................................................................       839
           Writing Conditions With Legacy Parameters .......................................................................                       840
    Current Low-Level API Version (2012-08-10) ..............................................................................                      846
    Previous Low-Level API Version (2011-12-05) ............................................................................                       846
           BatchGetItem .................................................................................................................          847
           BatchWriteItem ..............................................................................................................           851
           CreateTable ....................................................................................................................        856
           DeleteItem .....................................................................................................................        861
           DeleteTable ....................................................................................................................        865
           DescribeTables ...............................................................................................................          868
           GetItem .........................................................................................................................       870
           ListTables ......................................................................................................................       873
           PutItem .........................................................................................................................       875
           Query ............................................................................................................................      879
           Scan ..............................................................................................................................     888
           UpdateItem ....................................................................................................................         898
           UpdateTable ..................................................................................................................          904
Document History ..........................................................................................................................        908
   Amazon DynamoDB is a fully managed NoSQL database service that provides fast and predictable
   performance with seamless scalability. DynamoDB lets you offload the administrative burdens
   of operating and scaling a distributed database, so that you don't have to worry about hardware
   provisioning, setup and configuration, replication, software patching, or cluster scaling. Also, DynamoDB
   offers encryption at rest, which eliminates the operational burden and complexity involved in protecting
   sensitive data. For more information, see Amazon DynamoDB Encryption at Rest (p. 567).
   With DynamoDB, you can create database tables that can store and retrieve any amount of data, and
   serve any level of request traffic. You can scale up or scale down your tables' throughput capacity
   without downtime or performance degradation, and use the AWS Management Console to monitor
   resource utilization and performance metrics.
   Amazon DynamoDB provides on-demand backup capability. It allows you to create full backups of your
   tables for long-term retention and archival for regulatory compliance needs. For more information, see
   On-Demand Backup and Restore for DynamoDB (p. 546).
   DynamoDB allows you to delete expired items from tables automatically to help you reduce storage
   usage and the cost of storing data that is no longer relevant. For more information, see Time To
   Live (p. 360).
   DynamoDB automatically spreads the data and traffic for your tables over a sufficient number of
   servers to handle your throughput and storage requirements, while maintaining consistent and fast
   performance. All of your data is stored on solid state disks (SSDs) and automatically replicated across
   multiple Availability Zones in an AWS region, providing built-in high availability and data durability. You
   can use global tables to keep DynamoDB tables in sync across AWS Regions. For more information, see
   Global Tables (p. 558).
   • Amazon DynamoDB: How It Works (p. 2)—To learn essential DynamoDB concepts.
   • Setting Up DynamoDB (p. 41)—To learn how to setup DynamoDB (Downloadable Version or Web
     Service).
   • Accessing DynamoDB (p. 48)—To learn how to access DynamoDB using the console, CLI, or API.
To get started quickly with DynamoDB, see Getting Started with DynamoDB (p. 52).
   To quickly find recommendations for maximizing performance and minimizing throughput costs see
   Best Practices for DynamoDB (p. 703). To learn how to tag DynamoDB resources see Tagging for
   DynamoDB (p. 313).
   For best practices, how-to guides and tools, be sure to check the DynamoDB Developer Resources page:
   http://aws.amazon.com/dynamodb/developer-resources/.
    You can use AWS Database Migration Service to migrate data from a Relational Database or MongoDB
    to an Amazon DynamoDB table. For more information, see AWS Database Migration Service User
    Guide. To learn how to use MongoDB as a migration source, see Using MongoDB as a Source for AWS
    Database Migration Service. To learn how to use DynamoDB as a migration target, see Using an Amazon
    DynamoDB Database as a Target for AWS Database Migration Service.
    After you read this introduction, try working through the Creating Tables and Loading Sample
    Data (p. 281) section, which walks you through the process of creating sample tables, uploading data,
    and performing some basic database operations.
For language-specific tutorials with sample code, see Getting Started with DynamoDB (p. 52).
    Topics
     • DynamoDB Core Components (p. 2)
     • The DynamoDB API (p. 9)
     • Naming Rules and Data Types (p. 11)
     • Read Consistency (p. 15)
     • Throughput Capacity for Reads and Writes (p. 15)
     • Partitions and Data Distribution (p. 18)
There are limits in DynamoDB. For more information, see Limits in DynamoDB (p. 769).
    Topics
     • Tables, Items, and Attributes (p. 2)
     • Primary Key (p. 5)
     • Secondary Indexes (p. 5)
     • DynamoDB Streams (p. 8)
    • Tables – Similar to other database systems, DynamoDB stores data in tables. A table is a collection of
      data. For example, see the example table called People that you could use to store personal contact
      information about friends, family, or anyone else of interest. You could also have a Cars table to store
      information about vehicles that people drive.
    • Items – Each table contains multiple items. An item is a group of attributes that is uniquely identifiable
      among all of the other items. In a People table, each item represents a person. For a Cars table, each
  item represents one vehicle. Items in DynamoDB are similar in many ways to rows, records, or tuples
  in other database systems. In DynamoDB, there is no limit to the number of items you can store in a
  table.
• Attributes – Each item is composed of one or more attributes. An attribute is a fundamental data
  element, something that does not need to be broken down any further. For example, an item in a
  People table contains attributes called PersonID, LastName, FirstName, and so on. For a Department
  table, an item might have attributes such as DepartmentID, Name,Manager, and so on. Attributes in
  DynamoDB are similar in many ways to fields or columns in other database systems.
The following diagram shows a table named People with some example items and attributes.
• Each item in the table has a unique identifier, or primary key, that distinguishes the item from all of
  the others in the table. In the People table, the primary key consists of one attribute (PersonID).
• Other than the primary key, the People table is schemaless, which means that neither the attributes
  nor their data types need to be defined beforehand. Each item can have its own distinct attributes.
• Most of the attributes are scalar, which means that they can have only one value. Strings and numbers
  are common examples of scalars.
• Some of the items have a nested attribute (Address). DynamoDB supports nested attributes up to 32
  levels deep.
The following is another example table named Music that you could use to keep track of your music
collection.
• The primary key for Music consists of two attributes (Artist and SongTitle). Each item in the table must
  have these two attributes. The combination of Artist and SongTitle distinguishes each item in the table
  from all of the others.
• Other than the primary key, the Music table is schemaless, which means that neither the attributes nor
  their data types need to be defined beforehand. Each item can have its own distinct attributes.
• One of the items has a nested attribute (PromotionInfo), which contains other nested attributes.
  DynamoDB supports nested attributes up to 32 levels deep.
For more information, see Working with Tables in DynamoDB (p. 291).
Primary Key
When you create a table, in addition to the table name, you must specify the primary key of the table.
The primary key uniquely identifies each item in the table, so that no two items can have the same key.
• Partition key – A simple primary key, composed of one attribute known as the partition key.
  DynamoDB uses the partition key's value as input to an internal hash function. The output from the
  hash function determines the partition (physical storage internal to DynamoDB) in which the item will
  be stored.
In a table that has only a partition key, no two items can have the same partition key value.
  The People table described in Tables, Items, and Attributes (p. 2) is an example of a table with a
  simple primary key (PersonID). You can access any item in the People table immediately by providing
  the PersonId value for that item.
• Partition key and sort key – Referred to as a composite primary key, this type of key is composed of
  two attributes. The first attribute is the partition key, and the second attribute is the sort key.
  DynamoDB uses the partition key value as input to an internal hash function. The output from the
  hash function determines the partition (physical storage internal to DynamoDB) in which the item will
  be stored. All items with the same partition key are stored together, in sorted order by sort key value.
  In a table that has a partition key and a sort key, it's possible for two items to have the same partition
  key value. However, those two items must have different sort key values.
  The Music table described in Tables, Items, and Attributes (p. 2) is an example of a table with a
  composite primary key (Artist and SongTitle). You can access any item in the Music table immediately, if
  you provide the Artist and SongTitle values for that item.
  A composite primary key gives you additional flexibility when querying data. For example, if you
  provide only the value for Artist, DynamoDB retrieves all of the songs by that artist. You could even
  provide a value for Artist and a range of SongTitle values, to retrieve only a subset of songs by a
  particular artist.
    Note
    The partition key of an item is also known as its hash attribute. The term hash attribute derives
    from the use of an internal hash function in DynamoDB that evenly distributes data items across
    partitions, based on their partition key values.
    The sort key of an item is also known as its range attribute. The term range attribute derives
    from the way DynamoDB stores items with the same partition key physically close together, in
    sorted order by the sort key value.
Each primary key attribute must be a scalar (meaning that it can hold only a single value). The only data
types allowed for primary key attributes are string, number, or binary. There are no such restrictions for
other, non-key attributes.
Secondary Indexes
You can create one or more secondary indexes on a table. A secondary index lets you query the data
in the table using an alternate key, in addition to queries against the primary key. DynamoDB doesn't
require that you use indexes, but they give your applications more flexibility when querying your data.
After you create a secondary index on a table, you can read data from the index in much the same way as
you do from the table.
• Global secondary index – An index with a partition key and sort key that can be different from those
  on the table.
• Local secondary index – An index that has the same partition key as the table, but a different sort key.
You can define up to 5 global secondary indexes and 5 local secondary indexes per table.
In the example Music table shown previously, you can query data items by Artist (partition key) or by
Artist and SongTitle (partition key and sort key). What if you also wanted to query the data by Genre and
AlbumTitle? To do this, you could create an index on Genre and AlbumTitle, and then query the index in
much the same way as you'd query the Music table.
The following diagram shows the example Music table, with a new index called GenreAlbumTitle. In the
index, Genre is the partition key and AlbumTitle is the sort key.
• Every index belongs to a table, which is called the base table for the index. In the preceding example,
  Music is the base table for the GenreAlbumTitle index.
• DynamoDB maintains indexes automatically. When you add, update, or delete an item in the base
  table, DynamoDB adds, updates, or deletes the corresponding item in any indexes that belong to that
  table.
• When you create an index, you specify which attributes will be copied, or projected, from the base
  table to the index. At a minimum, DynamoDB projects the key attributes from the base table into the
  index. This is the case with GenreAlbumTitle, where only the key attributes from the Music table
  are projected into the index.
You can query the GenreAlbumTitle index to find all albums of a particular genre (for example, all Rock
albums). You can also query the index to find all albums within a particular genre that have certain
album titles (for example, all Country albums with titles that start with the letter H).
For more information, see Improving Data Access with Secondary Indexes (p. 444).
DynamoDB Streams
DynamoDB Streams is an optional feature that captures data modification events in DynamoDB tables.
The data about these events appear in the stream in near real time, and in the order that the events
occurred.
Each event is represented by a stream record. If you enable a stream on a table, DynamoDB Streams
writes a stream record whenever one of the following events occurs:
• If a new item is added to the table, the stream captures an image of the entire item, including all of its
  attributes.
• If an item is updated, the stream captures the "before" and "after" image of any attributes that were
  modified in the item.
• If an item is deleted from the table, the stream captures an image of the entire item before it was
  deleted.
Each stream record also contains the name of the table, the event timestamp, and other metadata.
Stream records have a lifetime of 24 hours; after that, they are automatically removed from the stream.
You can use DynamoDB Streams together with AWS Lambda to create a trigger—code that executes
automatically whenever an event of interest appears in a stream. For example, consider a Customers
table that contains customer information for a company. Suppose that you want to send a "welcome"
email to each new customer. You could enable a stream on that table, and then associate the stream
with a Lambda function. The Lambda function would execute whenever a new stream record appears,
but only process new items added to the Customers table. For any item that has an EmailAddress
attribute, the Lambda function would invoke Amazon Simple Email Service (Amazon SES) to send an
email to that address.
    Note
    In this example, the last customer, Craig Roe, will not receive an email because he doesn't have
    an EmailAddress.
In addition to triggers, DynamoDB Streams enables powerful solutions such as data replication within
and across AWS regions, materialized views of data in DynamoDB tables, data analysis using Kinesis
materialized views, and much more.
For more information, see Capturing Table Activity with DynamoDB Streams (p. 517).
Topics
 • Control Plane (p. 9)
 • Data Plane (p. 10)
 • DynamoDB Streams (p. 11)
Control Plane
Control plane operations let you create and manage DynamoDB tables. They also let you work with
indexes, streams, and other objects that are dependent on tables.
• CreateTable – Creates a new table. Optionally, you can create one or more secondary indexes, and
  enable DynamoDB Streams for the table.
• DescribeTable– Returns information about a table, such as its primary key schema, throughput
  settings, index information, and so on.
• ListTables – Returns the names of all of your tables in a list.
• UpdateTable – Modifies the settings of a table or its indexes, creates or remove new indexes on a
  table, or modifies DynamoDB Streams settings for a table.
• DeleteTable – Removes a table and all of its dependent objects from DynamoDB.
Data Plane
Data plane operations let you perform create, read, update, and delete (also called CRUD) actions on
data in a table. Some of the data plane operations also let you read data from a secondary index.
Creating Data
• PutItem – Writes a single item to a table. You must specify the primary key attributes, but you don't
  have to specify other attributes.
• BatchWriteItem – Writes up to 25 items to a table. This is more efficient than calling PutItem
  multiple times because your application only needs a single network round trip to write the items. You
  can also use BatchWriteItem for deleting multiple items from one or more tables.
Reading Data
• GetItem – Retrieves a single item from a table. You must specify the primary key for the item that you
  want. You can retrieve the entire item, or just a subset of its attributes.
• BatchGetItem – Retrieves up to 100 items from one or more tables. This is more efficient than calling
  GetItem multiple times because your application only needs a single network round trip to read the
  items.
• Query – Retrieves all items that have a specific partition key. You must specify the partition key value.
  You can retrieve entire items, or just a subset of their attributes. Optionally, you can apply a condition
  to the sort key values, so that you only retrieve a subset of the data that has the same partition key.
  You can use this operation on a table, provided that the table has both a partition key and a sort key.
  You can also use this operation on an index, provided that the index has both a partition key and a sort
  key.
• Scan – Retrieves all items in the specified table or index. You can retrieve entire items, or just a subset
  of their attributes. Optionally, you can apply a filtering condition to return only the values that you are
  interested in and discard the rest.
Updating Data
• UpdateItem – Modifies one or more attributes in an item. You must specify the primary key for the
  item that you want to modify. You can add new attributes and modify or remove existing attributes.
  You can also perform conditional updates, so that the update is only successful when a user-defined
  condition is met. Optionally, you can implement an atomic counter, which increments or decrements a
  numeric attribute without interfering with other write requests.
Deleting Data
• DeleteItem – Deletes a single item from a table. You must specify the primary key for the item that
  you want to delete.
• BatchWriteItem – Deletes up to 25 items from one or more tables. This is more efficient than calling
  DeleteItem multiple times because your application only needs a single network round trip to delete
  the items. You can also use BatchWriteItem for adding multiple items to one or more tables.
DynamoDB Streams
DynamoDB Streams operations let you enable or disable a stream on a table, and allow access to the
data modification records contained in a stream.
• ListStreams – Returns a list of all your streams, or just the stream for a specific table.
• DescribeStream – Returns information about a stream, such as its Amazon Resource Name (ARN)
  and where your application can begin reading the first few stream records.
• GetShardIterator – Returns a shard iterator, which is a data structure that your application uses to
  retrieve the records from the stream.
• GetRecords – Retrieves one or more stream records, using a given shard iterator.
Topics
 • Naming Rules (p. 11)
 • Data Types (p. 12)
Naming Rules
Tables, attributes, and other objects in DynamoDB must have names. Names should be meaningful and
concise—for example, names such as Products, Books, and Authors are self-explanatory.
Although DynamoDB allows you to use these reserved words and special characters for names, we
recommend that you avoid it because you have to define placeholder variables whenever you use these
names in an expression. For more information, see Expression Attribute Names (p. 341).
Data Types
DynamoDB supports many different data types for attributes within a table. They can be categorized as
follows:
• Scalar Types – A scalar type can represent exactly one value. The scalar types are number, string,
  binary, Boolean, and null.
• Document Types – A document type can represent a complex structure with nested attributes—such
  as you would find in a JSON document. The document types are list and map.
• Set Types – A set type can represent multiple scalar values. The set types are string set, number set,
  and binary set.
When you create a table or a secondary index, you must specify the names and data types of each
primary key attribute (partition key and sort key). Furthermore, each primary key attribute must be
defined as type string, number, or binary.
DynamoDB is a NoSQL database and is schemaless. This means that, other than the primary key
attributes, you don't have to define any attributes or data types when you create tables. By comparison,
relational databases require you to define the names and data types of each column when you create a
table.
The following are descriptions of each data type, along with examples in JSON format.
Scalar Types
The scalar types are number, string, binary, Boolean, and null.
String
Strings are Unicode with UTF-8 binary encoding. The length of a string must be greater than zero and is
constrained by the maximum DynamoDB item size limit of 400 KB.
If you define a primary key attribute as a string type attribute, the following additional constraints apply:
• For a simple primary key, the maximum length of the first attribute value (the partition key) is 2048
  bytes.
• For a composite primary key, the maximum length of the second attribute value (the sort key) is 1024
  bytes.
DynamoDB collates and compares strings using the bytes of the underlying UTF-8 string encoding. For
example, "a" (0x61) is greater than "A" (0x41), and "¿" (0xC2BF) is greater than "z" (0x7A).
You can use the string data type to represent a date or a time stamp. One way to do this is by using ISO
8601 strings, as shown in these examples:
• 2016-02-15
• 2015-12-21T17:42:34Z
• 20150311T122706Z
Number
Numbers can be positive, negative, or zero. Numbers can have up to 38 digits precision. Exceeding this
results in an exception.
In DynamoDB, numbers are represented as variable length. Leading and trailing zeroes are trimmed.
All numbers are sent across the network to DynamoDB as strings, to maximize compatibility across
languages and libraries. However, DynamoDB treats them as number type attributes for mathematical
operations.
    Note
    If number precision is important, you should pass numbers to DynamoDB using strings that you
    convert from number type.
You can use the number data type to represent a date or a time stamp. One way to do this is by using
epoch time—the number of seconds since 00:00:00 UTC on 1 January 1970. For example, the epoch time
1437136300 represents 12:31:40 UTC on 17 July 2015.
Binary
Binary type attributes can store any binary data, such as compressed text, encrypted data, or images.
Whenever DynamoDB compares binary values, it treats each byte of the binary data as unsigned.
The length of a binary attribute must be greater than zero, and is constrained by the maximum
DynamoDB item size limit of 400 KB.
If you define a primary key attribute as a binary type attribute, the following additional constraints
apply:
• For a simple primary key, the maximum length of the first attribute value (the partition key) is 2048
  bytes.
• For a composite primary key, the maximum length of the second attribute value (the sort key) is 1024
  bytes.
Your applications must encode binary values in base64-encoded format before sending them to
DynamoDB. Upon receipt of these values, DynamoDB decodes the data into an unsigned byte array and
uses that as the length of the binary attribute.
dGhpcyB0ZXh0IGlzIGJhc2U2NC1lbmNvZGVk
Boolean
Null
Document Types
The document types are list and map. These data types can be nested within each other, to represent
complex data structures up to 32 levels deep.
There is no limit on the number of values in a list or a map, as long as the item containing the values fits
within the DynamoDB item size limit (400 KB).
An attribute value cannot be an empty String or empty Set (String Set, Number Set, or Binary Set).
However, empty Lists and Maps are allowed. For more information, see Attributes (p. 773).
List
A list type attribute can store an ordered collection of values. Lists are enclosed in square brackets:
[ ... ]
A list is similar to a JSON array. There are no restrictions on the data types that can be stored in a list
element, and the elements in a list element do not have to be of the same type.
The following example shows a list that contains two strings and a number:
       Note
       DynamoDB lets you work with individual elements within lists, even if those elements are deeply
       nested. For more information, see Using Expressions in DynamoDB (p. 337).
Map
A map type attribute can store an unordered collection of name-value pairs. Maps are enclosed in curly
braces: { ... }
A map is similar to a JSON object. There are no restrictions on the data types that can be stored in a map
element, and the elements in a map do not have to be of the same type.
Maps are ideal for storing JSON documents in DynamoDB. The following example shows a map that
contains a string, a number, and a nested list that contains another map.
{
       Day: "Monday",
       UnreadEmails: 42,
       ItemsOnMyDesk: [
           "Coffee Cup",
           "Telephone",
           {
               Pens: { Quantity : 3},
               Pencils: { Quantity : 2},
               Erasers: { Quantity : 1}
           }
       ]
}
       Note
       DynamoDB lets you work with individual elements within maps, even if those elements are
       deeply nested. For more information, see Using Expressions in DynamoDB (p. 337).
Sets
DynamoDB supports types that represent sets of Number, String, or Binary values. All of the elements
within a set must be of the same type. For example, an attribute of type Number Set can only contain
numbers; String Set can only contain strings; and so on.
There is no limit on the number of values in a set, as long as the item containing the values fits within
the DynamoDB item size limit (400 KB).
Each value within a set must be unique. The order of the values within a set is not preserved; therefore,
your applications must not rely on any particular order of elements within the set. Finally, DynamoDB
does not support empty sets.
The following example shows a string set, a number set, and a binary set:
Read Consistency
Amazon DynamoDB is available in multiple AWS regions around the world. Each region is independent
and isolated from other AWS regions. For example, if you have a table called People in the us-east-2
region and another table named People in the us-west-2 region, these are considered two entirely
separate tables. For a list of all the AWS regions in which DynamoDB is available, see AWS Regions and
Endpoints in the Amazon Web Services General Reference.
Every AWS region consists of multiple distinct locations called Availability Zones. Each Availability Zone
is isolated from failures in other Availability Zones, and provides inexpensive, low-latency network
connectivity to other Availability Zones in the same region. This allows rapid replication of your data
among multiple Availability Zones in a region.
When your application writes data to a DynamoDB table and receives an HTTP 200 response (OK), all
copies of the data are updated. The data is eventually consistent across all storage locations, usually
within one second or less.
When you read data from a DynamoDB table, the response might not reflect the results of a recently
completed write operation. The response might include some stale data. If you repeat your read request
after a short time, the response should return the latest data.
When you request a strongly consistent read, DynamoDB returns a response with the most up-to-date
data, reflecting the updates from all prior write operations that were successful. A strongly consistent
read might not be available if there is a network delay or outage.
    Note
    DynamoDB uses eventually consistent reads, unless you specify otherwise. Read operations
    (such as GetItem, Query, and Scan) provide a ConsistentRead parameter. If you set this
    parameter to true, DynamoDB uses strongly consistent reads during the operation.
When you create a table or index in Amazon DynamoDB, you must specify your capacity requirements
for read and write activity. By defining your throughput capacity in advance, DynamoDB can reserve
the necessary resources to meet the read and write activity your application requires, while ensuring
consistent, low-latency performance.
You specify throughput capacity in terms of read capacity units and write capacity units:
• One read capacity unit represents one strongly consistent read per second, or two eventually
  consistent reads per second, for an item up to 4 KB in size. If you need to read an item that is larger
  than 4 KB, DynamoDB will need to consume additional read capacity units. The total number of read
  capacity units required depends on the item size, and whether you want an eventually consistent or
  strongly consistent read.
• One write capacity unit represents one write per second for an item up to 1 KB in size. If you need to
  write an item that is larger than 1 KB, DynamoDB will need to consume additional write capacity units.
  The total number of write capacity units required depends on the item size.
For example, suppose that you create a table with 5 read capacity units and 5 write capacity units. With
these settings, your application could:
If your application reads or writes larger items (up to the DynamoDB maximum item size of 400 KB), it
will consume more capacity units.
If your read or write requests exceed the throughput settings for a table, DynamoDB can throttle that
request. DynamoDB can also throttle read requests exceeds for an index. Throttling prevents your
application from consuming too many capacity units. When a request is throttled, it fails with an HTTP
400 code (Bad Request) and a ProvisionedThroughputExceededException. The AWS SDKs have
built-in support for retrying throttled requests (see Error Retries and Exponential Backoff (p. 193)), so
you do not need to write this logic yourself.
You can use the AWS Management Console to monitor your provisioned and actual throughput, and to
modify your throughput settings if necessary.
With DynamoDB auto scaling, a table or a global secondary index can increase its provisioned read and
write capacity to handle sudden increases in traffic, without request throttling. When the workload
decreases, DynamoDB auto scaling can decrease the throughput so that you don't pay for unused
provisioned capacity.
    Note
    If you use the AWS Management Console to create a table or a global secondary index,
    DynamoDB auto scaling is enabled by default.
    You can manage auto scaling settings at any time by using the console, the AWS CLI, or one of
    the AWS SDKs.
For more information, see Managing Throughput Capacity Automatically with DynamoDB Auto
Scaling (p. 299).
Provisioned Throughput
If you aren't using DynamoDB auto scaling, you have to manually define your throughput requirements.
Provisioned throughput is the maximum amount of capacity that an application can consume from a
table or index. If your application exceeds your provisioned throughput settings, it is subject to request
throttling.
For example, suppose that you want to read 80 items per second from a table. The items are 3 KB in
size, and you want strongly consistent reads. For this scenario, each read requires one provisioned read
capacity unit. To determine this, you divide the item size of the operation by 4 KB, and then round up to
the nearest whole number, as in this example:
For this scenario, you have to set the table's provisioned read throughput to 80 read capacity units:
• 1 read capacity unit per item × 80 reads per second = 80 read capacity units
Now suppose that you want to write 100 items per second to your table, and that the items are 512
bytes in size. For this scenario, each write requires one provisioned write capacity unit. To determine this,
you divide the item size of the operation by 1 KB, and then round up to the nearest whole number:
For this scenario, you would want to set the table's provisioned write throughput to 100 write capacity
units:
• 1 write capacity unit per item × 100 writes per second = 100 write capacity units
For more information see Item Sizes and Capacity Unit Consumption (p. 297).
Reserved Capacity
As a DynamoDB customer, you can purchase reserved capacity in advance, as described at Amazon
DynamoDB Pricing. With reserved capacity, you pay a one-time upfront fee and commit to a minimum
usage level over a period of time. By reserving your read and write capacity units ahead of time, you
realize significant cost savings compared to on-demand provisioned throughput settings.
To manage reserved capacity, go to the DynamoDB console and choose Reserved Capacity.
    Note
    You can prevent users from viewing or purchasing reserved capacity, while still allowing them
    to access the rest of the console. For more information, see "Grant Permissions to Prevent
    Purchasing of Reserved Capacity Offerings" in Authentication and Access Control for Amazon
    DynamoDB (p. 645).
When you create a table, the initial status of the table is CREATING. During this phase, DynamoDB
allocates sufficient partitions to the table so that it can handle your provisioned throughput
requirements. You can begin writing and reading table data after the table status changes to ACTIVE.
• If you increase the table's provisioned throughput settings beyond what the existing partitions can
  support.
• If an existing partition fills to capacity and more storage space is required.
Partition management occurs automatically in the background and is transparent to your applications.
Your table remains available throughout and fully supports your provisioned throughput requirements.
Global secondary indexes in DynamoDB are also composed of partitions. The data in a GSI is stored
separately from the data in its base table, but index partitions behave in much the same way as table
partitions.
To write an item to the table, DynamoDB uses the value of the partition key as input to an internal hash
function. The output value from the hash function determines the partition in which the item will be
stored.
To read an item from the table, you must specify the partition key value for the item. DynamoDB uses
this value as input to its hash function, yielding the partition in which the item can be found.
The following diagram shows a table named Pets, which spans multiple partitions. The table's primary
key is AnimalType (only this key attribute is shown). DynamoDB uses its hash function to determine
where to store a new item, in this case based on the hash value of the string Dog. Note that the items are
not stored in sorted order. Each item's location is determined by the hash value of its partition key.
    Note
    DynamoDB is optimized for uniform distribution of items across a table's partitions, no matter
    how many partitions there may be. We recommend that you choose a partition key that can
    have a large number of distinct values relative to the number of items in the table. For more
    information, see Best Practices for Tables (p. 704).
To write an item to the table, DynamoDB calculates the hash value of the partition key to determine
which partition should contain the item. In that partition, there could be several items with the same
partition key value, so DynamoDB stores the item among the others with the same partition key, in
ascending order by sort key.
To read an item from the table, you must specify its partition key value and sort key value. DynamoDB
calculates the partition key's hash value, yielding the partition in which the item can be found.
You can read multiple items from the table in a single operation (Query), provided that the items you
want have the same partition key value. DynamoDB returns all of the items with that partition key value.
Optionally, you can apply a condition to the sort key so that it returns only the items within a certain
range of values.
    Suppose that the Pets table has a composite primary key consisting of AnimalType (partition key) and
    Name (sort key). The following diagram shows DynamoDB writing an item with a partition key value of
    Dog and a sort key value of Fido.
    To read that same item from the Pets table, DynamoDB calculates the hash value of Dog, yielding the
    partition in which these items are stored. DynamoDB then scans the sort key attribute values until it
    finds Fido.
    To read all of the items with an AnimalType of Dog, you can issue a Query operation without specifying
    a sort key condition. By default, the items are be returned in the order that they are stored (that is, in
    ascending order by sort key). Optionally, you can request descending order instead.
    To query only some of the Dog items, you can apply a condition to the sort key (for example, only the
    Dog items where Name is within the range A through K).
        Note
        In a DynamoDB table, there is no upper limit on the number of distinct sort key values per
        partition key value. If you needed to store many billions of Dog items in the Pets table,
        DynamoDB automatically allocates enough storage to handle this requirement.
Amazon DynamoDB, you will encounter many similarities, but also many things that are different.
This section describes common database tasks, comparing and contrasting SQL statements with their
equivalent DynamoDB operations.
NoSQL is a term used to describe non-relational database systems that are highly available, scalable,
and optimized for high performance. Instead of the relational model, NoSQL databases (like DynamoDB)
use alternate models for data management, such as key-value pairs or document storage. For more
information, see http://aws.amazon.com/nosql.
    Note
    The SQL examples in this section are compatible with the MySQL relational database
    management system.
    The DynamoDB examples in this section show the name of the DynamoDB operation, along with
    the parameters for that operation in JSON format. For code samples that use these operations,
    see Getting Started with DynamoDB (p. 52).
Topics
 • SQL or NoSQL? (p. 21)
 • Accessing the Database (p. 22)
 • Creating a Table (p. 24)
 • Getting Information About a Table (p. 26)
 • Writing Data To a Table (p. 27)
 • Reading Data From a Table (p. 29)
 • Managing Indexes (p. 34)
 • Modifying Data in a Table (p. 37)
 • Deleting Data from a Table (p. 38)
 • Removing a Table (p. 39)
SQL or NoSQL?
Today's applications have more demanding requirements than ever before. For example, an online game
might start out with just a few users and a very small amount of data. However, if the game becomes
successful, it can easily outstrip the resources of the underlying database management system. It is not
uncommon for web-based applications to have hundreds, thousands, or millions of concurrent users,
with terabytes or more of new data generated per day. Databases for such applications must handle tens
(or hundreds) of thousands of reads and writes per second.
Amazon DynamoDB is well-suited for these kinds of workloads. As a developer, you can start with a small
amount of provisioned throughput and gradually increase it as your application becomes more popular.
DynamoDB scales seamlessly to handle very large amounts of data and very large numbers of users.
The following table shows some high-level differences between a relational database management
system (RDBMS) and DynamoDB:
 Data Access                        SQL (Structured Query              You can use the AWS
                                    Language) is the standard for      Management Console or
                                    storing and retrieving data.       the AWS CLI to work with
                                    Relational databases offer a rich   DynamoDB and perform ad
                                    set of tools for simplifying the   hoc tasks. Applications can
                                    development of database-driven     leverage the AWS software
                                    applications, but all of these     development kits (SDKs) to work
                                    tools use SQL.                     with DynamoDB using object-
                                                                       based, document-centric, or low-
                                                                       level interfaces.
The following diagram shows client interaction with a relational database, and with DynamoDB.
The following table has more details about client interaction tasks:
 Tools for Accessing the            Most relational databases           In most cases, you write
 Database                           provide a command line              application code. You can also
                                    interface (CLI), so that you can    use the AWS Management
                                    enter ad hoc SQL statements         Console or the AWS Command
                                    and see the results immediately.    Line Interface (AWS CLI) to send
                                                                        ad hoc requests to DynamoDB
                                                                        and view the results.
Sending a Request                 The application issues a SQL        The application sends HTTP(S)
                                  statement for every database        requests to DynamoDB. The
                                  operation that it wants to          requests contain the name of
                                  perform. Upon receipt of the        the DynamoDB operation to
                                  SQL statement, the RDBMS            perform, along with parameters.
                                  checks its syntax, creates a plan   DynamoDB executes the request
                                  for performing the operation,       immediately.
                                  and then executes the plan.
Receiving a Response              The RDBMS returns the results       DynamoDB returns an HTTP(S)
                                  from the SQL statement. If there    response containing the results
                                  is an error, the RDBMS returns      of the operation. If there is
                                  an error status and message.        an error, DynamoDB returns
                                                                      an HTTP error status and
                                                                      message(s).
Creating a Table
Tables are the fundamental data structures in relational databases and in DynamoDB. A relational
database management systems (RDBMS) requires you to define the table's schema when you create it. In
contrast, DynamoDB tables are schemaless—other than the primary key, you do not need to define any
attributes or data types at table creation time.
SQL
Use the CREATE TABLE statement to create a table, as shown in the following example.
The primary key for this table consists of Artist and SongTitle.
You must define all of the table's columns and data types, and the table's primary key. (You can use the
ALTER TABLE statement to change these definitions later, if necessary.)
Many SQL implementations let you define storage specifications for your table, as part of the CREATE
TABLE statement. Unless you indicate otherwise, the table is created with default storage settings. In a
production environment, a database administrator can help determine the optimal storage parameters.
DynamoDB
Use the CreateTable action to create a table, specifying parameters as shown following:
{
    TableName : "Music",
    KeySchema: [
        {
            AttributeName: "Artist",
            KeyType: "HASH", //Partition key
        },
        {
            AttributeName: "SongTitle",
            KeyType: "RANGE" //Sort key
        }
    ],
    AttributeDefinitions: [
        {
            AttributeName: "Artist",
            AttributeType: "S"
        },
        {
            AttributeName: "SongTitle",
            AttributeType: "S"
        }
    ],
    ProvisionedThroughput: {
        ReadCapacityUnits: 1,
        WriteCapacityUnits: 1
    }
}
The primary key for this table consists of Artist (partition key) and SongTitle (sort key).
    Note
    For code samples using CreateTable, see Getting Started with DynamoDB (p. 52).
SQL
Most relational database management systems (RDBMS) allow you to describe a table's structure—
columns, data types, primary key definition, and so on. There is no standard way to do this in SQL.
However, many database systems provide a DESCRIBE command. Here is an example from MySQL:
DESCRIBE Music;
This returns the structure of your table, with all of the column names, data types, and sizes:
+------------+-------------+------+-----+---------+-------+
| Field      | Type        | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+-------+
| Artist     | varchar(20) | NO   | PRI | NULL    |       |
| SongTitle | varchar(30) | NO    | PRI | NULL    |       |
| AlbumTitle | varchar(25) | YES |      | NULL    |       |
| Year       | int(11)     | YES |      | NULL    |       |
| Price      | float       | YES |      | NULL    |       |
| Genre      | varchar(10) | YES |      | NULL    |       |
| Tags       | text        | YES |      | NULL    |       |
+------------+-------------+------+-----+---------+-------+
The primary key for this table consists of Artist and SongTitle.
DynamoDB
DynamoDB has a DescribeTable action, which is similar. The only parameter is the table name, as
shown following:
{
      TableName : "Music"
}
{
    "Table": {
      "AttributeDefinitions": [
         {
            "AttributeName": "Artist",
            "AttributeType": "S"
         },
         {
            "AttributeName": "SongTitle",
            "AttributeType": "S"
         }
      ],
      "TableName": "Music",
      "KeySchema": [
         {
            "AttributeName": "Artist",
            "KeyType": "HASH" //Partition key
       },
       {
           "AttributeName": "SongTitle",
           "KeyType": "RANGE" //Sort key
       }
    ],
...
DescribeTable also returns information about indexes on the table, provisioned throughput settings,
an approximate item count, and other metadata.
This section describes how to write one row (or item) to a table.
SQL
A table in a relational database is a two-dimensional data structure composed of rows and columns.
Some database management systems also provide support for semi-structured data, usually with native
JSON or XML data types. However, the implementation details vary among vendors.
The primary key for this table consists of Artist and SongTitle. You must specify values for these columns.
    Note
    In this example, we are using the Tags column to store semi-structured data about the songs in
    the Music table. We have defined the Tags column as type TEXT, which can store up to 65535
    characters in MySQL.
DynamoDB
In Amazon DynamoDB, you use the PutItem action to add an item to a table:
{
    TableName: "Music",
    Item: {
        "Artist":"No One You Know",
        "SongTitle":"Call Me Today",
        "AlbumTitle":"Somewhat Famous",
        "Year": 2015,
        "Price": 2.14,
        "Genre": "Country",
        "Tags": {
              "Composers": [
                    "Smith",
                    "Jones",
                    "Davis"
              ],
              "LengthInSeconds": 214
         }
    }
}
The primary key for this table consists of Artist and SongTitle. You must specify values for these
attributes.
Here are some key things to know about this PutItem example:
• DynamoDB provide native support for documents, using JSON. This makes DynamoDB ideal for storing
  semi-structured data, such as Tags. You can also retrieve and manipulate data from within JSON
  documents.
• The Music table does not have any predefined attributes, other than the primary key (Artist and
  SongTitle).
• Most SQL databases are transaction-oriented. When you issue an INSERT statement, the data
  modifications are not permanent until you issue a COMMIT statement. With Amazon DynamoDB, the
  effects of a PutItem action are permanent when DynamoDB replies with an HTTP 200 status code
  (OK).
    Note
    For code samples using PutItem, see Getting Started with DynamoDB (p. 52).
{
    TableName: "Music",
    Item: {
        "Artist": "No One You Know",
        "SongTitle": "My Dog Spot",
        "AlbumTitle":"Hey Now",
        "Price": 1.98,
        "Genre": "Country",
        "CriticRating": 8.4
    }
}
{
    TableName: "Music",
    Item: {
        "Artist": "No One You Know",
        "SongTitle": "Somewhere Down The Road",
        "AlbumTitle":"Somewhat Famous",
        "Genre": "Country",
        "CriticRating": 8.4,
        "Year": 1984
    }
}
{
    TableName: "Music",
    Item: {
        "Artist": "The Acme Band",
        "SongTitle": "Still In Love",
{
    TableName: "Music",
    Item: {
        "Artist": "The Acme Band",
        "SongTitle": "Look Out, World",
        "AlbumTitle":"The Buck Starts Here",
        "Price": 0.99,
        "Genre": "Rock"
    }
}
    Note
    In addition to PutItem, Amazon DynamoDB supports a BatchWriteItem action for writing
    multiple items at the same time.
• GetItem – Retrieves a single item from a table. This is the most efficient way to read a single item,
  because it provides direct access to the physical location of the item. (DynamoDB also provides
  BatchGetItem operation, allowing you to perform up to 100 GetItem calls in a single operation.)
• Query – Retrieves all of the items that have a specific partition key. Within those items, you can apply
  a condition to the sort key and retrieve only a subset of the data. Query provides quick, efficient
  access to the partitions where the data is stored. (For more information, see Partitions and Data
  Distribution (p. 18).)
• Scan – Retrieves all of the items in the specified table. (This operation should not be used with large
  tables, because it can consume large amounts of system resources.)
    Note
    With a relational database, you can use the SELECT statement to join data from multiple
    tables and return the results. Joins are fundamental to the relational model. To ensure that
    joins execute efficiently, the database and its applications should be performance-tuned on an
    ongoing basis.
    DynamoDB is a non-relational NoSQL database, and does not support table joins. Instead,
    applications read data from one table at a time.
The following sections describe different use cases for reading data, and how to perform these tasks with
a relational database and with DynamoDB.
Topics
 • Reading an Item Using Its Primary Key (p. 30)
 • Querying a Table (p. 31)
    • Scanning a Table (p. 33)
SQL
In SQL, you use the SELECT statement to retrieve data from a table. You can request one or more
columns in the result (or all of them, if you use the * operator). The WHERE clause determines which
row(s) to return.
The following is a SELECT statement to retrieve a single row from the Music table. The WHERE clause
specifies the primary key values.
SELECT *
FROM Music
WHERE Artist='No One You Know' AND SongTitle = 'Call Me Today'
You can modify this query to retrieve only a subset of the columns:
Note that the primary key for this table consists of Artist and SongTitle.
DynamoDB
DynamoDB provides the GetItem action for retrieving an item by its primary key. GetItem is highly
efficient because it provides direct access to the physical location of the item. (For more information, see
Partitions and Data Distribution (p. 18).)
By default, GetItem returns the entire item with all of its attributes.
{
      TableName: "Music",
      Key: {
          "Artist": "No One You Know",
          "SongTitle": "Call Me Today"
      }
}
You can add a ProjectionExpression parameter to return only some of the attributes:
{
      TableName: "Music",
      Key: {
          "Artist": "No One You Know",
          "SongTitle": "Call Me Today"
      },
      "ProjectionExpression": "AlbumTitle, Year, Price"
Note that the primary key for this table consists of Artist and SongTitle.
The DynamoDB GetItem action is very efficient: It uses the primary key value(s) to determine the exact
storage location of the item in question, and retrieves it directly from there. The SQL SELECT statement
is similarly efficient, in the case of retrieving items by primary key values.
The SQL SELECT statement supports many kinds of queries and table scans. DynamoDB provides similar
functionality with its Query and Scan actions, which are described in Querying a Table (p. 31) and
Scanning a Table (p. 33).
The SQL SELECT statement can perform table joins, allowing you to retrieve data from multiple
tables at the same time. Joins are most effective where the database tables are normalized and the
relationships among the tables are clear. However, if you join too many tables in one SELECT statement
application performance can be affected. You can work around such issues by using database replication,
materialized views, or query rewrites.
DynamoDB is a non-relational database. As such, it does not support table joins. If you are migrating an
existing application from a relational database to DynamoDB, you need to denormalize your data model
to eliminate the need for joins.
    Note
    For code samples using GetItem, see Getting Started with DynamoDB (p. 52).
Querying a Table
Another common access pattern is reading multiple items from a table, based on your query criteria.
SQL
The SQL SELECT statement lets you query on key columns, non-key columns, or any combination. The
WHERE clause determines which rows are returned, as shown in the following examples:
/* Return all of the songs by an artist, with a particular word in the title...
...but only if the price is less than 1.00 */
Note that the primary key for this table consists of Artist and SongTitle.
DynamoDB
The DynamoDB Query action lets you retrieve data in a similar fashion. The Query action provides quick,
efficient access to the physical locations where the data is stored. (For more information, see Partitions
and Data Distribution (p. 18).)
You can use Query with any table that has a composite primary key (partition key and sort key).
You must specify an equality condition for the partition key, and you can optionally provide another
condition for the sort key.
The KeyConditionExpression parameter specifies the key values that you want to query. You can use
an optional FilterExpression to remove certain items from the results before they are returned to
you.
Note that the primary key for this table consists of Artist and SongTitle.
{
    TableName: "Music",
    KeyConditionExpression: "Artist = :a and SongTitle = :t",
    ExpressionAttributeValues: {
        ":a": "No One You Know",
        ":t": "Call Me Today"
    }
}
{
    TableName: "Music",
    KeyConditionExpression: "Artist = :a",
    ExpressionAttributeValues: {
        ":a": "No One You Know"
    }
}
{
    TableName: "Music",
    KeyConditionExpression: "Artist = :a and begins_with(SongTitle, :t)",
    ExpressionAttributeValues: {
        ":a": "No One You Know",
        ":t": "Call"
    }
}
// Return all of the songs by an artist, with a particular word in the title...
// ...but only if the price is less than 1.00
    TableName: "Music",
    KeyConditionExpression: "Artist = :a and contains(SongTitle, :t)",
    FilterExpression: "price < :p",
    ExpressionAttributeValues: {
        ":a": "No One You Know",
        ":t": "Today",
        ":p": 1.00
    }
}
    Note
    For code samples using Query, see Getting Started with DynamoDB (p. 52).
Scanning a Table
In SQL, a SELECT statement without a WHERE clause will return every row in a table. In DynamoDB,
the Scan operation does the same thing. In both cases, you can retrieve all of the items, or just some of
them.
Whether you are using a SQL or a NoSQL database, scans should be used sparingly because they can
consume large amounts of system resources. Sometimes a scan is appropriate (such as scanning a small
table) or unavoidable (such as performing a bulk export of data). However, as a general rule, you should
design your applications to avoid performing scans.
SQL
In SQL, you can scan a table and retrieve all of its data by using a SELECT statement without specifying
a WHERE clause. You can request one or more columns in the result. Or, you can request all of them if you
use the wildcard character (*).
DynamoDB
DynamoDB provides a Scan action that works in a similar way. Here are some examples:
The Scan action also provides a FilterExpression parameter, to discard items that you do not want
to appear in the results. A FilterExpression is applied after the entire table is scanned, but before
the results are returned to you. (This is not recommended with large tables: You are still charged for the
entire Scan, even if only a few matching items are returned.)
    Note
    For code samples that use Scan, see Getting Started with DynamoDB (p. 52).
Managing Indexes
Indexes give you access to alternate query patterns, and can speed up queries. This section compares and
contrasts index creation and usage in SQL and DynamoDB.
Whether you are using a relational database or DynamoDB, you should be judicious with index creation.
Whenever a write occurs on a table, all of the table's indexes must be updated. In a write-heavy
environment with large tables, this can consume large amounts of system resources. In a read-only or
read-mostly environment, this is not as much of a concern—however, you should ensure that the indexes
are actually being used by your application, and not simply taking up space.
Topics
 • Creating an Index (p. 34)
 • Querying and Scanning an Index (p. 35)
Creating an Index
SQL
In a relational database, an index is a data structure that let you perform fast queries on different
columns in a table. You can use the CREATE INDEX SQL statement to add an index to an existing table,
specifying the columns to be indexed. After the index has been created, you can query the data in the
table as usual, but now the database can use the index to quickly find the specified rows in the table
instead of scanning the entire table.
After you create an index, the database maintains it for you. Whenever you modify data in the table, the
index is automatically modified to reflect changes in the table.
DynamoDB
In DynamoDB, you can create and use a secondary index for similar purposes.
Indexes in DynamoDB are different from their relational counterparts. When you create a secondary
index, you must specify its key attributes – a partition key and a sort key. After you create the secondary
index, you can Query it or Scan it just as you would with a table. DynamoDB does not have a query
optimizer, so a secondary index is only used when you Query it or Scan it.
• Global secondary indexes – The primary key of the index can be any two attributes from its table.
• Local secondary indexes – The partition key of the index must be the same as the partition key of its
  table. However, the sort key can be any other attribute.
DynamoDB ensures that the data in a secondary index is eventually consistent with its table. You can
request strongly consistent Query or Scan actions on a table or a local secondary index. However, global
secondary indexes only support eventual consistency.
You can add a global secondary index to an existing table, using the UpdateTable action and specifying
GlobalSecondaryIndexUpdates:
{
      TableName: "Music",
      AttributeDefinitions:[
          {AttributeName: "Genre", AttributeType: "S"},
          {AttributeName: "Price", AttributeType: "N"}
      ],
      GlobalSecondaryIndexUpdates: [
          {
              Create: {
                  IndexName: "GenreAndPriceIndex",
                  KeySchema: [
                      {AttributeName: "Genre", KeyType: "HASH"}, //Partition key
                      {AttributeName: "Price", KeyType: "RANGE"}, //Sort key
                  ],
                  Projection: {
                      "ProjectionType": "ALL"
                  },
                  ProvisionedThroughput: {
                      "ReadCapacityUnits": 1,"WriteCapacityUnits": 1
                  }
              }
          }
      ]
}
Part of this operation involves backfilling data from the table into the new index. During backfilling, the
table remains available. However, the index is not ready until its Backfilling attribute changes from
true to false. You can use the DescribeTable action to view this attribute.
      Note
      For code samples that use UpdateTable, see Getting Started with DynamoDB (p. 52).
A query optimizer is a relational database management systems (RDBMS) component that evaluates the
available indexes, and determines whether they can be used to speed up a query. If the indexes can be
used to speed up a query, the RDBMS accesses the index first and then uses it to locate the data in the
table.
Here are some SQL statements that can use GenreAndPriceIndex to improve performance. We assume
that the Music table has enough data in it that the query optimizer decides to use this index, rather than
simply scanning the entire table.
DynamoDB
In DynamoDB, you perform Query operations directly on the index, in the same way that you would do
so on a table. You must specify both TableName and IndexName.
The following are some queries on GenreAndPriceIndex in DynamoDB. (The key schema for this index
consists of Genre and Price.)
{
     TableName: "Music",
     IndexName: "GenreAndPriceIndex",
     KeyConditionExpression: "Genre = :genre",
     ExpressionAttributeValues: {
         ":genre": "Rock"
     },
};
{
     TableName: "Music",
     IndexName: "GenreAndPriceIndex",
     KeyConditionExpression: "Genre = :genre and Price < :price",
     ExpressionAttributeValues: {
         ":genre": "Country",
         ":price": 0.50
     },
     ProjectionExpression: "Artist, SongTitle, Price"
};
This example uses a ProjectionExpression to indicate that we only want some of the attributes,
rather than all of them, to appear in the results.
You can also perform Scan operations on a secondary index, in the same way that you would do so on a
table. The following is a scan on GenreAndPriceIndex:
{
     TableName: "Music",
     IndexName: "GenreAndPriceIndex"
}
SQL
In SQL, you use the UPDATE statement to modify one or more rows. The SET clause specifies new values
for one or more columns, and the WHERE clause determines which rows are modified. Here is an example:
UPDATE Music
SET RecordLabel = 'Global Records'
WHERE Artist = 'No One You Know' AND SongTitle = 'Call Me Today';
If no rows match the WHERE clause, the UPDATE statement has no effect.
DynamoDB
In DynamoDB, you use the UpdateItem action to modify a single item. (If you want to modify multiple
items, you must use multiple UpdateItem operations.)
Here is an example:
{
    TableName: "Music",
    Key: {
        "Artist":"No One You Know",
        "SongTitle":"Call Me Today"
    },
    UpdateExpression: "SET RecordLabel = :label",
    ExpressionAttributeValues: {
        ":label": "Global Records"
    }
}
You must specify the Key attributes of the item to be modified, and an UpdateExpression to specify
attribute values.
UpdateItem replaces the entire item, rather than replacing individual attributes.
UpdateItem behaves like an "upsert" operation: The item is updated if it exists in the table, but if not, a
new item is added (inserted).
UpdateItem supports conditional writes, where the operation succeeds only if a specific
ConditionExpression evaluates to true. For example, the following UpdateItem action does not
perform the update unless the price of the song is greater than or equal to 2.00:
{
    TableName: "Music",
    Key: {
        "Artist":"No One You Know",
        "SongTitle":"Call Me Today"
    },
    UpdateExpression: "SET RecordLabel = :label",
    ConditionExpression: "Price >= :p",
    ExpressionAttributeValues: {
        ":label": "Global Records",
        ":p": 2.00
    }
}
UpdateItem also supports atomic counters, or attributes of type Number that can be incremented or
decremented. Atomic counters are similar in many ways to sequence generators, identity columns, or
auto-increment fields in SQL databases.
The following is an example of an UpdateItem action to initialize a new attribute (Plays) to keep track
of the number of times a song has been played:
{
    TableName: "Music",
    Key: {
        "Artist":"No One You Know",
        "SongTitle":"Call Me Today"
    },
    UpdateExpression: "SET Plays = :val",
    ExpressionAttributeValues: {
        ":val": 0
    },
    ReturnValues: "UPDATED_NEW"
}
The ReturnValues parameter is set to UPDATED_NEW, which returns the new values of any attributes
that were updated. In this case, it returns 0 (zero).
Whenever someone plays this song, we can use the following UpdateItem action to increment Plays by
one:
{
    TableName: "Music",
    Key: {
        "Artist":"No One You Know",
        "SongTitle":"Call Me Today"
    },
    UpdateExpression: "SET Plays = Plays + :incr",
    ExpressionAttributeValues: {
        ":incr": 1
    },
    ReturnValues: "UPDATED_NEW"
}
    Note
    For code samples using UpdateItem, see Getting Started with DynamoDB (p. 52).
SQL
In SQL, you use the DELETE statement to delete one or more rows. The WHERE clause determines the
rows that you want to modify. Here is an example:
You can modify the WHERE clause to delete multiple rows. For example, you could delete all of the songs
by a particular artist, as shown following:
    Note
    If you omit the WHERE clause, the database attempts to delete all of the rows from the table.
DynamoDB
In DynamoDB, you use the DeleteItem action to delete data from a table, one item at a time. You must
specify the item's primary key values. Here is an example:
{
     TableName: "Music",
     Key: {
         Artist: "The Acme Band",
         SongTitle: "Look Out, World"
     }
}
    Note
    In addition to DeleteItem, Amazon DynamoDB supports a BatchWriteItem action for
    deleting multiple items at the same time.
DeleteItem supports conditional writes, where the operation succeeds only if a specific
ConditionExpression evaluates to true. For example, the following DeleteItem action deletes the
item only if it has a RecordLabel attribute:
{
     TableName: "Music",
     Key: {
         Artist: "The Acme Band",
         SongTitle: "Look Out, World"
     },
    ConditionExpression: "attribute_exists(RecordLabel)"
}
    Note
    For code samples using DeleteItem, see Getting Started with DynamoDB (p. 52).
Removing a Table
In SQL, you use the DROP TABLE statement to remove a table. In DynamoDB, you use the DeleteTable
operation.
SQL
When you no longer need a table and want to discard it permanently, you use the DROP TABLE
statement in SQL:
After a table is dropped, it cannot be recovered. (Some relational databases do allow you to undo a DROP
TABLE operation, but this is vendor-specific functionality and it is not widely implemented.)
DynamoDB
DynamoDB has a similar action: DeleteTable. In the following example, the table is permanently
deleted:
{
    TableName: "Music"
}
    Note
    For code samples using DeleteTable, see Getting Started with DynamoDB (p. 52).
Setting Up DynamoDB
    In addition to the Amazon DynamoDB web service, AWS provides a downloadable version of DynamoDB
    that you can run on your computer. The downloadable version lets you write and test applications locally
    without accessing the DynamoDB web service.
    The topics in this section describe how to set up DynamoDB (Downloadable Version) and the DynamoDB
    web service).
    Topics
     • Setting Up DynamoDB Local (Downloadable Version) (p. 41)
     • Setting Up DynamoDB (Web Service) (p. 45)
    Having this local version helps you save on provisioned throughput, data storage, and data transfer fees.
    In addition, you don't need an Internet connection while you're developing your application.
    Topics
     • Downloading and Running DynamoDB on Your Computer (p. 41)
     • Setting the Local Endpoint (p. 44)
     • Usage Notes (p. 44)
     Downloadable DynamoDB is available on Apache Maven. For more information, see DynamoDB
     (Downloadable Version) and Apache Maven (p. 42), later in this topic. DynamoDB is also available
     as part of the AWS Toolkit for Eclipse. For more information, see AWS Toolkit For Eclipse.
         Important
         To run DynamoDB on your computer, you must have the Java Runtime Environment (JRE)
         version 6.x or newer. The application doesn't run on older JRE versions.
2.   After you download the archive, extract the contents and copy the extracted directory to a location
     of your choice.
3.   To start DynamoDB on your computer, open a command prompt window, navigate to the directory
     where you extracted DynamoDBLocal.jar, and type the following command:
         Note
         DynamoDB processes incoming requests until you stop it. To stop DynamoDB, type Ctrl+C at
         the command prompt.
         DynamoDB uses port 8000 by default. If port 8000 is unavailable, this command throws an
         exception. For a complete list of DynamoDB runtime options, including -port , type this
         command:
         java -Djava.library.path=./DynamoDBLocal_lib -jar DynamoDBLocal.jar -
         help
1.   Download and install Apache Maven. For more information, see Downloading Apache Maven and
     Installing Apache Maven.
2.   Add the DynamoDB Maven repository to your application's Project Object Model (POM) file:
     <!--Dependency:-->
     <dependencies>
         <dependency>
            <groupId>com.amazonaws</groupId>
            <artifactId>DynamoDBLocal</artifactId>
            <version>[1.11,2.0)</version>
         </dependency>
     </dependencies>
     <!--Custom repository:-->
     <repositories>
         <repository>
            <id>dynamodb-local-oregon</id>
            <name>DynamoDB Local Release Repository</name>
            <url>https://s3-us-west-2.amazonaws.com/dynamodb-local/release</url>
         </repository>
     </repositories>
        Note
        Alternatively, you can use one of the following repository URLs, depending on your region:
id Repository URL
         dynamodb-local-mumbai                        https://s3.ap-south-1.amazonaws.com/
                                                      dynamodb-local-mumbai/release
         dynamodb-local-singapore                     https://s3-ap-
                                                      southeast-1.amazonaws.com/dynamodb-
                                                      local-singapore/release
         dynamodb-local-tokyo                         https://s3-ap-
                                                      northeast-1.amazonaws.com/dynamodb-
                                                      local-tokyo/release
         dynamodb-local-frankfurt                     https://s3.eu-central-1.amazonaws.com/
                                                      dynamodb-local-frankfurt/release
         dynamodb-local-sao-paulo                     https://s3-sa-east-1.amazonaws.com/
                                                      dynamodb-local-sao-paulo/release
The aws-dynamodb-examples repository in GitHub contains examples for starting and stopping
DynamoDB Local inside a Java program and using DynamoDB Local in JUnit tests.
• -cors value — Enables support for cross-origin resource sharing (CORS) for JavaScript. You must
  provide a comma-separated "allow" list of specific domains. The default setting for -cors is an
  asterisk (*), which allows public access.
• -dbPath value — The directory where DynamoDB writes its database file. If you don't specify this
  option, the file is written to the current directory. You can't specify both -dbPath and -inMemory at
  once.
• -delayTransientStatuses — Causes DynamoDB to introduce delays for certain operations.
  DynamoDB (Downloadable Version) can perform some tasks almost instantaneously, such as create/
  update/delete operations on tables and indexes. However, the DynamoDB service requires more time
  for these tasks. Setting this parameter helps DynamoDB running on your computer simulate the
  behavior of the DynamoDB web service more closely. (Currently, this parameter introduces delays only
  for global secondary indexes that are in either CREATING or DELETING status.)
• -help — Prints a usage summary and options.
• -inMemory — DynamoDB runs in memory instead of using a database file. When you stop
  DynamoDB, none of the data is saved. You can't specify both -dbPath and -inMemory at once.
• -optimizeDbBeforeStartup — Optimizes the underlying database tables before starting
  DynamoDB on your computer. You also must specify -dbPath when you use this parameter.
• -port value — The port number that DynamoDB uses to communicate with your application. If you
  don't specify this option, the default port is 8000.
      Note
      DynamoDB uses port 8000 by default. If port 8000 is unavailable, this command throws an
      exception. You can use the -port option to specify a different port number. For a complete
      list of DynamoDB runtime options, including -port , type this command:
      java -Djava.library.path=./DynamoDBLocal_lib -jar DynamoDBLocal.jar -
      help
• -sharedDb — DynamoDB uses a single database file instead of separate files for each credential and
  region. If you specify -sharedDb, all DynamoDB clients interact with the same set of tables regardless
  of their region and credential configuration.
http://localhost:8000
To access DynamoDB running locally, use the --endpoint-url parameter. The following is an example
of using the AWS CLI to list the tables in DynamoDB on your computer:
    Note
    The AWS CLI can't use the downloadable version of DynamoDB as a default endpoint; therefore,
    you must specify --endpoint-url with each AWS CLI command.
AWS SDKs
The way you specify an endpoint depends on the programming language and AWS SDK you're using. The
following sections describe how to do this:
    Note
    For examples in other programming languages, see Getting Started with DynamoDB (p. 52).
Usage Notes
Except for the endpoint, applications that run with the downloadable version of DynamoDB should also
work with the DynamoDB web service. However, when using DynamoDB locally, you should be aware of
the following:
• If you use the -sharedDb option, DynamoDB creates a single database file named shared-local-
  instance.db. Every program that connects to DynamoDB accesses this file. If you delete the file, you
  lose any data you have stored in it.
• If you omit -sharedDb, the database file is named myaccesskeyid_region.db, with the AWS access key
  ID and region as they appear in your application configuration. If you delete the file, you lose any data
  you have stored in it.
• If you use the -inMemory option, DynamoDB doesn't write any database files at all. Instead, all data is
  written to memory, and the data is not saved when you terminate DynamoDB.
• If you use the -optimizeDbBeforeStartup option, you must also specify the -dbPath parameter
  so that DynamoDB can find its database file.
    • The AWS SDKs for DynamoDB require that your application configuration specify an access key value
      and an AWS region value. Unless you're using the -sharedDb or the -inMemory option, DynamoDB
      uses these values to name the local database file.
      These values don't have to be valid AWS values to run locally. However, you might find it convenient
      to use valid values so that you can run your code in the cloud later simply by changing the endpoint
      you're using.
The downloadable version of DynamoDB differs from the web service in the following ways:
    • Regions and distinct AWS accounts are not supported at the client level.
    • Provisioned throughput settings are ignored in downloadable DynamoDB, even though the
      CreateTable operation requires them. For CreateTable, you can specify any numbers you want
      for provisioned read and write throughput, even though these numbers are not used. You can call
      UpdateTable as many times as you want per day. However, any changes to provisioned throughput
      values are ignored.
    • Scan operations are performed sequentially. Parallel scans are not supported. The Segment and
      TotalSegments parameters of the Scan operation are ignored.
    • The speed of read and write operations on table data is limited only by the speed of your computer.
      CreateTable, UpdateTable, and DeleteTable operations occur immediately, and table state is
      always ACTIVE. UpdateTable operations that change only the provisioned throughput settings on
      tables or global secondary indexes occur immediately. If an UpdateTable operation creates or deletes
      any global secondary indexes, then those indexes transition through normal states (such as CREATING
      and DELETING, respectively) before they become an ACTIVE state. The table remains ACTIVE during
      this time.
    • Read operations are eventually consistent. However, due to the speed of DynamoDB running on your
      computer, most reads appear to be strongly consistent.
    • Consumed capacity units are not tracked. In operation responses, nulls are returned instead of capacity
      units.
    • Item collection metrics and item collection sizes are not tracked. In operation responses, nulls are
      returned instead of item collection metrics.
    • In DynamoDB, there is a 1 MB limit on data returned per result set. Both the DynamoDB web service
      and the downloadable version enforce this limit. However, when querying an index, the DynamoDB
      service calculates only the size of the projected key and attributes. By contrast, the downloadable
      version of DynamoDB calculates the size of the entire item.
    • If you're using DynamoDB Streams, the rate at which shards are created might differ. In the DynamoDB
      web service, shard creation behavior is partially influenced by table partition activity. When you
      run DynamoDB locally, there is no table partitioning. In either case, shards are ephemeral, so your
      application should not be dependent on shard behavior.
2. Get an AWS access key (p. 46) (used to access DynamoDB programmatically).
       Note
       If you plan to interact with DynamoDB only through the AWS Management Console, you
       don't need an AWS access key, and you can skip ahead to Using the Console (p. 48).
3. Configure your credentials (p. 47) (used to access DynamoDB programmatically).
     Part of the sign-up procedure involves receiving a phone call and entering a PIN using the phone
     keypad.
To get the access key ID and secret access key for an IAM user
Access keys consist of an access key ID and secret access key, which are used to sign programmatic
requests that you make to AWS. If you don't have access keys, you can create them from the AWS
Management Console. We recommend that you use IAM access keys instead of AWS account root user
access keys. IAM lets you securely control access to AWS services and resources in your AWS account.
The only time that you can view or download the secret access keys is when you create the keys. You
cannot recover them later. However, you can create new access keys at any time. You must also have
permissions to perform the required IAM actions. For more information, see Permissions Required to
Access IAM Resources in the IAM User Guide.
    Keep the keys confidential in order to protect your AWS account, and never email them. Do not
    share them outside your organization, even if an inquiry appears to come from AWS or Amazon.com.
    No one who legitimately represents Amazon will ever ask you for your secret key.
Related topics
There are several ways to do this. For example, you can manually create the credentials file to store your
AWS access key ID and secret access key. You can also use the aws configure command of the AWS CLI
to automatically create the file. Alternatively, you can use environment variables. For more information
on configuring your credentials, see the programming-specific AWS SDK developer guide.
To install and configure the AWS CLI, see Using the CLI (p. 49).
Accessing DynamoDB
    You can access Amazon DynamoDB using the AWS Management Console, the AWS Command Line
    Interface (AWS CLI), or the DynamoDB API.
    Topics
        • Using the Console (p. 48)
        • Using the CLI (p. 49)
        • Using the API (p. 50)
    • Monitor recent alerts, total capacity, service health, and the latest DynamoDB news on the DynamoDB
      dashboard.
    • Create, update, and delete tables. The capacity calculator provides estimates of how many capacity
      units to request based on the usage information you provide.
    • Manage streams.
    • View, add, update, and delete items that are stored in tables. Manage Time To Live (TTL) to define
      when items in a table expire so that they can be automatically deleted from the database.
    • Query and scan a table.
    • Set up and view alarms to monitor your table's capacity usage. View your table's top monitoring
      metrics on real-time graphs from CloudWatch.
    • Modify a table's provisioned capacity.
    • Create and delete global secondary indexes.
    • Create triggers to connect DynamoDB streams to AWS Lambda functions.
    • Apply tags to your resources to help organize and identify them.
    • Purchase reserved capacity.
    The console displays an introductory screen that prompts you to create your first table. To view your
    tables, from the navigation pane on the left side of the console, choose Tables.
Here's a high-level overview of the actions available per table within each navigation tab:
    • Overview – View stream and table details, and manage streams and Time To Live (TTL).
    •   Items – Manage items and perform queries and scans.
    •   Metrics – Monitor CloudWatch metrics.
    •   Alarms – Manage CloudWatch alarms.
    •   Capacity – Modify a table's provisioned capacity.
    •   Indexes – Manage global secondary indexes.
    • Triggers – Manage triggers to connect DynamoDB streams to Lambda functions.
    • Access control – Set up fine-grained access control with web identity federation.
    • Tags – Apply tags to your resources to help organize and identify them.
    Before you can use the AWS CLI with DynamoDB, you must get an access key ID and secret access key. For
    more information, see Getting an AWS Access Key (p. 46).
    For a complete listing of all the commands available for DynamoDB in the AWS CLI, go to http://
    docs.aws.amazon.com/cli/latest/reference/dynamodb/index.html.
    Topics
     • Downloading and Configuring the AWS CLI (p. 49)
     • Using the AWS CLI with DynamoDB (p. 49)
     • Using the AWS CLI with Downloadable DynamoDB (p. 50)
    For example, the following command creates a table named Music. The partition key is Artist, and the
    sort key is SongTitle. (For easier readability, long commands in this section are broken into separate
    lines.)
    The following commands add new items to the table. These examples use a combination of shorthand
    syntax and JSON.
        --item '{ \
            "Artist": {"S": "Acme Band"}, \
            "SongTitle": {"S": "Happy Day"}, \
            "AlbumTitle": {"S": "Songs About Life"} }' \
        --return-consumed-capacity TOTAL
    On the command line, it can be difficult to compose valid JSON. However, the AWS CLI can read
    JSON files. For example, consider the following JSON snippet, which is stored in a file named key-
    conditions.json:
    {
        "Artist": {
            "AttributeValueList": [
                {
                    "S": "No One You Know"
                }
            ],
            "ComparisonOperator": "EQ"
        },
        "SongTitle": {
            "AttributeValueList": [
                {
                    "S": "Call Me Today"
                }
            ],
            "ComparisonOperator": "EQ"
        }
    }
    You can now issue a Query request using the AWS CLI. In this example, the contents of the key-
    conditions.json file are used for the --key-conditions parameter:
--endpoint-url http://localhost:8000
Here is an example that uses the AWS CLI to list the tables in a local database:
    If DynamoDB is using a port number other than the default (8000), modify the --endpoint-url value
    accordingly.
        Note
        The AWS CLI can't use the downloadable version of DynamoDB as a default endpoint. Therefore,
        you must specify --endpoint-url with each command.
The AWS SDKs provide broad support for DynamoDB in Java, JavaScript in the browser, .NET, Node.js,
PHP, Python, Ruby, C++, Go, Android, and iOS. To get started quickly with these languages, see Getting
Started with DynamoDB (p. 52).
Before you can use the AWS SDKs with DynamoDB, you must get an AWS access key ID and secret access
key. For more information, see Setting Up DynamoDB (Web Service) (p. 45).
For a high-level overview of DynamoDB application programming with the AWS SDKs, see Programming
with DynamoDB and the AWS SDKs (p. 180).
    Topics
     • Java and DynamoDB (p. 52)
     • JavaScript and DynamoDB (p. 69)
     • Node.js and DynamoDB (p. 89)
     • .NET and DynamoDB (p. 103)
     • PHP and DynamoDB (p. 132)
     • Python and DynamoDB (p. 149)
     • Ruby and DynamoDB (p. 164)
    • Create a table called Movies and load sample data in JSON format.
    • Perform create, read, update, and delete operations on the table.
    • Run simple queries.
    The SDK for Java offers several programming models for different use cases. In this exercise, the Java
    code uses the document model that provides a level of abstraction that makes it easier for you to work
    with JSON documents.
As you work through this tutorial, you can refer to the AWS SDK for Java API Reference.
    Tutorial Prerequisites
    • Download and run DynamoDB on your computer. For more information, see Setting Up DynamoDB
      Local (Downloadable Version) (p. 41).
      DynamoDB (Downloadable Version) is also available as part of the AWS Toolkit for Eclipse. For more
      information, see AWS Toolkit For Eclipse.
          Note
          You use the downloadable version of DynamoDB in this tutorial. For information about how to
          run the same code against the DynamoDB web service, see the Summary (p. 68).
• Set up an AWS access key to use the AWS SDKs. For more information, see Setting Up DynamoDB (Web
  Service) (p. 45).
• Set up the AWS SDK for Java:
     • Install a Java development environment. If you are using the Eclipse IDE, install the AWS Toolkit for
       Eclipse.
     • Install the AWS SDK for Java.
     • Set up your AWS security credentials for use with the SDK for Java.
For instructions, see Getting Started in the AWS SDK for Java Developer Guide.
1. Copy and paste the following program into your Java development environment:
package com.amazonaws.codesamples.gsg;
import java.util.Arrays;
       import   com.amazonaws.client.builder.AwsClientBuilder;
       import   com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
       import   com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder;
       import   com.amazonaws.services.dynamodbv2.document.DynamoDB;
       import   com.amazonaws.services.dynamodbv2.document.Table;
       import   com.amazonaws.services.dynamodbv2.model.AttributeDefinition;
       import   com.amazonaws.services.dynamodbv2.model.KeySchemaElement;
       import   com.amazonaws.services.dynamodbv2.model.KeyType;
       import   com.amazonaws.services.dynamodbv2.model.ProvisionedThroughput;
       import   com.amazonaws.services.dynamodbv2.model.ScalarAttributeType;
                 try {
                     System.out.println("Attempting to create table; please wait...");
                     Table table = dynamoDB.createTable(tableName,
                         Arrays.asList(new KeySchemaElement("year", KeyType.HASH), // Partition
                                                                                   // key
                             new KeySchemaElement("title", KeyType.RANGE)), // Sort key
                         Arrays.asList(new AttributeDefinition("year", ScalarAttributeType.N),
                             new AttributeDefinition("title", ScalarAttributeType.S)),
                }
                catch (Exception e) {
                    System.err.println("Unable to create table: ");
                    System.err.println(e.getMessage());
                }
            }
      }
Note
            • You set the endpoint to indicate that you are creating the table in DynamoDB on your
              computer.
            • In the createTable call, you specify table name, primary key attributes, and its data
              types.
            • The ProvisionedThroughput parameter is required, but the downloadable version of
              DynamoDB ignores it. (Provisioned throughput is beyond the scope of this exercise.)
2.    Compile and run the program.
To learn more about managing tables, see Working with Tables in DynamoDB (p. 291).
Topics
    • Step 2.1: Download the Sample Data File (p. 55)
    • Step 2.2: Load the Sample Data into the Movies Table (p. 55)
This scenario uses a sample data file that contains information about a few thousand movies from the
Internet Movie Database (IMDb). The movie data is in JSON format, as shown in the following example.
For each movie, there is a year, a title, and a JSON map named info.
[
     {
          "year" : ... ,
          "title" : ... ,
          "info" : { ... }
     },
     {
          "year" : ...,
          "title" : ...,
          "info" : { ... }
     },
...
• You use the year and title as the primary key attribute values for the Movies table.
• You store the rest of the info values in a single attribute called info. This program illustrates how
  you can store JSON in a DynamoDB attribute.
{
    "year" : 2013,
    "title" : "Turn It Down, Or Else!",
    "info" : {
         "directors" : [
             "Alice Smith",
             "Bob Jones"
         ],
         "release_date" : "2013-01-18T00:00:00Z",
         "rating" : 6.2,
         "genres" : [
             "Comedy",
             "Drama"
         ],
         "image_url" : "http://ia.media-imdb.com/images/N/
O9ERWAU7FS797AJ7LU8HN09AMUP908RLlo5JF90EWR7LJKQ7@@._V1_SX400_.jpg",
         "plot" : "A rock band plays their music at high volumes, annoying the neighbors.",
         "rank" : 11,
         "running_time_secs" : 5215,
         "actors" : [
             "David Matthewman",
             "Ann Thomas",
             "Jonathan G. Neff"
       ]
    }
}
Step 2.2: Load the Sample Data into the Movies Table
After you download the sample data, you can run the following program to populate the Movies table.
1. Copy and paste the following program into your Java development environment:
package com.amazonaws.codesamples.gsg;
     import java.io.File;
     import java.util.Iterator;
     import   com.amazonaws.client.builder.AwsClientBuilder;
     import   com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
     import   com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder;
     import   com.amazonaws.services.dynamodbv2.document.DynamoDB;
     import   com.amazonaws.services.dynamodbv2.document.Item;
     import   com.amazonaws.services.dynamodbv2.document.Table;
     import   com.fasterxml.jackson.core.JsonFactory;
     import   com.fasterxml.jackson.core.JsonParser;
     import   com.fasterxml.jackson.databind.JsonNode;
     import   com.fasterxml.jackson.databind.ObjectMapper;
     import   com.fasterxml.jackson.databind.node.ObjectNode;
ObjectNode currentNode;
               while (iter.hasNext()) {
                   currentNode = (ObjectNode) iter.next();
                 try {
                     table.putItem(new Item().withPrimaryKey("year", year, "title",
      title).withJSON("info",
                         currentNode.path("info").toString()));
                     System.out.println("PutItem succeeded: " + year + " " + title);
                   }
                   catch (Exception e) {
                       System.err.println("Unable to add movie: " + year + " " + title);
                       System.err.println(e.getMessage());
                       break;
                   }
               }
               parser.close();
         }
     }
     This program uses the open source Jackson library to process JSON. Jackson is included in the AWS
     SDK for Java. You don't have to install it separately.
2.   Compile and run the program.
To learn more about reading and writing data, see Working with Items in DynamoDB (p. 326).
Topics
 • Step 3.1: Create a New Item (p. 57)
 • Step 3.2: Read an Item (p. 58)
1. Copy and paste the following program into your Java development environment.
package com.amazonaws.codesamples.gsg;
     import java.util.HashMap;
     import java.util.Map;
     import   com.amazonaws.client.builder.AwsClientBuilder;
     import   com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
     import   com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder;
     import   com.amazonaws.services.dynamodbv2.document.DynamoDB;
     import   com.amazonaws.services.dynamodbv2.document.Item;
     import   com.amazonaws.services.dynamodbv2.document.PutItemOutcome;
     import   com.amazonaws.services.dynamodbv2.document.Table;
             try {
                 System.out.println("Adding a new item...");
                 PutItemOutcome outcome = table
                     .putItem(new Item().withPrimaryKey("year", year, "title",
      title).withMap("info", infoMap));
               }
               catch (Exception e) {
                   System.err.println("Unable to add item: " + year + " " + title);
                   System.err.println(e.getMessage());
               }
         Note
         The primary key is required. This code adds an item that has primary key (year, title) and
         info attributes. The info attribute stores sample JSON that provides more information
         about the movie.
2.   Compile and run the program.
{
     year: 2015,
     title: "The Big New Movie",
     info: {
          plot: "Nothing happens at all.",
          rating: 0
     }
}
You can use the getItem method to read the item from the Movies table. You must specify the primary
key values, so you can read any item from Movies if you know its year and title.
1. Copy and paste the following program into your Java development environment:
package com.amazonaws.codesamples.gsg;
     import   com.amazonaws.client.builder.AwsClientBuilder;
     import   com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
     import   com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder;
     import   com.amazonaws.services.dynamodbv2.document.DynamoDB;
     import   com.amazonaws.services.dynamodbv2.document.Item;
     import   com.amazonaws.services.dynamodbv2.document.Table;
     import   com.amazonaws.services.dynamodbv2.document.spec.GetItemSpec;
               try {
                   System.out.println("Attempting to read the item...");
               }
               catch (Exception e) {
                   System.err.println("Unable to read item: " + year + " " + title);
                   System.err.println(e.getMessage());
               }
         }
     }
{
     year: 2015,
     title: "The Big New Movie",
     info: {
          plot: "Nothing happens at all.",
          rating: 0
     }
}
To the following:
{
     year: 2015,
     title: "The Big New Movie",
     info: {
             plot: "Everything happens all at once.",
             rating: 5.5,
             actors: ["Larry", "Moe", "Curly"]
     }
}
1. Copy and paste the following program into your Java development environment:
import java.util.Arrays;
     import   com.amazonaws.client.builder.AwsClientBuilder;
     import   com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
     import   com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder;
     import   com.amazonaws.services.dynamodbv2.document.DynamoDB;
     import   com.amazonaws.services.dynamodbv2.document.Table;
     import   com.amazonaws.services.dynamodbv2.document.UpdateItemOutcome;
     import   com.amazonaws.services.dynamodbv2.document.spec.UpdateItemSpec;
     import   com.amazonaws.services.dynamodbv2.document.utils.ValueMap;
     import   com.amazonaws.services.dynamodbv2.model.ReturnValue;
             try {
                 System.out.println("Updating the item...");
                 UpdateItemOutcome outcome = table.updateItem(updateItemSpec);
                 System.out.println("UpdateItem succeeded:\n" +
      outcome.getItem().toJSONPretty());
               }
               catch (Exception e) {
                   System.err.println("Unable to update item: " + year + " " + title);
                   System.err.println(e.getMessage());
               }
         }
     }
         Note
         This program uses an UpdateExpression to describe all updates you want to perform on
         the specified item.
         The ReturnValues parameter instructs DynamoDB to return only the updated attributes
         (UPDATED_NEW).
2.   Compile and run the program.
The following program shows how to increment the rating for a movie. Each time you run it, the
program increments this attribute by one.
1. Copy and paste the following program into your Java development environment:
package com.amazonaws.codesamples.gsg;
     import   com.amazonaws.client.builder.AwsClientBuilder;
     import   com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
     import   com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder;
     import   com.amazonaws.services.dynamodbv2.document.DynamoDB;
     import   com.amazonaws.services.dynamodbv2.document.Table;
     import   com.amazonaws.services.dynamodbv2.document.UpdateItemOutcome;
     import   com.amazonaws.services.dynamodbv2.document.spec.UpdateItemSpec;
     import   com.amazonaws.services.dynamodbv2.document.utils.ValueMap;
     import   com.amazonaws.services.dynamodbv2.model.ReturnValue;
             try {
                 System.out.println("Incrementing an atomic counter...");
                 UpdateItemOutcome outcome = table.updateItem(updateItemSpec);
                 System.out.println("UpdateItem succeeded:\n" +
      outcome.getItem().toJSONPretty());
               }
               catch (Exception e) {
                   System.err.println("Unable to update item: " + year + " " + title);
                   System.err.println(e.getMessage());
               }
         }
     }
In this case, the movie item is updated only if there are more than three actors.
1. Copy and paste the following program into your Java development environment:
package com.amazonaws.codesamples.gsg;
     import   com.amazonaws.client.builder.AwsClientBuilder;
     import   com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
     import   com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder;
     import   com.amazonaws.services.dynamodbv2.document.DynamoDB;
     import   com.amazonaws.services.dynamodbv2.document.PrimaryKey;
     import   com.amazonaws.services.dynamodbv2.document.Table;
     import   com.amazonaws.services.dynamodbv2.document.UpdateItemOutcome;
     import   com.amazonaws.services.dynamodbv2.document.spec.UpdateItemSpec;
     import   com.amazonaws.services.dynamodbv2.document.utils.ValueMap;
     import   com.amazonaws.services.dynamodbv2.model.ReturnValue;
               }
               catch (Exception e) {
                   System.err.println("Unable to update item: " + year + " " + title);
                   System.err.println(e.getMessage());
               }
         }
     }
     This is because the movie has three actors in it, but the condition is checking for greater than three
     actors.
3.   Modify the program so that the ConditionExpression looks like this:
In the following example, you try to delete a specific movie item if its rating is 5 or less.
1. Copy and paste the following program into your Java development environment:
package com.amazonaws.codesamples.gsg;
     import   com.amazonaws.client.builder.AwsClientBuilder;
     import   com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
     import   com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder;
     import   com.amazonaws.services.dynamodbv2.document.DynamoDB;
     import   com.amazonaws.services.dynamodbv2.document.PrimaryKey;
     import   com.amazonaws.services.dynamodbv2.document.Table;
     import   com.amazonaws.services.dynamodbv2.document.spec.DeleteItemSpec;
     import   com.amazonaws.services.dynamodbv2.document.utils.ValueMap;
               try {
                   System.out.println("Attempting a conditional delete...");
                   table.deleteItem(deleteItemSpec);
                   System.out.println("DeleteItem succeeded");
               }
               catch (Exception e) {
                   System.err.println("Unable to delete item: " + year + " " + title);
                   System.err.println(e.getMessage());
              }
         }
     }
     This is because the rating for this particular move is greater than 5.
3.   Modify the program to remove the condition in DeleteItemSpec.
4. Compile and run the program. Now, the delete succeeds because you removed the condition.
The primary key for the Movies table is composed of the following:
To find all movies released during a year, you need to specify only the year. You can also provide the
title to retrieve a subset of movies based on some condition (on the sort key); for example, to find
movies released in 2014 that have a title starting with the letter "A".
In addition to query, there is also a scan method that can retrieve all of the table data.
To learn more about querying and scanning data, see Working with Queries (p. 408) and Working with
Scans (p. 425), respectively.
Topics
 • Step 4.1: Query (p. 64)
 • Step 4.2: Scan (p. 66)
1. Copy and paste the following program into your Java development environment:
package com.amazonaws.codesamples.gsg;
import java.util.HashMap;
import java.util.Iterator;
import   com.amazonaws.client.builder.AwsClientBuilder;
import   com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
import   com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder;
import   com.amazonaws.services.dynamodbv2.document.DynamoDB;
import   com.amazonaws.services.dynamodbv2.document.Item;
import   com.amazonaws.services.dynamodbv2.document.ItemCollection;
import   com.amazonaws.services.dynamodbv2.document.QueryOutcome;
import   com.amazonaws.services.dynamodbv2.document.Table;
import   com.amazonaws.services.dynamodbv2.document.spec.QuerySpec;
          try {
              System.out.println("Movies from 1985");
              items = table.query(querySpec);
            iterator = items.iterator();
            while (iterator.hasNext()) {
                item = iterator.next();
                System.out.println(item.getNumber("year") + ": " +
 item.getString("title"));
            }
          }
          catch (Exception e) {
              System.err.println("Unable to query movies from 1985");
              System.err.println(e.getMessage());
          }
          valueMap.put(":yyyy", 1992);
          valueMap.put(":letter1", "A");
          valueMap.put(":letter2", "L");
             try {
                 System.out.println("Movies from 1992 - titles A-L, with genres and lead
      actor");
                 items = table.query(querySpec);
                 iterator = items.iterator();
                 while (iterator.hasNext()) {
                     item = iterator.next();
                     System.out.println(item.getNumber("year") + ": " +
      item.getString("title") + " " + item.getMap("info"));
                 }
              }
              catch (Exception e) {
                  System.err.println("Unable to query movies from 1992:");
                  System.err.println(e.getMessage());
              }
         }
     }
Note
     First, you create the querySpec object, which describes the query parameters, and then you pass
     the object to the query method.
2.   Compile and run the program.
     Note
     The preceding program shows how to query a table by its primary key attributes. In DynamoDB,
     you can optionally create one or more secondary indexes on a table, and query those indexes
     in the same way that you query a table. Secondary indexes give your applications additional
     flexibility by allowing queries on non-key attributes. For more information, see Improving Data
     Access with Secondary Indexes (p. 444).
The following program scans the entire Movies table, which contains approximately 5,000 items. The
scan specifies the optional filter to retrieve only the movies from the 1950s (approximately 100 items),
and discard all the others.
1. Copy and paste the following program into your Java development environment:
package com.amazonaws.codesamples.gsg;
import java.util.Iterator;
     import   com.amazonaws.client.builder.AwsClientBuilder;
     import   com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
     import   com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder;
     import   com.amazonaws.services.dynamodbv2.document.DynamoDB;
     import   com.amazonaws.services.dynamodbv2.document.Item;
     import   com.amazonaws.services.dynamodbv2.document.ItemCollection;
     import   com.amazonaws.services.dynamodbv2.document.ScanOutcome;
     import   com.amazonaws.services.dynamodbv2.document.Table;
     import   com.amazonaws.services.dynamodbv2.document.spec.ScanSpec;
     import   com.amazonaws.services.dynamodbv2.document.utils.NameMap;
     import   com.amazonaws.services.dynamodbv2.document.utils.ValueMap;
               try {
                   ItemCollection<ScanOutcome> items = table.scan(scanSpec);
               }
               catch (Exception e) {
                   System.err.println("Unable to scan the table:");
                   System.err.println(e.getMessage());
               }
         }
     }
     Note
     You can also use the Scan operation with any secondary indexes that you created on the table.
     For more information, see Improving Data Access with Secondary Indexes (p. 444).
1. Copy and paste the following program into your Java development environment:
package com.amazonaws.codesamples.gsg;
     import   com.amazonaws.client.builder.AwsClientBuilder;
     import   com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
     import   com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder;
     import   com.amazonaws.services.dynamodbv2.document.DynamoDB;
     import   com.amazonaws.services.dynamodbv2.document.Table;
               try {
                   System.out.println("Attempting to delete table; please wait...");
                   table.delete();
                   table.waitForDelete();
                   System.out.print("Success.");
               }
               catch (Exception e) {
                   System.err.println("Unable to delete table: ");
                   System.err.println(e.getMessage());
               }
         }
     }
Summary
In this tutorial, you created the Movies table in DynamoDB on your computer and performed basic
operations. The downloadable version of DynamoDB is useful during application development and
testing. However, when you're ready to run your application in a production environment, you must
modify your code so that it uses the Amazon DynamoDB web service.
import com.amazonaws.client.builder.AwsClientBuilder;
         AmazonDynamoDB client =
          AmazonDynamoDBClientBuilder.standard().withEndpointConfiguration(
         new AwsClientBuilder.EndpointConfiguration("http://localhost:8000", "us-west-2"))
         .build();
3. Now modify the client so that it accesses an AWS region instead of a specific endpoint:
For example, if you want to access the us-west-2 region, you would do this:
    Instead of using DynamoDB on your computer, the program now uses the Amazon DynamoDB web
    service endpoint in US West (Oregon).
    Amazon DynamoDB is available in several regions worldwide. For the complete list, see Regions and
    Endpoints in the AWS General Reference. For more information about setting regions and endpoints in
    your code, see AWS Region Selection in the AWS SDK for Java Developer Guide.
    • Create a table called Movies and load sample data in JSON format.
    • Perform create, read, update, and delete operations on the table.
    • Run simple queries.
As you work through this tutorial, you can refer to the AWS SDK for JavaScript API Reference.
    Tutorial Prerequisites
    • Download and run DynamoDB on your computer. For more information, see Setting Up DynamoDB
      Local (Downloadable Version) (p. 41).
           Note
           You use the downloadable version of DynamoDB in this tutorial. For information about how to
           run the same code against the DynamoDB web service, see the Summary (p. 88).
    • Set up an AWS access key to use AWS SDKs. For more information, see Setting Up DynamoDB (Web
      Service) (p. 45).
    • Set up the AWS SDK for JavaScript. To do this, add or modify the following script tag to your HTML
      pages:
<script src="https://sdk.amazonaws.com/js/aws-sdk-2.7.16.min.js"></script>
           Note
     The version of AWS SDK for JavaScript might have been updated. For the latest version, see
     the AWS SDK for JavaScript API Reference.
• Enable cross-origin resource sharing (CORS) so your computer's browser can communicate with the
  downloadable version of DynamoDB.
To enable CORS
     1.    Download the free ModHeader Chrome browser extension (or any other browser extension that
           allows you to modify HTTP response headers).
     2.    Run the ModHeader Chrome browser extension, and add an HTTP response header with the name
           set to "Access-Control-Allow-Origin" and a value of "null" or "*".
               Important
               This configuration is required only while running this tutorial program for JavaScript
               on your computer. After you finish the tutorial, you should disable or remove this
               configuration.
     3.    You can now run the JavaScript tutorial program files.
If you prefer to run a complete version of the JavaScript tutorial program instead of performing step-by-
step instructions, do the following:
1. Copy and paste the following program into a file named MoviesCreateTable.html:
          <html>
          <head>
          <script src="https://sdk.amazonaws.com/js/aws-sdk-2.7.16.min.js"></script>
          <script>
          AWS.config.update({
            region: "us-west-2",
            endpoint: 'http://localhost:8000',
            // accessKeyId default can be used while using the downloadable version of DynamoDB.
            // For security reasons, do not store AWS Credentials in your files. Use Amazon
           Cognito instead.
            accessKeyId: "fakeMyKeyId",
            // secretAccessKey default can be used while using the downloadable version of
           DynamoDB.
       // For security reasons, do not store AWS Credentials in your files. Use Amazon
      Cognito instead.
       secretAccessKey: "fakeSecretAccessKey"
     });
     function createMovies() {
         var params = {
             TableName : "Movies",
             KeySchema: [
                 { AttributeName: "year", KeyType: "HASH"},
                 { AttributeName: "title", KeyType: "RANGE" }
             ],
             AttributeDefinitions: [
                 { AttributeName: "year", AttributeType: "N" },
                 { AttributeName: "title", AttributeType: "S" }
             ],
             ProvisionedThroughput: {
                 ReadCapacityUnits: 5,
                 WriteCapacityUnits: 5
             }
         };
     </script>
     </head>
     <body>
     <input id="createTableButton" type="button" value="Create Table"
      onclick="createMovies();" />
     <br><br>
     <textarea readonly id= "textarea" style="width:400px; height:800px"></textarea>
     </body>
     </html>
Note
         • You set the endpoint to indicate that you are creating the table in DynamoDB on your
           computer.
         • In the createMovies function, you specify the table name, primary key attributes, and
           its data types.
         • The ProvisionedThroughput parameter is required, but the downloadable version of
           DynamoDB ignores it. (Provisioned throughput is beyond the scope of this tutorial.)
2.   Open the MoviesCreateTable.html file in your browser.
3.   Choose Create Table.
To learn more about managing tables, see Working with Tables in DynamoDB (p. 291).
Topics
    • Step 2.1: Download the Sample Data File (p. 73)
    • Step 2.2: Load the Sample Data into the Movies Table (p. 73)
This scenario uses a sample data file that contains information about a few thousand movies from the
Internet Movie Database (IMDb). The movie data is in JSON format, as shown in the following example.
For each movie, there is a year, a title, and a JSON map named info.
[
     {
          "year" : ... ,
          "title" : ... ,
          "info" : { ... }
     },
     {
          "year" : ...,
          "title" : ...,
          "info" : { ... }
     },
...
• The year and title are used as the primary key attribute values for the Movies table.
• The rest of the info values are stored in a single attribute called info. This program illustrates how
  you can store JSON in a DynamoDB attribute.
{
    "year" : 2013,
    "title" : "Turn It Down, Or Else!",
    "info" : {
        "directors" : [
            "Alice Smith",
            "Bob Jones"
        ],
        "release_date" : "2013-01-18T00:00:00Z",
        "rating" : 6.2,
        "genres" : [
            "Comedy",
            "Drama"
        ],
        "image_url" : "http://ia.media-imdb.com/images/N/
O9ERWAU7FS797AJ7LU8HN09AMUP908RLlo5JF90EWR7LJKQ7@@._V1_SX400_.jpg",
        "plot" : "A rock band plays their music at high volumes, annoying the neighbors.",
        "rank" : 11,
        "running_time_secs" : 5215,
        "actors" : [
            "David Matthewman",
             "Ann Thomas",
             "Jonathan G. Neff"
         ]
     }
}
Step 2.2: Load the Sample Data into the Movies Table
After you download the sample data, you can run the following program to populate the Movies table.
1. Copy and paste the following program into a file named MoviesLoadData.html:
     <html>
     <head>
     <script src="https://sdk.amazonaws.com/js/aws-sdk-2.7.16.min.js"></script>
     <script type="text/javascript">
     AWS.config.update({
       region: "us-west-2",
       endpoint: 'http://localhost:8000',
       // accessKeyId default can be used while using the downloadable version of DynamoDB.
       // For security reasons, do not store AWS Credentials in your files. Use Amazon
      Cognito instead.
       accessKeyId: "fakeMyKeyId",
       // secretAccessKey default can be used while using the downloadable version of
      DynamoDB.
       // For security reasons, do not store AWS Credentials in your files. Use Amazon
      Cognito instead.
       secretAccessKey: "fakeSecretAccessKey"
     });
     function processFile(evt) {
         document.getElementById('textarea').innerHTML = "";
         document.getElementById('textarea').innerHTML += "Importing movies into DynamoDB.
      Please wait..." + "\n";
         var file = evt.target.files[0];
         if (file) {
             var r = new FileReader();
             r.onload = function(e) {
                 var contents = e.target.result;
                 var allMovies = JSON.parse(contents);
                 allMovies.forEach(function (movie) {
                     document.getElementById('textarea').innerHTML += "Processing: " +
      movie.title + "\n";
                     var params = {
                          TableName: "Movies",
                          Item: {
                              "year": movie.year,
                              "title": movie.title,
                              "info": movie.info
                          }
                      };
                      docClient.put(params, function (err, data) {
                          if (err) {
                              document.getElementById('textarea').innerHTML += "Unable to add
      movie: " + count + movie.title + "\n";
                              document.getElementById('textarea').innerHTML += "Error JSON: "
      + JSON.stringify(err) + "\n";
                          } else {
                              document.getElementById('textarea').innerHTML += "PutItem
      succeeded: " + movie.title + "\n";
                              textarea.scrollTop = textarea.scrollHeight;
                          }
                      });
                  });
         };
             r.readAsText(file);
         } else {
             alert("Could not read movie data file");
         }
     }
     </script>
     </head>
     <body>
     <input type="file" id="fileinput" accept='application/json'/>
     <br><br>
     <textarea readonly id= "textarea" style="width:400px; height:800px"></textarea>
     <script>
         document.getElementById('fileinput').addEventListener('change', processFile,
      false);
     </script>
     </body>
     </html>
To learn more about reading and writing data, see Working with Items in DynamoDB (p. 326).
Topics
 • Step 3.1: Create a New Item (p. 74)
 • Step 3.2: Read an Item (p. 76)
 • Step 3.3: Update an Item (p. 77)
 • Step 3.4: Increment an Atomic Counter (p. 79)
 • Step 3.5: Update an Item (Conditionally) (p. 80)
 • Step 3.6: Delete an Item (p. 81)
1. Copy and paste the following program into a file named MoviesItemOps01.html:
     <html>
     <head>
     <script src="https://sdk.amazonaws.com/js/aws-sdk-2.7.16.min.js"></script>
     <script>
     AWS.config.update({
       region: "us-west-2",
       endpoint: 'http://localhost:8000',
       // accessKeyId default can be used while using the downloadable version of DynamoDB.
       // For security reasons, do not store AWS Credentials in your files. Use Amazon
      Cognito instead.
       accessKeyId: "fakeMyKeyId",
       // secretAccessKey default can be used while using the downloadable version of
      DynamoDB.
       // For security reasons, do not store AWS Credentials in your files. Use Amazon
      Cognito instead.
       secretAccessKey: "fakeSecretAccessKey"
     });
     function createItem() {
          var params = {
              TableName :"Movies",
              Item:{
                  "year": 2015,
                  "title": "The Big New Movie",
                  "info":{
                       "plot": "Nothing happens at all.",
                       "rating": 0
                  }
              }
          };
          docClient.put(params, function(err, data) {
              if (err) {
                  document.getElementById('textarea').innerHTML = "Unable to add item: " +
       "\n" + JSON.stringify(err, undefined, 2);
              } else {
                  document.getElementById('textarea').innerHTML = "PutItem succeeded: " +
       "\n" + JSON.stringify(data, undefined, 2);
              }
          });
     }
     </script>
     </head>
     <body>
     <input id="createItem" type="button" value="Create Item" onclick="createItem();" />
     <br><br>
     <textarea readonly id= "textarea" style="width:400px; height:800px"></textarea>
     </body>
     </html>
        Note
        The primary key is required. This code adds an item that has a primary key (year,
        title) and info attributes. The info attribute stores sample JSON that provides more
        information about the movie.
2.   Open the MoviesItemOps01.html file in your browser.
3.   Choose Create Item.
{
     year: 2015,
     title: "The Big New Movie",
     info: {
          plot: "Nothing happens at all.",
          rating: 0
     }
}
You can use the get method to read the item from the Movies table. You must specify the primary key
values, so you can read any item from Movies if you know its year and title.
1. Copy and paste the following program into a file named MoviesItemOps02.html:
     <html>
     <head>
     <script src="https://sdk.amazonaws.com/js/aws-sdk-2.7.16.min.js"></script>
     <script>
     AWS.config.update({
       region: "us-west-2",
       endpoint: 'http://localhost:8000',
       // accessKeyId default can be used while using the downloadable version of DynamoDB.
       // For security reasons, do not store AWS Credentials in your files. Use Amazon
      Cognito instead.
       accessKeyId: "fakeMyKeyId",
       // secretAccessKey default can be used while using the downloadable version of
      DynamoDB.
       // For security reasons, do not store AWS Credentials in your files. Use Amazon
      Cognito instead.
       secretAccessKey: "fakeSecretAccessKey"
     });
     function readItem() {
         var table = "Movies";
         var year = 2015;
         var title = "The Big New Movie";
         var params = {
             TableName: table,
             Key:{
                 "year": year,
                 "title": title
             }
         };
         docClient.get(params, function(err, data) {
             if (err) {
                 document.getElementById('textarea').innerHTML = "Unable to read item: " +
      "\n" + JSON.stringify(err, undefined, 2);
             } else {
                 document.getElementById('textarea').innerHTML = "GetItem succeeded: " +
      "\n" + JSON.stringify(data, undefined, 2);
             }
         });
     }
     </script>
     </head>
     <body>
     <input id="readItem" type="button" value="Read Item" onclick="readItem();" />
     <br><br>
     <textarea readonly id= "textarea" style="width:400px; height:800px"></textarea>
     </body>
     </html>
{
     year: 2015,
     title: "The Big New Movie",
     info: {
          plot: "Nothing happens at all.",
          rating: 0
     }
}
To the following:
{
     year: 2015,
     title: "The Big New Movie",
     info: {
             plot: "Everything happens all at once.",
             rating: 5.5,
             actors: ["Larry", "Moe", "Curly"]
     }
}
1. Copy and paste the following program into a file named MoviesItemOps03.html:
     <html>
     <head>
     <script src="https://sdk.amazonaws.com/js/aws-sdk-2.7.16.min.js"></script>
     <script>
     AWS.config.update({
       region: "us-west-2",
       endpoint: 'http://localhost:8000',
       // accessKeyId default can be used while using the downloadable version of DynamoDB.
       // For security reasons, do not store AWS Credentials in your files. Use Amazon
      Cognito instead.
       accessKeyId: "fakeMyKeyId",
       // secretAccessKey default can be used while using the downloadable version of
      DynamoDB.
       // For security reasons, do not store AWS Credentials in your files. Use Amazon
      Cognito instead.
       secretAccessKey: "fakeSecretAccessKey"
     });
     function updateItem() {
         var table = "Movies";
         var year = 2015;
         var title = "The Big New Movie";
         var params = {
             TableName:table,
             Key:{
                 "year": year,
                 "title": title
             },
             UpdateExpression: "set info.rating = :r, info.plot=:p, info.actors=:a",
             ExpressionAttributeValues:{
                 ":r":5.5,
                 ":p":"Everything happens all at once.",
                 ":a":["Larry", "Moe", "Curly"]
             },
             ReturnValues:"UPDATED_NEW"
         };
     </script>
     </head>
     <body>
     <input id="updateItem" type="button" value="Update Item" onclick="updateItem();" />
     <br><br>
     <textarea readonly id= "textarea" style="width:400px; height:800px"></textarea>
     </body>
     </html>
         Note
         This program uses UpdateExpression to describe all updates you want to perform on the
         specified item.
         The ReturnValues parameter instructs DynamoDB to return only the updated attributes
         ("UPDATED_NEW").
2.   Open the MoviesItemOps03.html file in your browser.
3.   Choose Update Item.
The following program shows how to increment the rating for a movie. Each time you run it, the
program increments this attribute by one.
1. Copy and paste the following program into a file named MoviesItemOps04.html:
     <html>
     <head>
     <script src="https://sdk.amazonaws.com/js/aws-sdk-2.7.16.min.js"></script>
     <script>
     AWS.config.update({
       region: "us-west-2",
       endpoint: 'http://localhost:8000',
       // accessKeyId default can be used while using the downloadable version of DynamoDB.
       // For security reasons, do not store AWS Credentials in your files. Use Amazon
      Cognito instead.
       accessKeyId: "fakeMyKeyId",
       // secretAccessKey default can be used while using the downloadable version of
      DynamoDB.
       // For security reasons, do not store AWS Credentials in your files. Use Amazon
      Cognito instead.
       secretAccessKey: "fakeSecretAccessKey"
     });
     function increaseRating() {
         var table = "Movies";
         var year = 2015;
         var title = "The Big New Movie";
         var params = {
             TableName:table,
             Key:{
                 "year": year,
                 "title": title
             },
             UpdateExpression: "set info.rating = info.rating + :val",
             ExpressionAttributeValues:{
                 ":val":1
             },
             ReturnValues:"UPDATED_NEW"
         };
     </script>
     </head>
     <body>
     <input id="increaseRating" type="button" value="Increase Rating"
      onclick="increaseRating();" />
     <br><br>
     <textarea readonly id= "textarea" style="width:400px; height:800px"></textarea>
     </body>
     </html>
In this case, the item is updated only if there are more than three actors in the movie.
1. Copy and paste the following program into a file named MoviesItemOps05.html:
     <html>
     <head>
     <script src="https://sdk.amazonaws.com/js/aws-sdk-2.7.16.min.js"></script>
     <script>
     AWS.config.update({
       region: "us-west-2",
       endpoint: 'http://localhost:8000',
       // accessKeyId default can be used while using the downloadable version of DynamoDB.
       // For security reasons, do not store AWS Credentials in your files. Use Amazon
      Cognito instead.
       accessKeyId: "fakeMyKeyId",
       // secretAccessKey default can be used while using the downloadable version of
      DynamoDB.
       // For security reasons, do not store AWS Credentials in your files. Use Amazon
      Cognito instead.
       secretAccessKey: "fakeSecretAccessKey"
     });
     function conditionalUpdate() {
         var table = "Movies";
         var year = 2015;
         var title = "The Big New Movie";
};
     </script>
     </head>
     <body>
     <input id="conditionalUpdate" type="button" value="Conditional Update"
      onclick="conditionalUpdate();" />
     <br><br>
     <textarea readonly id= "textarea" style="width:400px; height:800px"></textarea>
     </body>
     </html>
     This is because the movie has three actors in it, but the condition is checking for greater than three
     actors.
4.   Modify the program so that the ConditionExpression looks like this:
In the following example, you try to delete a specific movie item if its rating is 5 or less.
1. Copy and paste the following program into a file named MoviesItemOps06.html:
     <html>
     <head>
     <script src="https://sdk.amazonaws.com/js/aws-sdk-2.7.16.min.js"></script>
     <script>
     AWS.config.update({
       region: "us-west-2",
       endpoint: 'http://localhost:8000',
       // accessKeyId default can be used while using the downloadable version of DynamoDB.
       // For security reasons, do not store AWS Credentials in your files. Use Amazon
      Cognito instead.
       accessKeyId: "fakeMyKeyId",
       // secretAccessKey default can be used while using the downloadable version of
      DynamoDB.
       // For security reasons, do not store AWS Credentials in your files. Use Amazon
      Cognito instead.
       secretAccessKey: "fakeSecretAccessKey"
     });
     function conditionalDelete() {
         var table = "Movies";
         var year = 2015;
         var title = "The Big New Movie";
         var params = {
             TableName:table,
             Key:{
                 "year":year,
                 "title":title
             },
             ConditionExpression:"info.rating <= :val",
             ExpressionAttributeValues: {
                 ":val": 5.0
             }
         };
     </script>
     </head>
     <body>
     <input id="conditionalDelete" type="button" value="Conditional Delete"
      onclick="conditionalDelete();" />
     <br><br>
     <textarea readonly id= "textarea" style="width:400px; height:800px"></textarea>
     </body>
     </html>
     This is because the rating for this particular movie is greater than 5.
4.   Modify the program to remove the condition from params.
     var params = {
         TableName:table,
          Key:{
              "title":title,
              "year":year
          }
     };
5. Run the program again. The delete succeeds because you removed the condition.
The primary key for the Movies table is composed of the following:
To find all movies released during a year, you need to specify only the year. You can also provide the
title to retrieve a subset of movies based on some condition (on the sort key); for example, to find
movies released in 2014 that have a title starting with the letter "A".
In addition to query, there is also a scan method that can retrieve all the table data.
To learn more about querying and scanning data, see Working with Queries (p. 408) and Working with
Scans (p. 425), respectively.
Topics
 • Step 4.1: Query - All Movies Released in a Year (p. 83)
 • Step 4.2: Query - All Movies Released in a Year with Certain Titles (p. 85)
 • Step 4.3: Scan (p. 86)
1. Copy and paste the following program into a file named MoviesQuery01.html:
     <html>
     <head>
     <script src="https://sdk.amazonaws.com/js/aws-sdk-2.7.16.min.js"></script>
     <script>
     AWS.config.update({
       region: "us-west-2",
       endpoint: 'http://localhost:8000',
       // accessKeyId default can be used while using the downloadable version of DynamoDB.
       // For security reasons, do not store AWS Credentials in your files. Use Amazon
      Cognito instead.
       accessKeyId: "fakeMyKeyId",
       // secretAccessKey default can be used while using the downloadable version of
      DynamoDB.
       // For security reasons, do not store AWS Credentials in your files. Use Amazon
      Cognito instead.
       secretAccessKey: "fakeSecretAccessKey"
});
     function queryData() {
         document.getElementById('textarea').innerHTML += "Querying for movies from 1985.";
           var params = {
               TableName : "Movies",
               KeyConditionExpression: "#yr = :yyyy",
               ExpressionAttributeNames:{
                   "#yr": "year"
               },
               ExpressionAttributeValues: {
                   ":yyyy":1985
               }
           };
     </script>
     </head>
     <body>
     <input id="queryData" type="button" value="Query" onclick="queryData();" />
     <br><br>
     <textarea readonly id= "textarea" style="width:400px; height:800px"></textarea>
     </body>
     </html>
           Note
           ExpressionAttributeNames provides name substitution. This is used because year
           is a reserved word in DynamoDB—you can't use it directly in any expression, including
           KeyConditionExpression. For this reason, you use the expression attribute name #yr.
           ExpressionAttributeValues provides value substitution. This is used because you can't
           use literals in any expression, including KeyConditionExpression. For this reason, you
           use the expression attribute value :yyyy.
2.   Open the MoviesQuery01.html file in your browser.
3.   Choose Query.
     Note
     The preceding program shows how to query a table by its primary key attributes. In DynamoDB,
     you can optionally create one or more secondary indexes on a table, and query those indexes
     in the same way that you query a table. Secondary indexes give your applications additional
     flexibility by allowing queries on non-key attributes. For more information, see Improving Data
     Access with Secondary Indexes (p. 444).
1. Copy and paste the following program into a file named MoviesQuery02.html:
     <html>
     <head>
     <script src="https://sdk.amazonaws.com/js/aws-sdk-2.7.16.min.js"></script>
     <script>
     AWS.config.update({
       region: "us-west-2",
       endpoint: 'http://localhost:8000',
       // accessKeyId default can be used while using the downloadable version of DynamoDB.
       // For security reasons, do not store AWS Credentials in your files. Use Amazon
      Cognito instead.
       accessKeyId: "fakeMyKeyId",
       // secretAccessKey default can be used while using the downloadable version of
      DynamoDB.
       // For security reasons, do not store AWS Credentials in your files. Use Amazon
      Cognito instead.
       secretAccessKey: "fakeSecretAccessKey"
     });
     function queryData() {
         document.getElementById('textarea').innerHTML += "Querying for movies from 1985.";
         var params = {
             TableName : "Movies",
             ProjectionExpression:"#yr, title, info.genres, info.actors[0]",
             KeyConditionExpression: "#yr = :yyyy and title between :letter1 and :letter2",
             ExpressionAttributeNames:{
                 "#yr": "year"
             },
             ExpressionAttributeValues: {
                 ":yyyy":1992,
                 ":letter1": "A",
                 ":letter2": "L"
             }
         };
     </script>
     </head>
<body>
     </body>
     </html>
The following program scans the entire Movies table, which contains approximately 5,000 items. The
scan specifies the optional filter to retrieve only the movies from the 1950s (approximately 100 items),
and discard all the others.
1. Copy and paste the following program into a file named MoviesScan.html:
     <html>
     <head>
     <script src="https://sdk.amazonaws.com/js/aws-sdk-2.7.16.min.js"></script>
     <script>
     AWS.config.update({
       region: "us-west-2",
       endpoint: 'http://localhost:8000',
       // accessKeyId default can be used while using the downloadable version of DynamoDB.
       // For security reasons, do not store AWS Credentials in your files. Use Amazon
      Cognito instead.
       accessKeyId: "fakeMyKeyId",
       // secretAccessKey default can be used while using the downloadable version of
      DynamoDB.
       // For security reasons, do not store AWS Credentials in your files. Use Amazon
      Cognito instead.
       secretAccessKey: "fakeSecretAccessKey"
     });
     function scanData() {
         document.getElementById('textarea').innerHTML += "Scanning Movies table." + "\n";
         var params = {
             TableName: "Movies",
             ProjectionExpression: "#yr, title, info.rating",
             FilterExpression: "#yr between :start_yr and :end_yr",
             ExpressionAttributeNames: {
                 "#yr": "year",
             },
             ExpressionAttributeValues: {
                 ":start_yr": 1950,
                 ":end_yr": 1959
             }
         };
docClient.scan(params, onScan);
     </script>
     </head>
     <body>
     <input id="scanData" type="button" value="Scan" onclick="scanData();" />
     <br><br>
     <textarea readonly id= "textarea" style="width:400px; height:800px"></textarea>
     </body>
     </html>
     Note
     You can also use the Scan operation with any secondary indexes that you create on the table.
     For more information, see Improving Data Access with Secondary Indexes (p. 444).
1. Copy and paste the following program into a file named MoviesDeleteTable.html:
     <html>
     <head>
     <script src="https://sdk.amazonaws.com/js/aws-sdk-2.7.16.min.js"></script>
     <script>
     AWS.config.update({
       region: "us-west-2",
       endpoint: 'http://localhost:8000',
// accessKeyId default can be used while using the downloadable version of DynamoDB.
       // For security reasons, do not store AWS Credentials in your files. Use Amazon
      Cognito instead.
       accessKeyId: "fakeMyKeyId",
       // secretAccessKey default can be used while using the downloadable version of
      DynamoDB.
       // For security reasons, do not store AWS Credentials in your files. Use Amazon
      Cognito instead.
       secretAccessKey: "fakeSecretAccessKey"
     });
     function deleteMovies() {
         var params = {
             TableName : "Movies"
         };
     </script>
     </head>
     <body>
     <input id="deleteTableButton" type="button" value="Delete Table"
      onclick="deleteMovies();" />
     <br><br>
     <textarea readonly id= "textarea" style="width:400px; height:800px"></textarea>
     </body>
     </html>
Summary
In this tutorial, you created the Movies table in DynamoDB on your computer and performed basic
operations. The downloadable version of DynamoDB is useful during application development and
testing. However, when you're ready to run your application in a production environment, you must
modify your code so that it uses the Amazon DynamoDB web service.
AWS.config.update({region: "aws-region"});
For example, if you want to use the us-west-2 region, set the following region:
AWS.config.update({region: "us-west-2"});
The program now uses the Amazon DynamoDB web service region in US West (Oregon).
    DynamoDB is available in several regions worldwide. For the complete list, see Regions and Endpoints in
    the AWS General Reference. For more information about setting regions and endpoints in your code, see
    Setting the Region in the AWS SDK for JavaScript Getting Started Guide.
For more information, see Configure AWS Credentials in Your Files Using Amazon Cognito (p. 731).
    • Create a table called Movies and load sample data in JSON format.
    • Perform create, read, update, and delete operations on the table.
    • Run simple queries.
As you work through this tutorial, you can refer to the AWS SDK for JavaScript API Reference.
    Tutorial Prerequisites
    • Download and run DynamoDB on your computer. For more information, see Setting Up DynamoDB
      Local (Downloadable Version) (p. 41).
          Note
          You use the downloadable version of DynamoDB in this tutorial. For information about how to
          run the same code against the DynamoDB web service, see the Summary (p. 103).
    • Set up an AWS access key to use the AWS SDKs. For more information, see Setting Up DynamoDB (Web
      Service) (p. 45).
    • Set up the AWS SDK for JavaScript:
      • Go to http://nodejs.org and install Node.js.
      • Go to https://aws.amazon.com/sdk-for-node-js and install the AWS SDK for JavaScript.
For more information, see the AWS SDK for JavaScript Getting Started Guide.
1. Copy and paste the following program into a file named MoviesCreateTable.js.
     AWS.config.update({
       region: "us-west-2",
       endpoint: "http://localhost:8000"
     });
     var params = {
         TableName : "Movies",
         KeySchema: [
             { AttributeName: "year", KeyType: "HASH"}, //Partition key
             { AttributeName: "title", KeyType: "RANGE" } //Sort key
         ],
         AttributeDefinitions: [
             { AttributeName: "year", AttributeType: "N" },
             { AttributeName: "title", AttributeType: "S" }
         ],
         ProvisionedThroughput: {
             ReadCapacityUnits: 10,
             WriteCapacityUnits: 10
         }
     };
Note
         • You set the endpoint to indicate that you are creating the table in DynamoDB on your
           computer.
         • In the createTable call, you specify table name, primary key attributes, and its data
           types.
         • The ProvisionedThroughput parameter is required, but the downloadable version of
           DynamoDB ignores it. (Provisioned throughput is beyond the scope of this exercise.)
2.   To run the program, type the following command:
node MoviesCreateTable.js
To learn more about managing tables, see Working with Tables in DynamoDB (p. 291).
Topics
    • Step 2.1: Download the Sample Data File (p. 92)
    • Step 2.2: Load the Sample Data into the Movies Table (p. 92)
We use a sample data file that contains information about a few thousand movies from the Internet
Movie Database (IMDb). The movie data is in JSON format, as shown in the following example. For each
movie, there is a year, a title, and a JSON map named info.
[
     {
          "year" : ... ,
          "title" : ... ,
          "info" : { ... }
     },
     {
          "year" : ...,
          "title" : ...,
          "info" : { ... }
     },
...
• The year and title are used as the primary key attribute values for the Movies table.
• The rest of the info values are stored in a single attribute called info. This program illustrates how
  you can store JSON in a DynamoDB attribute.
{
    "year" : 2013,
    "title" : "Turn It Down, Or Else!",
    "info" : {
         "directors" : [
             "Alice Smith",
             "Bob Jones"
         ],
         "release_date" : "2013-01-18T00:00:00Z",
         "rating" : 6.2,
         "genres" : [
             "Comedy",
             "Drama"
         ],
         "image_url" : "http://ia.media-imdb.com/images/N/
O9ERWAU7FS797AJ7LU8HN09AMUP908RLlo5JF90EWR7LJKQ7@@._V1_SX400_.jpg",
         "plot" : "A rock band plays their music at high volumes, annoying the neighbors.",
         "rank" : 11,
         "running_time_secs" : 5215,
         "actors" : [
             "David Matthewman",
             "Ann Thomas",
             "Jonathan G. Neff"
       ]
    }
}
Step 2.2: Load the Sample Data into the Movies Table
After you download the sample data, you can run the following program to populate the Movies table.
1. Copy and paste the following program into a file named MoviesLoadData.js:
     AWS.config.update({
         region: "us-west-2",
         endpoint: "http://localhost:8000"
     });
node MoviesLoadData.js
To learn more about reading and writing data, see Working with Items in DynamoDB (p. 326).
Topics
 • Step 3.1: Create a New Item (p. 93)
 • Step 3.2: Read an Item (p. 93)
1. Copy and paste the following program into a file named MoviesItemOps01.js:
     AWS.config.update({
       region: "us-west-2",
       endpoint: "http://localhost:8000"
     });
     var params = {
         TableName:table,
         Item:{
             "year": year,
             "title": title,
             "info":{
                 "plot": "Nothing happens at all.",
                 "rating": 0
             }
         }
     };
         Note
         The primary key is required. This code adds an item that has a primary key (year,
         title) and info attributes. The info attribute stores sample JSON that provides more
         information about the movie.
2.   To run the program, type the following command:
node MoviesItemOps01.js
{
     year: 2015,
     title: "The Big New Movie",
     info: {
          plot: "Nothing happens at all.",
          rating: 0
     }
}
You can use the get method to read the item from the Movies table. You must specify the primary key
values, so you can read any item from Movies if you know its year and title.
1. Copy and paste the following program into a file named MoviesItemOps02.js:
     AWS.config.update({
       region: "us-west-2",
       endpoint: "http://localhost:8000"
     });
     var params = {
         TableName: table,
         Key:{
             "year": year,
             "title": title
         }
     };
node MoviesItemOps02.js
{
     year: 2015,
     title: "The Big New Movie",
     info: {
          plot: "Nothing happens at all.",
          rating: 0
     }
}
To the following:
{
     year: 2015,
     title: "The Big New Movie",
     info: {
             plot: "Everything happens all at once.",
             rating: 5.5,
             actors: ["Larry", "Moe", "Curly"]
     }
}
1. Copy and paste the following program into a file named MoviesItemOps03.js:
     AWS.config.update({
       region: "us-west-2",
       endpoint: "http://localhost:8000"
     });
     var params = {
         TableName:table,
         Key:{
             "year": year,
             "title": title
         },
         UpdateExpression: "set info.rating = :r, info.plot=:p, info.actors=:a",
         ExpressionAttributeValues:{
             ":r":5.5,
             ":p":"Everything happens all at once.",
             ":a":["Larry", "Moe", "Curly"]
         },
         ReturnValues:"UPDATED_NEW"
     };
});
           Note
           This program uses UpdateExpression to describe all updates you want to perform on the
           specified item.
           The ReturnValues parameter instructs DynamoDB to return only the updated attributes
           ("UPDATED_NEW").
2.   To run the program, type the following command:
node MoviesItemOps03.js
The following program shows how to increment the rating for a movie. Each time you run it, the
program increments this attribute by one.
1. Copy and paste the following program into a file named MoviesItemOps04.js:
     AWS.config.update({
       region: "us-west-2",
       endpoint: "http://localhost:8000"
     });
     var params = {
         TableName:table,
         Key:{
             "year": year,
             "title": title
         },
         UpdateExpression: "set info.rating = info.rating + :val",
         ExpressionAttributeValues:{
             ":val":1
         },
         ReturnValues:"UPDATED_NEW"
     };
node MoviesItemOps04.js
In this case, the item is updated only if there are more than three actors in the movie.
1. Copy and paste the following program into a file named MoviesItemOps05.js:
     AWS.config.update({
       region: "us-west-2",
       endpoint: "http://localhost:8000"
     });
     var params = {
         TableName:table,
         Key:{
             "year": year,
             "title": title
         },
         UpdateExpression: "remove info.actors[0]",
         ConditionExpression: "size(info.actors) > :num",
         ExpressionAttributeValues:{
             ":num":3
         },
         ReturnValues:"UPDATED_NEW"
     };
node MoviesItemOps05.js
     This is because the movie has three actors in it, but the condition is checking for greater than three
     actors.
3.   Modify the program so that the ConditionExpression looks like this:
In the following example, you try to delete a specific movie item if its rating is 5 or less.
1. Copy and paste the following program into a file named MoviesItemOps06.js:
     AWS.config.update({
       region: "us-west-2",
       endpoint: "http://localhost:8000"
     });
     var params = {
         TableName:table,
         Key:{
             "year":year,
             "title":title
         },
         ConditionExpression:"info.rating <= :val",
         ExpressionAttributeValues: {
             ":val": 5.0
         }
     };
node MoviesItemOps06.js
     This is because the rating for this particular movie is greater than 5.
3.   Modify the program to remove the condition from params.
     var params = {
         TableName:table,
         Key:{
             "title":title,
             "year":year
         }
     };
4. Run the program again. Now, the delete succeeds because you removed the condition.
The primary key for the Movies table is composed of the following:
To find all movies released during a year, you need to specify only the year. You can also provide the
title to retrieve a subset of movies based on some condition (on the sort key); for example, to find
movies released in 2014 that have a title starting with the letter "A".
In addition to query, there is also a scan method that can retrieve all the table data.
To learn more about querying and scanning data, see Working with Queries (p. 408) and Working with
Scans (p. 425), respectively.
Topics
 • Step 4.1: Query - All Movies Released in a Year (p. 99)
 • Step 4.2: Query - All Movies Released in a Year with Certain Titles (p. 100)
 • Step 4.3: Scan (p. 101)
1. Copy and paste the following program into a file named MoviesQuery01.js:
     AWS.config.update({
       region: "us-west-2",
       endpoint: "http://localhost:8000"
     });
     var params = {
         TableName : "Movies",
         KeyConditionExpression: "#yr = :yyyy",
         ExpressionAttributeNames:{
             "#yr": "year"
         },
         ExpressionAttributeValues: {
             ":yyyy":1985
         }
     };
         Note
         ExpressionAttributeNames provides name substitution. We use this because year
         is a reserved word in DynamoDB—you cannot use it directly in any expression, including
         KeyConditionExpression. We use the expression attribute name #yr to address this.
         ExpressionAttributeValues provides value substitution. We use this because you
         cannot use literals in any expression, including KeyConditionExpression. We use the
         expression attribute value :yyyy to address this.
2.   To run the program, type the following command:
node MoviesQuery01.js
     Note
     The preceding program shows how to query a table by its primary key attributes. In DynamoDB,
     you can optionally create one or more secondary indexes on a table, and query those indexes
     in the same way that you query a table. Secondary indexes give your applications additional
     flexibility by allowing queries on non-key attributes. For more information, see Improving Data
     Access with Secondary Indexes (p. 444).
1. Copy and paste the following program into a file named MoviesQuery02.js:
     AWS.config.update({
       region: "us-west-2",
       endpoint: "http://localhost:8000"
     });
console.log("Querying for movies from 1992 - titles A-L, with genres and lead actor");
     var params = {
         TableName : "Movies",
         ProjectionExpression:"#yr, title, info.genres, info.actors[0]",
         KeyConditionExpression: "#yr = :yyyy and title between :letter1 and :letter2",
         ExpressionAttributeNames:{
             "#yr": "year"
         },
         ExpressionAttributeValues: {
             ":yyyy":1992,
             ":letter1": "A",
             ":letter2": "L"
         }
     };
node MoviesQuery02.js
The following program scans the entire Movies table, which contains approximately 5,000 items. The
scan specifies the optional filter to retrieve only the movies from the 1950s (approximately 100 items),
and discard all of the others.
1. Copy and paste the following program into a file named MoviesScan.js:
     AWS.config.update({
         region: "us-west-2",
         endpoint: "http://localhost:8000"
     });
     var params = {
         TableName: "Movies",
         ProjectionExpression: "#yr, title, info.rating",
         FilterExpression: "#yr between :start_yr and :end_yr",
         ExpressionAttributeNames: {
             "#yr": "year",
          },
          ExpressionAttributeValues: {
               ":start_yr": 1950,
               ":end_yr": 1959
          }
     };
node MoviesScan.js
     Note
     You can also use the Scan operation with any secondary indexes that you have created on the
     table. For more information, see Improving Data Access with Secondary Indexes (p. 444).
1. Copy and paste the following program into a file named MoviesDeleteTable.js:
     AWS.config.update({
       region: "us-west-2",
       endpoint: "http://localhost:8000"
     });
         var params = {
             TableName : "Movies"
         };
node MoviesDeleteTable.js
    Summary
    In this tutorial, you created the Movies table in DynamoDB on your computer and performed basic
    operations. The downloadable version of DynamoDB is useful during application development and
    testing. However, when you're ready to run your application in a production environment, you must
    modify your code so that it uses the Amazon DynamoDB web service.
AWS.config.update({endpoint: "https://dynamodb.aws-region.amazonaws.com"});
For example, if you want to use the us-west-2 Region, you set the following endpoint:
AWS.config.update({endpoint: "https://dynamodb.us-west-2.amazonaws.com"});
    Instead of using DynamoDB on your computer, the program now uses the DynamoDB web service
    endpoint in US West (Oregon).
    Amazon DynamoDB is available in several Regions worldwide. For the complete list, see Regions and
    Endpoints in the AWS General Reference. For more information about setting Regions and endpoints in
    your code, see Setting the Region in the AWS SDK for JavaScript Developer Guide.
    • Create a table called Movies using a utility program written in C#, and load sample data in JSON
      format.
    • Perform create, read, update, and delete operations on the table.
The DynamoDB module of the AWS SDK for .NET offers several programming models for different use
cases. In this exercise, the C# code uses both the document model, which provides a level of abstraction
that is often convenient, and also the low-level API, which handles nested attributes more effectively. For
information about the document model API, see .NET: Document Model (p. 232). For information about
the low-level API, see Working with Tables: .NET (p. 320).
Tutorial Prerequisites
• Use a computer that is running a recent version of Windows and a current version of Microsoft Visual
  Studio. If you don't have Visual Studio installed, you can download a free copy of the Community
  edition from the Microsoft Visual Studio website.
• Download and run DynamoDB (Downloadable Version). For more information, see Setting Up
  DynamoDB Local (Downloadable Version) (p. 41).
        Note
        You use the downloadable version of DynamoDB in this tutorial. For more information
        about how to run the same code against the DynamoDB service in the cloud, see the
        Summary (p. 131).
• Set up an AWS access key to use the AWS SDKs. For more information, see Setting Up DynamoDB (Web
  Service) (p. 45).
• Set up a security profile for DynamoDB in Visual Studio. For step-by-step instructions, see .NET Code
  Samples (p. 288).
• In Visual Studio, create a new project called DynamoDB_intro using the Console Application
  template in the Installed/Templates/Visual C#/ node. This is the project you use throughout this
  tutorial.
        Note
        The following tutorial does not work with .NET core because it does not support synchronous
        methods. For more information, see AWS Asynchronous APIs for .NET.
• Install the NuGet package for the DynamoDB module of the AWS SDK for .NET, version 3, into your
  DynamoDB_intro project. To do this, in Visual Studio, open the NuGet Package Manager Console
  from the Tools menu. Then type the following command at the PM> prompt:
In this step, you create a table named Movies. The primary key for the table is composed of the
following attributes:
1. Copy and paste the following program into the Program.cs file, replacing its current contents:
      using System;
      using System.Collections.Generic;
using System.IO;
using System.Text;
using   Amazon;
using   Amazon.DynamoDBv2;
using   Amazon.DynamoDBv2.Model;
using   Amazon.DynamoDBv2.DocumentModel;
namespace DynamoDB_intro
{
     class Program
     {
         public static void Main(string[] args)
         {
             // First, set up a DynamoDB client for DynamoDB Local
             AmazonDynamoDBConfig ddbConfig = new AmazonDynamoDBConfig();
             ddbConfig.ServiceURL = "http://localhost:8000";
             AmazonDynamoDBClient client;
             try
             {
                 client = new AmazonDynamoDBClient(ddbConfig);
             }
             catch (Exception ex)
             {
                 Console.WriteLine("\n Error: failed to create a DynamoDB client; " +
  ex.Message);
                 PauseForDebugWindow();
                 return;
             }
Note
To learn more about managing tables, see Working with Tables in DynamoDB (p. 291).
Topics
 • Step 2.1: Download the Sample Data File (p. 107)
 • Step 2.2: Load the Sample Data into the Movies Table (p. 107)
This scenario uses a sample data file that contains information about a few thousand movies from the
Internet Movie Database (IMDb).
The movie data is encoded as JSON. For each movie, the JSON defines a year name-value pair, a title
name-value pair, and a complex info object, as shown in the following example:
{
    "year" : 2013,
    "title" : "Turn It Down, Or Else!",
    "info" : {
         "directors" : [
             "Alice Smith",
             "Bob Jones"
         ],
         "release_date" : "2013-01-18T00:00:00Z",
         "rating" : 6.2,
         "genres" : [
             "Comedy",
             "Drama"
         ],
         "image_url" : "http://ia.media-imdb.com/images/N/
O9ERWAU7FS797AJ7LU8HN09AMUP908RLlo5JF90EWR7LJKQ7@@._V1_SX400_.jpg",
         "plot" : "A rock band plays their music at high volumes, annoying the neighbors.",
         "rank" : 11,
         "running_time_secs" : 5215,
         "actors" : [
             "David Matthewman",
             "Ann Thomas",
             "Jonathan G. Neff"
       ]
    }
}
Step 2.2: Load the Sample Data into the Movies Table
Build a program that loads movie data into the table you created in Step 1.
1.   This program uses the open source Newtonsoft Json.NET library for deserializing JSON data,
     licensed under the MIT License (MIT) (see https://github.com/JamesNK/Newtonsoft.Json/blob/
     master/LICENSE.md).
     To load the Json.NET library into your project, in Visual Studio, open the NuGet Package Manager
     Console from the Tools menu. Then type the following command at the PM> prompt:
2. Copy and paste the following program into the Program.cs file, replacing its current contents:
     using   System;
     using   System.Collections.Generic;
     using   System.IO;
     using   System.Linq;
     using   System.Text;
using   Amazon;
using   Amazon.DynamoDBv2;
using   Amazon.DynamoDBv2.Model;
using   Amazon.DynamoDBv2.DocumentModel;
using Newtonsoft;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace DynamoDB_intro
{
     class Program
     {
         public static Table GetTableObject(string tableName)
         {
             // First, set up a DynamoDB client for DynamoDB Local
             AmazonDynamoDBConfig ddbConfig = new AmazonDynamoDBConfig();
             ddbConfig.ServiceURL = "http://localhost:8000";
             AmazonDynamoDBClient client;
             try
             {
                 client = new AmazonDynamoDBClient(ddbConfig);
             }
             catch (Exception ex)
             {
                 Console.WriteLine("\n Error: failed to create a DynamoDB client; " +
  ex.Message);
                 return (null);
             }
                  {
                       if (jtr != null)
                           jtr.Close();
                       if (sr != null)
                           sr.Close();
                  }
                  // Get a Table object for the table that you created in Step 1
                  Table table = GetTableObject("Movies");
                  if (table == null)
                  {
                      PauseForDebugWindow();
                      return;
                  }
                 // Load the movie data into the table (this could take some time)
                 Console.Write("\n    Now writing {0:#,##0} movie records from moviedata.json
      (might take 15 minutes)...\n    ...completed: ", movieArray.Count);
                 for (int i = 0, j = 99; i < movieArray.Count; i++)
                 {
                     try
                     {
                         string itemJson = movieArray[i].ToString();
                         Document doc = Document.FromJson(itemJson);
                         table.PutItem(doc);
                     }
                     catch (Exception ex)
                     {
                         Console.WriteLine("\nError: Could not write the movie record
      #{0:#,##0}, because {1}", i, ex.Message);
                         PauseForDebugWindow();
                         return;
                     }
                     if (i >= j)
                     {
                         j++;
                         Console.Write("{0,5:#,##0}, ", j);
                         if (j % 1000 == 0)
                              Console.Write("\n                 ");
                         j += 99;
                     }
                 }
                 Console.WriteLine("\n    Finished writing all movie records to DynamoDB!");
                 PauseForDebugWindow();
             }
3. Now compile the project, leaving it in Debug mode, and run it.
To learn more about reading and writing data, see Working with Items in DynamoDB (p. 326).
Topics
 • Step 3.1: Create a New Item (p. 110)
 • Step 3.2: Read an Item (p. 111)
 • Step 3.3: Update an Item (p. 113)
 • Step 3.4: Increment an Atomic Counter (p. 116)
 • Step 3.5: Update an Item (Conditionally) (p. 118)
 • Step 3.6: Delete an Item (p. 120)
1. Copy and paste the following program into the Program.cs file, replacing its current contents:
     using   System;
     using   System.Collections.Generic;
     using   System.Linq;
     using   System.Text;
     using   Amazon;
     using   Amazon.DynamoDBv2;
     using   Amazon.DynamoDBv2.Model;
     using   Amazon.DynamoDBv2.DocumentModel;
     namespace DynamoDB_intro
     {
         class Program
         {
             static void Main(string[] args)
             {
                 // Get a Table object for the table that you created in Step 1
                 Table table = GetTableObject("Movies");
                 if (table == null)
                 {
                     PauseForDebugWindow();
                     return;
                 }
         Note
         The primary key is required. In this table, the primary key is a composite of both a partition-
         key attribute (year) and a sort-key attribute (title).
         This code writes an item to the table that has the two primary key attributes (year +
         title) and a complex info attribute that stores more information about the movie.
2.   Compile and run the program.
{
     year: 2015,
     title: "The Big New Movie",
     info: {
          plot: "Nothing happens at all.",
          rating: 0
     }
You can use the GetItem method to read the item from the Movies table. You must specify the primary
key values so that you can read any item from Movies if you know its year and title.
1. Copy and paste the following program into the Program.cs file, replacing its current contents:
     using   System;
     using   System.Collections.Generic;
     using   System.Linq;
     using   System.Text;
     using   Amazon;
     using   Amazon.DynamoDBv2;
     using   Amazon.DynamoDBv2.Model;
     using   Amazon.DynamoDBv2.DocumentModel;
     namespace DynamoDB_intro
     {
         class Program
         {
             static void Main(string[] args)
             {
                 // Get a Table object for the table that you created in Step 1
                 Table table = GetTableObject("Movies");
                 if (table == null)
                 {
                     PauseForDebugWindow();
                     return;
                 }
                   try
                   {
                     Document document = table.GetItem(2015, "The Big New Movie");
                     if (document != null)
                          Console.WriteLine("\nGetItem succeeded: \n" +
      document.ToJsonPretty());
                     else
                          Console.WriteLine("\nGetItem succeeded, but the item was not
      found");
                 }
                 catch (Exception e)
                 {
                     Console.WriteLine(e.Message);
                 }
             }
{
     year: 2015,
     title: "The Big New Movie",
     info: {
          plot: "Nothing happens at all.",
          rating: 0
     }
}
To the following:
{
     year: 2015,
     title: "The Big New Movie",
     info: {
             plot: "Everything happens all at once.",
             rating: 5.5,
             actors: ["Larry", "Moe", "Curly"]
     }
}
1. Copy and paste the following program into the Program.cs file, replacing its current contents:
     using   System;
     using   System.Collections.Generic;
     using   System.Linq;
     using   System.Text;
     using   System.Threading.Tasks;
     using   Amazon;
     using   Amazon.DynamoDBv2;
     using   Amazon.DynamoDBv2.Model;
     using   Amazon.DynamoDBv2.DocumentModel;
     namespace DynamoDB_intro
     {
         class Program
         {
             static void Main(string[] args)
             {
                 // Get an AmazonDynamoDBClient for the local database
                 AmazonDynamoDBClient client = GetLocalClient();
                 if (client == null)
                 {
                     PauseForDebugWindow();
                     return;
                 }
            {
                Console.WriteLine("\nError: UpdateItem failed, because: " +
ex.Message);
               if (uir != null)
                   Console.WriteLine("         Status code was " +
uir.HttpStatusCode.ToString());
               PauseForDebugWindow();
               return;
           }
            // Get the item from the table and display it to validate that the update
succeeded
            DisplayMovieItem(client, "2015", "The Big New Movie");
       }
         Note
         Because the document model in the AWS SDK for .NET doesn't support updating nested
         attributes, you must use the AmazonDynamoDBClient.UpdateItem API instead of
         Table.UpdateItem to update attributes under the top-level info attribute.
         To do this, create an UpdateItemRequest that specifies the item you want to update and
         the new values you want to set.
The following program increments the rating for a movie. Each time you run it, the program
increments this attribute by one. Again, it is the UpdateExpression that determines what happens:
1. Copy and paste the following program into the Program.cs file, replacing its current contents:
     using   System;
     using   System.Collections.Generic;
     using   System.Linq;
     using   System.Text;
     using   System.Threading.Tasks;
     using   Amazon;
     using   Amazon.DynamoDBv2;
     using   Amazon.DynamoDBv2.Model;
     using   Amazon.DynamoDBv2.DocumentModel;
     namespace DynamoDB_intro
     {
         class Program
         {
             static void Main(string[] args)
             {
                 // Get an AmazonDynamoDBClient for the local database
                 AmazonDynamoDBClient client = GetLocalClient();
                 if (client == null)
                 {
                     PauseForDebugWindow();
                     return;
                 }
            {
                 { "year", new AttributeValue {
                        N = "2015"
                   } },
                 { "title", new AttributeValue {
                        S = "The Big New Movie"
                   }}
            },
                 ExpressionAttributeValues = new Dictionary<string, AttributeValue>
            {
                 { ":inc", new AttributeValue {
                       N = "1"
                   } }
            },
                 UpdateExpression = "SET info.rating = info.rating + :inc",
                 ReturnValues = "UPDATED_NEW"
            };
            // Get the item from the table and display it to validate that the update
succeeded
            DisplayMovieItem(client, "2015", "The Big New Movie");
       }
In this case, the item is only updated if there are more than three actors in the movie.
1. Copy and paste the following program into the Program.cs file, replacing its current contents:
     using   System;
     using   System.Collections.Generic;
     using   System.Linq;
     using   System.Text;
     using   System.Threading.Tasks;
     using   Amazon;
     using   Amazon.DynamoDBv2;
     using   Amazon.DynamoDBv2.Model;
     using   Amazon.DynamoDBv2.DocumentModel;
     namespace DynamoDB_intro
     {
         class Program
         {
             static void Main(string[] args)
             {
                 // Get an AmazonDynamoDBClient for the local database
                 AmazonDynamoDBClient client = GetLocalClient();
                 if (client == null)
                 {
                     PauseForDebugWindow();
                     return;
                 }
            // Get the item from the table and display it to validate that the update
succeeded
            DisplayMovieItem(client, "2015", "The Big New Movie");
       }
                   }
                   return (client);
              }
     This is because the movie has three actors in it, but the condition is checking for greater than three
     actors.
3.   Modify the program so that the number of actors that the ConditionExpression uses is 2 instead
     of 3:
     The condition now specifies that the number of actors must be greater than 2.
4.   When you compile and run the program now, the UpdateItem operation should succeed.
In the following example, you try to delete a specific movie item if its rating is 5 or less.
                                     API Version 2012-08-10
                                               120
                             Amazon DynamoDB Developer Guide
                       Step 3: Create, Read, Update, and Delete an Item
1. Copy and paste the following program into the Program.cs file, replacing its current contents:
     using   System;
     using   System.Collections.Generic;
     using   System.Linq;
     using   System.Text;
     using   System.Threading.Tasks;
     using   Amazon;
     using   Amazon.DynamoDBv2;
     using   Amazon.DynamoDBv2.Model;
     using   Amazon.DynamoDBv2.DocumentModel;
     namespace DynamoDB_intro
     {
         class Program
         {
             static void Main(string[] args)
             {
                 // Get a Table object for the table that you created in Step 1
                 Table table = GetTableObject("Movies");
                 if (table == null)
                     return;
                   try
                   {
                     client = new AmazonDynamoDBClient(ddbConfig);
                 }
                 catch (Exception ex)
                 {
                     Console.WriteLine("\n Error: failed to create a DynamoDB client; " +
      ex.Message);
                     return (null);
                 }
     This is because the rating for this particular move is greater than 5.
3.   Modify the program to remove the DeleteItemOperationConfig named opConfig from the call
     to table.DeleteItem:
4. Compile and run the program. Now, the delete succeeds because you removed the condition.
The primary key for the Movies table is composed of the following:
To find all movies released during a year, you need to specify only the year partition-key attribute. You
can add the title sort-key attribute to retrieve a subset of movies based on some condition (on the
sort-key attribute), such as finding movies released in 2014 that have a title starting with the letter "A".
In addition to Query, there is also a Scan method that can retrieve all of the table data.
To learn more about querying and scanning data, see Working with Queries (p. 408) and Working with
Scans (p. 425), respectively.
Topics
 • Step 4.1: Query (p. 123)
 • Step 4.2: Scan (p. 127)
1. Copy and paste the following program into the Program.cs file, replacing its current contents:
     using   System;
     using   System.Collections.Generic;
     using   System.Linq;
     using   System.Text;
     using   System.Threading.Tasks;
     using   Amazon;
     using   Amazon.DynamoDBv2;
     using   Amazon.DynamoDBv2.Model;
     using   Amazon.DynamoDBv2.DocumentModel;
     namespace DynamoDB_intro
     {
         class Program
         {
             static string commaSep = ", ";
             static string movieFormatString = "          \"{0}\", lead actor: {1}, genres: {2}";
                   // Get a Table object for the table that you created in Step 1
                   Table table = GetTableObject(client, "Movies");
                   if (table == null)
                   {
                       PauseForDebugWindow();
                       return;
                   }
                   /*-----------------------------------------------------------------------
                    * 4.1.1: Call Table.Query to initiate a query for all movies with
                    *          year == 1985, using an empty filter expression.
                    *-----------------------------------------------------------------------
     */
                 Search search;
                 try
                 {
                     search = table.Query(1985, new Expression());
                 }
                 catch (Exception ex)
                 {
                     Console.WriteLine("\n Error: 1985 query failed because: " +
      ex.Message);
                     PauseForDebugWindow();
                    return;
              }
              /*-----------------------------------------------------------------------
               * 4.1.2a: Call Table.Query to initiate a query for all movies where
               *           year equals 1992 AND title is between "B" and "Hzzz",
               *           returning the lead actor and genres of each.
               *-----------------------------------------------------------------------
*/
              Primitive y_1992 = new Primitive("1992", true);
              QueryOperationConfig config = new QueryOperationConfig();
              config.Filter = new QueryFilter();
              config.Filter.AddCondition("year", QueryOperator.Equal, new DynamoDBEntry[]
 { 1992 });
            config.Filter.AddCondition("title", QueryOperator.Between, new
 DynamoDBEntry[] { "B", "Hzz" });
            config.AttributesToGet = new List<string> { "title", "info" };
            config.Select = SelectValues.SpecificAttributes;
              try
              {
                search = table.Query(config);
            }
            catch (Exception ex)
            {
                Console.WriteLine("\n Error: 1992 query failed because: " +
 ex.Message);
                PauseForDebugWindow();
                return;
            }
 "\n-----------------------------------------------------------------------------");
            docList = new List<Document>();
            Document infoDoc;
            do
            {
                try
                {
                    docList = search.GetNextSet();
                }
                catch (Exception ex)
                {
                    Console.WriteLine("\n Error: Search.GetNextStep failed because: " +
 ex.Message);
                    break;
                }
                foreach (var doc in docList)
                {
                    infoDoc = doc["info"].AsDocument();
                    Console.WriteLine(movieFormatString,
                               doc["title"],
                               infoDoc["actors"].AsArrayOfString()[0],
                               string.Join(commaSep,
 infoDoc["genres"].AsArrayOfString()));
                }
            } while (!search.IsDone);
              /*-----------------------------------------------------------------------
               * 4.1.2b: Call AmazonDynamoDBClient.Query to initiate a query for all
               *           movies where year equals 1992 AND title is between M and Tzz,
               *           returning the genres and the lead actor of each.
               *-----------------------------------------------------------------------
*/
              QueryRequest qRequest = new QueryRequest
              {
                  TableName = "Movies",
                  ExpressionAttributeNames = new Dictionary<string, string>
              {
                  { "#yr", "year" }
              },
                  ExpressionAttributeValues = new Dictionary<string, AttributeValue>
              {
                  { ":y_1992", new AttributeValue {
                         N = "1992"
                    } },
                  { ":M",        new AttributeValue {
                         S = "M"
                    } },
                  { ":Tzz",      new AttributeValue {
                         S = "Tzz"
                    } }
              },
                  KeyConditionExpression = "#yr = :y_1992 and title between :M
 and :Tzz",
                   ProjectionExpression = "title, info.actors[0], info.genres"
              };
            QueryResponse qResponse;
            try
            {
                qResponse = client.Query(qRequest);
            }
            catch (Exception ex)
            {
                Console.WriteLine("\n Error: Low-level query failed, because: " +
 ex.Message);
                PauseForDebugWindow();
                return;
            }
 "\n-------------------------------------------------------------------------");
            foreach (Dictionary<string, AttributeValue> item in qResponse.Items)
            {
                Dictionary<string, AttributeValue> info = item["info"].M;
                Console.WriteLine(movieFormatString,
                          item["title"].S,
                          info["actors"].L[0].S,
                          GetDdbListAsString(info["genres"].L));
           }
       }
                   return (table);
               }
Note
         • In the first query, for all movies released in 1985, an empty expression indicates that
           filtering on the sort-key part of the primary key is not wanted.
         • In the second query, which uses the AWS SDK for .NET document model to query for all
           movies released in 1992 with titles starting with the letters A through L, you can query
           only for top-level attributes. So you must retrieve the entire info attribute. The display
           code then accesses the nested attributes you're interested in.
         • In the third query, you use the low-level AWS SDK for .NET API, which gives more control
           over what is returned. Here, you can retrieve only those nested attributes within the info
           attribute that you're interested in, namely info.genres and info.actors[0].
2.   Compile and run the program.
     Note
     The preceding program shows how to query a table by its primary key attributes. In DynamoDB,
     you can also optionally create one or more secondary indexes on a table, and query those
     indexes in the same way that you query a table. Secondary indexes give your applications
     additional flexibility by allowing queries on non-key attributes. For more information, see
     Improving Data Access with Secondary Indexes (p. 444).
The following program scans the entire Movies table, which contains approximately 5,000 items. The
scan specifies the optional filter to retrieve only the movies from the 1950s (approximately 100 items)
and discard all the others.
1. Copy and paste the following program into the Program.cs file, replacing its current contents:
     using   System;
     using   System.Collections.Generic;
     using   System.Linq;
     using   System.Text;
     using   System.Threading.Tasks;
     using   Amazon;
     using   Amazon.DynamoDBv2;
     using   Amazon.DynamoDBv2.Model;
     using   Amazon.DynamoDBv2.DocumentModel;
     namespace DynamoDB_intro
     {
     class Program
     {
         static void Main(string[] args)
         {
             // Get an AmazonDynamoDBClient for the local DynamoDB database
             AmazonDynamoDBClient client = GetLocalClient();
             // Get a Table object for the table that you created in Step 1
             Table table = GetTableObject(client, "Movies");
             if (table == null)
             {
                 PauseForDebugWindow();
                 return;
             }
             /*-----------------------------------------------------------------------
              * 4.2a: Call Table.Scan to return the movies released in the 1950's,
              *         displaying title, year, lead actor and lead director.
              *-----------------------------------------------------------------------
*/
            ScanFilter filter = new ScanFilter();
            filter.AddCondition("year", ScanOperator.Between, new DynamoDBEntry[]
 { 1950, 1959 });
            ScanOperationConfig config = new ScanOperationConfig
            {
                AttributesToGet = new List<string> { "year, title, info" },
                Filter = filter
            };
            Search search = table.Scan(filter);
             /*-----------------------------------------------------------------------
              * 4.2b: Call AmazonDynamoDBClient.Scan to return all movies released
              *         in the 1960's, only downloading the title, year, lead
              *         actor and lead director attributes.
             *-----------------------------------------------------------------------
*/
            ScanRequest sRequest = new ScanRequest
            {
                TableName = "Movies",
                ExpressionAttributeNames = new Dictionary<string, string>
            {
                { "#yr", "year" }
            },
                ExpressionAttributeValues = new Dictionary<string, AttributeValue>
            {
                { ":y_a", new AttributeValue {
                       N = "1960"
                  } },
                { ":y_z", new AttributeValue {
                       N = "1969"
                  } },
            },
                FilterExpression = "#yr between :y_a and :y_z",
                ProjectionExpression = "#yr, title, info.actors[0], info.directors[0]"
            };
            ScanResponse sResponse;
            try
            {
                sResponse = client.Scan(sRequest);
            }
            catch (Exception ex)
            {
                Console.WriteLine("\n Error: Low-level scan failed, because: " +
 ex.Message);
                PauseForDebugWindow();
                return;
            }
     • The first scan uses the AWS SDK for .NET document model to scan the Movies table and return
       movies released in the 1950s. Because the document model doesn't support nested attributes in
       the AttributesToGet field, you must download the entire info attribute to have access to the
       lead actor and director.
     • The second scan uses the AWS SDK for .NET low-level API to scan the Movies table and return
       movies released in the 1960s. In this case, you can download only those attribute values in info
       that you're interested in, namely info.actors[0] and info.directors[0].
2.   Compile and run the program.
     Note
     You can also use the Scan operation with any secondary indexes that you have created on the
     table. For more information, see Improving Data Access with Secondary Indexes (p. 444).
1. Copy and paste the following program into the Program.cs file, replacing its current contents:
     using System;
     using System.Text;
     using Amazon.DynamoDBv2;
     using Amazon.DynamoDBv2.Model;
     namespace DynamoDB_intro
     {
         class Program
          {
                static void Main(string[] args)
                {
                    // Get an AmazonDynamoDBClient for the local DynamoDB database
                    AmazonDynamoDBClient client = GetLocalClient();
                    try
                    {
                        client.DeleteTable("Movies");
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("\n Error: the \'Movies\" table could not be deleted!
     \n       Reason: " + ex.Message);
                        Console.Write("\n\n ...Press any key to continue");
                        Console.ReadKey();
                        Console.WriteLine();
                        return;
                    }
                    Console.WriteLine("\n Deleted the \'Movies\" table successfully!");
                }
Summary
In this tutorial, you created the Movies table in DynamoDB on your computer and performed basic
operations. DynamoDB (Downloadable Version) is useful during application development and testing.
However, when you're ready to run your application in a production environment, you must modify your
code so that it uses the Amazon DynamoDB service.
ddbConfig.ServiceURL = "http://localhost:8000";
2. Add a new line that specifies the AWS Region you want to access:
ddbConfig.RegionEndpoint = RegionEndpoint.REGION;
For example, if you want to access the us-west-2 region, you would do this:
ddbConfig.RegionEndpoint = RegionEndpoint.USWest2;
   Instead of using the downloadable version of DynamoDB, the program now uses the DynamoDB service
   endpoint in US West (Oregon).
   Amazon DynamoDB is available in several Regions worldwide. For the complete list, see Regions and
   Endpoints in the AWS General Reference. For more information about setting Regions and endpoints in
   your code, see AWS Region Selection in the AWS SDK for .NET Developer Guide.
   • Create a table called Movies and load sample data in JSON format.
   • Perform create, read, update, and delete operations on the table.
   • Run simple queries.
   As you work through this tutorial, you can refer to the AWS SDK for PHP Developer Guide. The Amazon
   DynamoDB section in the AWS SDK for PHP API Reference describes the parameters and results for
   DynamoDB operations.
   Tutorial Prerequisites
   • Download and run DynamoDB on your computer. For more information, see Setting Up DynamoDB
     Local (Downloadable Version) (p. 41).
         Note
         You use the downloadable version of DynamoDB in this tutorial. For information about how to
         run the same code against the DynamoDB service, see the Summary (p. 149).
   • Set up an AWS access key to use the AWS SDKs. For more information, see Setting Up DynamoDB (Web
     Service) (p. 45).
   • Set up the AWS SDK for PHP:
     • Go to http://php.net and install PHP.
     • Go to https://aws.amazon.com/sdk-for-php and install the SDK for PHP.
For more information, see Getting Started in the AWS SDK for PHP Getting Started Guide.
1. Copy and paste the following program into a file named MoviesCreateTable.php:
     <?php
     require 'vendor/autoload.php';
date_default_timezone_set('UTC');
use Aws\DynamoDb\Exception\DynamoDbException;
$dynamodb = $sdk->createDynamoDb();
     $params = [
         'TableName' => 'Movies',
         'KeySchema' => [
             [
                 'AttributeName' => 'year',
                 'KeyType' => 'HASH' //Partition key
             ],
             [
                 'AttributeName' => 'title',
                 'KeyType' => 'RANGE' //Sort key
             ]
         ],
         'AttributeDefinitions' => [
             [
                 'AttributeName' => 'year',
                 'AttributeType' => 'N'
             ],
             [
                 'AttributeName' => 'title',
                 'AttributeType' => 'S'
             ],
          ],
          'ProvisionedThroughput' => [
              'ReadCapacityUnits' => 10,
              'WriteCapacityUnits' => 10
          ]
     ];
     try {
         $result = $dynamodb->createTable($params);
         echo 'Created table. Status: ' .
             $result['TableDescription']['TableStatus'] ."\n";
?>
Note
          • You set the endpoint to indicate that you are creating the table in DynamoDB on your
            computer.
            • In the createTable call, you specify table name, primary key attributes, and its data
              types.
            • The ProvisionedThroughput parameter is required, but the downloadable version of
              DynamoDB ignores it. (Provisioned throughput is beyond the scope of this exercise.)
2.    To run the program, type the following command:
php MoviesCreateTable.php
To learn more about managing tables, see Working with Tables in DynamoDB (p. 291).
Topics
    • Step 2.1: Download the Sample Data File (p. 135)
    • Step 2.2: Load the Sample Data into the Movies Table (p. 135)
This scenario uses a sample data file that contains information about a few thousand movies from the
Internet Movie Database (IMDb). The movie data is in JSON format, as shown in the following example.
For each movie, there is a year, a title, and a JSON map named info.
[
     {
          "year" : ... ,
          "title" : ... ,
          "info" : { ... }
     },
     {
          "year" : ...,
          "title" : ...,
          "info" : { ... }
     },
...
• The year and title are used as the primary key attribute values for the Movies table.
• The rest of the info values are stored in a single attribute called info. This program illustrates how
  you can store JSON in a DynamoDB attribute.
{
      "year" : 2013,
      "title" : "Turn It Down, Or Else!",
      "info" : {
          "directors" : [
              "Alice Smith",
              "Bob Jones"
          ],
         "release_date" : "2013-01-18T00:00:00Z",
         "rating" : 6.2,
         "genres" : [
             "Comedy",
             "Drama"
         ],
         "image_url" : "http://ia.media-imdb.com/images/N/
O9ERWAU7FS797AJ7LU8HN09AMUP908RLlo5JF90EWR7LJKQ7@@._V1_SX400_.jpg",
         "plot" : "A rock band plays their music at high volumes, annoying the neighbors.",
         "rank" : 11,
         "running_time_secs" : 5215,
         "actors" : [
             "David Matthewman",
             "Ann Thomas",
             "Jonathan G. Neff"
       ]
    }
}
Step 2.2: Load the Sample Data into the Movies Table
After you download the sample data, you can run the following program to populate the Movies table.
1. Copy and paste the following program into a file named MoviesLoadData.php:
     <?php
     require 'vendor/autoload.php';
date_default_timezone_set('UTC');
     use Aws\DynamoDb\Exception\DynamoDbException;
     use Aws\DynamoDb\Marshaler;
     $dynamodb = $sdk->createDynamoDb();
     $marshaler = new Marshaler();
$tableName = 'Movies';
         $year = $movie['year'];
         $title = $movie['title'];
         $info = $movie['info'];
         $json = json_encode([
             'year' => $year,
             'title' => $title,
          $params = [
              'TableName' => $tableName,
              'Item' => $marshaler->marshalJson($json)
          ];
          try {
              $result = $dynamodb->putItem($params);
              echo "Added movie: " . $movie['year'] . " " . $movie['title'] . "\n";
          } catch (DynamoDbException $e) {
              echo "Unable to add movie:\n";
              echo $e->getMessage() . "\n";
              break;
          }
?>
          Note
          The DynamoDB Marshaler class has methods for converting JSON documents and PHP
          arrays to the DynamoDB format. In this program, $marshaler->marshalJson($json)
          takes a JSON document and converts it into a DynamoDB item.
2.   To run the program, type the following command:
php MoviesLoadData.php
To learn more about reading and writing data, see Working with Items in DynamoDB (p. 326).
Topics
 • Step 3.1: Create a New Item (p. 136)
 • Step 3.2: Read an Item (p. 137)
 • Step 3.3: Update an Item (p. 138)
 • Step 3.4: Increment an Atomic Counter (p. 140)
 • Step 3.5: Update an Item (Conditionally) (p. 141)
 • Step 3.6: Delete an Item (p. 143)
1. Copy and paste the following program into a file named MoviesItemOps01.php:
     <?php
     require 'vendor/autoload.php';
date_default_timezone_set('UTC');
     use Aws\DynamoDb\Exception\DynamoDbException;
     use Aws\DynamoDb\Marshaler;
     $dynamodb = $sdk->createDynamoDb();
     $marshaler = new Marshaler();
$tableName = 'Movies';
     $year = 2015;
     $title = 'The Big New Movie';
     $item = $marshaler->marshalJson('
         {
             "year": ' . $year . ',
             "title": "' . $title . '",
             "info": {
                 "plot": "Nothing happens at all.",
                 "rating": 0
             }
         }
     ');
     $params = [
         'TableName' => 'Movies',
         'Item' => $item
     ];
     try {
         $result = $dynamodb->putItem($params);
         echo "Added item: $year - $title\n";
?>
          Note
         The primary key is required. This code adds an item that has primary key (year, title) and
         info attributes. The info attribute stores a map that provides more information about the
         movie.
2.   To run the program, type the following command:
php MoviesItemOps01.php
{
     year: 2015,
     title: "The Big New Movie",
     info: {
          plot: "Nothing happens at all.",
          rating: 0
     }
You can use the getItem method to read the item from the Movies table. You must specify the primary
key values, so you can read any item from Movies if you know its year and title.
1. Copy and paste the following program into a file named MoviesItemOps02.php:
     <?php
     require 'vendor/autoload.php';
date_default_timezone_set('UTC');
     use Aws\DynamoDb\Exception\DynamoDbException;
     use Aws\DynamoDb\Marshaler;
     $dynamodb = $sdk->createDynamoDb();
     $marshaler = new Marshaler();
$tableName = 'Movies';
     $year = 2015;
     $title = 'The Big New Movie';
     $key = $marshaler->marshalJson('
         {
             "year": ' . $year . ',
             "title": "' . $title . '"
         }
     ');
     $params = [
         'TableName' => $tableName,
         'Key' => $key
     ];
     try {
         $result = $dynamodb->getItem($params);
         print_r($result["Item"]);
?>
php MoviesItemOps02.php
{
     year: 2015,
     title: "The Big New Movie",
     info: {
          plot: "Nothing happens at all.",
          rating: 0
     }
}
To the following:
{
     year: 2015,
     title: "The Big New Movie",
     info: {
             plot: "Everything happens all at once.",
             rating: 5.5,
             actors: ["Larry", "Moe", "Curly"]
     }
}
1. Copy and paste the following program into a file named MoviesItemOps03.php:
     <?php
     require 'vendor/autoload.php';
date_default_timezone_set('UTC');
     use Aws\DynamoDb\Exception\DynamoDbException;
     use Aws\DynamoDb\Marshaler;
     $dynamodb = $sdk->createDynamoDb();
     $marshaler = new Marshaler();
$tableName = 'Movies';
     $year = 2015;
     $title = 'The Big New Movie';
     $key = $marshaler->marshalJson('
         {
             "year": ' . $year . ',
             "title": "' . $title . '"
         }
     ');
     $eav = $marshaler->marshalJson('
         {
             ":r": 5.5 ,
     $params = [
         'TableName' => $tableName,
         'Key' => $key,
         'UpdateExpression' =>
             'set info.rating = :r, info.plot=:p, info.actors=:a',
         'ExpressionAttributeValues'=> $eav,
         'ReturnValues' => 'UPDATED_NEW'
     ];
     try {
         $result = $dynamodb->updateItem($params);
         echo "Updated item.\n";
         print_r($result['Attributes']);
?>
           Note
           This program uses UpdateExpression to describe all updates you want to perform on the
           specified item.
           The ReturnValues parameter instructs DynamoDB to return only the updated attributes
           (UPDATED_NEW).
2.   To run the program, type the following command:
php MoviesItemOps03.php
The following program shows how to increment the rating for a movie. Each time you run it, the
program increments this attribute by one.
1. Copy and paste the following program into a file named MoviesItemOps04.php:
     <?php
     require 'vendor/autoload.php';
date_default_timezone_set('UTC');
     use Aws\DynamoDb\Exception\DynamoDbException;
     use Aws\DynamoDb\Marshaler;
$dynamodb = $sdk->createDynamoDb();
$tableName = 'Movies';
     $year = 2015;
     $title = 'The Big New Movie';
     $key = $marshaler->marshalJson('
         {
             "year": ' . $year . ',
             "title": "' . $title . '"
         }
     ');
     $eav = $marshaler->marshalJson('
         {
             ":val": 1
         }
     ');
     $params = [
         'TableName' => $tableName,
         'Key' => $key,
         'UpdateExpression' => 'set info.rating = info.rating + :val',
         'ExpressionAttributeValues'=> $eav,
         'ReturnValues' => 'UPDATED_NEW'
     ];
     try {
         $result = $dynamodb->updateItem($params);
         echo "Updated item. ReturnValues are:\n";
         print_r($result['Attributes']);
?>
php MoviesItemOps04.php
In this case, the item is updated only if there are more than three actors in the movie.
1. Copy and paste the following program into a file named MoviesItemOps05.php.
     <?php
     require 'vendor/autoload.php';
date_default_timezone_set('UTC');
     use Aws\DynamoDb\Exception\DynamoDbException;
     use Aws\DynamoDb\Marshaler;
     $dynamodb = $sdk->createDynamoDb();
     $marshaler = new Marshaler();
$tableName = 'Movies';
     $year = 2015;
     $title = 'The Big New Movie';
     $key = $marshaler->marshalJson('
         {
             "year": ' . $year . ',
             "title": "' . $title . '"
         }
     ');
     $eav = $marshaler->marshalJson('
         {
             ":num": 3
         }
     ');
     $params = [
         'TableName' => $tableName,
         'Key' => $key,
         'UpdateExpression' => 'remove info.actors[0]',
         'ConditionExpression' => 'size(info.actors) > :num',
         'ExpressionAttributeValues'=> $eav,
         'ReturnValues' => 'UPDATED_NEW'
     ];
     try {
         $result = $dynamodb->updateItem($params);
         echo "Updated item. ReturnValues are:\n";
         print_r($result['Attributes']);
?>
php MoviesItemOps05.php
     This is because the movie has three actors in it, but the condition is checking for greater than three
     actors.
3.   Modify the program so that the ConditionExpression looks like this:
In the following example, you try to delete a specific movie item if its rating is 5 or less.
1. Copy and paste the following program into a file named MoviesItemOps06.php:
     <?php
     require 'vendor/autoload.php';
date_default_timezone_set('UTC');
     use Aws\DynamoDb\Exception\DynamoDbException;
     use Aws\DynamoDb\Marshaler;
     $dynamodb = $sdk->createDynamoDb();
     $marshaler = new Marshaler();
$tableName = 'Movies';
     $year = 2015;
     $title = 'The Big New Movie';
     $key = $marshaler->marshalJson('
         {
             "year": ' . $year . ',
             "title": "' . $title . '"
         }
     ');
     $eav = $marshaler->marshalJson('
         {
             ":val": 5
         }
     ');
     $params = [
         'TableName' => $tableName,
         'Key' => $key,
         'ConditionExpression' => 'info.rating <= :val',
         'ExpressionAttributeValues'=> $eav
     ];
     try {
         $result = $dynamodb->deleteItem($params);
         echo "Deleted item.\n";
?>
php MoviesItemOps06.php
     This is because the rating for this particular move is greater than 5.
3.   Modify the program to remove the condition:
     $params = [
         'TableName' => $tableName,
         'Key' => $key
     ];
4. Run the program. Now, the delete succeeds because you removed the condition.
The primary key for the Movies table is composed of the following:
To find all movies released during a year, you need to specify only the year. You can also provide the
title to retrieve a subset of movies based on some condition (on the sort key). For example, to find
movies released in 2014 that have a title starting with the letter "A".
In addition to query, there is also a scan method that can retrieve all of the table data.
To learn more about querying and scanning data, see Working with Queries (p. 408) and Working with
Scans (p. 425), respectively.
Topics
 • Step 4.1: Query - All Movies Released in a Year (p. 144)
 • Step 4.2: Query - All Movies Released in a Year with Certain Titles (p. 146)
 • Step 4.3: Scan (p. 147)
1. Copy and paste the following program into a file named MoviesQuery01.php:
     <?php
     require 'vendor/autoload.php';
date_default_timezone_set('UTC');
     use Aws\DynamoDb\Exception\DynamoDbException;
     use Aws\DynamoDb\Marshaler;
     $dynamodb = $sdk->createDynamoDb();
     $marshaler = new Marshaler();
$tableName = 'Movies';
     $eav = $marshaler->marshalJson('
         {
             ":yyyy": 1985
         }
     ');
     $params = [
         'TableName' => $tableName,
         'KeyConditionExpression' => '#yr = :yyyy',
         'ExpressionAttributeNames'=> [ '#yr' => 'year' ],
         'ExpressionAttributeValues'=> $eav
     ];
     try {
         $result = $dynamodb->query($params);
?>
Note
php MoviesItemQuery01.php
     Note
     The preceding program shows how to query a table by its primary key attributes. In DynamoDB,
     you can optionally create one or more secondary indexes on a table, and query those indexes
     in the same way that you query a table. Secondary indexes give your applications additional
     flexibility by allowing queries on non-key attributes. For more information, see Improving Data
     Access with Secondary Indexes (p. 444).
1. Copy and paste the following program into a file named MoviesQuery02.php:
     <?php
     require 'vendor/autoload.php';
date_default_timezone_set('UTC');
     use Aws\DynamoDb\Exception\DynamoDbException;
     use Aws\DynamoDb\Marshaler;
     $dynamodb = $sdk->createDynamoDb();
     $marshaler = new Marshaler();
$tableName = 'Movies';
     $eav = $marshaler->marshalJson('
         {
             ":yyyy":1992,
             ":letter1": "A",
             ":letter2": "L"
         }
     ');
     $params = [
         'TableName' => $tableName,
         'ProjectionExpression' => '#yr, title, info.genres, info.actors[0]',
         'KeyConditionExpression' =>
             '#yr = :yyyy and title between :letter1 and :letter2',
         'ExpressionAttributeNames'=> [ '#yr' => 'year' ],
         'ExpressionAttributeValues'=> $eav
     ];
echo "Querying for movies from 1992 - titles A-L, with genres and lead actor\n";
     try {
         $result = $dynamodb->query($params);
?>
php MoviesQuery02.php
The following program scans the entire Movies table, which contains approximately 5,000 items. The
scan specifies the optional filter to retrieve only the movies from the 1950s (approximately 100 items),
and discard all of the others.
1. Copy and paste the following program into a file named MoviesScan.php:
     <?php
     require 'vendor/autoload.php';
date_default_timezone_set('UTC');
     use Aws\DynamoDb\Exception\DynamoDbException;
     use Aws\DynamoDb\Marshaler;
$dynamodb = $sdk->createDynamoDb();
     $params = [
         'TableName' => 'Movies',
         'ProjectionExpression' => '#yr, title, info.rating',
         'FilterExpression' => '#yr between :start_yr and :end_yr',
         'ExpressionAttributeNames'=> [ '#yr' => 'year' ],
         'ExpressionAttributeValues'=> $eav
     ];
     try {
         while (true) {
             $result = $dynamodb->scan($params);
              if (isset($result['LastEvaluatedKey'])) {
                  $params['ExclusiveStartKey'] = $result['LastEvaluatedKey'];
              } else {
                  break;
              }
          }
?>
php MoviesScan.php
     Note
     You can also use the Scan operation with any secondary indexes that you have created on the
     table. For more information, see Improving Data Access with Secondary Indexes (p. 444).
1. Copy and paste the following program into a file named MoviesDeleteTable.php:
     <?php
     require 'vendor/autoload.php';
date_default_timezone_set('UTC');
use Aws\DynamoDb\Exception\DynamoDbException;
$dynamodb = $sdk->createDynamoDb();
     $params = [
         'TableName' => 'Movies'
     ];
try {
              $result = $dynamodb->deleteTable($params);
              echo "Deleted table.\n";
?>
php MoviesDeleteTable.php
    Summary
    In this tutorial, you created the Movies table in DynamoDB on your computer and performed basic
    operations. The downloadable version of DynamoDB is useful during application development and
    testing. However, when you're ready to run your application in a production environment, you need to
    modify your code so that it uses the Amazon DynamoDB web service.
Remove the endpoint parameter so that the code looks like this:
    After you remove this line, the code can access the DynamoDB service in the Region specified by the
    region config value. For example, the following line specifies that you want to use the US West
    (Oregon) Region:
    Instead of using the downloadable version of DynamoDB on your computer, the program now uses the
    DynamoDB service endpoint in US West (Oregon).
    DynamoDB is available in several Regions worldwide. For the complete list, see Regions and Endpoints in
    the AWS General Reference. For more information about setting Regions and endpoints in your code, see
    the boto: A Python interface to Amazon Web Services.
• Create a table called Movies and load sample data in JSON format.
• Perform create, read, update, and delete operations on the table.
• Run simple queries.
As you work through this tutorial, you can refer to the AWS SDK for Python (Boto) documentation. The
following sections are specific to DynamoDB:
• DynamoDB tutorial
• DynamoDB low-level client
Tutorial Prerequisites
• Download and run DynamoDB on your computer. For more information, see Setting Up DynamoDB
  Local (Downloadable Version) (p. 41).
      Note
      You use the downloadable version of DynamoDB in this tutorial. In the Summary (p. 164),
      we explain how to run the same code against the DynamoDB web service.
• Set up an AWS access key to use the AWS SDKs. For more information, see Setting Up DynamoDB (Web
  Service) (p. 45).
• Install Python 2.6 or later. For more information, see https://www.python.org/downloads. For
  instructions, see Quickstart in the Boto 3 documentation.
1. Copy and paste the following program into a file named MoviesCreateTable.py.
     table = dynamodb.create_table(
         TableName='Movies',
         KeySchema=[
             {
                 'AttributeName': 'year',
                 'KeyType': 'HASH' #Partition key
             },
             {
                 'AttributeName': 'title',
                 'KeyType': 'RANGE' #Sort key
             }
         ],
         AttributeDefinitions=[
             {
                    'AttributeName': 'year',
                    'AttributeType': 'N'
               },
               {
                    'AttributeName': 'title',
                    'AttributeType': 'S'
               },
           ],
           ProvisionedThroughput={
               'ReadCapacityUnits': 10,
               'WriteCapacityUnits': 10
           }
      )
Note
           • You set the endpoint to indicate that you are creating the table in the downloadable
             version of DynamoDB on your computer.
           • In the create_table call, you specify table name, primary key attributes, and its data
             types.
           • The ProvisionedThroughput parameter is required. However, the downloadable
             version of DynamoDB ignores it. (Provisioned throughput is beyond the scope of this
             exercise.)
           • These examples use the Python 3 style print function. The line from __future__
             import print_function enables Python 3 printing in Python 2.6 and later.
2.    To run the program, type the following command:
python MoviesCreateTable.py
To learn more about managing tables, see Working with Tables in DynamoDB (p. 291).
Topics
    • Step 2.1: Download the Sample Data File (p. 152)
    • Step 2.2: Load the Sample Data into the Movies Table (p. 152)
This scenario uses a sample data file that contains information about a few thousand movies from the
Internet Movie Database (IMDb). The movie data is in JSON format, as shown in the following example.
For each movie, there is a year, a title, and a JSON map named info.
[
     {
          "year" : ... ,
          "title" : ... ,
          "info" : { ... }
     },
     {
          "year" : ...,
          "title" : ...,
          "info" : { ... }
     },
...
• The year and title are used as the primary key attribute values for the Movies table.
• The rest of the info values are stored in a single attribute called info. This program illustrates how
  you can store JSON in a DynamoDB attribute.
{
    "year" : 2013,
    "title" : "Turn It Down, Or Else!",
    "info" : {
         "directors" : [
             "Alice Smith",
             "Bob Jones"
         ],
         "release_date" : "2013-01-18T00:00:00Z",
         "rating" : 6.2,
         "genres" : [
             "Comedy",
             "Drama"
         ],
         "image_url" : "http://ia.media-imdb.com/images/N/
O9ERWAU7FS797AJ7LU8HN09AMUP908RLlo5JF90EWR7LJKQ7@@._V1_SX400_.jpg",
         "plot" : "A rock band plays their music at high volumes, annoying the neighbors.",
         "rank" : 11,
         "running_time_secs" : 5215,
         "actors" : [
             "David Matthewman",
             "Ann Thomas",
             "Jonathan G. Neff"
       ]
    }
}
Step 2.2: Load the Sample Data into the Movies Table
After you download the sample data, you can run the following program to populate the Movies table.
1. Copy and paste the following program into a file named MoviesLoadData.py:
table = dynamodb.Table('Movies')
             table.put_item(
                Item={
                    'year': year,
                    'title': title,
                    'info': info,
                 }
             )
python MoviesLoadData.py
To learn more about reading and writing data, see Working with Items in DynamoDB (p. 326).
Topics
 • Step 3.1: Create a New Item (p. 153)
 • Step 3.2: Read an Item (p. 154)
 • Step 3.3: Update an Item (p. 155)
 • Step 3.4: Increment an Atomic Counter (p. 157)
 • Step 3.5: Update an Item (Conditionally) (p. 157)
 • Step 3.6: Delete an Item (p. 159)
1. Copy and paste the following program into a file named MoviesItemOps01.py:
                     return float(o)
                 else:
                     return int(o)
             return super(DecimalEncoder, self).default(o)
table = dynamodb.Table('Movies')
     response = table.put_item(
        Item={
             'year': year,
             'title': title,
             'info': {
                 'plot':"Nothing happens at all.",
                 'rating': decimal.Decimal(0)
             }
         }
     )
     print("PutItem succeeded:")
     print(json.dumps(response, indent=4, cls=DecimalEncoder))
Note
         • The primary key is required. This code adds an item that has primary key (year, title)
           and info attributes. The info attribute stores sample JSON that provides more
           information about the movie.
         • The DecimalEncoder class is used to print out numbers stored using the Decimal class.
           The Boto SDK uses the Decimal class to hold DynamoDB number values.
2.   To run the program, type the following command:
python MoviesItemOps01.py
{
     year: 2015,
     title: "The Big New Movie",
     info: {
          plot: "Nothing happens at all.",
          rating: 0
     }
}
You can use the get_item method to read the item from the Movies table. You must specify the
primary key values, so you can read any item from Movies if you know its year and title.
1. Copy and paste the following program into a file named MoviesItemOps02.py.
     import decimal
     from boto3.dynamodb.conditions import Key, Attr
     from botocore.exceptions import ClientError
table = dynamodb.Table('Movies')
     try:
         response = table.get_item(
             Key={
                 'year': year,
                 'title': title
             }
         )
     except ClientError as e:
         print(e.response['Error']['Message'])
     else:
         item = response['Item']
         print("GetItem succeeded:")
         print(json.dumps(item, indent=4, cls=DecimalEncoder))
python MoviesItemOps02.py
{
     year: 2015,
     title: "The Big New Movie",
     info: {
          plot: "Nothing happens at all.",
          rating: 0
     }
}
To the following:
{
     year: 2015,
     title: "The Big New Movie",
     info: {
             plot: "Everything happens all at once.",
             rating: 5.5,
             actors: ["Larry", "Moe", "Curly"]
     }
}
1. Copy and paste the following program into a file named MoviesItemOps03.py:
table = dynamodb.Table('Movies')
     response = table.update_item(
         Key={
             'year': year,
             'title': title
         },
         UpdateExpression="set info.rating = :r, info.plot=:p, info.actors=:a",
         ExpressionAttributeValues={
             ':r': decimal.Decimal(5.5),
             ':p': "Everything happens all at once.",
             ':a': ["Larry", "Moe", "Curly"]
         },
         ReturnValues="UPDATED_NEW"
     )
     print("UpdateItem succeeded:")
     print(json.dumps(response, indent=4, cls=DecimalEncoder))
         Note
         This program uses UpdateExpression to describe all updates you want to perform on the
         specified item.
         The ReturnValues parameter instructs DynamoDB to return only the updated attributes
         (UPDATED_NEW).
2.   To run the program, type the following command:
python MoviesItemOps03.py
The following program shows how to increment the rating for a movie. Each time you run it, the
program increments this attribute by one.
1. Copy and paste the following program into a file named MoviesItemOps04.py:
table = dynamodb.Table('Movies')
     response = table.update_item(
         Key={
             'year': year,
             'title': title
         },
         UpdateExpression="set info.rating = info.rating + :val",
         ExpressionAttributeValues={
             ':val': decimal.Decimal(1)
         },
         ReturnValues="UPDATED_NEW"
     )
     print("UpdateItem succeeded:")
     print(json.dumps(response, indent=4, cls=DecimalEncoder))
python MoviesItemOps04.py
In this case, the item is updated only if there are more than three actors.
1. Copy and paste the following program into a file named MoviesItemOps05.py:
table = dynamodb.Table('Movies')
     try:
         response = table.update_item(
             Key={
                 'year': year,
                 'title': title
             },
             UpdateExpression="remove info.actors[0]",
             ConditionExpression="size(info.actors) > :num",
             ExpressionAttributeValues={
                 ':num': 3
             },
             ReturnValues="UPDATED_NEW"
         )
     except ClientError as e:
         if e.response['Error']['Code'] == "ConditionalCheckFailedException":
             print(e.response['Error']['Message'])
         else:
             raise
     else:
         print("UpdateItem succeeded:")
         print(json.dumps(response, indent=4, cls=DecimalEncoder))
python MoviesItemOps05.py
     This is because the movie has three actors in it, but the condition is checking for greater than three
     actors.
3.   Modify the program so that the ConditionExpression looks like this:
In the following example, you try to delete a specific movie item if its rating is 5 or less.
1. Copy and paste the following program into a file named MoviesItemOps06.py:
table = dynamodb.Table('Movies')
     try:
         response = table.delete_item(
             Key={
                 'year': year,
                 'title': title
             },
             ConditionExpression="info.rating <= :val",
             ExpressionAttributeValues= {
                 ":val": decimal.Decimal(5)
             }
         )
     except ClientError as e:
         if e.response['Error']['Code'] == "ConditionalCheckFailedException":
             print(e.response['Error']['Message'])
         else:
             raise
     else:
         print("DeleteItem succeeded:")
         print(json.dumps(response, indent=4, cls=DecimalEncoder))
python MoviesItemOps06.py
     This is because the rating for this particular move is greater than 5.
3.   Modify the program to remove the condition in table.delete_item:
     response = table.delete_item(
         Key={
             'year': year,
             'title': title
         }
     )
4. Run the program. Now, the delete succeeds because you removed the condition.
The primary key for the Movies table is composed of the following:
To find all movies released during a year, you need to specify only the year. You can also provide the
title to retrieve a subset of movies based on some condition (on the sort key); for example, to find
movies released in 2014 that have a title starting with the letter "A".
In addition to query, there is also a scan method that can retrieve all the table data.
To learn more about querying and scanning data, see Working with Queries (p. 408) and Working with
Scans (p. 425), respectively.
Topics
 • Step 4.1: Query - All Movies Released in a Year (p. 160)
 • Step 4.2: Query - All Movies Released in a Year with Certain Titles (p. 161)
 • Step 4.3: Scan (p. 162)
1. Copy and paste the following program into a file named MoviesQuery01.py.
     class DecimalEncoder(json.JSONEncoder):
         def default(self, o):
             if isinstance(o, decimal.Decimal):
                 if o % 1 > 0:
                     return float(o)
                 else:
                     return int(o)
             return super(DecimalEncoder, self).default(o)
table = dynamodb.Table('Movies')
     response = table.query(
         KeyConditionExpression=Key('year').eq(1985)
     )
     for i in response['Items']:
         print(i['year'], ":", i['title'])
         Note
         The Boto 3 SDK constructs a ConditionExpression for you when you use the Key and
         Attr functions imported from boto3.dynamodb.conditions. You can also specify a
         ConditionExpression as a string.
         For a list of available conditions for DynamoDB, see the DynamoDB Conditions in AWS SDK
         for Python (Boto 3) Getting Started.
         For more information, see Condition Expressions (p. 344).
2.   To run the program, type the following command:
python MoviesQuery01.py
     Note
     The preceding program shows how to query a table by its primary key attributes. In DynamoDB,
     you can optionally create one or more secondary indexes on a table, and query those indexes
     in the same way that you query a table. Secondary indexes give your applications additional
     flexibility by allowing queries on non-key attributes. For more information, see Improving Data
     Access with Secondary Indexes (p. 444).
1. Copy and paste the following program into a file named MoviesQuery02.py:
                 return str(o)
             return super(DecimalEncoder, self).default(o)
table = dynamodb.Table('Movies')
print("Movies from 1992 - titles A-L, with genres and lead actor")
     response = table.query(
          ProjectionExpression="#yr, title, info.genres, info.actors[0]",
          ExpressionAttributeNames={ "#yr": "year" }, # Expression Attribute Names for
       Projection Expression only.
          KeyConditionExpression=Key('year').eq(1992) & Key('title').between('A', 'L')
     )
     for i in response[u'Items']:
         print(json.dumps(i, cls=DecimalEncoder))
python MoviesQuery02.py
The following program scans the entire Movies table, which contains approximately 5,000 items. The
scan specifies the optional filter to retrieve only the movies from the 1950s (approximately 100 items),
and discard all the others.
1. Copy and paste the following program into a file named MoviesScan.py:
table = dynamodb.Table('Movies')
     fe = Key('year').between(1950, 1959);
     pe = "#yr, title, info.rating"
     # Expression Attribute Names for Projection Expression only.
     ean = { "#yr": "year", }
     esk = None
     response = table.scan(
         FilterExpression=fe,
         ProjectionExpression=pe,
         ExpressionAttributeNames=ean
         )
     for i in response['Items']:
         print(json.dumps(i, cls=DecimalEncoder))
         for i in response['Items']:
             print(json.dumps(i, cls=DecimalEncoder))
Note
python MoviesScan.py
     Note
     You can also use the Scan operation with any secondary indexes that you create on the table.
     For more information, see Improving Data Access with Secondary Indexes (p. 444).
1. Copy and paste the following program into a file named MoviesDeleteTable.py:
table = dynamodb.Table('Movies')
table.delete()
python MoviesDeleteTable.py
    Summary
    In this tutorial, you created the Movies table in the downloadable version of DynamoDB on your
    computer and performed basic operations. The downloadable version of DynamoDB is useful during
    application development and testing. However, when you're ready to run your application in a
    production environment, you must modify your code so that it uses the Amazon DynamoDB web service.
dynamodb = boto3.resource('dynamodb',endpoint_url="http://localhost:8000")
dynamodb = boto3.resource('dynamodb',region_name='us-west-2')
    Instead of using the downloadable version of DynamoDB on your computer, the program now uses the
    DynamoDB service in US West (Oregon).
    DynamoDB is available in several Regions worldwide. For the complete list, see Regions and Endpoints in
    the AWS General Reference. For more information about setting Regions and endpoints in your code, see
    AWS Region Selection in the AWS SDK for Java Developer Guide.
    • Create a table called Movies and load sample data in JSON format.
    • Perform create, read, update, and delete operations on the table.
    • Run simple queries.
    As you work through this tutorial, you can refer to the AWS SDK for Ruby API Reference. The DynamoDB
    section describes the parameters and results for DynamoDB operations.
    Tutorial Prerequisites
    • Download and run DynamoDB on your computer. For more information, see Setting Up DynamoDB
      Local (Downloadable Version) (p. 41).
           Note
           You use the downloadable version of DynamoDB in this tutorial. For information about how to
           run the same code against the DynamoDB service, see the Summary (p. 179).
• Set up an AWS access key to use the AWS SDKs. For more information, see Setting Up DynamoDB (Web
  Service) (p. 45).
• Set up the AWS SDK for Ruby:
     • Go to https://www.ruby-lang.org/en/documentation/installation/ and install Ruby.
     • Go to https://aws.amazon.com/sdk-for-ruby and install the AWS SDK for Ruby.
For more information, see Installation in the AWS SDK for Ruby API Reference.
1. Copy and paste the following program into a file named MoviesCreateTable.rb:
require "aws-sdk"
       Aws.config.update({
          region: "us-west-2",
          endpoint: "http://localhost:8000"
       })
dynamodb = Aws::DynamoDB::Client.new
       params = {
           table_name: "Movies",
           key_schema: [
               {
                   attribute_name: "year",
                   key_type: "HASH" #Partition key
               },
               {
                   attribute_name: "title",
                   key_type: "RANGE" #Sort key
               }
           ],
           attribute_definitions: [
               {
                   attribute_name: "year",
                   attribute_type: "N"
               },
               {
                   attribute_name: "title",
                   attribute_type: "S"
               },
               ],
               provisioned_throughput: {
                   read_capacity_units: 10,
                   write_capacity_units: 10
           }
       }
      begin
          result = dynamodb.create_table(params)
          puts "Created table. Status: " +
              result.table_description.table_status;
Note
            • You set the endpoint to indicate that you are creating the table in the downloadable
              version of DynamoDB on your computer.
            • In the create_table call, you specify table name, primary key attributes, and its data
              types.
            • The provisioned_throughput parameter is required. However, the downloadable
              version of DynamoDB ignores it. (Provisioned throughput is beyond the scope of this
              exercise.)
2.    To run the program, type the following command:
ruby MoviesCreateTable.rb
To learn more about managing tables, see Working with Tables in DynamoDB (p. 291).
Topics
 • Step 2.1: Download the Sample Data File (p. 167)
    • Step 2.2: Load the Sample Data into the Movies Table (p. 167)
You use a sample data file that contains information about a few thousand movies from the Internet
Movie Database (IMDb). The movie data is in JSON format, as shown in the following example. For each
movie, there is a year, a title, and a JSON map named info.
[
     {
          "year" : ... ,
          "title" : ... ,
          "info" : { ... }
     },
     {
          "year" : ...,
          "title" : ...,
          "info" : { ... }
     },
...
• The year and title are used as the primary key attribute values for the Movies table.
• You store the rest of the info values in a single attribute called info. This program illustrates how
  you can store JSON in a DynamoDB attribute.
{
    "year" : 2013,
    "title" : "Turn It Down, Or Else!",
    "info" : {
         "directors" : [
             "Alice Smith",
             "Bob Jones"
         ],
         "release_date" : "2013-01-18T00:00:00Z",
         "rating" : 6.2,
         "genres" : [
             "Comedy",
             "Drama"
         ],
         "image_url" : "http://ia.media-imdb.com/images/N/
O9ERWAU7FS797AJ7LU8HN09AMUP908RLlo5JF90EWR7LJKQ7@@._V1_SX400_.jpg",
         "plot" : "A rock band plays their music at high volumes, annoying the neighbors.",
         "rank" : 11,
         "running_time_secs" : 5215,
         "actors" : [
             "David Matthewman",
             "Ann Thomas",
             "Jonathan G. Neff"
       ]
    }
}
Step 2.2: Load the Sample Data into the Movies Table
After you download the sample data, you can run the following program to populate the Movies table.
1. Copy and paste the following program into a file named MoviesLoadData.rb:
     require "aws-sdk"
     require "json"
     Aws.config.update({
        region: "us-west-2",
        endpoint: "http://localhost:8000"
     })
dynamodb = Aws::DynamoDB::Client.new
table_name = 'Movies'
     file = File.read('moviedata.json')
     movies = JSON.parse(file)
movies.each{|movie|
         params = {
             table_name: table_name,
             item: movie
         }
         begin
             result = dynamodb.put_item(params)
             puts "Added movie: #{movie["year"]} #{movie["title"]}"
ruby MoviesLoadData.rb
To learn more about reading and writing data, see Working with Items in DynamoDB (p. 326).
Topics
 • Step 3.1: Create a New Item (p. 168)
 • Step 3.2: Read an Item (p. 169)
 • Step 3.3: Update an Item (p. 170)
 • Step 3.4: Increment an Atomic Counter (p. 171)
 • Step 3.5: Update an Item (Conditionally) (p. 172)
 • Step 3.6: Delete an Item (p. 174)
1. Copy and paste the following program into a file named MoviesItemOps01.rb:
require "aws-sdk"
     Aws.config.update({
        region: "us-west-2",
        endpoint: "http://localhost:8000"
     })
dynamodb = Aws::DynamoDB::Client.new
table_name = 'Movies'
     year = 2015
     title = "The Big New Movie"
item = {
         year: year,
         title: title,
         info: {
                 plot: "Nothing happens at all.",
                 rating: 0
         }
     }
     params = {
         table_name: table_name,
         item: item
     }
     begin
         result = dynamodb.put_item(params)
         puts "Added item: #{year} - #{title}"
         Note
         The primary key is required. This code adds an item that has primary key (year, title) and
         info attributes. The info attribute stores a map that provides more information about the
         movie.
2.   To run the program, type the following command:
ruby MoviesItemOps01.rb
{
     year: 2015,
     title: "The Big New Movie",
     info: {
          plot: "Nothing happens at all.",
          rating: 0
     }
}
You can use the get_item method to read the item from the Movies table. You must specify the
primary key values, so you can read any item from Movies if you know its year and title.
1. Copy and paste the following program into a file named MoviesItemOps02.rb:
require "aws-sdk"
     Aws.config.update({
        region: "us-west-2",
        endpoint: "http://localhost:8000"
     })
dynamodb = Aws::DynamoDB::Client.new
table_name = 'Movies'
year = 2015
     key = {
         year: year,
         title: title
     }
     params = {
         table_name: table_name,
         key: {
             year: year,
             title: title
         }
     }
     begin
         result = dynamodb.get_item(params)
         printf "%i - %s\n%s\n%d\n",
             result.item["year"],
             result.item["title"],
             result.item["info"]["plot"],
             result.item["info"]["rating"]
ruby MoviesItemOps02.rb
{
     year: 2015,
     title: "The Big New Movie",
     info: {
          plot: "Nothing happens at all.",
          rating: 0
     }
}
To the following:
{
     year: 2015,
     title: "The Big New Movie",
     info: {
             plot: "Everything happens all at once.",
            rating: 5.5,
            actors: ["Larry", "Moe", "Curly"]
     }
}
1. Copy and paste the following program into a file named MoviesItemOps03.rb:
require "aws-sdk"
     Aws.config.update({
        region: "us-west-2",
        endpoint: "http://localhost:8000"
     })
dynamodb = Aws::DynamoDB::Client.new
table_name = 'Movies'
     year = 2015
     title = "The Big New Movie"
     params = {
          table_name: table_name,
          key: {
              year: year,
              title: title
          },
          update_expression: "set info.rating = :r, info.plot=:p, info.actors=:a",
          expression_attribute_values: {
              ":r" => 5.5,
              ":p" => "Everything happens all at once.", # value
       <Hash,Array,String,Numeric,Boolean,IO,Set,nil>
              ":a" => ["Larry", "Moe", "Curly"]
          },
          return_values: "UPDATED_NEW"
     }
     begin
         result = dynamodb.update_item(params)
         puts "Added item: #{year} - #{title}"
         Note
         This program uses update_expression to describe all updates you want to perform on
         the specified item.
         The return_values parameter instructs DynamoDB to return only the updated attributes
         (UPDATED_NEW).
2.   To run the program, type the following command:
ruby MoviesItemOps03.rb
The following program shows how to increment the rating for a movie. Each time you run it, the
program increments this attribute by one.
1. Copy and paste the following program into a file named MoviesItemOps04.rb:
require "aws-sdk"
     Aws.config.update({
        region: "us-west-2",
        endpoint: "http://localhost:8000"
     })
dynamodb = Aws::DynamoDB::Client.new
table_name = 'Movies'
     year = 2015
     title = "The Big New Movie"
     params = {
         table_name: table_name,
         key: {
             year: year,
             title: title
         },
         update_expression: "set info.rating = info.rating + :val",
         expression_attribute_values: {
             ":val" => 1
         },
         return_values: "UPDATED_NEW"
     }
     begin
         result = dynamodb.update_item(params)
         puts "Updated item. ReturnValues are:"
         result.attributes["info"].each do |key, value|
             if key == "rating"
                  puts "#{key}: #{value.to_f}"
             else
                  puts "#{key}: #{value}"
             end
         end
     rescue Aws::DynamoDB::Errors::ServiceError => error
         puts "Unable to update item:"
         puts "#{error.message}"
     end
ruby MoviesItemOps04.rb
In this case, the item is updated only if there are more than three actors.
1. Copy and paste the following program into a file named MoviesItemOps05.rb:
require "aws-sdk"
     Aws.config.update({
        region: "us-west-2",
        endpoint: "http://localhost:8000"
     })
dynamodb = Aws::DynamoDB::Client.new
table_name = 'Movies'
     year = 2015
     title = "The Big New Movie"
     params = {
         table_name: table_name,
         key: {
             year: year,
             title: title
         },
         update_expression: "remove info.actors[0]",
         condition_expression: "size(info.actors) > :num",
         expression_attribute_values: {
             ":num" => 3
         },
         return_values: "UPDATED_NEW"
     }
     begin
         result = dynamodb.update_item(params)
         puts "Updated item. ReturnValues are:"
         result.attributes["info"].each do |key, value|
             if key == "rating"
                  puts "#{key}: #{value.to_f}"
             else
                  puts "#{key}: #{value}"
             end
         end
ruby MoviesItemOps05.rb
     This is because the movie has three actors in it, but the condition is checking for greater than three
     actors.
3.   Modify the program so that the ConditionExpression looks like this:
In the following example, you try to delete a specific movie item if its rating is 5 or less.
1. Copy and paste the following program into a file named MoviesItemOps06.rb:
require "aws-sdk"
     Aws.config.update({
        region: "us-west-2",
        endpoint: "http://localhost:8000"
     })
dynamodb = Aws::DynamoDB::Client.new
table_name = 'Movies'
     year = 2015
     title = "The Big New Movie"
     params = {
         table_name: table_name,
         key: {
             year: year,
             title: title
         },
         condition_expression: "info.rating <= :val",
         expression_attribute_values: {
             ":val" => 5
         }
     }
     begin
         result = dynamodb.delete_item(params)
         puts "Deleted item."
ruby MoviesItemOps06.rb
     This is because the rating for this particular move is greater than 5.
3.   Modify the program to remove the condition:
     params = {
         table_name: "Movies",
         key: {
             year: year,
             title: title
         }
4. Run the program. Now, the delete succeeds because you removed the condition.
The primary key for the Movies table is composed of the following:
To find all movies released during a year, you need to specify only the year. You can also provide the
title to retrieve a subset of movies based on some condition (on the sort key); for example, to find
movies released in 2014 that have a title starting with the letter "A".
In addition to query, there is also a scan method that can retrieve all of the table data.
To learn more about querying and scanning data, see Working with Queries (p. 408) and Working with
Scans (p. 425), respectively.
Topics
 • Step 4.1: Query - All Movies Released in a Year (p. 175)
 • Step 4.2: Query - All Movies Released in a Year with Certain Titles (p. 176)
 • Step 4.3: Scan (p. 177)
1. Copy and paste the following program into a file named MoviesQuery01.rb:
require "aws-sdk"
     Aws.config.update({
        region: "us-west-2",
        endpoint: "http://localhost:8000"
     })
dynamodb = Aws::DynamoDB::Client.new
table_name = "Movies"
     params = {
         table_name: table_name,
         key_condition_expression: "#yr = :yyyy",
         expression_attribute_names: {
             "#yr" => "year"
         },
         expression_attribute_values: {
             ":yyyy" => 1985
         }
     }
     begin
         result = dynamodb.query(params)
         puts "Query succeeded."
         result.items.each{|movie|
              puts "#{movie["year"].to_i} #{movie["title"]}"
         }
Note
ruby MoviesItemQuery01.rb
     Note
     The preceding program shows how to query a table by its primary key attributes. In DynamoDB,
     you can optionally create one or more secondary indexes on a table, and query those indexes
     in the same way that you query a table. Secondary indexes give your applications additional
     flexibility by allowing queries on non-key attributes. For more information, see Improving Data
     Access with Secondary Indexes (p. 444).
1. Copy and paste the following program into a file named MoviesQuery02.rb:
require "aws-sdk"
     Aws.config.update({
        region: "us-west-2",
        endpoint: "http://localhost:8000"
     })
dynamodb = Aws::DynamoDB::Client.new
table_name = "Movies"
     params = {
         table_name: table_name,
         projection_expression: "#yr, title, info.genres, info.actors[0]",
         key_condition_expression:
             "#yr = :yyyy and title between :letter1 and :letter2",
         expression_attribute_names: {
puts "Querying for movies from 1992 - titles A-L, with genres and lead actor";
     begin
         result = dynamodb.query(params)
         puts "Query succeeded."
         result.items.each{|movie|
              print "#{movie["year"].to_i}: #{movie["title"]} ... "
              movie['info']['genres'].each{|gen|
                 print gen + " "
              }
ruby MoviesQuery02.rb
The following program scans the Movies table, which contains approximately 5,000 items. The scan
specifies the optional filter to retrieve only the movies from the 1950s (approximately 100 items), and
discard all the others.
1. Copy and paste the following program into a file named MoviesScan.rb:
require "aws-sdk"
     Aws.config.update({
        region: "us-west-2",
        endpoint: "http://localhost:8000"
     })
dynamodb = Aws::DynamoDB::Client.new
table_name = "Movies"
     params = {
         table_name: table_name,
         projection_expression: "#yr, title, info.rating",
         filter_expression: "#yr between :start_yr and :end_yr",
         expression_attribute_names: {"#yr"=> "year"},
         expression_attribute_values: {
             ":start_yr" => 1950,
             ":end_yr" => 1959
         }
     }
     begin
         loop do
             result = dynamodb.scan(params)
               result.items.each{|movie|
                   puts "#{movie["year"].to_i}: " +
                       "#{movie["title"]} ... " +
                       "#{movie["info"]["rating"].to_f}"
               }
break if result.last_evaluated_key.nil?
ruby MoviesScan.rb
     Note
     You can also use the scan method with any secondary indexes that you create on the table. For
     more information, see Improving Data Access with Secondary Indexes (p. 444).
1. Copy and paste the following program into a file named MoviesDeleteTable.rb:
require "aws-sdk"
     Aws.config.update({
        region: "us-west-2",
        endpoint: "http://localhost:8000"
     })
dynamodb = Aws::DynamoDB::Client.new
     params = {
         table_name: "Movies"
     }
     begin
         result = dynamodb.delete_table(params)
         puts "Deleted table."
ruby MoviesDeleteTable.rb
Summary
In this tutorial, you created the Movies table in the downloadable version of DynamoDB on your
computer and performed basic operations. The downloadable version of DynamoDB is useful during
application development and testing. However, when you're ready to run your application in a
production environment, you must modify your code so that it uses the Amazon DynamoDB web service.
Aws.config.update({
region: "us-west-2",
endpoint: "http://localhost:8000"
})
Remove the endpoint parameter so that the code looks like this:
Aws.config.update({
region: "us-west-2"
]);
After you remove this line, the code can access the DynamoDB service in the Region specified by the
region config value.
Instead of using the version of DynamoDB on your computer, the program uses the DynamoDB service
endpoint in US West (Oregon).
DynamoDB is available in several Regions worldwide. For the complete list, see Regions and Endpoints in
the AWS General Reference. For more information, see the AWS SDK for Ruby Getting Started Guide.
    Topics
     • Overview of AWS SDK Support for DynamoDB (p. 180)
     • Programmatic Interfaces (p. 182)
     • DynamoDB Low-Level API (p. 185)
     • Error Handling (p. 189)
     • Higher-Level Programming Interfaces for DynamoDB (p. 194)
     • Running the Code Samples In This Developer Guide (p. 280)
1. You write an application using an AWS SDK for your programming language.
2. Each AWS SDK provides one or more programmatic interfaces for working with DynamoDB. The
   specific interfaces available depend on which programming language and AWS SDK you use.
3. The AWS SDK constructs HTTP(S) requests for use with the low-level DynamoDB API.
4. The AWS SDK sends the request to the DynamoDB endpoint.
5. DynamoDB executes the request. If the request is successful, DynamoDB returns an HTTP 200
   response code (OK). If the request is unsuccessful, DynamoDB returns an HTTP error code and an error
   message.
6. The AWS SDK processes the response and propagates it back to your application.
Each of the AWS SDKs provides important services to your application, including the following:
Programmatic Interfaces
    Every AWS SDK provides one or more programmatic interfaces for working with DynamoDB. These
    interfaces range from simple low-level DynamoDB wrappers to object-oriented persistence layers. The
    available interfaces vary depending on the AWS SDK and programming language that you use.
    The following section highlights some of the interfaces available, using the AWS SDK for Java as an
    example. (Not all interfaces are available in all AWS SDKs.)
    Topics
     • Low-Level Interfaces (p. 182)
     • Document Interfaces (p. 183)
     • Object Persistence Interface (p. 184)
    Low-Level Interfaces
    Every language-specific AWS SDK provides a low-level interface for DynamoDB, with methods that
    closely resemble low-level DynamoDB API requests.
    In some cases, you will need to identify the data types of the attributes using Data Type
    Descriptors (p. 188), such as S for string or N for number.
        Note
        A low-level interface is available in every language-specific AWS SDK.
The following Java program uses the low-level interface of the AWS SDK for Java. The program issues a
GetItem request for a song in the Music table, and prints the year that the song was released.
package com.amazonaws.codesamples;
import java.util.HashMap;
import   com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
import   com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder;
import   com.amazonaws.services.dynamodbv2.model.AttributeValue;
import   com.amazonaws.services.dynamodbv2.model.GetItemRequest;
import   com.amazonaws.services.dynamodbv2.model.GetItemResult;
          try {
              GetItemResult result = client.getItem(request);
              if (result && result.getItem() != null) {
                  AttributeValue year = result.getItem().get("Year");
                  System.out.println("The song was released in " + year.getN());
              } else {
                  System.out.println("No matching song was found");
              }
          } catch (Exception e) {
              System.err.println("Unable to retrieve data: ");
              System.err.println(e.getMessage());
          }
    }
}
Document Interfaces
Many AWS SDKs provide a document interface, allowing you to perform data plane operations (create,
read, update, delete) on tables and indexes. With a document interface, you do not need to specify Data
Type Descriptors (p. 188); the data types are implied by the semantics of the data itself. These AWS
SDKs also provide methods to easily convert JSON documents to and from native DynamoDB data types.
    Note
    Document interfaces are available in the AWS SDKs for Java, .NET, Node.js, and JavaScript in the
    Browser.
The following Java program uses the document interface of the AWS SDK for Java. The program creates
a Table object that represents the Music table, and then asks that object to use GetItem to retrieve a
song. The program then prints the year that the song was released.
package com.amazonaws.codesamples.gsg;
import   com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
import   com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder;
import   com.amazonaws.services.dynamodbv2.document.DynamoDB;
import   com.amazonaws.services.dynamodbv2.document.GetItemOutcome;
import   com.amazonaws.services.dynamodbv2.document.Table;
    }
}
The following Java program uses DynamoDBMapper, the object persistence interface of the AWS SDK for
Java. The MusicItem class represents an item the Music table.
package com.amazonaws.codesamples;
import   com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBAttribute;
import   com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBHashKey;
import   com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBRangeKey;
import   com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBTable;
@DynamoDBTable(tableName="Music")
public class MusicItem {
    private String artist;
    private String songTitle;
    private String albumTitle;
    private int year;
    @DynamoDBHashKey(attributeName="Artist")
    public String getArtist() { return artist;}
    public void setArtist(String artist) {this.artist = artist;}
          @DynamoDBRangeKey(attributeName="SongTitle")
          public String getSongTitle() { return songTitle;}
          public void setSongTitle(String songTitle) {this.songTitle = songTitle;}
          @DynamoDBAttribute(attributeName = "AlbumTitle")
          public String getAlbumTitle() { return albumTitle;}
          public void setAlbumTitle(String albumTitle) {this.albumTitle = albumTitle;}
          @DynamoDBAttribute(attributeName = "Year")
          public int getYear() { return year; }
          public void setYear(int year) { this.year = year; }
    }
    You can then instantiate a MusicItem object, and retrieve a song using the load() method of
    DynamoDBMapper. The program then prints the year that the song was released.
package com.amazonaws.codesamples;
    import com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
    import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder;
    import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper;
               try {
                   MusicItem result = mapper.load(keySchema);
                   if (result != null) {
                       System.out.println(
                       "The song was released in "+ result.getYear());
                   } else {
                       System.out.println("No matching song was found");
                   }
               } catch (Exception e) {
                   System.err.println("Unable to retrieve data: ");
                   System.err.println(e.getMessage());
               }
The DynamoDB low-level API is the protocol-level interface for Amazon DynamoDB. At this level, every
HTTP(S) request must be correctly formatted and carry a valid digital signature.
The AWS SDKs construct low-level DynamoDB API requests on your behalf and process the responses
from DynamoDB. This lets you focus on your application logic, instead of low-level details. However, you
can still benefit from a basic knowledge of how the low-level DynamoDB API works.
For more information about the low-level DynamoDB API, see Amazon DynamoDB API Reference.
    Note
    DynamoDB Streams has its own low-level API, which is separate from that of DynamoDB and is
    fully supported by the AWS SDKs.
    For more information, see Capturing Table Activity with DynamoDB Streams (p. 517). For the
    low-level DynamoDB Streams API, see the Amazon DynamoDB Streams API Reference
The low-level DynamoDB API uses JavaScript Object Notation (JSON) as a wire protocol format. JSON
presents data in a hierarchy, so that both data values and data structure are conveyed simultaneously.
Name-value pairs are defined in the format name:value. The data hierarchy is defined by nested
brackets of name-value pairs.
DynamoDB uses JSON only as a transport protocol, not as a storage format. The AWS SDKs use JSON
to send data to DynamoDB, and DynamoDB responds with JSON, but DynamoDB does not store data
persistently in JSON format.
    Note
    For more information about JSON, see the Introducing JSON at the JSON.org website.
Request Format
The DynamoDB low-level API accepts HTTP(S) POST requests as input. The AWS SDKs construct these
requests for you.
Suppose that you have a table named Pets, with a key schema consisting of AnimalType (partition key)
and Name (sort key). Both of these attributes are of type string. To retrieve an item from Pets, the AWS
SDK constructs a request as shown following:
POST / HTTP/1.1
Host: dynamodb.<region>.<domain>;
Accept-Encoding: identity
Content-Length: <PayloadSizeBytes>
User-Agent: <UserAgentString>
Content-Type: application/x-amz-json-1.0
Authorization: AWS4-HMAC-SHA256 Credential=<Credential>, SignedHeaders=<Headers>,
 Signature=<Signature>
X-Amz-Date: <Date>
X-Amz-Target: DynamoDB_20120810.GetItem
{
    "TableName": "Pets",
    "Key": {
        "AnimalType": {"S": "Dog"},
        "Name": {"S": "Fido"}
    }
}
• The Authorization header contains information required for DynamoDB to authenticate the
  request. For more information, see Signing AWS API Requests and Signature Version 4 Signing Process
  in the Amazon Web Services General Reference.
• The X-Amz-Target header contains the name of a DynamoDB operation: GetItem. (This is also
  accompanied by the low-level API version, in this case 20120810.)
• The payload (body) of the request contains the parameters for the operation, in JSON format. For the
  GetItem operation, the parameters are TableName and Key.
Response Format
Upon receipt of the request, DynamoDB processes it and returns a response. For the request shown
above, the HTTP(S) response payload contains the results from the operation, as in this example:
HTTP/1.1 200 OK
x-amzn-RequestId: <RequestId>
x-amz-crc32: <Checksum>
Content-Type: application/x-amz-json-1.0
Content-Length: <PayloadSizeBytes>
Date: <Date>
{
    "Item": {
At this point, the AWS SDK returns the response data to your application for further processing.
     Note
     If DynamoDB cannot process a request, it returns an HTTP error code and message. The AWS
     SDK propagates these to your application, in the form of exceptions. For more information, see
     Error Handling (p. 189).
The examples in Request Format (p. 187) and Response Format (p. 187) show examples of how
data type descriptors are used. The GetItem request specifies S for the Pets key schema attributes
(AnimalType and Name), which are of type string. The GetItem response contains a Pets item with
attributes of type string (S), number (N), map (M), and list (L).
• S – String
• N – Number
• B – Binary
• BOOL – Boolean
•   NULL – Null
•   M – Map
•   L – List
•   SS – String Set
•   NS – Number Set
• BS – Binary Set
     Note
     For detailed descriptions of DynamoDB data types, see Data Types (p. 12).
    Numeric Data
    Different programming languages offer different levels of support for JSON. In some cases, you might
    decide to use a third party library for validating and parsing JSON documents.
    Some third party libraries build upon the JSON number type, providing their own types such as int,
    long or double. However, the native number data type in DynamoDB does not map exactly to these
    other data types, so these type distinctions can cause conflicts. In addition, many JSON libraries do
    not handle fixed-precision numeric values, and they automatically infer a double data type for digit
    sequences that contain a decimal point.
    To solve these problems, DynamoDB provides a single numeric type with no data loss. To avoid
    unwanted implicit conversions to a double value, DynamoDB uses strings for the data transfer of numeric
    values. This approach provides flexibility for updating attribute values while maintaining proper sorting
    semantics, such as putting the values "01", "2", and "03" in the proper sequence.
    If number precision is important to your application, you should convert numeric values to strings before
    you pass them to DynamoDB.
    Binary Data
    DynamoDB supports binary attributes. However, JSON does not natively support encoding binary
    data. To send binary data in a request, you will need to encode it in Base64 format. Upon receiving the
    request, DynamoDB decodes the Base64 data back to binary.
    The Base64 encoding scheme used by DynamoDB is described at RFC 4648 at the Internet Engineering
    Task Force (IETF) website.
Error Handling
    This section describes runtime errors and how to handle them. It also describes error messages and codes
    that are specific to DynamoDB.
    Topics
     • Error Components (p. 189)
     • Error Messages and Codes (p. 190)
     • Error Handling in Your Application (p. 192)
     • Error Retries and Exponential Backoff (p. 193)
     • Batch Operations and Error Handling (p. 194)
    Error Components
    When your program sends a request, DynamoDB attempts to process it. If the request is successful,
    DynamoDB returns an HTTP success status code (200 OK), along with the results from the requested
    operation.
If the request is unsuccessful, DynamoDB returns an error. Each error has three components:
The AWS SDKs take care of propagating errors to your application, so that you can take
appropriate action. For example, in a Java program, you can write try-catch logic to handle a
ResourceNotFoundException.
If you are not using an AWS SDK, you will need to parse the content of the low-level response from
DynamoDB. The following is an example of such a response:
{"__type":"com.amazonaws.dynamodb.v20120810#ResourceNotFoundException",
"message":"Requested resource not found: Table: tablename not found"}
AccessDeniedException
    The client did not correctly sign the request. If you are using an AWS SDK, requests are signed for
    you automatically; otherwise, go to the Signature Version 4 Signing Process in the AWS General
    Reference.
OK to retry? No
ConditionalCheckFailedException
    You specified a condition that evaluated to false. For example, you might have tried to perform a
    conditional update on an item, but the actual value of the attribute did not match the expected
    value in the condition.
OK to retry? No
IncompleteSignatureException
    The request signature did not include all of the required components. If you are using an AWS SDK,
    requests are signed for you automatically; otherwise, go to the Signature Version 4 Signing Process
    in the AWS General Reference.
OK to retry? No
ItemCollectionSizeLimitExceededException
   For a table with a local secondary index, a group of items with the same partition key value has
   exceeded the maximum size limit of 10 GB. For more information on item collections, see Item
   Collections (p. 490).
OK to retry? Yes
LimitExceededException
   There are too many concurrent control plane operations. The cumulative number of tables and
   indexes in the CREATING, DELETING or UPDATING state cannot exceed 10.
OK to retry? Yes
MissingAuthenticationTokenException
Message: Request must contain a valid (registered) AWS Access Key ID.
   The request did not include the required authorization header, or it was malformed. See DynamoDB
   Low-Level API (p. 185).
OK to retry? No
ProvisionedThroughputExceededException
   Message: You exceeded your maximum allowed provisioned throughput for a table or for one or more
   global secondary indexes. To view performance metrics for provisioned throughput vs. consumed
   throughput, open the Amazon CloudWatch console.
   Example: Your request rate is too high. The AWS SDKs for DynamoDB automatically retry requests
   that receive this exception. Your request is eventually successful, unless your retry queue is too large
   to finish. Reduce the frequency of requests, using Error Retries and Exponential Backoff (p. 193).
OK to retry? Yes
ResourceInUseException
Example: You tried to recreate an existing table, or delete a table currently in the CREATING state.
OK to retry? No
ResourceNotFoundException
Example: Table which is being requested does not exist, or is too early in the CREATING state.
OK to retry? No
ThrottlingException
    This exception might be returned if you perform any of the following operations too rapidly:
    CreateTable; UpdateTable; DeleteTable.
OK to retry? Yes
UnrecognizedClientException
The request signature is incorrect. The most likely cause is an invalid AWS access key ID or secret key.
OK to retry? Yes
ValidationException
    This error can occur for several reasons, such as a required parameter that is missing, a value that is
    out range, or mismatched data types. The error message contains details about the specific part of
    the request that caused the error.
OK to retry? No
    OK to retry? Yes
        Note
        You may encounter Internal Server Errors while working with items. These are expected
        during the lifetime of a table. Any failed requests can be retried immediately.
OK to retry? Yes
The AWS SDKs perform their own retries and error checking. If you encounter an error while using one of
the AWS SDKs, the error code and description can help you troubleshoot it.
You should also see a Request ID in the response. The Request ID can be helpful if you need to work
with AWS Support to diagnose an issue.
The following Java code snippet attempts to delete an item from a DynamoDB table, and performs
rudimentary error handling. (In this case, it simply informs the user that the request failed).
try {
    Item item = table.getItem("year", 1978, "title", "Superman");
    if (item != null) {
        System.out.println("Result: " + item);
    } else {
        //No such item exists in the table
        System.out.println("Item not found");
    }
In this code snippet, the try-catch construct handles two different kinds of exceptions:
Each AWS SDK implements retry logic, automatically. You can modify the retry parameters to your
needs. For example, consider a Java application that requires a fail-fast strategy, with no retries allowed
in case of an error. With the AWS SDK for Java, you could use the ClientConfiguration class and
provide a maxErrorRetry value of 0 to turn off the retries. For more information, see the AWS SDK
documentation for your programming language
If you're not using an AWS SDK, you should retry original requests that receive server
errors (5xx). However, client errors (4xx, other than a ThrottlingException or a
ProvisionedThroughputExceededException) indicate you need to revise the request itself to
correct the problem before trying again.
In addition to simple retries, each AWS SDK implements exponential backoff algorithm for better flow
control. The concept behind exponential backoff is to use progressively longer waits between retries
for consecutive error responses. For example, up to 50 milliseconds before the first retry, up to 100
milliseconds before the second, up to 200 milliseconds before third, and so on. However, after a minute,
    if the request has not succeeded, the problem might be the request size exceeding your provisioned
    throughput, and not the request rate. Set the maximum number of retries to stop around one minute. If
    the request is not successful, investigate your provisioned throughput options. For more information, see
    Best Practices for Tables (p. 704).
        Note
        The AWS SDKs implement automatic retry logic and exponential backoff.
    Most exponential backoff algorithms use jitter (randomized delay) to prevent successive collisions.
    Because you aren't trying to avoid such collisions in these cases, you do not need to use this random
    number. However, if you use concurrent clients, jitter can help your requests succeed faster. For more
    information, see the blog post for Exponential Backoff and Jitter.
    A batch operation can tolerate the failure of individual requests in the batch. For example, consider a
    BatchGetItem request to read five items. Even if some of the underlying GetItem requests fail, this
    will not cause the entire BatchGetItem operation to fail. On the other hand, if all of the five reads
    operations fail, then the entire BatchGetItem will fail.
    The batch operations return information about individual requests that fail, so that you can diagnose
    the problem and retry the operation. For BatchGetItem, the tables and primary keys in question are
    returned in the UnprocessedKeys parameter of the request. For BatchWriteItem, similar information
    is returned in UnprocessedItems.
    The most likely cause of a failed read or a failed write is throttling. For BatchGetItem, one or more
    of the tables in the batch request does not have enough provisioned read capacity to support the
    operation. For BatchWriteItem, one or more of the tables does not have enough provisioned write
    capacity.
    If DynamoDB returns any unprocessed items, you should retry the batch operation on those items.
    However, we strongly recommend that you use an exponential backoff algorithm. If you retry the batch
    operation immediately, the underlying read or write requests can still fail due to throttling on the
    individual tables. If you delay the batch operation using exponential backoff, the individual requests in
    the batch are much more likely to succeed.
    To simplify development, the AWS SDKs for Java and .NET provide additional interfaces with higher
    levels of abstraction. The higher-level interfaces for DynamoDB let you define the relationships between
    objects in your program and the database tables that store those objects' data. After you define this
    mapping, you call simple object methods such as save, load, or delete, and the underlying low-level
DynamoDB operations are automatically invoked on your behalf. This allows you to write object-centric
code, rather than database-centric code.
The higher-level programming interfaces for DynamoDB are available in the AWS SDKs for Java
and .NET.
Java
.NET
Java: DynamoDBMapper
Topics
 • Supported Data Types (p. 197)
 • Java Annotations for DynamoDB (p. 198)
 • The DynamoDBMapper Class (p. 202)
 • Optional Configuration Settings for DynamoDBMapper (p. 209)
 • Example: CRUD Operations (p. 210)
 • Example: Batch Write Operations (p. 212)
 • Example: Query and Scan (p. 218)
 • Optimistic Locking With Version Number (p. 227)
 • Mapping Arbitrary Data (p. 229)
The AWS SDK for Java provides a DynamoDBMapper class, allowing you to map your client-side classes to
DynamoDB tables. To use DynamoDBMapper, you define the relationship between items in a DynamoDB
table and their corresponding object instances in your code. The DynamoDBMapper class enables you
to access your tables, perform various create, read, update and delete (CRUD) operations, and execute
queries.
    Note
    The DynamoDBMapper class does not allow you to create, update, or delete tables. To perform
    those tasks, use the low-level SDK for Java interface instead. For more information, see Working
    with Tables: Java (p. 315).
The SDK for Java provides a set of annotation types, so that you can map your classes to tables. For
example, consider a ProductCatalog table that has Id as the partition key.
ProductCatalog(Id, ...)
You can map a class in your client application to the ProductCatalog table as shown in the following
Java code. This code snippet defines a plain old Java object (POJO) named CatalogItem, which uses
annotations to map object fields to DynamoDB attribute names:
Example
package com.amazonaws.codesamples;
import java.util.Set;
import   com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBAttribute;
import   com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBHashKey;
import   com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBIgnore;
import   com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBTable;
@DynamoDBTable(tableName="ProductCatalog")
public class CatalogItem {
    @DynamoDBHashKey(attributeName="Id")
    public Integer getId() { return id;}
    public void setId(Integer id) {this.id = id;}
    @DynamoDBAttribute(attributeName="Title")
    public String getTitle() {return title; }
    public void setTitle(String title) { this.title = title; }
    @DynamoDBAttribute(attributeName="ISBN")
    public String getISBN() { return ISBN; }
    public void setISBN(String ISBN) { this.ISBN = ISBN; }
    @DynamoDBAttribute(attributeName = "Authors")
    public Set<String> getBookAuthors() { return bookAuthors; }
    public void setBookAuthors(Set<String> bookAuthors) { this.bookAuthors = bookAuthors; }
    @DynamoDBIgnore
    public String getSomeProp() { return someProp;}
    public void setSomeProp(String someProp) {this.someProp = someProp;}
}
In the preceding code, the @DynamoDBTable annotation maps the CatalogItem class to the
ProductCatalog table. You can store individual class instances as items in the table. In the class
definition, the @DynamoDBHashKey annotation maps the Id property to the primary key.
By default, the class properties map to the same name attributes in the table. The properties Title and
ISBN map to the same name attributes in the table.
The @DynamoDBAttribute annotation is optional when the name of the DynamoDB attribute
matches the name of the property declared in the class. When they differ, use this annotation with
the attributeName() parameter to specify which DynamoDB attribute this property corresponds to. In
the preceding example, the @DynamoDBAttribute annotation is added to each property to ensure
that the property names match exactly with the tables created in Creating Tables and Loading Sample
Data (p. 281), and to be consistent with the attribute names used in other code examples in this guide.
Your class definition can have properties that don't map to any attributes in the table. You identify these
properties by adding the @DynamoDBIgnore annotation. In the preceding example, the SomeProp
property is marked with the @DynamoDBIgnore annotation. When you upload a CatalogItem instance
to the table, your DynamoDBMapper instance does not include SomeProp property. In addition, the
mapper does not return this attribute when you retrieve an item from the table.
After you have defined your mapping class, you can use DynamoDBMapper methods to write an instance
of that class to a corresponding item in the Catalog table. The following code snippet demonstrates this
technique:
mapper.save(item);
The following code snippet shows how to retrieve the item and access some of its attributes:
partitionKey.setId(102);
DynamoDBQueryExpression<CatalogItem> queryExpression = new
 DynamoDBQueryExpression<CatalogItem>()
    .withHashKeyValues(partitionKey);
DynamoDBMapper offers an intuitive, natural way of working with DynamoDB data within Java. It also
provides a number of built-in features such as optimistic locking, auto-generated partition key and sort
key values, and object versioning.
DynamoDB supports the following primitive data types and primitive wrapper classes.
• String
• Boolean, boolean
• Byte, byte
• Date (as ISO_8601 millisecond-precision string, shifted to UTC)
• Calendar (as ISO_8601 millisecond-precision string, shifted to UTC)
• Long, long
•   Integer, int
•   Double, double
•   Float, float
•   BigDecimal
• BigInteger
     Note
     For more information on the DynamoDB naming rules and the various supported data types see
     Naming Rules and Data Types (p. 11).
DynamoDB supports the Java Set collection types. If your mapped collection property is not a Set, then
an exception is thrown.
The following table summarizes how the preceding Java types map to the DynamoDB types.
The DynamoDBTypeConverter interface lets you map your own arbitrary data types to a data type that
is natively supported by DynamoDB. For more information, see Mapping Arbitrary Data (p. 229).
For the corresponding Javadoc documentation, see Annotation Types Summary in the AWS SDK for Java
API Reference.
    Note
    In the following annotations, only DynamoDBTable and the DynamoDBHashKey are required.
Topics
 • DynamoDBAttribute (p. 198)
 • DynamoDBAutoGeneratedKey (p. 199)
 • DynamoDBDocument (p. 199)
 • DynamoDBHashKey (p. 200)
 • DynamoDBIgnore (p. 201)
 • DynamoDBIndexHashKey (p. 201)
 • DynamoDBIndexRangeKey (p. 201)
 • DynamoDBRangeKey (p. 201)
 • DynamoDBTable (p. 201)
 • DynamoDBTypeConverted (p. 202)
 • DynamoDBTyped (p. 202)
 • DynamoDBVersionAttribute (p. 202)
DynamoDBAttribute
Maps a property to a table attribute. By default, each class property maps to an item attribute with the
same name. However, if the names are not the same, you can use this annotation to map a property to
the attribute. In the following Java snippet, the DynamoDBAttribute maps the BookAuthors property
to the Authors attribute name in the table.
@DynamoDBAttribute(attributeName = "Authors")
public List<String> getBookAuthors() { return BookAuthors; }
public void setBookAuthors(List<String> BookAuthors) { this.BookAuthors = BookAuthors; }
The DynamoDBMapper uses Authors as the attribute name when saving the object to the table.
DynamoDBAutoGeneratedKey
Marks a partition key or sort key property as being auto-generated. DynamoDBMapper will generate a
random UUID when saving these attributes. Only String properties can be marked as auto-generated
keys.
@DynamoDBTable(tableName="AutoGeneratedKeysExample")
public class AutoGeneratedKeys {
    private String id;
    private String payload;
    @DynamoDBHashKey(attributeName = "Id")
    @DynamoDBAutoGeneratedKey
    public String getId() { return id; }
    public void setId(String id) { this.id = id; }
    @DynamoDBAttribute(attributeName="payload")
    public String getPayload() { return this.payload; }
    public void setPayload(String payload) { this.payload = payload; }
DynamoDBDocument
Indicates that a class can be serialized as a DynamoDB document.
For example, suppose you wanted to map a JSON document to a DynamoDB attribute of type Map (M).
The following code snippet defines an item containing a nested attribute (Pictures) of type Map.
    @DynamoDBHashKey(attributeName="Id")
    public Integer getId() { return id;}
    public void setId(Integer id) {this.id = id;}
    @DynamoDBAttribute(attributeName="Pictures")
    public Pictures getPictures() { return pictures;}
    public void setPictures(Pictures pictures) {this.pictures = pictures;}
    @DynamoDBDocument
    public static class Pictures {
        private String frontView;
        private String rearView;
           @DynamoDBAttribute(attributeName = "FrontView")
           public String getFrontView() { return frontView; }
           public void setFrontView(String frontView) { this.frontView = frontView; }
           @DynamoDBAttribute(attributeName = "RearView")
           public String getRearView() { return rearView; }
           public void setRearView(String rearView) { this.rearView = rearView; }
           @DynamoDBAttribute(attributeName = "SideView")
           public String getSideView() { return sideView; }
           public void setSideView(String sideView) { this.sideView = sideView; }
       }
}
You could then save a new ProductCatalog item, with Pictures, as shown in the following snippet:
item.setId(123);
mapper.save(item);
The resulting ProductCatalog item would look like this (in JSON format):
{
    "Id" : 123
    "Pictures" : {
      "SideView" : "http://example.com/products/123_left_side.jpg",
      "RearView" : "http://example.com/products/123_rear.jpg",
      "FrontView" : "http://example.com/products/123_front.jpg"
    }
}
DynamoDBHashKey
Maps a class property to the partition key of the table. The property must be one of the scalar string,
number or binary types; it cannot be a collection type.
Assume that you have a table, ProductCatalog, that has Id as the primary key. The following Java code
snippet defines a CatalogItem class and maps its Id property to the primary key of the ProductCatalog
table using the @DynamoDBHashKey tag.
@DynamoDBTable(tableName="ProductCatalog")
public class CatalogItem {
     private Integer Id;
   @DynamoDBHashKey(attributeName="Id")
   public Integer getId() {
         return Id;
   }
   public void setId(Integer Id) {
         this.Id = Id;
   }
   // Additional properties go here.
DynamoDBIgnore
Indicates to the DynamoDBMapper instance that the associated property should be ignored. When saving
data to the table, the DynamoDBMapper does not save this property to the table.
DynamoDBIndexHashKey
Maps a class property to the partition key of a global secondary index. The property must be one of the
scalar string, number or binary types; it cannot be a collection type.
Use this annotation if you need to Query a global secondary index. You must specify the index name
(globalSecondaryIndexName). If the name of the class property is different from the index partition
key, you must also specify the name of that index attribute (attributeName).
DynamoDBIndexRangeKey
Maps a class property to the sort key of a global secondary index or a local secondary index. The
property must be one of the scalar string, number or binary types; it cannot be a collection type.
Use this annotation if you need to Query a local secondary index or a global secondary index
and want to refine your results using the index sort key. You must specify the index name (either
globalSecondaryIndexName or localSecondaryIndexName). If the name of the class
property is different from the index sort key, you must also specify the name of that index attribute
(attributeName).
DynamoDBRangeKey
Maps a class property to the sort key of the table. The property must be one of the scalar string, number
or binary types; it cannot be a collection type.
If the primary key is composite (partition key and sort key), you can use this tag to map your class
field to the sort key. For example, assume that you have a Reply table that stores replies for forum
threads. Each thread can have many replies. So the primary key of this table is both the ThreadId and
ReplyDateTime. The ThreadId is the partition key and ReplyDateTime is the sort key. The following Java
code snippet defines a Reply class and maps it to the Reply table. It uses both the @DynamoDBHashKey
and @DynamoDBRangeKey tags to identify class properties that map to the primary key.
@DynamoDBTable(tableName="Reply")
public class Reply {
    private Integer id;
    private String replyDateTime;
     @DynamoDBHashKey(attributeName="Id")
     public Integer getId() { return id; }
     public void setId(Integer id) { this.id = id; }
    @DynamoDBRangeKey(attributeName="ReplyDateTime")
    public String getReplyDateTime() { return replyDateTime; }
    public void setReplyDateTime(String replyDateTime) { this.replyDateTime =
 replyDateTime; }
DynamoDBTable
Identifies the target table in DynamoDB. For example, the following Java code snippet defines a class
Developer and maps it to the People table in DynamoDB.
@DynamoDBTable(tableName="People")
public class Developer { ...}
The @DynamoDBTable annotation can be inherited. Any new class that inherits from the Developer
class also maps to the People table. For example, assume that you create a Lead class that inherits from
the Developer class. Because you mapped the Developer class to the People table, the Lead class
objects are also stored in the same table.
The @DynamoDBTable can also be overridden. Any new class that inherits from the Developer class by
default maps to the same People table. However, you can override this default mapping. For example, if
you create a class that inherits from the Developer class, you can explicitly map it to another table by
adding the @DynamoDBTable annotation as shown in the following Java code snippet.
@DynamoDBTable(tableName="Managers")
public class Manager extends Developer { ...}
DynamoDBTypeConverted
Annotation to mark a property as using a custom type-converter. May be annotated on a user-defined
annotation to pass additional properties to the DynamoDBTypeConverter.
The DynamoDBTypeConverter interface lets you map your own arbitrary data types to a data type that
is natively supported by DynamoDB. For more information, see Mapping Arbitrary Data (p. 229).
DynamoDBTyped
Annotation to override the standard attribute type binding. Standard types do not require the
annotation if applying the default attribute binding for that type.
DynamoDBVersionAttribute
Identifies a class property for storing an optimistic locking version number. DynamoDBMapper
assigns a version number to this property when it saves a new item, and increments it each time you
update the item. Only number scalar types are supported. For more information about data type,
see Data Types (p. 12). For more information about versioning, see Optimistic Locking With Version
Number (p. 227).
For the corresponding Javadoc documentation, see DynamoDBMapper in the AWS SDK for Java API
Reference.
Topics
 •   save (p. 203)
 •   load (p. 203)
 •   delete (p. 203)
 •   query (p. 204)
 •   queryPage (p. 205)
 • scan (p. 205)
 • scanPage (p. 206)
save
Saves the specified object to the table. The object that you wish to save is the only required parameter
for this method. You can provide optional configuration parameters using the DynamoDBMapperConfig
object.
If an item that has the same primary key does not exist, this method creates a new item in the table.
If an item that has the same primary key exists, it updates the existing item. If the partition key and
sort key are of type String, and annotated with @DynamoDBAutoGeneratedKey, then they are
given a random universally unique identifier (UUID) if left uninitialized. Version fields annotated with
@DynamoDBVersionAttribute will be incremented by one. Additionally, if a version field is updated or
a key generated, the object passed in is updated as a result of the operation.
By default, only attributes corresponding to mapped class properties are updated; any additional existing
attributes on an item are unaffected. However, if you specify SaveBehavior.CLOBBER, you can force
the item to be completely overwritten.
If you have versioning enabled, then the client-side and server-side item versions must match.
However, the version does not need to match if the SaveBehavior.CLOBBER option is used. For more
information about versioning, see Optimistic Locking With Version Number (p. 227).
load
Retrieves an item from a table. You must provide the primary key of the item that you wish to retrieve.
You can provide optional configuration parameters using the DynamoDBMapperConfig object. For
example, you can optionally request strongly consistent reads to ensure that this method retrieves only
the latest item values as shown in the following Java statement.
By default, DynamoDB returns the item that has values that are eventually consistent. For information
about the eventual consistency model of DynamoDB, see Read Consistency (p. 15).
delete
Deletes an item from the table. You must pass in an object instance of the mapped class.
If you have versioning enabled, then the client-side and server-side item versions must match.
However, the version does not need to match if the SaveBehavior.CLOBBER option is used. For more
information about versioning, see Optimistic Locking With Version Number (p. 227).
query
Queries a table or a secondary index. You can query a table or an index only if it has a composite primary
key (partition key and sort key). This method requires you to provide a partition key value and a query
filter that is applied on the sort key. A filter expression includes a condition and a value.
Assume that you have a table, Reply, that stores forum thread replies. Each thread subject can have 0 or
more replies. The primary key of the Reply table consists of the Id and ReplyDateTime fields, where Id is
the partition key and ReplyDateTime is the sort key of the primary key.
Now, assume that you have created a mapping between a Reply class and the corresponding Reply table
in DynamoDB. The following Java code snippet uses DynamoDBMapper to find all replies in the past two
weeks for a specific thread subject.
Example
By default, the query method returns a "lazy-loaded" collection. It initially returns only one page of
results, and then makes a service call for the next page if needed. To obtain all the matching items, you
only need to iterate over the latestReplies collection.
To query an index, you must first model the index as a mapper class. Suppose that the Reply table has a
global secondary index named PostedBy-Message-Index. The partition key for this index is PostedBy, and
the sort key is Message. The class definition for an item in the index would look like this:
@DynamoDBTable(tableName="Reply")
public class PostedByMessage {
    private String postedBy;
    private String message;
    @DynamoDBIndexHashKey(globalSecondaryIndexName = "PostedBy-Message-Index",
 attributeName = "PostedBy")
    public String getPostedBy() { return postedBy; }
    public void setPostedBy(String postedBy) { this.postedBy = postedBy; }
    @DynamoDBIndexRangeKey(globalSecondaryIndexName = "PostedBy-Message-Index",
 attributeName = "Message")
The @DynamoDBTable annotation indicates that this index is associated with the Reply table. The
@DynamoDBIndexHashKey annotation denotes the partition key (PostedBy) of the index, and
@DynamoDBIndexRangeKey denotes the sort key (Message) of the index.
Now you can use DynamoDBMapper to query the index, retrieving a subset of messages that were
posted by a particular user. You must specify withIndexName so that DynamoDB knows which index
to query. In the following code snippet, we are querying a global secondary index. Because global
secondary indexes support eventually consistent reads, but not strongly consistent reads, we must
specify withConsistentRead(false).
queryPage
Queries a table or secondary index and returns a single page of matching results. As with the query
method, you must specify a partition key value and a query filter that is applied on the sort key attribute.
However, queryPage will only return the first "page" of data - that is, the amount of data that will fit
within 1 MB
scan
Scans an entire table or a secondary index. You can optionally specify a FilterExpression to filter the
result set.
Assume that you have a table, Reply, that stores forum thread replies. Each thread subject can have 0 or
more replies. The primary key of the Reply table consists of the Id and ReplyDateTime fields, where Id is
the partition key and ReplyDateTime is the sort key of the primary key.
If you have mapped a Java class to the Reply table, you can use the DynamoDBMapper to scan the table.
For example, the following Java code snippet scans the entire Reply table, returning only the replies for
a particular year.
Example
    .withFilterExpression("begins_with(ReplyDateTime,:v1)")
    .withExpressionAttributeValues(eav);
By default, the scan method returns a "lazy-loaded" collection. It initially returns only one page of
results, and then makes a service call for the next page if needed. To obtain all the matching items, you
only need to iterate over the replies collection.
To scan an index, you must first model the index as a mapper class. Suppose that the Reply table has a
global secondary index named PostedBy-Message-Index. The partition key for this index is PostedBy, and
the sort key is Message. A mapper class for this index is shown in the query (p. 204) section, where we
use the @DynamoDBIndexHashKey and @DynamoDBIndexRangeKey annotations to specify the index
partition key and sort key.
The following code snippet scans PostedBy-Message-Index. It does not use a scan filter, so all of the items
in the index are returned to you.
scanPage
Scans a table or secondary index and returns a single page of matching results. As with the scan
method, you can optionally specify a FilterExpression to filter the result set. However, scanPage
will only return the first "page" of data - that is, the amount of data that will fit within 1 MB
parallelScan
Performs a parallel scan of an entire table or secondary index. You specify a number of logical segments
for the table, along with a scan expression to filter the results. The parallelScan divides the scan
task among multiple workers, one for each logical segment; the workers process the data in parallel and
return the results.
The following Java code snippet performs a parallel scan on the Product table.
int numberOfThreads = 4;
For a Java code sample illustrating usage of parallelScan, see Example: Query and Scan (p. 218).
batchSave
Saves objects to one or more tables using one or more calls to the AmazonDynamoDB.batchWriteItem
method. This method does not provide transaction guarantees.
The following Java code snippet saves two items (books) to the ProductCatalog table.
mapper.batchSave(Arrays.asList(book1, book2));
batchLoad
Retrieves multiple items from one or more tables using their primary keys.
The following Java code snippet retrieves two items from two different tables.
batchDelete
Deletes objects from one or more tables using one or more calls to the
AmazonDynamoDB.batchWriteItem method. This method does not provide transaction guarantees.
The following Java code snippet deletes two items (books) from the ProductCatalog table.
batchWrite
Saves objects to and deletes objects from one or more tables using one or more calls to the
AmazonDynamoDB.batchWriteItem method. This method does not provide transaction guarantees or
support versioning (conditional puts or deletes).
The following Java code snippet writes a new item to the Forum table, writes a new item to the Thread
table, and deletes an item from the ProductCatalog table.
mapper.batchWrite(objectsToWrite, objectsToDelete);
count
Evaluates the specified scan expression and returns the count of matching items. No item data is
returned.
generateCreateTableRequest
Parses a POJO class that represents a DynamoDB table, and returns a CreateTableRequest for that
table.
createS3Link
Creates a link to an object in Amazon S3. You must specify a bucket name and a key name, which
uniquely identifies the object in the bucket.
To use createS3Link, your mapper class must define getter and setter methods. The following code
snippet illustrates this by adding a new attribute and getter/setter methods to the CatalogItem class:
@DynamoDBTable(tableName="ProductCatalog")
public class CatalogItem {
...
....
      @DynamoDBAttribute(attributeName = "ProductImage")
      public S3Link getProductImage() {
              return productImage;
      }
...
}
The following Java code defines a new item to be written to the Product table. The item includes a link
to a product image; the image data is uploaded to Amazon S3.
item.id = 150;
item.title = "Book 150 Title";
item.getProductImage().uploadFrom(new File("/file/path/book_150_cover.jpg"));
mapper.save(item);
The S3Link class provides many other methods for manipulating objects in Amazon S3. For more
information, see the Javadocs for S3Link.
getS3ClientCache
Returns the underlying S3ClientCache for accessing Amazon S3. An S3ClientCache is a smart Map
for AmazonS3Client objects. If you have multiple clients, then an S3ClientCache can help you keep the
clients organized by region, and can create new Amazon S3 clients on demand.
For more information, see DynamoDBMapperConfig in the AWS SDK for Java API Reference.
  If you do not specify a read consistency setting for your mapper instance, the default is EVENTUAL.
• A DynamoDBMapperConfig.PaginationLoadingStrategy enumeration value—Controls how the
  mapper instance processes a paginated list of data, such as the results from a query or scan:
  • LAZY_LOADING— the mapper instance loads data when possible, and keep all loaded results in
    memory.
  • EAGER_LOADING—the mapper instance loads the data as soon as the list is initialized.
  • ITERATION_ONLY—you can only use an Iterator to read from the list. During the iteration, the list
    will clear all the previous results before loading the next page, so that the list will keep at most
    one page of the loaded results in memory. This also means the list can only be iterated once. This
    strategy is recommended when handling large items, in order to reduce memory overhead.
  If you do not specify a pagination loading strategy for your mapper instance, the default is
  LAZY_LOADING.
• A DynamoDBMapperConfig.SaveBehavior enumeration value - Specifies how the mapper instance
  should deal with attributes during save operations:
  • UPDATE—during a save operation, all modeled attributes are updated, and unmodeled attributes are
    unaffected. Primitive number types (byte, int, long) are set to 0. Object types are set to null.
  • CLOBBER—clears and replaces all attributes, included unmodeled ones, during a save operation. This
    is done be deleting the item and re-creating it. Versioned field constraints are also disregarded.
  If you do not specify the save behavior for your mapper instance, the default is UPDATE.
• A DynamoDBMapperConfig.TableNameOverride object—Instructs the mapper instance to ignore
  the table name specified by a class's DynamoDBTable annotation, and instead use a different table
  name that you supply. This is useful when partitioning your data into multiple tables at run time.
You can override the default configuration object for DynamoDBMapper per operation, as needed.
import   java.io.IOException;
import   java.util.Arrays;
import   java.util.HashSet;
import   java.util.Set;
import   com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
import   com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder;
import   com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBAttribute;
import   com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBHashKey;
import   com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper;
import   com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapperConfig;
import   com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBTable;
    @DynamoDBTable(tableName = "ProductCatalog")
    public static class CatalogItem {
        private Integer id;
        private String title;
        private String ISBN;
        private Set<String> bookAuthors;
          // Partition key
          @DynamoDBHashKey(attributeName = "Id")
          public Integer getId() {
              return id;
          }
       @DynamoDBAttribute(attributeName = "Title")
       public String getTitle() {
           return title;
       }
       @DynamoDBAttribute(attributeName = "ISBN")
       public String getISBN() {
           return ISBN;
       }
       @DynamoDBAttribute(attributeName = "Authors")
       public Set<String> getBookAuthors() {
           return bookAuthors;
       }
       @Override
       public String toString() {
           return "Book [ISBN=" + ISBN + ", bookAuthors=" + bookAuthors + ", id=" + id +
", title=" + title + "]";
       }
   }
For more information about the tables used in this example, see Creating Tables and Loading
Sample Data (p. 281). For step-by-step instructions to test the following sample, see Java Code
Samples (p. 286).
Example
import   java.text.SimpleDateFormat;
import   java.util.ArrayList;
import   java.util.Arrays;
import   java.util.HashSet;
import   java.util.List;
import   java.util.Set;
import   com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
import   com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder;
import   com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBAttribute;
import   com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBHashKey;
import   com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper;
import   com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapperConfig;
import   com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBRangeKey;
import   com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBTable;
        testBatchSave(mapper);
        testBatchDelete(mapper);
        testBatchWrite(mapper);
System.out.println("Example complete!");
    }
    catch (Throwable t) {
        System.err.println("Error running the DynamoDBMapperBatchWriteExample: " + t);
        t.printStackTrace();
    }
}
       threadItem.forumName = "AmazonDynamoDB";
       threadItem.subject = "My sample question";
       threadItem.message = "BatchWrite message";
       List<String> tags = new ArrayList<String>();
       tags.add("batch operations");
       tags.add("write");
       threadItem.tags = new HashSet<String>(tags);
   @DynamoDBTable(tableName = "ProductCatalog")
   public static class Book {
       private int id;
       private String title;
       private String ISBN;
       private int price;
       private int pageCount;
       private String productCategory;
       private boolean inPublication;
       // Partition key
       @DynamoDBHashKey(attributeName = "Id")
       public int getId() {
           return id;
       }
       @DynamoDBAttribute(attributeName = "Title")
       public String getTitle() {
           return title;
       }
       @DynamoDBAttribute(attributeName = "ISBN")
       public String getISBN() {
           return ISBN;
       }
       @DynamoDBAttribute(attributeName = "Price")
       public int getPrice() {
           return price;
       }
       @DynamoDBAttribute(attributeName = "PageCount")
       public int getPageCount() {
           return pageCount;
       }
       @DynamoDBAttribute(attributeName = "ProductCategory")
       public String getProductCategory() {
           return productCategory;
       }
       @DynamoDBAttribute(attributeName = "InPublication")
       public boolean getInPublication() {
           return inPublication;
       }
       @Override
       public String toString() {
           return "Book [ISBN=" + ISBN + ", price=" + price + ", product category=" +
productCategory + ", id=" + id
               + ", title=" + title + "]";
       }
   @DynamoDBTable(tableName = "Reply")
   public static class Reply {
       private String id;
       private String replyDateTime;
       private String message;
       private String postedBy;
       // Partition key
       @DynamoDBHashKey(attributeName = "Id")
       public String getId() {
           return id;
       }
       // Sort key
       @DynamoDBRangeKey(attributeName = "ReplyDateTime")
       public String getReplyDateTime() {
           return replyDateTime;
       }
       @DynamoDBAttribute(attributeName = "Message")
       public String getMessage() {
           return message;
    @DynamoDBAttribute(attributeName = "PostedBy")
    public String getPostedBy() {
        return postedBy;
    }
@DynamoDBTable(tableName = "Thread")
public static class Thread {
    private String forumName;
    private String subject;
    private String message;
    private String lastPostedDateTime;
    private String lastPostedBy;
    private Set<String> tags;
    private int answered;
    private int views;
    private int replies;
    // Partition key
    @DynamoDBHashKey(attributeName = "ForumName")
    public String getForumName() {
        return forumName;
    }
    // Sort key
    @DynamoDBRangeKey(attributeName = "Subject")
    public String getSubject() {
        return subject;
    }
    @DynamoDBAttribute(attributeName = "Message")
    public String getMessage() {
        return message;
    }
    @DynamoDBAttribute(attributeName = "LastPostedDateTime")
    public String getLastPostedDateTime() {
        return lastPostedDateTime;
    }
    @DynamoDBAttribute(attributeName = "LastPostedBy")
    public String getLastPostedBy() {
        return lastPostedBy;
    }
    @DynamoDBAttribute(attributeName = "Tags")
    public Set<String> getTags() {
        return tags;
    }
    @DynamoDBAttribute(attributeName = "Answered")
    public int getAnswered() {
        return answered;
    }
    @DynamoDBAttribute(attributeName = "Views")
    public int getViews() {
        return views;
    }
    @DynamoDBAttribute(attributeName = "Replies")
    public int getReplies() {
        return replies;
    }
@DynamoDBTable(tableName = "Forum")
public static class Forum {
    private String name;
    private String category;
    private int threads;
    // Partition key
    @DynamoDBHashKey(attributeName = "Name")
    public String getName() {
        return name;
    }
    @DynamoDBAttribute(attributeName = "Category")
    public String getCategory() {
        return category;
           @DynamoDBAttribute(attributeName = "Threads")
           public int getThreads() {
               return threads;
           }
The example then executes the follow query and scan operations using a DynamoDBMapper instance.
    The ProductCatalog table has Id as its primary key. It does not have a sort key as part of its primary
    key. Therefore, you cannot query the table. You can get an item using its id value.
• Execute the following queries against the Reply table.
    The Reply table's primary key is composed of Id and ReplyDateTime attributes. The ReplyDateTime is a
    sort key. Therefore, you can query this table.
    • Find replies to a forum thread posted in the last 15 days
    • Find replies to a forum thread posted in a specific date range
• Scan ProductCatalog table to find books whose price is less than a specified value.
    For performance reasons, you should use query instead of the scan operation. However, there are
    times you might need to scan a table. Suppose there was a data entry error and one of the book
    prices was set to less than 0. This example scans the ProductCategory table to find book items
    (ProductCategory is book) and price is less than 0.
• Perform a parallel scan of the ProductCatalog table to find bicycles of a specific type.
      Note
      This code sample assumes that you have already loaded data into DynamoDB for your account
      by following the instructions in the Creating Tables and Loading Sample Data (p. 281) section.
      For step-by-step instructions to run the following example, see Java Code Samples (p. 286).
Example
import   java.text.SimpleDateFormat;
import   java.util.Date;
import   java.util.HashMap;
import   java.util.List;
import   java.util.Map;
import   java.util.Set;
import   java.util.TimeZone;
import   com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
import   com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder;
import   com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBAttribute;
import   com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBHashKey;
import   com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper;
import   com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBQueryExpression;
import   com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBRangeKey;
import   com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBScanExpression;
import   com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBTable;
import   com.amazonaws.services.dynamodbv2.model.AttributeValue;
              // Scan a table and find book items priced less than specified
              // value.
              FindBooksPricedLessThanSpecifiedValue(mapper, "20");
              // Scan a table with multiple threads and find bicycle items with a
              // specified bicycle type
              int numberOfThreads = 16;
              FindBicyclesOfSpecificTypeWithMultipleThreads(mapper, numberOfThreads, "Road");
System.out.println("Example complete!");
          }
          catch (Throwable t) {
              System.err.println("Error running the DynamoDBMapperQueryScanExample: " + t);
              t.printStackTrace();
          }
    }
        System.out.println(
            "FindRepliesPostedWithinTimePeriod: Find replies for thread Message = 'DynamoDB
 Thread 2' posted within a period.");
        long startDateMilli = (new Date()).getTime() - (14L * 24L * 60L * 60L * 1000L); //
 Two
                                                                                        //
 weeks
                                                                                        //
 ago.
        long endDateMilli = (new Date()).getTime() - (7L * 24L * 60L * 60L * 1000L); // One
                                                                                     //
 week
                                                                                     //
 ago.
        SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-
dd'T'HH:mm:ss.SSS'Z'");
        dateFormatter.setTimeZone(TimeZone.getTimeZone("UTC"));
        String startDate = dateFormatter.format(startDateMilli);
        String endDate = dateFormatter.format(endDateMilli);
       System.out.println("FindBicyclesOfSpecificTypeWithMultipleThreads: Scan
ProductCatalog With Multiple Threads.");
       Map<String, AttributeValue> eav = new HashMap<String, AttributeValue>();
       eav.put(":val1", new AttributeValue().withS("Bicycle"));
       eav.put(":val2", new AttributeValue().withS(bicycleType));
   @DynamoDBTable(tableName = "ProductCatalog")
   public static class Book {
       private int id;
       private String title;
       private String ISBN;
       private int price;
       private int pageCount;
       private String productCategory;
       private boolean inPublication;
@DynamoDBHashKey(attributeName = "Id")
       @DynamoDBAttribute(attributeName = "Title")
       public String getTitle() {
           return title;
       }
       @DynamoDBAttribute(attributeName = "ISBN")
       public String getISBN() {
           return ISBN;
       }
       @DynamoDBAttribute(attributeName = "Price")
       public int getPrice() {
           return price;
       }
       @DynamoDBAttribute(attributeName = "PageCount")
       public int getPageCount() {
           return pageCount;
       }
       @DynamoDBAttribute(attributeName = "ProductCategory")
       public String getProductCategory() {
           return productCategory;
       }
       @DynamoDBAttribute(attributeName = "InPublication")
       public boolean getInPublication() {
           return inPublication;
       }
       @Override
       public String toString() {
           return "Book [ISBN=" + ISBN + ", price=" + price + ", product category=" +
productCategory + ", id=" + id
@DynamoDBTable(tableName = "ProductCatalog")
public static class Bicycle {
    private int id;
    private String title;
    private String description;
    private String bicycleType;
    private String brand;
    private int price;
    private List<String> color;
    private String productCategory;
    @DynamoDBHashKey(attributeName = "Id")
    public int getId() {
        return id;
    }
    @DynamoDBAttribute(attributeName = "Title")
    public String getTitle() {
        return title;
    }
    @DynamoDBAttribute(attributeName = "Description")
    public String getDescription() {
        return description;
    }
    @DynamoDBAttribute(attributeName = "BicycleType")
    public String getBicycleType() {
        return bicycleType;
    }
    @DynamoDBAttribute(attributeName = "Brand")
    public String getBrand() {
        return brand;
    }
    @DynamoDBAttribute(attributeName = "Price")
    public int getPrice() {
        return price;
    }
       @DynamoDBAttribute(attributeName = "Color")
       public List<String> getColor() {
           return color;
       }
       @DynamoDBAttribute(attributeName = "ProductCategory")
       public String getProductCategory() {
           return productCategory;
       }
       @Override
       public String toString() {
           return "Bicycle [Type=" + bicycleType + ", color=" + color + ", price=" + price
+ ", product category="
               + productCategory + ", id=" + id + ", title=" + title + "]";
       }
   @DynamoDBTable(tableName = "Reply")
   public static class Reply {
       private String id;
       private String replyDateTime;
       private String message;
       private String postedBy;
       // Partition key
       @DynamoDBHashKey(attributeName = "Id")
       public String getId() {
           return id;
       }
       // Range key
       @DynamoDBRangeKey(attributeName = "ReplyDateTime")
       public String getReplyDateTime() {
           return replyDateTime;
       }
       @DynamoDBAttribute(attributeName = "Message")
       public String getMessage() {
           return message;
       }
    @DynamoDBAttribute(attributeName = "PostedBy")
    public String getPostedBy() {
        return postedBy;
    }
@DynamoDBTable(tableName = "Thread")
public static class Thread {
    private String forumName;
    private String subject;
    private String message;
    private String lastPostedDateTime;
    private String lastPostedBy;
    private Set<String> tags;
    private int answered;
    private int views;
    private int replies;
    // Partition key
    @DynamoDBHashKey(attributeName = "ForumName")
    public String getForumName() {
        return forumName;
    }
    // Range key
    @DynamoDBRangeKey(attributeName = "Subject")
    public String getSubject() {
        return subject;
    }
    @DynamoDBAttribute(attributeName = "Message")
    public String getMessage() {
        return message;
    }
    @DynamoDBAttribute(attributeName = "LastPostedDateTime")
    public String getLastPostedDateTime() {
        return lastPostedDateTime;
    }
    @DynamoDBAttribute(attributeName = "LastPostedBy")
    public String getLastPostedBy() {
        return lastPostedBy;
    }
    @DynamoDBAttribute(attributeName = "Tags")
    public Set<String> getTags() {
        return tags;
    }
    @DynamoDBAttribute(attributeName = "Answered")
    public int getAnswered() {
        return answered;
    }
    @DynamoDBAttribute(attributeName = "Views")
    public int getViews() {
        return views;
    }
    @DynamoDBAttribute(attributeName = "Replies")
    public int getReplies() {
        return replies;
    }
@DynamoDBTable(tableName = "Forum")
public static class Forum {
    private String name;
    private String category;
    private int threads;
    @DynamoDBHashKey(attributeName = "Name")
    public String getName() {
        return name;
    }
    @DynamoDBAttribute(attributeName = "Category")
    public String getCategory() {
        return category;
    }
         @DynamoDBAttribute(attributeName = "Threads")
         public int getThreads() {
             return threads;
         }
With optimistic locking, each item has an attribute that acts as a version number. If you retrieve an item
from a table, the application records the version number of that item. You can update the item, but only
if the version number on the server side has not changed. If there is a version mismatch, it means that
someone else has modified the item before you did; the update attempt fails, because you have a stale
version of the item. If this happens, you simply try again by retrieving the item and then attempting to
update it. Optimistic locking prevents you from accidentally overwriting changes that were made by
others; it also prevents others from accidentally overwriting your changes.
To support optimistic locking, the AWS SDK for Java provides the @DynamoDBVersionAttribute
annotation. In the mapping class for your table, you designate one property to store the version number,
and mark it using this annotation. When you save an object, the corresponding item in the DynamoDB
table will have an attribute that stores the version number. The DynamoDBMapper assigns a version
number when you first save the object, and it automatically increments the version number each time
you update the item. Your update or delete requests will succeed only if the client-side object version
matches the corresponding version number of the item in the DynamoDB table.
• You use optimistic locking with @DynamoDBVersionAttribute and the version value on the server is
  different from the value on the client side.
• You specify your own conditional constraints while saving data by using DynamoDBMapper with
  DynamoDBSaveExpression and these constraints failed.
For example, the following Java code snippet defines a CatalogItem class that has several properties.
The Version property is tagged with the @DynamoDBVersionAttribute annotation.
Example
@DynamoDBTable(tableName="ProductCatalog")
public class CatalogItem {
    @DynamoDBHashKey(attributeName="Id")
    public Integer getId() { return id; }
    public void setId(Integer Id) { this.id = Id; }
      @DynamoDBAttribute(attributeName="Title")
      public String getTitle() { return title; }
      public void setTitle(String title) { this.title = title; }
      @DynamoDBAttribute(attributeName="ISBN")
      public String getISBN() { return ISBN; }
      public void setISBN(String ISBN) { this.ISBN = ISBN;}
      @DynamoDBAttribute(attributeName = "Authors")
      public Set<String> getBookAuthors() { return bookAuthors; }
      public void setBookAuthors(Set<String> bookAuthors) { this.bookAuthors = bookAuthors; }
      @DynamoDBIgnore
      public String getSomeProp() { return someProp;}
      public void setSomeProp(String someProp) {this.someProp = someProp;}
      @DynamoDBVersionAttribute
      public Long getVersion() { return version; }
      public void setVersion(Long version) { this.version = version;}
}
You can apply the @DynamoDBVersionAttribute annotation to nullable types provided by the
primitive wrappers classes that provide a nullable type, such as Long and Integer.
• save — For a new item, the DynamoDBMapper assigns an initial version number 1. If you
  retrieve an item, update one or more of its properties and attempt to save the changes, the save
  operation succeeds only if the version number on the client-side and the server-side match. The
  DynamoDBMapper increments the version number automatically.
• delete — The delete method takes an object as parameter and the DynamoDBMapper
  performs a version check before deleting the item. The version check can be disabled if
  DynamoDBMapperConfig.SaveBehavior.CLOBBER is specified in the request.
    The internal implementation of optimistic locking within DynamoDBMapper uses conditional update
    and conditional delete support provided by DynamoDB.
You can also set locking behavior for a specific operation only. For example, the
following Java snippet uses the DynamoDBMapper to save a catalog item. It specifies
DynamoDBMapperConfig.SaveBehavior by adding the optional DynamoDBMapperConfig parameter
to the save method.
Example
For example, consider the following CatalogItem class that defines a property, Dimension, that is of
DimensionType. This property stores the item dimensions, as height, width, and thickness. Assume that
you decide to store these item dimensions as a string (such as 8.5x11x.05) in DynamoDB. The following
example provides converter code that converts the DimensionType object to a string and a string to the
DimensionType.
    Note
    This code sample assumes that you have already loaded data into DynamoDB for your account
    by following the instructions in the Creating Tables and Loading Sample Data (p. 281) section.
    For step-by-step instructions to run the following example, see Java Code Samples (p. 286).
Example
package com.amazonaws.codesamples.datamodeling;
import   java.io.IOException;
import   java.util.Arrays;
import   java.util.HashSet;
import   java.util.Set;
import   com.amazonaws.regions.Regions;
import   com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
import   com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder;
import   com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBAttribute;
import   com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBHashKey;
import   com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper;
import   com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBTypeConverted;
import   com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBTypeConverter;
import   com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBTable;
    book.setId(502);
    book.setTitle("Book 502");
    book.setISBN("555-5555555555");
    book.setBookAuthors(new HashSet<String>(Arrays.asList("Author1", "Author2")));
    book.setDimensions(dimType);
    bookRetrieved.getDimensions().setHeight("9.0");
    bookRetrieved.getDimensions().setLength("12.0");
    bookRetrieved.getDimensions().setThickness("2.0");
mapper.save(bookRetrieved);
@DynamoDBTable(tableName = "ProductCatalog")
public static class Book {
    private int id;
    private String title;
    private String ISBN;
    private Set<String> bookAuthors;
    private DimensionType dimensionType;
    // Partition key
    @DynamoDBHashKey(attributeName = "Id")
    public int getId() {
        return id;
    }
    @DynamoDBAttribute(attributeName = "Title")
    public String getTitle() {
        return title;
    }
    @DynamoDBAttribute(attributeName = "ISBN")
    public String getISBN() {
        return ISBN;
    }
    @DynamoDBAttribute(attributeName = "Authors")
    public Set<String> getBookAuthors() {
        return bookAuthors;
    }
       @DynamoDBTypeConverted(converter = DimensionTypeConverter.class)
       @DynamoDBAttribute(attributeName = "Dimensions")
       public DimensionType getDimensions() {
           return dimensionType;
       }
       @DynamoDBAttribute(attributeName = "Dimensions")
       public void setDimensions(DimensionType dimensionType) {
           this.dimensionType = dimensionType;
       }
       @Override
       public String toString() {
           return "Book [ISBN=" + ISBN + ", bookAuthors=" + bookAuthors + ",
dimensionType= "
               + dimensionType.getHeight() + " X " + dimensionType.getLength() + " X " +
dimensionType.getThickness()
               + ", Id=" + id + ", Title=" + title + "]";
       }
   }
       @Override
       public String convert(DimensionType object) {
           DimensionType itemDimensions = (DimensionType) object;
           String dimension = null;
           try {
               if (itemDimensions != null) {
                   dimension = String.format("%s x %s x %s", itemDimensions.getLength(),
itemDimensions.getHeight(),
                       itemDimensions.getThickness());
                    }
                }
                catch (Exception e) {
                    e.printStackTrace();
                }
                return dimension;
           }
           @Override
           public DimensionType unconvert(String s) {
                return itemDimension;
           }
      }
}
The AWS SDK for .NET provides document model classes that wrap some of the low-level DynamoDB
operations, to further simplify your coding. In the document model, the primary classes are Table
and Document. The Table class provides data operation methods such as PutItem, GetItem, and
DeleteItem. It also provides the Query and the Scan methods. The Document class represents a single
item in a table.
Working with Items in DynamoDB Using the AWS SDK for .NET
Document Model
Topics
 • Putting an Item - Table.PutItem Method (p. 234)
 • Specifying Optional Parameters (p. 235)
To perform data operations using the document model, you must first call the Table.LoadTable
method, which creates an instance of the Table class that represents a specific table. The following C#
snippet creates a Table object that represents the ProductCatalog table in DynamoDB.
Example
    Note
    In general, you use the LoadTable method once at the beginning of your application because it
    makes a DescribeTable call that adds to the round trip to DynamoDB.
You can then use the table object to perform various data operations. Each of these data operations have
two types of overloads; one that takes the minimum required parameters and another that also takes
operation specific optional configuration information. For example, to retrieve an item, you must provide
the table's primary key value in which case you can use the following GetItem overload:
Example
// Get the item from a table that has a primary key that is composed of only a partition
 key.
Table.GetItem(Primitive partitionKey);
// Get the item from a table whose primary key is composed of both a partition key and sort
 key.
Table.GetItem(Primitive partitionKey, Primitive sortKey);
You can also pass optional parameters to these methods. For example, the preceding GetItem returns
the entire item including all its attributes. You can optionally specify a list of attributes to retrieve. In this
case, you use the following GetItem overload that takes in the operation specific configuration object
parameter:
Example
You can use the configuration object to specify several optional parameters such as request a specific
list of attributes or specify the page size (number of items per page). Each data operation method has
its own configuration class. For example, the GetItemOperationConfig class enables you to provide
options for the GetItem operation and the PutItemOperationConfig class enables you to provide
optional parameters for the PutItem operation.
The following sections discuss each of the data operations that are supported by the Table class.
1. Execute the Table.LoadTable method that provides the table name in which you want to put an
   item.
2. Create a Document object that has a list of attribute names and their values.
3. Execute Table.PutItem by providing the Document instance as a parameter.
The following C# code snippet demonstrates the preceding tasks. The example uploads an item to the
ProductCatalog table.
Example
table.PutItem(book);
In the preceding example, the Document instance creates an item that has Number, String, String
Set, Boolean, and Null attributes. (Null is used to indicate that the QuantityOnHand for this product is
unknown.) For Boolean and Null, use the constructor methods DynamoDBBool and DynamoDBNull.
In DynamoDB, the List and Map data types can contain elements composed of other data types. Here is
how to map these data types to the document model API:.
You can modify the preceding example to add a List attribute to the item. To do this, use a
DynamoDBList constructor, as shown in the following code snippet:
Example
table.PutItem(book);
To add a Map attribute to the book, you define another Document. The following code snippet illustrates
how to do this.
Example
book.Add("Pictures", pictures);
table.PutItem(book);
These examples are based on the item shown in Specifying Item Attributes (p. 337). The document
model lets you create complex nested attributes, such as the ProductReviews attribute shown in the
case study.
• The ConditionalExpression parameter to make this a conditional put request. The example
  creates an expression that specifies the ISBN attribute must have a specific value that has to be
  present in the item that you are replacing.
Example
table.PutItem(book, config);
Example
The GetItem operation returns all the attributes of the item and performs an eventually consistent read
(see Read Consistency (p. 15)) by default.
Example
When you retrieve an item using the document model API, you can access individual elements within the
Document object is returned:
Example
int id = doc["Id"].AsInt();
string title = doc["Title"].AsString();
List<string> authors = doc["Authors"].AsListOfString();
bool inStock = doc["InStock"].AsBoolean();
DynamoDBNull quantityOnHand = doc["QuantityOnHand"].AsDynamoDBNull();
For attributes that are of type List or Map, here is how to map these attributes to the document model
API:
The following code snippet shows how to retrieve a List (RelatedItems) and a Map (Pictures) from the
Document object:
Example
Example
• The ConditionalExpression parameter to ensure that the book item being deleted has a specific
  value for the ISBN attribute.
• The ReturnValues parameter to request that the Delete method return the item that it deleted.
Example
You can use the UpdateItem operation to update existing attribute values, add new attributes to
the existing collection, or delete attributes from the existing collection. You provide these updates by
creating a Document instance that describes the updates you wish to perform.
• If the item does not exist, UpdateItem adds a new item using the primary key that is specified in the
  input.
• If the item exists, UpdateItem applies the updates as follows:
  • Replaces the existing attribute values with the values in the update.
  • If an attribute that you provide in the input does not exist, it adds a new attribute to the item.
  • If the input attribute value is null, it deletes the attributes, if it is present.
    Note
    This mid-level UpdateItem operation does not support the Add action (see UpdateItem)
    supported by the underlying DynamoDB operation.
    Note
    The PutItem operation (Putting an Item - Table.PutItem Method (p. 234)) can also can
    perform an update. If you call PutItem to upload an item and the primary key exists, the
    PutItem operation replaces the entire item. Note that, if there are attributes in the existing
    item and those attributes are not specified on the Document that is being put, the PutItem
    operation deletes those attributes. However, UpdateItem only updates the specified input
    attributes. Any other existing attributes of that item will remain unchanged.
The following are the steps to update an item using the AWS SDK for .NET document model.
1. Execute the Table.LoadTable method by providing the name of the table in which you want to
   perform the update operation.
2. Create a Document instance by providing all the updates that you wish to perform.
3. Call the Table.UpdateItem method and provide the Document instance as an input parameter.
You must provide the primary key either in the Document instance or explicitly as a parameter.
The following C# code snippet demonstrates the preceding tasks. The code sample updates an item
in the Book table. The UpdateItem operation updates the existing Authors attribute, deletes the
PageCount attribute, and adds a new attribute XYZ. The Document instance includes the primary key of
the book to update.
Example
table.Update(book);
The following C# code snippet updates a book item price to 25. It specifies the two following optional
parameters:
• The ConditionalExpression parameter that identifies the Price attribute with value 20 that you
  expect to be present.
• The ReturnValues parameter to request the UpdateItem operation to return the item that is
  updated.
Example
1. Create a Table object by executing the Table.LoadTable method by providing the name of the table
   in which you want to perform the batch operation.
2. Execute the CreateBatchWrite method on the table instance you created in the preceding step and
   create DocumentBatchWrite object.
3. Use DocumentBatchWrite object methods to specify documents you wish to upload or delete.
4. Call the DocumentBatchWrite.Execute method to execute the batch operation.
  When using the document model API, you can specify any number of operations in a batch. However,
  note that DynamoDB limits the number of operations in a batch and the total size of the batch in a
  batch operation. For more information about the specific limits, see BatchWriteItem. If the document
  model API detects your batch write request exceeded the number of allowed write requests or the
  HTTP payload size of a batch exceeded the limit allowed by BatchWriteItem, it breaks the batch in
  to several smaller batches. Additionally, if a response to a batch write returns unprocessed items, the
  document model API will automatically send another batch request with those unprocessed items.
The following C# code snippet demonstrates the preceding steps. The code snippet uses batch write
operation to perform two writes; upload a book item and delete another book item.
batchWrite.AddDocumentToPut(book1);
// specify delete item using overload that takes PK.
batchWrite.AddKeyToDelete(12345);
batchWrite.Execute();
For a working example, see Example: Batch Operations Using AWS SDK for .NET Document Model
API (p. 244).
You can use the batch write operation to perform put and delete operations on multiple tables. The
following are the steps to put or delete multiple items from multiple table using the AWS SDK for .NET
document model.
1. You create DocumentBatchWrite instance for each table in which you want to put or delete multiple
   items as described in the preceding procedure.
2. Create an instance of the MultiTableDocumentBatchWrite and add the individual
   DocumentBatchWrite objects in it.
The following C# code snippet demonstrates the preceding steps. The code snippet uses batch write
operation to perform the following write operations:
superBatch.Execute();
For step-by-step instructions to test the following sample, see .NET Code Samples (p. 288).
Example
using System;
using   System.Collections.Generic;
using   System.Linq;
using   Amazon.DynamoDBv2;
using   Amazon.DynamoDBv2.DocumentModel;
using   Amazon.Runtime;
namespace com.amazonaws.codesamples
{
    class MidlevelItemCRUD
    {
        private static AmazonDynamoDBClient client = new AmazonDynamoDBClient();
        private static string tableName = "ProductCatalog";
        // The sample uses the following id PK value to add book item.
        private static int sampleBookId = 555;
                  // Delete.
                  DeleteBook(productCatalog);
                  Console.WriteLine("To continue, press Enter");
                  Console.ReadLine();
              }
              catch (AmazonDynamoDBException e) { Console.WriteLine(e.Message); }
              catch (AmazonServiceException e) { Console.WriteLine(e.Message); }
              catch (Exception e) { Console.WriteLine(e.Message); }
          }
              productCatalog.PutItem(book);
          }
           // Optional parameters.
           UpdateItemOperationConfig config = new UpdateItemOperationConfig
           {
               // Get updated item in response.
               ReturnValues = ReturnValues.AllNewAttributes
           };
           Document updatedBook = productCatalog.UpdateItem(book, config);
           Console.WriteLine("UpdateMultipleAttributes: Printing item after
updates ...");
           PrintDocument(updatedBook);
       }
           // Optional parameters.
           UpdateItemOperationConfig config = new UpdateItemOperationConfig
           {
               ConditionalExpression = expr,
               ReturnValues = ReturnValues.AllNewAttributes
           };
           Document updatedBook = productCatalog.UpdateItem(book, config);
           Console.WriteLine("UpdateBookPriceConditionally: Printing item whose price was
conditionally updated");
           PrintDocument(updatedBook);
       }
Example: Batch Write Using AWS SDK for .NET Document Model
The following C# code example illustrates single table and multi-table batch write operations. The
example performs the following tasks:
• To illustrate a single table batch write, it adds two items to the ProductCatalog table.
• To illustrate a multi-table batch write, it adds an item to both the Forum and Thread tables and
  deletes and item from the Thread table.
If you followed the steps in Creating Tables and Loading Sample Data (p. 281), you already have
the ProductCatalog, Forum and Thread tables created. You can also create these sample tables
programmatically. For more information, see Creating Example Tables and Uploading Data Using the
AWS SDK for .NET (p. 795). For step-by-step instructions to test the following sample, see .NET Code
Samples (p. 288).
Example
using     System;
using     System.Collections.Generic;
using     Amazon.DynamoDBv2;
using     Amazon.DynamoDBv2.DocumentModel;
using     Amazon.Runtime;
namespace com.amazonaws.codesamples
{
    class MidLevelBatchWriteItem
    {
        private static AmazonDynamoDBClient client = new AmazonDynamoDBClient();
        static void Main(string[] args)
       {
           try
           {
                 SingleTableBatchWrite();
                 MultiTableBatchWrite();
           }
           catch (AmazonDynamoDBException e) { Console.WriteLine(e.Message); }
           catch (AmazonServiceException e) { Console.WriteLine(e.Message); }
           catch (Exception e) { Console.WriteLine(e.Message); }
           batchWrite.AddDocumentToPut(book1);
           // Specify delete item using overload that takes PK.
           batchWrite.AddKeyToDelete(12345);
           Console.WriteLine("Performing batch write in SingleTableBatchWrite()");
           batchWrite.Execute();
       }
               superBatch.AddBatch(forumBatchWrite);
               superBatch.AddBatch(threadBatchWrite);
               Console.WriteLine("Performing batch write in MultiTableBatchWrite()");
               superBatch.Execute();
           }
      }
}
The Query method provides two overloads. The minimum required parameters to the Query method are
a partition key value and a sort key filter. You can use the following overload to provide these minimum
required parameters.
Example
For example, the following C# code snippet queries for all forum replies that were posted in the last 15
days.
Example
This creates a Search object. You can now call the Search.GetNextSet method iteratively to retrieve
one page of results at a time as shown in the following C# code snippet. The code prints the attribute
values for each item that the query returns.
Example
  documentSet = search.GetNextSet();
  foreach (var document in documentSet)
    PrintDocument(document);
} while (!search.IsDone);
You can also specify optional parameters for Query, such as specifying a list of attributes to retrieve,
strongly consistent reads, page size, and the number of items returned per page. For a complete list of
parameters, see Query. To specify optional parameters, you must use the following overload in which
you provide the QueryOperationConfig object.
Example
Query(QueryOperationConfig config);
Assume that you want to execute the query in the preceding example (retrieve forum replies posted in
the last 15 days). However, assume that you want to provide optional query parameters to retrieve only
specific attributes and also request a strongly consistent read. The following C# code snippet constructs
the request using the QueryOperationConfig object.
Example
The following C# code example uses the Table.Query method to execute the following sample queries:
• Find forum thread replies that were posted in the last 15 days.
    This query is executed twice. In the first Table.Query call, the example provides only the required
    query parameters. In the second Table.Query call, you provide optional query parameters to request
    a strongly consistent read and a list of attributes to retrieve.
  • Find forum thread replies posted during a period of time.
    This query uses the Between query operator to find replies posted in between two dates.
• Get a product from the ProductCatalog table.
  Because the ProductCatalog table has a primary key that is only a partition key, you can only get items;
  you cannot query the table. The example retrieves a specific product item using the item Id.
Example
using   System;
using   System.Collections.Generic;
using   System.Linq;
using   Amazon.DynamoDBv2;
using   Amazon.DynamoDBv2.DocumentModel;
using   Amazon.Runtime;
using   Amazon.SecurityToken;
namespace com.amazonaws.codesamples
{
    class MidLevelQueryAndScan
    {
        private static AmazonDynamoDBClient client = new AmazonDynamoDBClient();
                  // Get Example.
                  Table productCatalogTable = Table.LoadTable(client, "ProductCatalog");
                  int productId = 101;
                  GetProduct(productCatalogTable, productId);
           else
           {
                Console.WriteLine("Error: product " + productId + " does not exist");
           }
       }
           // Use Query overloads that takes the minimum required query parameters.
           Search search = table.Query(filter);
           do
           {
               documentList = search.GetNextSet();
               Console.WriteLine("\nFindRepliesPostedWithinTimePeriod: printing replies
posted within dates: {0} and {1} ............", startDate, endDate);
               foreach (var document in documentList)
               {
                   PrintDocument(document);
               }
           } while (!search.IsDone);
       }
Example
Scan(ScanFilter filter);
For example, assume that you maintain a table of forum threads tracking information such as thread
subject (primary), the related message, forum Id to which the thread belongs, Tags, and other
information. Assume that the subject is the primary key.
Example
This is a simplified version of forums and threads that you see on AWS forums (see Discussion Forums).
The following C# code snippet queries all threads in a specific forum (ForumId = 101) that are tagged
"sortkey". Because the ForumId is not a primary key, the example scans the table. The ScanFilter
includes two conditions. Query returns all the threads that satisfy both of the conditions.
Example
You can also specify optional parameters to Scan, such as a specific list of attributes to retrieve or
whether to perform a strongly consistent read. To specify optional parameters, you must create a
ScanOperationConfig object that includes both the required and optional parameters and use the
following overload.
Example
Scan(ScanOperationConfig config);
The following C# code snippet executes the same preceding query (find forum threads in which the
ForumId is 101 and the Tag attribute contains the "sortkey" keyword). However, this time assume that
you want to add an optional parameter to retrieve only a specific attribute list. In this case, you must
create a ScanOperationConfig object by providing all the parameters, required and optional as shown
in the following code example.
Example
  Filter = scanFilter
};
  You can pass the ScanFilter parameter when passing in only the required parameters.
• Table.Scan that takes the ScanOperationConfig object as a parameter.
  You must use the ScanOperationConfig parameter if you want to pass any optional parameters to
  the Scan method.
Example
using   System;
using   System.Collections.Generic;
using   System.Linq;
using   Amazon.DynamoDBv2;
using   Amazon.DynamoDBv2.DocumentModel;
namespace com.amazonaws.codesamples
{
    class MidLevelScanOnly
    {
        private static AmazonDynamoDBClient client = new AmazonDynamoDBClient();
               } while (!search.IsDone);
           }
 • Example: CRUD Operations Using the AWS SDK for .NET Object Persistence Model (p. 270)
 • Example: Batch Write Operation Using the AWS SDK for .NET Object Persistence Model (p. 272)
 • Example: Query and Scan in DynamoDB Using the AWS SDK for .NET Object Persistence
   Model (p. 276)
The AWS SDK for .NET provides an object persistence model that enables you to map your client-
side classes to the DynamoDB tables. Each object instance then maps to an item in the corresponding
tables. To save your client-side objects to the tables the object persistence model provides the
DynamoDBContext class, an entry point to DynamoDB. This class provides you a connection to
DynamoDB and enables you to access tables, perform various CRUD operations, and execute queries.
The object persistence model provides a set of attributes to map client-side classes to tables, and
properties/fields to table attributes.
      Note
      The object persistence model does not provide an API to create, update, or delete tables. It
      provides only data operations. You can use only the AWS SDK for .NET low-level API to create,
      update, and delete tables. For more information, see Working with Tables: .NET (p. 320).
To show you how the object persistence model works, let's walk through an example. We'll start with the
ProductCatalog table. It has Id as the primary key.
ProductCatalog(Id, ...)
Suppose you have a Book class with Title, ISBN, and Authors properties. You can map the Book class to
the ProductCatalog table by adding the attributes defined by the object persistence model, as shown in
the following C# code snippet.
Example
[DynamoDBTable("ProductCatalog")]
  public class Book
  {
    [DynamoDBHashKey]
    public int Id { get; set; }
      [DynamoDBProperty("Authors")]
      public List<string> BookAuthors { get; set; }
      [DynamoDBIgnore]
      public string CoverPage { get; set; }
  }
In the preceding example, the DynamoDBTable attribute maps the Book class to the ProductCatalog
table.
The object persistence model supports both the explicit and default mapping between class properties
and table attributes.
• Explicit mapping—To map a property to a primary key, you must use the DynamoDBHashKey
  and DynamoDBRangeKey object persistence model attributes. Additionally, for the non-primary
  key attributes, if a property name in your class and the corresponding table attribute to which
  you want to map it are not the same, then you must define the mapping by explicitly adding the
  DynamoDBProperty attribute.
    In the preceding example, Id property maps to the primary key with the same name and the
    BookAuthors property maps to the Authors attribute in the ProductCatalog table.
• Default mapping—By default, the object persistence model maps the class properties to the
  attributes with the same name in the table.
    In the preceding example, the properties Title and ISBN map to the attributes with the same name
    in the ProductCatalog table.
You don't have to map every single class property. You identify these properties by adding the
DynamoDBIgnore attribute. When you save a Book instance to the table, the DynamoDBContext does
not include the CoverPage property. It also does not return this property when you retrieve the book
instance.
You can map properties of .NET primitive types such as int and string. You can also map any arbitrary
data types as long as you provide an appropriate converter to map the arbitrary data to one of the
DynamoDB types. To learn about mapping arbitrary types, see Mapping Arbitrary Data with DynamoDB
Using the AWS SDK for .NET Object Persistence Model (p. 264).
The object persistence model supports optimistic locking. During an update operation this ensures you
have the latest copy of the item you are about to update. For more information, see Optimistic Locking
Using Version Number with DynamoDB Using the AWS SDK for .NET Object Persistence Model (p. 262).
DynamoDB Attributes
This section describes the attributes the object persistence model offers so you can map your classes and
properties to DynamoDB tables and attributes.
      Note
      In the following attributes, only DynamoDBTable and DynamoDBHashKey are required.
DynamoDBGlobalSecondaryIndexHashKey
Maps a class property to the partition key of a global secondary index. Use this attribute if you need to
Query a global secondary index.
DynamoDBGlobalSecondaryIndexRangeKey
Maps a class property to the sort key of a global secondary index. Use this attribute if you need to Query
a global secondary index and want to refine your results using the index sort key.
DynamoDBHashKey
Maps a class property to the partition key of the table's primary key. The primary key attributes cannot
be a collection type.
The following C# code examples maps the Book class to the ProductCatalog table, and the Id property
to the table's primary key partition key.
[DynamoDBTable("ProductCatalog")]
   public class Book {
   [DynamoDBHashKey]
   public int Id { get; set; }
DynamoDBIgnore
Indicates that the associated property should be ignored. If you don't want to save any of your class
properties you can add this attribute to instruct DynamoDBContext not to include this property when
saving objects to the table.
DynamoDBLocalSecondaryIndexRangeKey
Maps a class property to the sort key of a local secondary index. Use this attribute if you need to Query a
local secondary index and want to refine your results using the index sort key.
DynamoDBProperty
Maps a class property to a table attribute. If the class property maps to the same name table attribute,
then you don't need to specify this attribute. However, if the names are not the same, you can use
this tag to provide the mapping. In the following C# statement the DynamoDBProperty maps the
BookAuthors property to the Authors attribute in the table.
[DynamoDBProperty("Authors")]
public List<string> BookAuthors { get; set; }
DynamoDBContext uses this mapping information to create the Authors attribute when saving object
data to the corresponding table.
DynamoDBRenamable
Specifies an alternative name for a class property. This is useful if you are writing a custom converter
for mapping arbitrary data to a DynamoDB table where the name of a class property is different from a
table attribute.
DynamoDBRangeKey
Maps a class property to the sort key of the table's primary key. If the table has a composite
primary key (partition key and sort key), then you must specify both the DynamoDBHashKey and
DynamoDBRangeKey attributes in your class mapping.
For example, the sample table Reply has a primary key made of the Id partition key and Replenishment
sort key. The following C# code example maps the Reply class to the Reply table. The class definition also
indicates that two of its properties map to the primary key.
For more information about sample tables, see Creating Tables and Loading Sample Data (p. 281).
[DynamoDBTable("Reply")]
public class Reply {
   [DynamoDBHashKey]
   public int ThreadId { get; set; }
   [DynamoDBRangeKey]
   public string Replenishment { get; set; }
   // Additional properties go here.
}
DynamoDBTable
Identifies the target table in DynamoDB to which the class maps. For example, the following C# code
example maps the Developer class to the People table in DynamoDB.
[DynamoDBTable("People")]
public class Developer { ...}
• The DynamoDBTable attribute can be inherited. In the preceding example, if you add a new class,
  Lead, that inherits from the Developer class, it also maps to the People table. Both the Developer
  and Lead objects are stored in the People table.
• The DynamoDBTable attribute can also be overridden. In the following C# code example, the Manager
  class inherits from the Developer class, however the explicit addition of the DynamoDBTable
  attribute maps the class to another table (Managers).
  [DynamoDBTable("Managers")]
  public class Manager extends Developer { ...}
You can add the optional parameter, LowerCamelCaseProperties, to request DynamoDB to lower
case the first letter of the property name when storing the objects to a table as shown in the following
C# snippet.
[DynamoDBTable("People", LowerCamelCaseProperties=true)]
public class Developer {
   string DeveloperName;
   ...}
When saving instances of the Developer class, DynamoDBContext saves the DeveloperName property
as the developerName.
DynamoDBVersion
Identifies a class property for storing the item version number. To more information about versioning,
see Optimistic Locking Using Version Number with DynamoDB Using the AWS SDK for .NET Object
Persistence Model (p. 262).
DynamoDBContext Class
The DynamoDBContext class is the entry point to the DynamoDB database. It provides a connection to
DynamoDB and enables you to access your data in various tables, perform various CRUD operations, and
execute queries. The DynamoDBContext class provides the following methods:
CreateMultiTableBatchGet
Creates a MultiTableBatchGet object, composed of multiple individual BatchGet objects. Each of
these BatchGet objects can be used for retrieving items from a single DynamoDB table.
To retrieve the items from the table(s), use the ExecuteBatchGet method, passing the
MultiTableBatchGet object as a parameter.
CreateMultiTableBatchWrite
Creates a MultiTableBatchWrite object, composed of multiple individual BatchWrite objects. Each
of these BatchWrite objects can be used for writing or deleting items in a single DynamoDB table.
To write to the table(s), use the ExecuteBatchWrite method, passing the MultiTableBatchWrite
object as a parameter.
CreateBatchGet
Creates a BatchGet object that you can use to retrieve multiple items from a table. For more
information, see Batch Get: Getting Multiple Items (p. 269).
CreateBatchWrite
Creates a BatchWrite object that you can use to put multiple items into a table, or to delete multiple
items from a table. For more information, see Batch Write: Putting and Deleting Multiple Items
 (p. 267).
Delete
Deletes an item from the table. The method requires the primary key of the item you want to delete.
You can provide either the primary key value or a client-side object containing a primary key value as a
parameter to this method.
• If you specify a client-side object as a parameter and you have enabled optimistic locking, the delete
  succeeds only if the client-side and the server-side versions of the object match.
• If you specify only the primary key value as a parameter, the delete succeeds regardless of whether
  you have enabled optimistic locking or not.
    Note
    To perform this operation in the background, use the DeleteAsync method instead.
Dispose
Disposes of all managed and unmanaged resources.
ExecuteBatchGet
Reads data from one or more tables, processing all of the BatchGet objects in a
MultiTableBatchGet.
    Note
    To perform this operation in the background, use the ExecuteBatchGetAsync method
    instead.
ExecuteBatchWrite
Writes or deletes data in one or more tables, processing all of the BatchWrite objects in a
MultiTableBatchWrite.
    Note
    To perform this operation in the background, use the ExecuteBatchWriteAsync method
    instead.
FromDocument
Given an instance of a Document, the FromDocument method returns an instance of a client-side class.
This is helpful if you want to use the document model classes along with the object persistence model to
perform any data operations. For more information about the document model classes provided by the
AWS SDK for .NET, see .NET: Document Model (p. 232).
Suppose you have a Document object named doc, containing a representation of a Forum item. (To
see how to construct this object, see the description for the ToDocument method below.) You can use
FromDocument to retrieve the Forum item from the Document as shown in the following C# code
snippet.
Example
forum101 = context.FromDocument<Forum>(101);
    Note
    If your Document object implements the IEnumerable interface, you can use the
    FromDocuments method instead. This will allow you to iterate over all of the class instances in
    the Document.
FromQuery
Executes a Query operation, with the query parameters defined in a QueryOperationConfig object.
    Note
    To perform this operation in the background, use the FromQueryAsync method instead.
FromScan
Executes a Scan operation, with the scan parameters defined in a ScanOperationConfig object.
    Note
    To perform this operation in the background, use the FromScanAsync method instead.
GetTargetTable
Retrieves the target table for the specified type. This is useful if you are writing a custom converter for
mapping arbitrary data to a DynamoDB table and need to determine which table is associated with a
custom data type.
Load
Retrieves an item from a table. The method requires only the primary key of the item you want to
retrieve.
By default, DynamoDB returns the item with values that are eventually consistent. For information on
the eventual consistency model, see Read Consistency (p. 15).
    Note
    To perform this operation in the background, use the LoadAsync method instead.
Query
Queries a table based on query parameters you provide.
You can query a table only if it has a composite primary key (partition key and sort key). When querying,
you must specify a partition key and a condition that applies to the sort key.
Suppose you have a client-side Reply class mapped to the Reply table in DynamoDB. The following
C# code snippet queries the Reply table to find forum thread replies posted in the past 15 days. The
Reply table has a primary key that has the Id partition key and the ReplyDateTime sort key. For more
information about the Reply table, see Creating Tables and Loading Sample Data (p. 281).
Example
The Query method returns a "lazy-loaded" IEnumerable collection. It initially returns only one page of
results, and then makes a service call for the next page if needed. To obtain all the matching items, you
only need to iterate over the IEnumerable.
If your table has a simple primary key (partition key), then you cannot use the Query method. Instead,
you can use the Load method and provide the partition key to retrieve the item.
    Note
    To perform this operation in the background, use the QueryAsync method instead.
Save
Saves the specified object to the table. If the primary key specified in the input object does not exist
in the table, the method adds a new item to the table. If primary key exists, the method updates the
existing item.
If you have optimistic locking configured, the update succeeds only if the client and the server side
versions of the item match. For more information, see Optimistic Locking Using Version Number with
DynamoDB Using the AWS SDK for .NET Object Persistence Model (p. 262).
    Note
    To perform this operation in the background, use the SaveAsync method instead.
Scan
Performs an entire table scan.
You can filter scan result by specifying a scan condition. The condition can be evaluated on any
attributes in the table. Suppose you have a client-side class Book mapped to the ProductCatalog table in
DynamoDB. The following C# snippet scans the table and returns only the book items priced less than 0.
Example
The Scan method returns a "lazy-loaded" IEnumerable collection. It initially returns only one page of
results, and then makes a service call for the next page if needed. To obtain all the matching items, you
only need to iterate over the IEnumerable.
For performance reasons you should query your tables and avoid a table scan.
    Note
    To perform this operation in the background, use the ScanAsync method instead.
ToDocument
Returns an instance of the Document document model class from your class instance.
This is helpful if you want to use the document model classes along with the object persistence model to
perform any data operations. For more information about the document model classes provided by the
AWS SDK for .NET, see .NET: Document Model (p. 232).
Suppose you have a client-side class mapped to the sample Forum table. You can then use a
DynamoDBContext to get an item, as a Document object, from the Forum table as shown in the
following C# code snippet.
Example
• ConsistentRead—When retrieving data using the Load, Query or Scan operations you can optionally
  add this parameter to request the latest values for the data.
• IgnoreNullValues—This parameter informs DynamoDBContext to ignore null values on attributes
  during a Save operation. If this parameter is false (or if it is not set), then a null value is interpreted as
  directives to delete the specific attribute.
• SkipVersionCheck— - This parameter informs DynamoDBContext to not compare versions when
  saving or deleting an item. For more information about versioning, see Optimistic Locking Using
  Version Number with DynamoDB Using the AWS SDK for .NET Object Persistence Model (p. 262).
• TableNamePrefix— - Prefixes all table names with a specific string. If this parameter is null (or if it is
  not set), then no prefix is used.
The following C# snippet creates a new DynamoDBContext by specifying two of the preceding optional
parameters.
Example
DynamoDBContext includes these optional parameters with each request you send using this context.
Instead of setting these parameters at the DynamoDBContext level, you can specify them for individual
operations you execute using DynamoDBContext as shown in the following C# code snippet. The
example loads a specific book item. The Load method of DynamoDBContext specifies the preceding
optional parameters.
Example
In this case DynamoDBContext includes these parameters only when sending the Get request.
• bool
• byte
• char
• DateTime
• decimal
• double
• float
• Int16
• Int32
• Int64
• SByte
• string
• UInt16
• UInt32
• UInt64
The object persistence model also supports the .NET collection types. DynamoDBContext is able to
convert concrete collection types and simple Plain Old CLR Objects (POCOs).
The following table summarizes the mapping of the preceding .NET types to the DynamoDB types.
The object persistence model also supports arbitrary data types. However, you must provide converter
code to map the complex types to the DynamoDB types.
The optimistic locking feature of the object persistence model provides the DynamoDBVersion tag that
you can use to enable optimistic locking. To use this feature you add a property to your class for storing
the version number. You add the DynamoDBVersion attribute on the property. When you first save the
object, the DynamoDBContext assigns a version number and increments this value each time you update
the item.
Your update or delete request succeeds only if the client-side object version matches the corresponding
version number of the item on the server-side. If your application has a stale copy, it must get the latest
version from the server before it can update or delete that item.
The following C# code snippet defines a Book class with object persistence attributes mapping it to the
ProductCatalog table. The VersionNumber property in the class decorated with the DynamoDBVersion
attribute stores the version number value.
Example
[DynamoDBTable("ProductCatalog")]
  public class Book
  {
    [DynamoDBHashKey]   //Partition key
    public int Id { get; set; }
    [DynamoDBProperty]
    public string Title { get; set; }
    [DynamoDBProperty]
    public string ISBN { get; set; }
    [DynamoDBProperty("Authors")]
    public List<string> BookAuthors { get; set; }
    [DynamoDBVersion]
    public int? VersionNumber { get; set; }
  }
    Note
    You can apply the DynamoDBVersion attribute only to a nullable numeric primitive type (such
    as int?).
• For a new item, DynamoDBContext assigns initial version number 0. If you retrieve an existing
  item, and then update one or more of its properties and attempt to save the changes, the save
  operation succeeds only if the version number on the client-side and the server-side match. The
  DynamoDBContext increments the version number. You don't need to set the version number.
• The Delete method provides overloads that can take either a primary key value or an object as
  parameter as shown in the following C# code snippet.
Example
  If you provide an object as the parameter, then the delete succeeds only if the object version matches
  the corresponding server-side item version. However, if you provide a primary key value as the
  parameter, the DynamoDBContext is unaware of any version numbers and it deletes the item without
  making the version check.
  Note that the internal implementation of optimistic locking in the object persistence model code uses
  the conditional update and the conditional delete API actions in DynamoDB.
Instead of setting the property at the context level, you can disable optimistic locking for a specific
operation as shown in the following C# code snippet. The code example uses the context to delete
a book item. The Delete method sets the optional SkipVersionCheck property to true, disabling
version check.
Example
You can create any types on the client-side, however the data stored in the tables is one of the
DynamoDB types and during query and scan any data comparisons made are against the data stored in
DynamoDB.
The following C# code example defines a Book class with Id, Title, ISBN, and Dimension properties.
The Dimension property is of the DimensionType that describes Height, Width, and Thickness
properties. The example code provides the converter methods, ToEntry and FromEntry to convert
data between the DimensionType and the DynamoDB string types. For example, when saving a Book
instance, the converter creates a book Dimension string such as "8.5x11x.05", and when you retrieve a
book, it converts the string to a DimensionType instance.
The example maps the Book type to the ProductCatalog table. For illustration, it saves a sample Book
instance, retrieves it, updates its dimensions and saves the updated Book again.
For step-by-step instructions on how to test the following sample, see .NET Code Samples (p. 288).
Example
using   System;
using   System.Collections.Generic;
using   Amazon.DynamoDBv2;
using   Amazon.DynamoDBv2.DataModel;
using Amazon.DynamoDBv2.DocumentModel;
using Amazon.Runtime;
using Amazon.SecurityToken;
namespace com.amazonaws.codesamples
{
    class HighLevelMappingArbitraryData
    {
        private static AmazonDynamoDBClient client = new AmazonDynamoDBClient();
                  // 1. Create a book.
                  DimensionType myBookDimensions = new DimensionType()
                  {
                      Length = 8M,
                      Height = 11M,
                      Thickness = 0.5M
                  };
context.Save(myBook);
       }
       [DynamoDBProperty]
       public string ISBN
       {
           get; set;
       }
       // Multi-valued (set type) attribute.
       [DynamoDBProperty("Authors")]
       public List<string> BookAuthors
       {
           get; set;
       }
       // Arbitrary type, with a converter to map it to DynamoDB type.
       [DynamoDBProperty(typeof(DimensionTypeConverter))]
       public DimensionType Dimensions
       {
           get; set;
       }
   }
      Note
      When using object persistence model, you can specify any number of operations in a batch.
      However, note that DynamoDB limits the number of operations in a batch and the total
      size of the batch in a batch operation. For more information about the specific limits, see
      BatchWriteItem. If the API detects your batch write request exceeded the allowed number of
      write requests or exceeded the maximum allowed HTTP payload size, it breaks the batch in to
      several smaller batches. Additionally, if a response to a batch write returns unprocessed items,
      the API will automatically send another batch request with those unprocessed items.
Suppose that you have defined a C# class Book class that maps to the ProductCatalog table in
DynamoDB. The following C# code snippet uses the BatchWrite object to upload two items and delete
one item from the ProductCatalog table.
Example
  ProductCategory = "Book",
  Title = "My book4 in batch write"
};
bookBatch.Execute();
• Create one instance of the BatchWrite class for each type and specify the items you want to put or
  delete as described in the preceding section.
• Create an instance of MultiTableBatchWrite using one of the following methods:
  • Execute the Combine method on one of the BatchWrite objects that you created in the preceding
    step.
  • Create an instance of the MultiTableBatchWrite type by providing a list of BatchWrite objects.
  • Execute the CreateMultiTableBatchWrite method of DynamoDBContext and pass in your list
    of BatchWrite objects.
• Call the Execute method of MultiTableBatchWrite, which performs the specified put and delete
  operations on various tables.
Suppose that you have defined Forum and Thread C# classes that map to the Forum and Thread tables
in DynamoDB. Also, suppose that the Thread class has versioning enabled. Because versioning is not
supported when using batch operations, you must explicitly disable versioning as shown in the following
C# code snippet. The code snippet uses the MultiTableBatchWrite object to perform a multi-table
update.
Example
threadBatch.AddPutItem(newThread);
For a working example, see Example: Batch Write Operation Using the AWS SDK for .NET Object
Persistence Model (p. 272).
    Note
    DynamoDB batch API limits the number of writes in batch and also limits the size of the batch.
    For more information, see BatchWriteItem. When using the .NET object persistence model API,
    you can specify any number of operations. However, if either the number of operations in a
    batch or size exceed the limit, the .NET API breaks the batch write request into smaller batches
    and sends multiple batch write requests to DynamoDB.
The following C# code sample retrieves three items from the ProductCatalog table. The items in the
result are not necessarily in the same order in which you specified the primary keys.
Example
• For each type, create an instance of the CreateBatchGet type and provide the primary key values
  you want to retrieve from each table.
• Create an instance of the MultiTableBatchGet class using one of the following methods:
  • Execute the Combine method on one of the BatchGet objects you created in the preceding step.
  • Create an instance of the MultiBatchGet type by providing a list of BatchGet objects.
  • Execute the CreateMultiTableBatchGet method of DynamoDBContext and pass in your list of
    BatchGet objects.
• Call the Execute method of MultiTableBatchGet which returns the typed results in the individual
  BatchGet objects.
The following C# code snippet retrieves multiple items from the Order and OrderDetail tables using the
CreateBatchGet method.
Example
Console.WriteLine(orderBatch.Results.Count);
Console.WriteLine(orderDetailBatch.Results.Count);
Example: CRUD Operations Using the AWS SDK for .NET Object
Persistence Model
The following C# code example declares a Book class with Id, title, ISBN, and Authors properties. It uses
the object persistence attributes to map these properties to the ProductCatalog table in DynamoDB.
The code example then uses the DynamoDBContext to illustrate typical CRUD operations. The example
creates a sample Book instance and saves it to the ProductCatalog table. The example then retrieves
the book item, and updates its ISBN and Authors properties. Note that the update replaces the existing
authors list. The example finally deletes the book item.
For more information about the ProductCatalog table used in this example, see Creating Tables and
Loading Sample Data (p. 281). For step-by-step instructions to test the following sample, see .NET
Code Samples (p. 288).
    Note
    The following example does not work with .NET core as it does not support synchronous
    methods. For more information, see AWS Asynchronous APIs for .NET.
Example
using   System;
using   System.Collections.Generic;
using   Amazon.DynamoDBv2;
using   Amazon.DynamoDBv2.DataModel;
using   Amazon.Runtime;
namespace com.amazonaws.codesamples
{
    class HighLevelItemCRUD
    {
        private static AmazonDynamoDBClient client = new AmazonDynamoDBClient();
               Console.ReadLine();
           }
           catch (AmazonDynamoDBException e) { Console.WriteLine(e.Message); }
           catch (AmazonServiceException e) { Console.WriteLine(e.Message); }
           catch (Exception e) { Console.WriteLine(e.Message); }
       }
           // Retrieve the updated book. This time add the optional ConsistentRead
parameter using DynamoDBContextConfig object.
           Book updatedBook = context.Load<Book>(bookID, new DynamoDBContextConfig
           {
               ConsistentRead = true
           });
   [DynamoDBTable("ProductCatalog")]
   public class Book
   {
       [DynamoDBHashKey] //Partition key
       public int Id
       {
           get; set;
       }
       [DynamoDBProperty]
       public string Title
       {
           get; set;
       }
       [DynamoDBProperty]
       public string ISBN
       {
           get; set;
       }
Example: Batch Write Operation Using the AWS SDK for .NET
Object Persistence Model
The following C# code example declares Book, Forum, Thread, and Reply classes and maps them to the
DynamoDB tables using the object persistence model attributes.
The code example then uses the DynamoDBContext to illustrate the following batch write operations.
• BatchWrite object to put and delete book items from the ProductCatalog table.
• MultiTableBatchWrite object to put and delete items from the Forum and the Thread tables.
For more information about the tables used in this example, see Creating Tables and Loading
Sample Data (p. 281). For step-by-step instructions to test the following sample, see .NET Code
Samples (p. 288).
    Note
    The following example does not work with .NET core as it does not support synchronous
    methods. For more information, see AWS Asynchronous APIs for .NET.
Example
using   System;
using   System.Collections.Generic;
using   Amazon.DynamoDBv2;
using   Amazon.DynamoDBv2.DataModel;
using   Amazon.Runtime;
using   Amazon.SecurityToken;
namespace com.amazonaws.codesamples
{
    class HighLevelBatchWriteItem
    {
        private static AmazonDynamoDBClient client = new AmazonDynamoDBClient();
            Id = 902,
            InPublication = true,
            ISBN = "902-11-11-1111",
            PageCount = "100",
            Price = 10,
            ProductCategory = "Book",
            Title = "My book3 in batch write"
        };
        Book book2 = new Book
        {
            Id = 903,
            InPublication = true,
            ISBN = "903-11-11-1111",
            PageCount = "200",
            Price = 10,
            ProductCategory = "Book",
            Title = "My book4 in batch write"
        };
[DynamoDBTable("Reply")]
public class Reply
{
    [DynamoDBHashKey] //Partition key
    public string Id
    {
        get; set;
    }
[DynamoDBTable("Thread")]
public class Thread
{
    // PK mapping.
    [DynamoDBHashKey]       //Partition key
    public string ForumName
    {
        get; set;
    }
    [DynamoDBRangeKey]       //Sort key
    public String Subject
    {
        get; set;
    }
    // Implicit mapping.
    public string Message
    {
        get; set;
    }
    public string LastPostedBy
    {
        get; set;
    }
    public int Views
    {
        get; set;
    }
    public int Replies
    {
        get; set;
    }
    public bool Answered
    {
        get; set;
    }
    public DateTime LastPostedDateTime
    {
        get; set;
    }
    // Explicit mapping (property and table attribute names are different.
    [DynamoDBProperty("Tags")]
    public List<string> KeywordTags
    {
        get; set;
    }
    // Property to store version number for optimistic locking.
    [DynamoDBVersion]
    public int? Version
    {
        get; set;
    }
}
[DynamoDBTable("Forum")]
public class Forum
{
    [DynamoDBHashKey]      //Partition key
    public string Name
    {
        get; set;
    }
    // All the following properties are explicitly mapped,
    // only to show how to provide mapping.
    [DynamoDBProperty]
    public int Threads
    {
        get; set;
    }
    [DynamoDBProperty]
    public int Views
    {
        get; set;
    }
    [DynamoDBProperty]
    public string LastPostBy
    {
        get; set;
    }
    [DynamoDBProperty]
    public DateTime LastPostDateTime
    {
        get; set;
    }
    [DynamoDBProperty]
    public int Messages
    {
        get; set;
    }
}
[DynamoDBTable("ProductCatalog")]
public class Book
{
    [DynamoDBHashKey] //Partition key
    public int Id
    {
        get; set;
    }
    public string Title
    {
        get; set;
    }
    public string ISBN
    {
        get; set;
    }
The example then executes the following query and scan operations using the DynamoDBContext.
  The ProductCatalog table has Id as its primary key. It does not have a sort key as part of its primary
  key. Therefore, you cannot query the table. You can get an item using its Id value.
• Execute the following queries against the Reply table (the Reply table's primary key is composed of Id
  and ReplyDateTime attributes. The ReplyDateTime is a sort key. Therefore, you can query this table).
  • Find replies to a forum thread posted in the last 15 days.
  • Find replies to a forum thread posted in a specific date range.
• Scan ProductCatalog table to find books whose price is less than zero.
    For performance reasons, you should use a query instead of a scan operation. However, there are times
    you might need to scan a table. Suppose there was a data entry error and one of the book prices is set
    to less than 0. This example scans the ProductCategory table to find book items (ProductCategory is
    book) at price of less than 0.
For instructions to create a working sample, see .NET Code Samples (p. 288).
      Note
      The following example does not work with .NET core as it does not support synchronous
      methods. For more information, see AWS Asynchronous APIs for .NET.
Example
using System;
using System.Collections.Generic;
using System.Configuration;
using   Amazon.DynamoDBv2;
using   Amazon.DynamoDBv2.DataModel;
using   Amazon.DynamoDBv2.DocumentModel;
using   Amazon.Runtime;
namespace com.amazonaws.codesamples
{
    class HighLevelQueryAndScan
    {
        private static AmazonDynamoDBClient client = new AmazonDynamoDBClient();
                  // Scan table.
                  FindProductsPricedLessThanZero(context);
                  Console.WriteLine("To continue, press Enter");
                  Console.ReadLine();
              }
              catch (AmazonDynamoDBException e) { Console.WriteLine(e.Message); }
              catch (AmazonServiceException e) { Console.WriteLine(e.Message); }
              catch (Exception e) { Console.WriteLine(e.Message); }
          }
   [DynamoDBTable("Reply")]
   public class Reply
   {
       [DynamoDBHashKey] //Partition key
       public string Id
       {
           get; set;
       }
   [DynamoDBTable("Thread")]
   public class Thread
   {
       // PK mapping.
       [DynamoDBHashKey] //Partition key
       public string ForumName
       {
           get; set;
    }
    [DynamoDBRangeKey] //Sort key
    public DateTime Subject
    {
        get; set;
    }
    // Implicit mapping.
    public string Message
    {
        get; set;
    }
    public string LastPostedBy
    {
        get; set;
    }
    public int Views
    {
        get; set;
    }
    public int Replies
    {
        get; set;
    }
    public bool Answered
    {
        get; set;
    }
    public DateTime LastPostedDateTime
    {
        get; set;
    }
    // Explicit mapping (property and table attribute names are different.
    [DynamoDBProperty("Tags")]
    public List<string> KeywordTags
    {
        get; set;
    }
    // Property to store version number for optimistic locking.
    [DynamoDBVersion]
    public int? Version
    {
        get; set;
    }
}
[DynamoDBTable("Forum")]
public class Forum
{
    [DynamoDBHashKey]
    public string Name
    {
        get; set;
    }
    // All the following properties are explicitly mapped,
    // only to show how to provide mapping.
    [DynamoDBProperty]
    public int Threads
    {
        get; set;
    }
    [DynamoDBProperty]
    public int Views
    {
        get; set;
    }
    [DynamoDBProperty]
        [DynamoDBTable("ProductCatalog")]
        public class Book
        {
            [DynamoDBHashKey] //Partition key
            public int Id
            {
                get; set;
            }
            public string Title
            {
                get; set;
            }
            public string ISBN
            {
                get; set;
            }
            public int Price
            {
                get; set;
            }
            public string PageCount
            {
                get; set;
            }
            public string ProductCategory
            {
                get; set;
            }
            public bool InPublication
            {
                get; set;
            }
        }
    }
    The code samples in this Developer Guide provide more in-depth coverage of DynamoDB operations,
    using the following programming languages:
Before you can begin with this exercise, you will need to sign up for AWS, get your access key and secret
key, and set up the AWS Command Line Interface on your computer. If you haven't done so, see Setting
Up DynamoDB (Web Service) (p. 45).
    Note
    If you are using the downloadable version of DynamoDB, you need to use the AWS CLI to create
    the tables and sample data. You also need to specify the --endpoint-url parameter with
    each AWS CLI command. For more information, see Setting the Local Endpoint (p. 44).
These tables and their data are used as examples throughout this Developer Guide.
    Note
    If you are an application developer, we recommend that you also read Getting Started with
    DynamoDB, where you use the downloadable version of DynamoDB. This lets you learn about
    the DynamoDB low-level API for free, without having to pay any fees for throughput, storage, or
    data transfer. For more information, see Getting Started with DynamoDB (p. 52).
Topics
 • Step 1: Create Example Tables (p. 281)
 • Step 2: Load Data into Tables (p. 283)
 • Step 3: Query the Data (p. 284)
 • Step 4: (Optional) Clean up (p. 285)
 • Summary (p. 286)
You can create a ProductCatalog table, where each item is uniquely identified by a single, numeric
attribute: Id.
the developer community, ask questions, or reply to other customers' posts. Each AWS service has a
dedicated forum. Anyone can start a new discussion thread by posting a message in a forum. Each thread
might receive any number of replies.
You can model this application by creating three tables: Forum, Thread, and Reply.
The Reply table has a global secondary index named PostedBy-Message-Index. This index will facilitate
queries on two non-key attributes of the Reply table.
          • In the Partition key field, type ForumName. Set the data type to String.
          • Choose Add sort key.
          • In the Sort key field, type Subject. Set the data type to String.
4.   When the settings are as you want them, choose Create.
          • In the Partition key field, type Id. Set the data type to String.
          • Choose Add sort key.
          • In the Sort key field, type ReplyDateTime. Set the data type to String.
     c.   In the Table settings section, deselect Use default settings.
     d.   In the Secondary indexes section, choose Add index.
     e.   In the Add index window, do the following:
You will download a .zip archive that contains JSON files with sample data for each table. For each file,
you will use the AWS CLI to load the data into DynamoDB. Each successful data load will produce the
following output:
{
     "UnprocessedItems": {}
}
     • sampledata.zip
2.   Extract the .json data files from the archive.
3.   Copy the .json data files to your current directory.
Repeat this procedure for each of the other tables you created:
• Forum
• Thread
• Reply
5. Choose the data filtering link, located just below the Create item button.
to this:
Take some time to explore your other tables using the DynamoDB console:
• ProductCatalog
• Forum
• Thread
However, if you don't want to keep these tables, you should delete them to avoid being charged for
resources you don't need.
                                    API Version 2012-08-10
                                              285
                              Amazon DynamoDB Developer Guide
                                     Java Code Samples
Repeat this procedure for each of the other tables you created:
• Forum
• Thread
• Reply
Summary
In this exercise, you used the DynamoDB console to create several tables in DynamoDB. You then used
the AWS CLI to load data into the tables, and performed some basic operations on the data using the
DynamoDB console.
The DynamoDB console and the AWS CLI are helpful for getting started quickly. However, you probably
want to learn more about how DynamoDB works, and how to write application programs with
DynamoDB. The rest of this Developer Guide addresses those topics.
This Developer Guide contains Java code snippets and ready-to-run programs. You can find these code
samples in the following sections:
You can get started quickly by using Eclipse with the AWS Toolkit for Eclipse. In addition to a full-
featured IDE, you also get the AWS SDK for Java with automatic updates, and preconfigured templates
for building AWS applications.
4.   In Select a wizard, choose AWS, choose AWS Java Project, and then choose Next.
5.   In Create an AWS Java, do the following:
          If this is your first time using the AWS Toolkit for Eclipse, choose Configure AWS Accounts to
          set up your AWS credentials.
6.   Choose Finish to create the project.
7.   From the Eclipse menu, choose File, New, and then Class.
8.   In Java Class, type a name for your class in Name (use the same name as the code sample that you
     want to run), and then choose Finish to create the class.
9.   Copy the code sample from the documentation page you are reading into the Eclipse editor.
10. To run the code, choose Run in the Eclipse menu.
The SDK for Java provides thread-safe clients for working with DynamoDB. As a best practice, your
applications should create one client and reuse the client between threads.
The following is an example of an AWS credentials file named ~/.aws/credentials, where the tilde
character (~) represents your home directory:
[default]
aws_access_key_id = AWS access key ID goes here
aws_secret_access_key = Secret key goes here
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder;
import com.amazonaws.regions.Regions;
...
// This client will default to US West (Oregon)
AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard()
.withRegion(Regions.US_WEST_2)
.build();
You can use the withRegion method to run your code against Amazon DynamoDB in any region where
it is available. For a complete list, see AWS Regions and Endpoints in the Amazon Web Services General
Reference.
If you want to run the code samples using DynamoDB locally on your computer, you need to set the
endpoint, like this:
This Developer Guide contains .NET code snippets and ready-to-run programs. You can find these code
samples in the following sections:
You can get started quickly by using the AWS SDK for .NET with the Toolkit for Visual Studio.
     If this is your first time using Toolkit for Visual Studio, choose Use a new profile to set up your AWS
     credentials.
6.   In your Visual Studio project, choose the tab for your program's source code (Program.cs). Copy
     the code sample from the documentation page you are reading into the Visual Studio editor,
     replacing any other code that you see in the editor.
7.   If you see error messages of the form The type or namespace name...could not be found,
     you need to install the AWS SDK assembly for DynamoDB as follows:
     a.   In Solution Explorer, open the context (right-click) menu for your project, and then choose
          Manage NuGet Packages.
     b.   In NuGet Package Manager, choose Browse.
     c.   In the search box, type AWSSDK.DynamoDBv2 and wait for the search to complete.
     d.   Choose AWSSDK.DynamoDBv2, and then choose Install.
     e.   When the installation is complete, choose the Program.cs tab to return to your program.
8.   To run the code, choose the Start button in the Visual Studio toolbar.
The AWS SDK for .NET provides thread-safe clients for working with DynamoDB. As a best practice, your
applications should create one client and reuse the client between threads.
The Toolkit for Visual Studio supports multiple sets of credentials from any number of accounts. Each
set is referred to as a profile. Visual Studio adds entries to the project's App.config file so that your
application can find the AWS credentials at runtime.
The following example shows the default App.config file that is generated when you create a new
project using Toolkit for Visual Studio:
At runtime, the program uses the default set of AWS credentials, as specified by the AWSProfileName
entry. The AWS credentials themselves are kept in the SDK Store, in encrypted form. The Toolkit for
Visual Studio provides a graphical user interface for managing your credentials, all from within Visual
Studio. For more information, see Specifying Credentials in the AWS Toolkit for Visual Studio User Guide.
     Note
     By default, the code samples access DynamoDB in the US West (Oregon) region. You can change
     the region by modifying the AWSRegion entry in the App.config file. You can set AWSRegion
     to any AWS region where Amazon DynamoDB is available. For a complete list, see AWS Regions
     and Endpoints in the Amazon Web Services General Reference.
The following code snippet instantiates a new AmazonDynamoDBClient. The client is modified so that
the code runs against DynamoDB in a different region.
clientConfig.RegionEndpoint = RegionEndpoint.USEast1;
AmazonDynamoDBClient client = new AmazonDynamoDBClient(clientConfig);
For a complete list of regions, see AWS Regions and Endpoints in the Amazon Web Services General
Reference.
If you want to run the code samples using DynamoDB locally on your computer, you need to set the
endpoint:
    Topics
     • Working with Tables in DynamoDB (p. 291)
     • Working with Items in DynamoDB (p. 326)
     • Working with Queries (p. 408)
     • Working with Scans (p. 425)
     • Improving Data Access with Secondary Indexes (p. 444)
     • Capturing Table Activity with DynamoDB Streams (p. 517)
    This section also provides more information about throughput capacity, using DynamoDB auto scaling or
    manually setting provisioned throughput.
    Topics
     • Basic Operations for Tables (p. 291)
     • Throughput Settings for Reads and Writes (p. 294)
     • Item Sizes and Capacity Unit Consumption (p. 297)
     • Managing Throughput Capacity Automatically with DynamoDB Auto Scaling (p. 299)
     • Tagging for DynamoDB (p. 313)
     • Working with Tables: Java (p. 315)
     • Working with Tables: .NET (p. 320)
Creating a Table
Use the CreateTable operation to create a table. You must provide the following information:
• Table name. The name must conform to the DynamoDB naming rules, and must be unique for the
  current AWS account and region. For example, you could create a People table in US East (N. Virginia)
  and another People table in EU (Ireland) - however, these two tables would be entirely different from
  each other. For more information, see Naming Rules and Data Types (p. 11).
• Primary key. The primary key can consist of one attribute (partition key) or two attributes (partition
  key and sort key). You need to provide the attribute names, data types, and the role of each attribute:
  HASH (for a partition key) and RANGE (for a sort key). For more information, see Primary Key (p. 5).
• Throughput settings. You must specify the initial read and write throughput settings for the table.
  You can modify these settings later, or enable DynamoDB auto scaling to manage the settings for
  you. For more information, see Throughput Settings for Reads and Writes (p. 294) and Managing
  Throughput Capacity Automatically with DynamoDB Auto Scaling (p. 299).
Example
The following AWS CLI example shows how to create a table (Music). The primary key consists of
Artist (partition key) and SongTitle (sort key), each of which has a data type of String. The maximum
throughput for this table is 10 read capacity units and 5 write capacity units.
The CreateTable operation returns metadata for the table, as shown below:
{
    "TableDescription": {
        "AttributeDefinitions": [
            {
                "AttributeName": "Artist",
                "AttributeType": "S"
            },
            {
                "AttributeName": "SongTitle",
                "AttributeType": "S"
            }
        ],
        "TableName": "Music",
        "KeySchema": [
            {
                "AttributeName": "Artist",
                "KeyType": "HASH"
            },
            {
                "AttributeName": "SongTitle",
                "KeyType": "RANGE"
            }
         ],
         "TableStatus": "CREATING",
         "CreationDateTime": 1491338657.039,
         "ProvisionedThroughput": {
             "NumberOfDecreasesToday": 0,
             "ReadCapacityUnits": 10,
             "WriteCapacityUnits": 5
         },
         "TableSizeBytes": 0,
         "ItemCount": 0,
         "TableArn": "arn:aws:dynamodb:us-east-1:123456789012:table/Music"
    }
}
The TableStatus element indicates the current state of the table (CREATING). It might take
a while to create the table, depending on the values you specify for ReadCapacityUnits and
WriteCapacityUnits. Larger values for these will require DynamoDB to allocate more resources for
the table.
Describing a Table
To view details about a table, use the DescribeTable operation. You must provide the table name.
The output from DescribeTable is in the same format as that from CreateTable; it includes the
timestamp when the table was created, its key schema, its provisioned throughput settings, its estimated
size, and any secondary indexes that are present.
Example
The table is ready for use when the TableStatus has changed from CREATING to ACTIVE.
    Note
    If you issue a DescribeTable request immediately after a CreateTable request, DynamoDB
    might return an error (ResourceNotFoundException). This is because DescribeTable uses
    an eventually consistent query, and the metadata for your table might not be available at that
    moment. Wait for a few seconds, and then try the DescribeTable request again.
    For billing purposes, your DynamoDB storage costs include a per-item overhead of 100 bytes.
    (For more information, go to DynamoDB Pricing). This extra 100 bytes per item is not used in
    capacity unit calculations or by the DescribeTable operation.
Updating a Table
The UpdateTable operation allows you to do one of the following:
Example
This AWS CLI example shows how to modify a table's provisioned throughput settings:
    Note
    When you issue an UpdateTable request, the status of the table changes from AVAILABLE to
    UPDATING. The table remains fully available for use while it is UPDATING. When this process is
    completed, the table status changes from UPDATING to AVAILABLE.
Deleting a Table
You can remove an unused table with the DeleteTable operation. Note that deleting a table is an
unrecoverable operation.
When you issue a DeleteTable request, the table's status changes from ACTIVE to DELETING. It might
take a while to delete the table, depending on the resources it uses (such as the data stored in the table,
and any streams or indexes on the table).
When the DeleteTable operation concludes, the table no longer exists in DynamoDB.
Example
Example
The output shows the upper limits of read and write capacity units for the current AWS account and
region.
For more information about these limits, and how to request limit increases, see Provisioned Throughput
Default Limits (p. 769).
When you create a new table in DynamoDB, you must specify its provisioned throughput capacity—the
amount of read and write activity that the table will be able to support. DynamoDB uses this information
to reserve sufficient system resources to meet your throughput requirements.
    Note
    You can optionally allow DynamoDB auto scaling to manage your table's throughput capacity;
    however, you still must provide initial settings for read and write capacity when you create the
    table. DynamoDB auto scaling uses these initial settings as a starting point, and then adjusts
    them dynamically in response to your application's requirements.
    For more information, see Managing Throughput Capacity Automatically with DynamoDB Auto
    Scaling (p. 299).
DynamoDB automatically distributes your data across partitions, which are stored on multiple servers
in the AWS Cloud. (For more information, see Partitions and Data Distribution (p. 18).) For optimal
throughput, you should distribute read requests as evenly as possible across these partitions. For
example, suppose that you provision a table with 10,000 read capacity units. If you issue 10,000 read
requests for a single item in the table, all of the read activity will be concentrated on a single partition.
However, if you spread your requests across all items in the table, DynamoDB can access the partitions in
parallel.
As your application data and access requirements change, you might need to adjust your table's
throughput settings. If you're using DynamoDB auto scaling, the throughput settings are automatically
adjusted in response to actual workloads. You can also use the UpdateTable operation to manually
adjust your table's throughput capacity. You might decide to do this if you need to bulk-load data from
an existing data store into your new DynamoDB table. You could create the table with a large write
throughput setting and then reduce this setting after the bulk data load is complete.
You specify throughput requirements in terms of capacity units—the amount of data your application
needs to read or write per second. You can modify these settings later, if needed, or enable DynamoDB
auto scaling to modify them automatically.
For example, suppose that you create a table with 10 provisioned read capacity units. This will allow you
to perform 10 strongly consistent reads per second, or 20 eventually consistent reads per second, for
items up to 4 KB.
Reading an item larger than 4 KB consumes more read capacity units. For example a strongly consistent
read of an item that is 8 KB (4 KB × 2) consumes 2 read capacity units. An eventually consistent read on
that same item consumes only 1 read capacity unit.
Item sizes for reads are rounded up to the next 4 KB multiple. For example, reading a 3,500-byte item
will consume the same throughput as reading a 4 KB item.
For example, suppose that you create a table with 10 write capacity units. This will allow you to perform
10 writes per second, for items up to 1 KB size per second.
Item sizes for writes are rounded up to the next 1 KB multiple. For example, writing a 500-byte item will
consume the same throughput as writing a 1 KB item.
The DynamoDB console displays Amazon CloudWatch metrics for your tables, so you can monitor
throttled read requests and write requests. If you encounter excessive throttling, you should consider
increasing your table's provisioned throughput settings.
In some cases, DynamoDB will use burst capacity to accommodate reads or writes in excess of your
table's throughput settings. With burst capacity, unexpected read or write requests can succeed where
they otherwise would be throttled. Burst capacity is available on a best-effort basis, and DynamoDB
does not guarantee that this capacity is always available. For more information, see Use Burst Capacity
Sparingly (p. 710).
• Item sizes. Some items are small enough that they can be read or written using a single capacity unit.
  Larger items will require multiple capacity units. By estimating the sizes of the items that will be in
  your table, you can specify accurate settings for your table's provisioned throughput.
• Expected read and write request rates. In addition to item size, you should estimate the number of
  reads and writes you need to perform per second.
• Read consistency requirements. Read capacity units are based on strongly consistent read operations,
  which consume twice as many database resources as eventually consistent reads. You should
  determine whether your application requires strongly consistent reads, or whether it can relax this
  requirement and perform eventually consistent reads instead. (Read operations in DynamoDB are
  eventually consistent, by default; you can request strongly consistent reads for these operations if
  necessary.)
    Note
    For recommendations on provisioned throughput and related topics, see Best Practices for
    Tables (p. 704).
You can modify your table's provisioned throughput settings using the AWS Management Console
or the UpdateTable operation . You can increase throughput capacity as often as needed, and
decrease it up to nine times per table in a single UTC calendar day. For more information, see Limits in
DynamoDB (p. 769).
Before you choose read and write capacity settings for your table, you should first understand your data
and how your application will access it. These inputs can help you determine your table's overall storage
and throughput needs, and how much throughput capacity your application will require. DynamoDB
tables are schemaless, except for the primary key, so the items in a table can all have different attributes,
sizes, and data types.
The total size of an item is the sum of the lengths of its attribute names and values. You can use the
following guidelines to estimate attribute sizes:
• Strings are Unicode with UTF-8 binary encoding. The size of a string is (length of attribute name) +
  (number of UTF-8-encoded bytes).
• Numbers are variable length, with up to 38 significant digits. Leading and trailing zeroes are trimmed.
  The size of a number is approximately (length of attribute name) + (1 byte per two significant digits) +
  (1 byte).
• A binary value must be encoded in base64 format before it can be sent to DynamoDB, but the value's
  raw byte length is used for calculating size. The size of a binary attribute is (length of attribute name) +
  (number of raw bytes).
• The size of a null attribute or a Boolean attribute is (length of attribute name) + (1 byte).
• An attribute of type List or Map requires 3 bytes of overhead, regardless of its contents. The size of
  a List or Map is (length of attribute name) + sum (size of nested elements) + (3 bytes) . The size of an
  empty List or Map is (length of attribute name) + (3 bytes).
    Note
    We recommend that you choose shorter attribute names rather than long ones. This will help
    you optimize capacity unit consumption and reduce the amount of storage required for your
    data.
• GetItem—reads a single item from a table. To determine the number of capacity units GetItem will
  consume, take the item size and round it up to the next 4 KB boundary. If you specified a strongly
  consistent read, this is the number of capacity units required. For an eventually consistent read (the
  default), take this number and divide it by two.
  For example, if you read an item that is 3.5 KB, DynamoDB rounds the item size to 4 KB. If you read an
  item of 10 KB, DynamoDB rounds the item size to 12 KB.
• BatchGetItem—reads up to 100 items, from one or more tables. DynamoDB processes each item
  in the batch as an individual GetItem request, so DynamoDB first rounds up the size of each item to
  the next 4 KB boundary, and then calculates the total size. The result is not necessarily the same as
  the total size of all the items. For example, if BatchGetItem reads a 1.5 KB item and a 6.5 KB item,
  DynamoDB will calculate the size as 12 KB (4 KB + 8 KB), not 8 KB (1.5 KB + 6.5 KB).
• Query—reads multiple items that have the same partition key value. All of the items returned are
  treated as a single read operation, where DynamoDB computes the total size of all items and then
  rounds up to the next 4 KB boundary. For example, suppose your query returns 10 items whose
  combined size is 40.8 KB. DynamoDB rounds the item size for the operation to 44 KB. If a query
  returns 1500 items of 64 bytes each, the cumulative size is 96 KB.
• Scan—reads all of the items in a table. DynamoDB considers the size of the items that are evaluated,
  not the size of the items returned by the scan.
If you perform a read operation on an item that does not exist, DynamoDB will still consume provisioned
read throughput: A strongly consistent read request consumes one read capacity unit, while an
eventually consistent read request consumes 0.5 of a read capacity unit.
For any operation that returns items, you can request a subset of attributes to retrieve; however, doing
so has no impact on the item size calculations. In addition, Query and Scan can return item counts
instead of attribute values. Getting the count of items uses the same quantity of read capacity units
and is subject to the same item size calculations, because DynamoDB has to read each item in order to
increment the count.
• PutItem—writes a single item to a table. If an item with the same primary key exists in the table, the
  operation replaces the item. For calculating provisioned throughput consumption, the item size that
  matters is the larger of the two.
• UpdateItem—modifies a single item in the table. DynamoDB considers the size of the item as it
  appears before and after the update. The provisioned throughput consumed reflects the larger of
  these item sizes. Even if you update just a subset of the item's attributes, UpdateItem will still
  consume the full amount of provisioned throughput (the larger of the "before" and "after" item sizes).
• DeleteItem—removes a single item from a table. The provisioned throughput consumption is based
  on the size of the deleted item.
• BatchWriteItem—writes up to 25 items to one or more tables. DynamoDB processes each item
  in the batch as an individual PutItem or DeleteItem request (updates are not supported), so
  DynamoDB first rounds up the size of each item to the next 1 KB boundary, and then calculates the
  total size. The result is not necessarily the same as the total size of all the items. For example, if
  BatchWriteItem writes a 500 byte item and a 3.5 KB item, DynamoDB will calculate the size as 5 KB
  (1 KB + 4 KB), not 4 KB (500 bytes + 3.5 KB).
For PutItem, UpdateItem, and DeleteItem operations, DynamoDB rounds the item size up to the
next 1 KB. For example, if you put or delete an item of 1.6 KB, DynamoDB rounds the item size up to 2
KB.
PutItem, UpdateItem, and DeleteItem allow conditional writes, where you specify an expression
that must evaluate to true in order for the operation to succeed. If the expression evaluates to false,
DynamoDB will still consume write capacity units from the table:
• If the item exists, the number of write capacity units consumed depends on the size of the item. (For
  example, a failed conditional write of a 1 KB item would consume one write capacity unit; if the item
  were twice that size, the failed conditional write would consume two write capacity units.)
• If the item does not exist, DynamoDB will consume one write capacity unit.
Many database workloads are cyclical in nature or are difficult to predict in advance. For example,
consider a social networking app where most of the users are active during daytime hours. The database
must be able to handle the daytime activity, but there's no need for the same levels of throughput
at night. Another example might be a new mobile gaming app that is experiencing rapid adoption.
If the game becomes too popular, it could exceed the available database resources, resulting in slow
performance and unhappy customers. These kinds of workloads often require manual intervention to
scale database resources up or down in response to varying usage levels.
DynamoDB auto scaling uses the AWS Application Auto Scaling service to dynamically adjust provisioned
throughput capacity on your behalf, in response to actual traffic patterns. This enables a table or a global
secondary index to increase its provisioned read and write capacity to handle sudden increases in traffic,
without throttling. When the workload decreases, Application Auto Scaling decreases the throughput so
that you don't pay for unused provisioned capacity.
    Important
    Currently, Auto Scaling does not scale down your provisioned capacity if your table’s consumed
    capacity becomes zero. As a workaround, you can send requests to the table until Auto Scaling
    scales down to the minimum capacity, or change the policy to reduce the maximum provisioned
    capacity to be the same as the minimum provisioned capacity.
    Note
    If you use the AWS Management Console to create a table or a global secondary index,
    DynamoDB auto scaling is enabled by default. You can modify your auto scaling settings at any
    time. For more information, see Using the AWS Management Console With DynamoDB Auto
    Scaling (p. 301).
With Application Auto Scaling, you create a scaling policy for a table or a global secondary index. The
scaling policy specifies whether you want to scale read capacity or write capacity (or both), and the
minimum and maximum provisioned capacity unit settings for the table or index.
The scaling policy also contains a target utilization—the percentage of consumed provisioned
throughput at a point in time. Application Auto Scaling uses a target tracking algorithm to adjust the
provisioned throughput of the table (or index) upward or downward in response to actual workloads, so
that the actual capacity utilization remains at or near your target utilization.
    Note
    In addition to tables, DynamoDB auto scaling also supports global secondary indexes. Every
    global secondary index has its own provisioned throughput capacity, separate from that of its
    base table. When you create a scaling policy for a global secondary index, Application Auto
    Scaling adjusts the provisioned throughput settings for the index to ensure that its actual
    utilization stays at or near your desired utilization ratio.
The following diagram provides a high-level overview of how DynamoDB auto scaling manages
throughput capacity for a table:
The following steps summarize the auto scaling process as shown in the previous diagram:
1. You create an Application Auto Scaling policy for your DynamoDB table.
2. DynamoDB publishes consumed capacity metrics to Amazon CloudWatch.
3. If the table's consumed capacity exceeds your target utilization (or falls below the target) for a
   specific length of time, Amazon CloudWatch triggers an alarm. You can view the alarm on the AWS
   Management Console and receive notifications using Amazon Simple Notification Service (Amazon
   SNS).
4. The CloudWatch alarm invokes Application Auto Scaling to evaluate your scaling policy.
5. Application Auto Scaling issues an UpdateTable request to adjust your table's provisioned
   throughput.
6. DynamoDB processes the UpdateTable request, dynamically increasing (or decreasing) the table's
   provisioned throughput capacity so that it approaches your target utilization.
To understand how DynamoDB auto scaling works, suppose that you have a table named ProductCatalog.
The table is bulk-loaded with data infrequently, so it doesn't incur very much write activity. However,
it does experience a high degree of read activity, which varies over time. By monitoring the Amazon
CloudWatch metrics for ProductCatalog, you determine that the table requires 1,200 read capacity
units (to avoid DynamoDB throttling read requests when activity is at its peak). You also determine that
ProductCatalog requires 150 read capacity units at a minimum, when read traffic is at its lowest point.
Within the range of 150 to 1,200 read capacity units, you decide that a target utilization of 70 percent
would be appropriate for the ProductCatalog table. Target utilization is the ratio of consumed capacity
units to provisioned capacity units, expressed as a percentage. Application Auto Scaling uses its target
tracking algorithm to ensure that the provisioned read capacity of ProductCatalog is adjusted as required
so that utilization remains at or near 70 percent.
    Note
    DynamoDB auto scaling modifies provisioned throughput settings only when the actual
    workload stays elevated (or depressed) for a sustained period of several minutes. The
    Application Auto Scaling target tracking algorithm seeks to keep the target utilization at or near
    your chosen value over the long term.
    Sudden, short-duration spikes of activity are accommodated by the table's built-in burst
    capacity. For more information, see Use Burst Capacity Sparingly (p. 710).
To enable DynamoDB auto scaling for the ProductCatalog table, you create a scaling policy. This policy
specifies the table or global secondary index that you want to manage, which capacity type to manage
(read capacity or write capacity), the upper and lower boundaries for the provisioned throughput
settings, and your target utilization.
When you create a scaling policy, Application Auto Scaling creates a pair of Amazon CloudWatch alarms
on your behalf. Each pair represents your upper and lower boundaries for provisioned throughput
settings. These CloudWatch alarms are triggered when the table's actual utilization deviates from your
target utilization for a sustained period of time.
When one of the CloudWatch alarms is triggered, Amazon SNS sends you a notification (if you have
enabled it). The CloudWatch alarm then invokes Application Auto Scaling, which in turn notifies
DynamoDB to adjust the ProductCatalog table's provisioned capacity upward or downward, as
appropriate.
Usage Notes
Before you begin using DynamoDB auto scaling, you should be aware of the following:
• DynamoDB auto scaling can increase read capacity or write capacity as often as necessary,
  in accordance with your auto scaling policy. You can decrease the ReadCapacityUnits or
  WriteCapacityUnits settings for a table up to four times any time per day. A day is defined
  according to the GMT time zone. Additionally, if there was no decrease in the past four hours, an
  additional dial down is allowed, effectively bringing maximum number of decreases in a day to nine
  times (4 decreases in the first 4 hours, and 1 decrease for each of the subsequent 4 hour windows in a
  day). All other DynamoDB limits remain in effect, as described in Limits in DynamoDB.
• DynamoDB auto scaling doesn't prevent you from manually modifying provisioned throughput
  settings. These manual adjustments don't affect any existing CloudWatch alarms that are related to
  DynamoDB auto scaling.
• If you enable DynamoDB auto scaling for a table that has one or more global secondary indexes, we
  highly recommend that you also apply auto scaling uniformly to those indexes. You can do this by
  choosing Apply same settings to global secondary indexes in the AWS Management Console. For
  more information, see Enabling DynamoDB Auto Scaling on Existing Tables (p. 303).
When you use the AWS Management Console to create a new table, DynamoDB auto scaling is enabled
for that table by default. You can also use the console to enable auto scaling for existing tables, modify
auto scaling settings, or disable auto scaling.
     Note
     For more advanced features like setting scale in and scale out cooldown times use the AWS
     Command Line Interface (AWS CLI) to manage DynamoDB auto scaling.
Before You Begin: Grant User Permissions for DynamoDB Auto Scaling
In AWS Identity and Access Management (IAM), the AWS-managed policy DynamoDBFullAccess
provides the required permissions for using the DynamoDB console. However, for DynamoDB auto
scaling, IAM users will require some additional privileges.
     Important
     application-autoscaling:* permissions are required to delete an autoscaling enabled
     table. The AWS-managed policy DynamoDBFullAccess attached next includes such
     permissions.
To set up an IAM user for DynamoDB console access and DynamoDB auto scaling, add the following two
policies:
     •   Select Read capacity, Write capacity, or both. (For write capacity, note that you can choose
         Same settings as read.) For each of these, do the following:
(For Write capacity, note that you can choose Same settings as read.)
To view these auto scaling activities in the DynamoDB console, choose the table that you want to work
with. Choose Capacity, and then expand the Scaling activities section. When your table's throughput
settings are modified, you will see informational messages here.
To disable DynamoDB auto scaling, go to the Capacity tab for your table and clear Read capacity, Write
capacity, or both.
Instead of using the AWS Management Console, you can use the AWS Command Line Interface (AWS
CLI) to manage DynamoDB auto scaling. The tutorial in this section demonstrates how to install and
configure the AWS CLI for managing DynamoDB auto scaling . In this tutorial, you do the following:
• Create a DynamoDB table named TestTable. The initial throughput settings are 5 read capacity units
  and 5 write capacity units.
• Create an Application Auto Scaling policy for TestTable. The policy seeks to maintain a 50 percent
  target ratio between consumed write capacity and provisioned write capacity. The range for this
  metric is between 5 and 10 write capacity units. (Application Auto Scaling is not allowed to adjust the
  throughput beyond this range.)
• Run a Python program to drive write traffic to TestTable. When the target ratio exceeds 50 percent for
  a sustained period of time, Application Auto Scaling notifies DynamoDB to adjust the throughput of
  TestTable upward, so that the 50 percent target utilization can be maintained.
• Verify that DynamoDB has successfully adjusted the provisioned write capacity for TestTable.
If you haven't already done so, you must install and configure the AWS CLI. To do this, go to the AWS
Command Line Interface User Guide and follow these instructions:
Install Python
Part of this tutorial requires you to run a Python program (see Step 4: Drive Write Traffic to
TestTable (p. 307)). If you don't already have Python installed, you can download it using this link:
https://www.python.org/downloads
         Note
         You can also register a scalable target against a GSI. For example, for a GSI ("test-index"),
         note that the resource id and scalable dimension arguments are updated appropriately:
      Note
      To further understand how TargetValue works, suppose that you have a table with a
      provisioned throughput setting of 200 write capacity units. You decide to create a scaling policy
      for this table, with a TargetValue of 70 percent.
      Now suppose that you begin driving write traffic to the table so that the actual write throughput
      is 150 capacity units. The consumed-to-provisioned ratio is now (150 / 200), or 75 percent. This
      ratio exceeds your target, so Application Auto Scaling increases the provisioned write capacity
      to 215 so that the ratio is (150 / 215), or 69.77 percent—as close to your TargetValue as
      possible, but not exceeding it.
For TestTable, you set TargetValue to 50 percent. Application Auto Scaling adjusts the table's
provisioned throughput within the range of 5 to 10 capacity units (see Step 2: Register a Scalable
Target (p. 305)) so that the consumed-to-provisioned ratio remains at or near 50 percent. You set the
values for ScaleOutCooldown and ScaleInCooldown to 60 seconds.
      {
          "PredefinedMetricSpecification": {
              "PredefinedMetricType": "DynamoDBWriteCapacityUtilization"
          },
          "ScaleOutCooldown": 60,
          "ScaleInCooldown": 60,
          "TargetValue": 50.0
      }
3.   In the output, note that Application Auto Scaling has created two CloudWatch alarms—one each for
     the upper and lower boundary of the scaling target range.
4.   Use the following AWS CLI command to view more details about the scaling policy:
5.   In the output, verify that the policy settings match your specifications from Step 2: Register a
     Scalable Target (p. 305) and Step 3: Create a Scaling Policy (p. 306).
     import boto3
     dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table("TestTable")
     i = 0
     while (i < 10):
         j = 0
         while (j < 10):
             print (i, j)
             table.put_item(
                 Item={
                     'pk':i,
                     'sk':j,
                     'filler':{"S":filler}
                 }
             )
             j += 1
         i += 1
python bulk-load-test-table.py
     The provisioned write capacity for TestTable is very low (5 write capacity units), so the program stalls
     occasionally due to write throttling. This is expected behavior.
Let the program continue running, while you move on to the next step.
1. Type the following command to view the Application Auto Scaling actions:
     Rerun this command occasionally, while the Python program is running. (It will take several minutes
     before your scaling policy is invoked.) You should eventually see the following output:
     ...
     {
         "ScalableDimension": "dynamodb:table:WriteCapacityUnits",
         "Description": "Setting write capacity units to 10.",
         "ResourceId": "table/TestTable",
         "ActivityId": "0cc6fb03-2a7c-4b51-b67f-217224c6b656",
         "StartTime": 1489088210.175,
         "ServiceNamespace": "dynamodb",
         "EndTime": 1489088246.85,
         "Cause": "monitor alarm AutoScaling-table/TestTable-AlarmHigh-1bb3c8db-1b97-4353-
     baf1-4def76f4e1b9 in state ALARM triggered policy MyScalingPolicy",
         "StatusMessage": "Successfully set write capacity units to 10. Change successfully
      fulfilled by dynamodb.",
         "StatusCode": "Successful"
     },
     ...
     This indicates that Application Auto Scaling has issued an UpdateTable request to DynamoDB.
2.   Type the following command to verify that DynamoDB increased the table's write capacity:
         --resource-id "table/TestTable" \
         --scalable-dimension "dynamodb:table:WriteCapacityUnits" \
         --policy-name "MyScalingPolicy"
• EnableDynamoDBAutoscaling.java
• DisableDynamoDBAutoscaling.java
• The program registers write capacity units as a scalable target for TestTable. The range for this metric
  is between 5 and 10 write capacity units.
• After the scalable target is created, the program builds a target tracking configuration. The policy
  seeks to maintain a 50 percent target ratio between consumed write capacity and provisioned write
  capacity.
• The program then creates the scaling policy, based on the target tracking configuration.
The program requires that you supply an ARN for a valid Application Auto Scaling
service linked role. (For example: "arn:aws:iam::122517410325:role/
AWSServiceRoleForApplicationAutoScaling_DynamoDBTable.) In the following program,
replace SERVICE_ROLE_ARN_GOES_HERE with the actual ARN.
package com.amazonaws.codesamples.autoscaling;
import   com.amazonaws.services.applicationautoscaling.AWSApplicationAutoScalingClient;
import   com.amazonaws.services.applicationautoscaling.model.DescribeScalableTargetsRequest;
import   com.amazonaws.services.applicationautoscaling.model.DescribeScalableTargetsResult;
import   com.amazonaws.services.applicationautoscaling.model.DescribeScalingPoliciesRequest;
import   com.amazonaws.services.applicationautoscaling.model.DescribeScalingPoliciesResult;
import   com.amazonaws.services.applicationautoscaling.model.MetricType;
import   com.amazonaws.services.applicationautoscaling.model.PolicyType;
import   com.amazonaws.services.applicationautoscaling.model.PredefinedMetricSpecification;
import com.amazonaws.services.applicationautoscaling.model.PutScalingPolicyRequest;
import com.amazonaws.services.applicationautoscaling.model.RegisterScalableTargetRequest;
import com.amazonaws.services.applicationautoscaling.model.ScalableDimension;
import com.amazonaws.services.applicationautoscaling.model.ServiceNamespace;
import
 com.amazonaws.services.applicationautoscaling.model.TargetTrackingScalingPolicyConfiguration;
 ServiceNamespace ns = ServiceNamespace.Dynamodb;
 ScalableDimension tableWCUs = ScalableDimension.DynamodbTableWriteCapacityUnits;
 String resourceID = "table/TestTable";
 try {
     aaClient.registerScalableTarget(rstRequest);
 } catch (Exception e) {
     System.err.println("Unable to register scalable target: ");
     System.err.println(e.getMessage());
 }
System.out.println();
    .withResourceId(resourceID)
    .withPolicyName("MyScalingPolicy")
    .withPolicyType(PolicyType.TargetTrackingScaling)
    .withTargetTrackingScalingPolicyConfiguration(targetTrackingScalingPolicyConfiguration);
 try {
     aaClient.putScalingPolicy(pspRequest);
 } catch (Exception e) {
     System.err.println("Unable to put scaling policy: ");
     System.err.println(e.getMessage());
 }
 try {
     DescribeScalingPoliciesResult dspResult =
 aaClient.describeScalingPolicies(dspRequest);
     System.out.println("DescribeScalingPolicies result: ");
     System.out.println(dspResult);
 } catch (Exception e) {
     e.printStackTrace();
     System.err.println("Unable to describe scaling policy: ");
     System.err.println(e.getMessage());
 }
package com.amazonaws.codesamples.autoscaling;
import    com.amazonaws.services.applicationautoscaling.AWSApplicationAutoScalingClient;
import    com.amazonaws.services.applicationautoscaling.model.DeleteScalingPolicyRequest;
import    com.amazonaws.services.applicationautoscaling.model.DeregisterScalableTargetRequest;
import    com.amazonaws.services.applicationautoscaling.model.DescribeScalableTargetsRequest;
import    com.amazonaws.services.applicationautoscaling.model.DescribeScalableTargetsResult;
import    com.amazonaws.services.applicationautoscaling.model.DescribeScalingPoliciesRequest;
import    com.amazonaws.services.applicationautoscaling.model.DescribeScalingPoliciesResult;
import    com.amazonaws.services.applicationautoscaling.model.ScalableDimension;
import    com.amazonaws.services.applicationautoscaling.model.ServiceNamespace;
 ServiceNamespace ns = ServiceNamespace.Dynamodb;
 ScalableDimension tableWCUs = ScalableDimension.DynamodbTableWriteCapacityUnits;
 String resourceID = "table/TestTable";
    .withScalableDimension(tableWCUs)
    .withResourceId(resourceID)
    .withPolicyName("MyScalingPolicy");
 try {
     aaClient.deleteScalingPolicy(delSPRequest);
 } catch (Exception e) {
     System.err.println("Unable to delete scaling policy: ");
     System.err.println(e.getMessage());
 }
 try {
     DescribeScalingPoliciesResult dspResult =
 aaClient.describeScalingPolicies(descSPRequest);
     System.out.println("DescribeScalingPolicies result: ");
     System.out.println(dspResult);
 } catch (Exception e) {
     e.printStackTrace();
     System.err.println("Unable to describe scaling policy: ");
     System.err.println(e.getMessage());
 }
System.out.println();
 try {
     aaClient.deregisterScalableTarget(delSTRequest);
 } catch (Exception e) {
     System.err.println("Unable to deregister scalable target: ");
     System.err.println(e.getMessage());
 }
 try {
     DescribeScalableTargetsResult dsaResult =
 aaClient.describeScalableTargets(dscRequest);
     System.out.println("DescribeScalableTargets result: ");
     System.out.println(dsaResult);
     System.out.println();
 } catch (Exception e) {
     System.err.println("Unable to describe scalable target: ");
     System.err.println(e.getMessage());
 }
Tagging is supported by AWS services like Amazon EC2, Amazon S3, DynamoDB, and more. Efficient
tagging can provide cost insights by allowing you to create reports across services that carry a specific
tag.
Finally, it is good practice to follow optimal tagging strategies. For information, see AWS Tagging
Strategies.
Tagging Restrictions
Each tag consists of a key and a value, both of which you define. The following restrictions apply:
• Each DynamoDB table can have only one tag with the same key. If you try to add an existing tag (same
  key), the existing tag value will be updated to the new value.
• Tag keys and values are case sensitive.
• Maximum key length: 128 Unicode characters
• Maximum value length: 256 Unicode characters
• Allowed characters are letters, whitespace, and numbers, plus the following special characters: + - = .
  _:/
• Maximum number of tags per resource: 50
• AWS-assigned tag names and values are automatically assigned the aws: prefix, which you cannot
  assign. AWS-assigned tag names do not count toward the tag limit of 50. User-assigned tag names
  have the prefix user: in the cost allocation report.
• You cannot tag a resource at the same time you create it. Tagging is a separate action that can be
  performed only after the resource is created.
• You cannot backdate the application of a tag.
Tagging Operations
This section describes how to use the DynamoDB console or CLI to add, list, edit, or delete tags. You can
then activate these user-defined tags so that they appear on the Billing and Cost Management console
for cost allocation tracking. For more information, see Cost Allocation Reports (p. 314).
For bulk editing, you can also use the Tag Editor in the AWS Management Console. For more information,
see Working with Tag Editor.
Topics
 • Tagging (console) (p. 314)
 • Tagging (CLI) (p. 314)
Tagging (console)
To use the console to add, list, edit, or delete tags:
  You can add, list, edit, or delete tags here. In this example, the Movies tag was created with a value of
  moviesProd for the Movies table.
Tagging (CLI)
To add the Owner tag with a value of blueTeam for the Movies table:
• An AWS-generated tag. AWS defines, creates, and applies this tag for you.
• User-defined tags. You define, create, and apply these tags.
You must activate both types of tags separately before they can appear in Cost Explorer or on a cost
allocation report.
1. Sign in to the AWS Management Console and open the Billing and Cost Management console at
   https://console.aws.amazon.com/billing/home#/.
2. In the navigation pane, choose Cost Allocation Tags.
3. Under AWS-Generated Cost Allocation Tags, choose Activate.
1. Sign in to the AWS Management Console and open the Billing and Cost Management console at
   https://console.aws.amazon.com/billing/home#/.
2. In the navigation pane, choose Cost Allocation Tags.
3. Under User-Defined Cost Allocation Tags, choose Activate.
After you create and activate tags, AWS generates a cost allocation report with your usage and costs
grouped by your active tags. The cost allocation report includes all of your AWS costs for each billing
period. The report includes both tagged and untagged resources, so that you can clearly organize the
charges for resources.
    Note
    Currently, any data transferred out from DynamoDB won't be broken down by tags on cost
    allocation reports.
You can use the AWS SDK for Java to create, update, and delete tables, list all the tables in your account,
or get information about a specific table.
The following are the common steps for table operations using the AWS SDK for Java Document API.
Creating a Table
To create a table, you must provide the table name, its primary key, and the provisioned throughput
values. The following code snippet creates an example table that uses a numeric type attribute Id as its
primary key.
   You must provide the table name, attribute definitions, key schema, and provisioned throughput
   values.
3. Execute the createTable method by providing the request object as a parameter.
         .withProvisionedThroughput(new ProvisionedThroughput()
             .withReadCapacityUnits(5L)
             .withWriteCapacityUnits(6L));
table.waitForActive();
The table will not be ready for use until DynamoDB creates it and sets its status to ACTIVE. The
createTable request returns a Table object that you can use to obtain more information about the
table.
Example
TableDescription tableDescription =
    dynamoDB.getTable(tableName).describe();
You can call the describe method of the client to get table information at any time.
Example
Updating a Table
You can update only the provisioned throughput values of an existing table. Depending on you
application requirements, you might need to update these values.
    Note
    You can increase throughput capacity as often as needed, and decrease it up to nine times per
    table in a single UTC calendar day. For more information, see Limits in DynamoDB (p. 769).
Example
table.updateTable(provisionedThroughput);
table.waitForActive();
Deleting a Table
To delete a table:
Example
table.delete();
table.waitForDelete();
Listing Tables
To list tables in your account, create an instance of DynamoDB and execute the listTables method.
The ListTables operation requires no parameters.
Example
while (iterator.hasNext()) {
    Table table = iterator.next();
    System.out.println(table.getTableName());
}
Example: Create, Update, Delete, and List Tables Using the AWS
SDK for Java Document API
The following code sample uses the AWS SDK for Java Document API to create, update, and delete a
table (ExampleTable). As part of the table update, it increases the provisioned throughput values. The
example also lists all the tables in your account and gets the description of a specific table. For step-by-
step instructions to run the following example, see Java Code Samples (p. 286).
import java.util.ArrayList;
import java.util.Iterator;
import   com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
import   com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder;
import   com.amazonaws.services.dynamodbv2.document.DynamoDB;
import   com.amazonaws.services.dynamodbv2.document.Table;
import   com.amazonaws.services.dynamodbv2.document.TableCollection;
import   com.amazonaws.services.dynamodbv2.model.AttributeDefinition;
import   com.amazonaws.services.dynamodbv2.model.CreateTableRequest;
import   com.amazonaws.services.dynamodbv2.model.KeySchemaElement;
import   com.amazonaws.services.dynamodbv2.model.KeyType;
import   com.amazonaws.services.dynamodbv2.model.ListTablesResult;
import   com.amazonaws.services.dynamodbv2.model.ProvisionedThroughput;
import   com.amazonaws.services.dynamodbv2.model.TableDescription;
          createExampleTable();
          listMyTables();
          getTableInformation();
          updateExampleTable();
          deleteExampleTable();
    }
try {
// key
getTableInformation();
          }
          catch (Exception e) {
              System.err.println("CreateTable request failed for " + tableName);
              System.err.println(e.getMessage());
        while (iterator.hasNext()) {
            Table table = iterator.next();
            System.out.println(table.getTableName());
        }
    }
        try {
            table.updateTable(new
 ProvisionedThroughput().withReadCapacityUnits(6L).withWriteCapacityUnits(7L));
            table.waitForActive();
        }
        catch (Exception e) {
            System.err.println("UpdateTable request failed for " + tableName);
            System.err.println(e.getMessage());
        }
    }
            table.waitForDelete();
        }
        catch (Exception e) {
            System.err.println("DeleteTable request failed for " + tableName);
            System.err.println(e.getMessage());
        }
    }
You can use the AWS SDK for .NET to create, update, and delete tables, list all the tables in your account,
or get information about a specific table.
The following are the common steps for table operations using the AWS SDK for .NET.
      Note
      The examples in this section do not work with .NET core as it does not support synchronous
      methods. For more information, see AWS Asynchronous APIs for .NET.
Creating a Table
To create a table, you must provide the table name, its primary key, and the provisioned throughput
values.
The following are the steps to create a table using the .NET low-level API.
    You must provide the table name, primary key, and the provisioned throughput values.
3. Execute the AmazonDynamoDBClient.CreateTable method by providing the request object as a
   parameter.
The following C# code snippet demonstrates the preceding steps. The sample creates a table
(ProductCatalog) that uses Id as the primary key and set of provisioned throughput values. Depending
on your application requirements, you can update the provisioned throughput values by using the
UpdateTable API.
You must wait until DynamoDB creates the table and sets the table status to ACTIVE. The CreateTable
response includes the TableDescription property that provides the necessary table information.
Example
You can also call the DescribeTable method of the client to get table information at anytime.
Example
Updating a Table
You can update only the provisioned throughput values of an existing table. Depending on you
application requirements, you might need to update these values.
      Note
      You can increase throughput capacity as often as needed, and decrease it up to nine times per
      table in a single UTC calendar day. For more information, see Limits in DynamoDB (p. 769).
The following are the steps to update a table using the .NET low-level API.
You must provide the table name and the new provisioned throughput values.
Example
Deleting a Table
The following are the steps to delete a table using the .NET low-level API.
Example
Listing Tables
To list tables in your account using the AWS SDK for .NET low-level API, create an instance of the
AmazonDynamoDBClient and execute the ListTables method. The ListTables operation requires
no parameters. However, you can specify optional parameters. For example, you can set the Limit
parameter if you want to use paging to limit the number of table names per page. This requires you to
create a ListTablesRequest object and provide optional parameters as shown in the following C#
code snippet. Along with the page size, the request sets the ExclusiveStartTableName parameter.
Initially, ExclusiveStartTableName is null, however, after fetching the first page of result, to retrieve
the next page of result, you must set this parameter value to the LastEvaluatedTableName property
of the current result.
Example
lastEvaluatedTableName = result.LastEvaluatedTableName;
Example: Create, Update, Delete, and List Tables Using the AWS
SDK for .NET Low-Level API
The following C# example create,, updates, and deletes a table (ExampleTable). It also lists all the
tables in your account and gets the description of a specific table. The table update increases the
provisioned throughput values. For step-by-step instructions to test the following sample, see .NET Code
Samples (p. 288).
using   System;
using   System.Collections.Generic;
using   Amazon.DynamoDBv2;
using   Amazon.DynamoDBv2.Model;
using   Amazon.Runtime;
namespace com.amazonaws.codesamples
{
    class LowLevelTableExample
    {
        private static AmazonDynamoDBClient client = new AmazonDynamoDBClient();
        private static string tableName = "ExampleTable";
DeleteExampleTable();
    WaitUntilTableReady(tableName);
}
Console.WriteLine(name);
        lastTableNameEvaluated = response.LastEvaluatedTableName;
    } while (lastTableNameEvaluated != null);
}
    WaitUntilTableReady(tableName);
}
                             {
                                   TableName = tableName
                             });
    In DynamoDB, an item is a collection of attributes. Each attribute has a name and a value. An attribute
    value can be a scalar, a set, or a document type. For more information, see Amazon DynamoDB: How It
    Works (p. 2).
    Each of these operations require you to specify the primary key of the item you want to work with. For
    example, to read an item using GetItem, you must specify the partition key and sort key (if applicable)
    for that item.
In addition to the four basic CRUD operations, DynamoDB also provides the following:
These batch operations combine multiple CRUD operations into a single request. In addition, the batch
operations read and write items in parallel to minimize response latencies.
This section describes how to use these operations and includes related topics, such as conditional
updates and atomic counters. This section also includes example code that uses the AWS SDKs. For best
practices, see Best Practices for Items (p. 714).
Reading an Item
To read an item from a DynamoDB table, use the GetItem operation. You must provide the name of the
table, along with the primary key of the item you want.
Example
The following AWS CLI example shows how to read an item from the ProductCatalog table.
    Note
    With GetItem, you must specify the entire primary key, not just part of it. For example, if a
    table has a composite primary key (partition key and sort key), you must supply a value for the
    partition key and a value for the sort key.
GetItem request performs an eventually consistent read, by default. You can use the ConsistentRead
parameter to request a strongly consistent read instead. (This will consume additional read capacity
units, but it will return the most up-to-date version of the item.)
GetItem returns all of the item's attributes. You can use a projection expression to return only some of
the attributes. (For more information, see Projection Expressions (p. 340).)
To return the number of read capacity units consumed by GetItem, set the ReturnConsumedCapacity
parameter to TOTAL.
Example
The following AWS CLI example shows some of the optional GetItem parameters.
Writing an Item
To create, update, or delete an item in a DynamoDB table, use one of the following operations:
• PutItem
• UpdateItem
• DeleteItem
For each of these operations, you need to specify the entire primary key, not just part of it. For example,
if a table has a composite primary key (partition key and sort key), you must supply a value for the
partition key and a value for the sort key.
To return the number of write capacity units consumed by any of these operations, set the
ReturnConsumedCapacity parameter to one of the following:
PutItem
PutItem creates a new item. If an item with the same key already exists in the table, it is replaced with
the new item.
Example
Write a new item to the Thread table. The primary key for Thread consists of ForumName (partition key)
and Subject (sort key).
 {
     "ForumName": {"S": "Amazon DynamoDB"},
     "Subject": {"S": "New discussion thread"},
     "Message": {"S": "First post in this thread"},
     "LastPostedBy": {"S": "fred@example.com"},
     "LastPostDateTime": {"S": "201603190422"}
}
UpdateItem
If an item with the specified key does not exist, UpdateItem creates a new item. Otherwise, it modifies
an existing item's attributes.
You use an update expression to specify the attributes you want to modify and their new values. (For
more information, see Update Expressions (p. 352).) Within the update expression, you use expression
attribute values as placeholders for the actual values. (For more information, see Expression Attribute
Values (p. 344).)
Example
Modify various attributes in the Thread item. The optional ReturnValues parameter shows the item as
it appears after the update. (For more information, see Return Values (p. 329).)
    --table-name Thread \
    --key file://key.json \
    --update-expression "SET Answered = :zero, Replies = :zero, LastPostedBy
 = :lastpostedby" \
    --expression-attribute-values file://expression-attribute-values.json \
    --return-values ALL_NEW
{
    "ForumName": {"S": "Amazon DynamoDB"},
    "Subject": {"S": "New discussion thread"}
}
{
    ":zero": {"N":"0"},
    ":lastpostedby": {"S":"barney@example.com"}
}
DeleteItem
DeleteItem deletes the item with the specified key.
Example
This AWS CLI example shows how to delete the Thread item.
Return Values
In some cases, you might want DynamoDB to return certain attribute values as they appeared
before or after you modified them. The PutItem, UpdateItem, and DeleteItem operations have
a ReturnValues parameter that you can use to return the attribute values before or after they are
modified.
The default value for ReturnValues is NONE, meaning that DynamoDB will not return any information
about attributes that were modified.
The following are the other valid settings for ReturnValues, organized by DynamoDB API operation:
PutItem
• ReturnValues: ALL_OLD
  • If you overwrite an existing item, ALL_OLD returns the entire item as it appeared before the
    overwrite.
UpdateItem
The most common usage for UpdateItem is to update an existing item. However, UpdateItem actually
performs an upsert, meaning that it will automatically create the item if it does not already exist.
• ReturnValues: ALL_OLD
  • If you update an existing item, ALL_OLD returns the entire item as it appeared before the update.
  • If you update a nonexistent item (upsert), ALL_OLD has no effect.
• ReturnValues: ALL_NEW
  • If you update an existing item, ALL_NEW returns the entire item as it appeared after the update.
  • If you update a nonexistent item (upsert), ALL_NEW returns the entire item.
• ReturnValues: UPDATED_OLD
  • If you update an existing item, UPDATED_OLD returns only the updated attributes, as they appeared
    before the update.
  • If you update a nonexistent item (upsert), UPDATED_OLD has no effect.
• ReturnValues: UPDATED_NEW
  • If you update an existing item, UPDATED_NEW returns only the affected attributes, as they appeared
    after the update.
  • If you update a nonexistent item (upsert), UPDATED_NEW returns only the updated attributes, as
    they appear after the update.
DeleteItem
• ReturnValues: ALL_OLD
  • If you delete an existing item, ALL_OLD returns the entire item as it appeared before you deleted it.
  • If you delete a nonexistent item, ALL_OLD does not return any data.
Batch Operations
For applications that need to read or write multiple items, DynamoDB provides the BatchGetItem
and BatchWriteItem operations. Using these operations can reduce the number of network round
trips from your application to DynamoDB. In addition, DynamoDB performs the individual read or
write operations in parallel. Your applications benefit from this parallelism without having to manage
concurrency or threading.
The batch operations are essentially wrappers around multiple read or write requests. For example, if
a BatchGetItem request contains five items, DynamoDB performs five GetItem operations on your
behalf. Similarly, if a BatchWriteItem request contains two put requests and four delete requests,
DynamoDB performs two PutItem and four DeleteItem requests.
In general, a batch operation does not fail unless all of the requests in the batch fail. For example,
suppose you perform a BatchGetItem operation, but one of the individual GetItem requests in the
batch fails. In this case, BatchGetItem returns the keys and data from the GetItem request that failed.
The other GetItem requests in the batch are not affected.
BatchGetItem
A single BatchGetItem operation can contain up to 100 individual GetItem requests and can retrieve
up to 16 MB of data. In addition, a BatchGetItem operation can retrieve items from multiple tables.
Example
Retrieve two items from the Thread table, using a projection expression to return only some of the
attributes.
{
    "Thread": {
        "Keys": [
            {
                "ForumName":{"S": "Amazon DynamoDB"},
                "Subject":{"S": "DynamoDB Thread 1"}
            },
            {
                "ForumName":{"S": "Amazon S3"},
                "Subject":{"S": "S3 Thread 1"}
            }
        ],
        "ProjectionExpression":"ForumName, Subject, LastPostedDateTime, Replies"
    }
}
BatchWriteItem
The BatchWriteItem operation can contain up to 25 individual PutItem and DeleteItem requests
and can write up to 16 MB of data. (The maximum size of an individual item is 400 KB.) In addition, a
BatchWriteItem operation can put or delete items in multiple tables.
    Note
    BatchWriteItem does not support UpdateItem requests.
Example
{
    "ProductCatalog": [
        {
            "PutRequest": {
                "Item": {
                    "Id": { "N": "601" },
                    "Description": { "S": "Snowboard" },
                    "QuantityOnHand": { "N": "5" },
                    "Price": { "N": "100" }
                }
            }
         },
         {
              "PutRequest": {
                  "Item": {
                      "Id": { "N": "602" },
                      "Description": { "S": "Snow shovel" }
                  }
              }
         }
    ]
}
Atomic Counters
You can use the UpdateItem operation to implement an atomic counter—a numeric attribute that
is incremented, unconditionally, without interfering with other write requests. (All write requests
are applied in the order in which they were received.) With an atomic counter, the updates are not
idempotent. In other words, the numeric value will increment each time you call UpdateItem.
You might use an atomic counter to keep track of the number of visitors to a website. In this case,
your application would increment a numeric value, regardless of its current value. If an UpdateItem
operation should fail, the application could simply retry the operation. This would risk updating the
counter twice, but you could probably tolerate a slight overcounting or undercounting of website visitors.
An atomic counter would not be appropriate where overcounting or undercounting cannot be tolerated
(For example, in a banking application). In this case, it is safer to use a conditional update instead of an
atomic counter.
For more information, see Incrementing and Decrementing Numeric Attributes (p. 356).
Example
The following AWS CLI example increments the Price of a product by 5. (Because UpdateItem is not
idempotent, the Price will increase every time you run this example.)
Conditional Writes
By default, the DynamoDB write operations (PutItem, UpdateItem, DeleteItem) are unconditional:
each of these operations will overwrite an existing item that has the specified primary key.
DynamoDB optionally supports conditional writes for these operations. A conditional write will succeed
only if the item attributes meet one or more expected conditions. Otherwise, it returns an error.
Conditional writes are helpful in many situations. For example, you might want a PutItem operation
to succeed only if there is not already an item with the same primary key. Or you could prevent an
UpdateItem operation from modifying an item if one of its attributes has a certain value.
Conditional writes are helpful in cases where multiple users attempt to modify the same item. Consider
the following diagram, in which two users (Alice and Bob) are working with the same item from a
DynamoDB table:
Suppose that Alice uses the AWS CLI to update the Price attribute to 8:
{
    ":newval":{"N":"8"}
}
Now suppose that Bob issues a similar UpdateItem request later, but changes the Price to 12. For Bob,
the --expression-attribute-values parameter looks like this:
{
    ":newval":{"N":"12"}
}
Now consider the following diagram, showing how conditional writes would prevent Alice's update from
being overwritten:
Alice first attempts to update Price to 8, but only if the current Price is 10:
{
    ":newval":{"N":"8"},
    ":currval":{"N":"10"}
Next, Bob attempts to update the Price to 12, but only if the current Price is 10. For Bob, the --
expression-attribute-values parameter looks like this:
{
    ":newval":{"N":"12"},
    ":currval":{"N":"10"}
}
Because Alice has previously changed the Price to 8, the condition expression evaluates to false and
Bob's update fails.
For example, suppose you issue an UpdateItem request to increase the Price of an item by 3, but only
if the Price is currently 20. After you send the request, but before you get the results back, a network
error occurs and you don't know whether the request was successful. Because conditional writes are
idempotent, you can retry the same UpdateItem request, and DynamoDB will update the item only if
the Price is currently 20.
• If the item does not currently exist in the table, DynamoDB will consume one write capacity unit.
• If the item does exist, then the number of write capacity units consumed depends on the size of the
  item. For example, a failed conditional write of a 1 KB item would consume one write capacity unit. If
  the item were twice that size, the failed conditional write would consume two write capacity units.
    Note
    Write operations consume write capacity units only. They never consume read capacity units.
A failed conditional write will return a ConditionalCheckFailedException. When this occurs, you will not
receive any information in the response about the write capacity that was consumed. However, you
can view the ConsumedWriteCapacityUnits metric for the table in Amazon CloudWatch. (For more
information, see DynamoDB Metrics (p. 680) in Monitoring DynamoDB (p. 678).)
To return the number of write capacity units consumed during a conditional write, you use the
ReturnConsumedCapacity parameter:
      Note
      Unlike a global secondary index, a local secondary index shares its provisioned throughput
      capacity with its table. Read and write activity on a local secondary index consumes provisioned
      throughput capacity from the table.
Topics
    • Specifying Item Attributes (p. 337)
    • Projection Expressions (p. 340)
    • Expression Attribute Names (p. 341)
    • Expression Attribute Values (p. 344)
    • Condition Expressions (p. 344)
    • Update Expressions (p. 352)
Topics
    • Top-Level Attributes (p. 338)
    • Nested Attributes (p. 339)
    • Document Paths (p. 339)
In this section, we will consider an item in the ProductCatalog table. (This table is described in Example
Tables and Data (p. 779).) Here is a representation of the item:
{
      "Id": 123,
      "Title": "Bicycle 123",
      "Description": "123 description",
      "BicycleType": "Hybrid",
      "Brand": "Brand-Company C",
      "Price": 500,
      "Color": {"Red", "Black"},
      "ProductCategory": "Bicycle",
      "InStock": true,
      "QuantityOnHand": null,
      "RelatedItems": {
          341,
          472,
          649
     },
     "Pictures": {
          "FrontView": "http://example.com/products/123_front.jpg",
          "RearView": "http://example.com/products/123_rear.jpg",
          "SideView": "http://example.com/products/123_left_side.jpg"
     },
     "ProductReviews": {
      "FiveStar": {
         "Excellent! Can't recommend it highly enough! Buy it!",
         "Do yourself a favor and buy this."
      },
      "OneStar": {
         "Terrible product! Do not buy this."
      }
     },
     "Comment": "This product sells out quickly during the summer",
     "Safety.Warning": "Always wear a helmet"
 }
Top-Level Attributes
An attribute is said to be top-level if it is not embedded within another attribute. For the ProductCatalog
item, the top-level attributes are:
• Id
• Title
• Description
• BicycleType
• Brand
• Price
• Color
• ProductCategory
• InStock
• QuantityOnHand
• RelatedItems
• Pictures
• ProductReviews
• Comment
• Safety.Warning
All of these top-level attributes are scalars, except for Color (list), RelatedItems (list), Pictures
(map) and ProductReviews (map).
Nested Attributes
An attribute is said to be nested if it is embedded within another attribute. To access a nested attribute,
you use dereference operators:
The dereference operator for a list element is [n], where n is the element number. List elements are zero-
based, so [0] represents the first element in the list, [1] represents the second, and so on. Here are some
examples:
• MyList[0]
• AnotherList[12]
• ThisList[5][11]
The element ThisList[5] is itself a nested list. Therefore, ThisList[5][11] refers to the twelfth
element in that list.
The number within the square brackets must be a non-negative integer. Therefore, the following
expressions are invalid:
• MyList[-1]
• MyList[0.4]
The dereference operator for a map element is . (a dot). Use a dot as a separator between elements in a
map:
• MyMap.nestedField
• MyMap.nestedField.deeplyNestedField
Document Paths
In an expression, you use a document path to tell DynamoDB where to find an attribute. For a top-level
attribute, the document path is simply the attribute name. For a nested attribute, you construct the
document path using dereference operators.
The following are some examples of document paths. (Refer to the item shown in Specifying Item
Attributes (p. 337).)
  ProductDescription
• A top-level list attribute. (This will return the entire list, not just some of the elements.)
  RelatedItems
• The third element from the RelatedItems list. (Remember that list elements are zero-based.)
  RelatedItems[2]
• The front-view picture of the product.
  Pictures.FrontView
• All of the five-star reviews.
  ProductReviews.FiveStar
• The first of the five-star reviews.
ProductReviews.FiveStar[0]
    Note
    The maximum depth for a document path is 32. Therefore, the number of dereferences
    operators in a path cannot exceed this limit.
You can use any attribute name in a document path, provided that the first character is a-z or A-
Z and the second character (if present) is a-z, A-Z, or 0-9. If an attribute name does not meet
this requirement, you will need to define an expression attribute name as a placeholder. For more
information, see Expression Attribute Names (p. 341).
Projection Expressions
To read data from a table, you use operations such as GetItem, Query, or Scan. DynamoDB returns
all of the item attributes by default. To get just some, rather than all of the attributes, use a projection
expression.
A projection expression is a string that identifies the attributes you want. To retrieve a single attribute,
specify its name. For multiple attributes, the names must be comma-separated.
The following are some examples of projection expressions, based on the ProductCatalog item from
Specifying Item Attributes (p. 337):
  Title
• Three top-level attributes. DynamoDB will retrieve the entire Color set.
You can use any attribute name in a projection expression, provided that the first character is a-z
or A-Z and the second character (if present) is a-z, A-Z, or 0-9. If an attribute name does not meet
this requirement, you will need to define an expression attribute name as a placeholder. For more
information, see Expression Attribute Names (p. 341).
The following AWS CLI example shows how to use a projection expression with a GetItem operation.
This projection expression retrieves a top-level scalar attribute (Description), the first element in a list
(RelatedItems[0]), and a list nested within a map (ProductReviews.FiveStar).
      --key file://key.json \
      --projection-expression "Description, RelatedItems[0], ProductReviews.FiveStar"
{
      "Id": { "N": "123" }
}
For programming language-specific code samples, see Getting Started with DynamoDB (p. 52).
This section describes several situations in which you will need to use expression attribute names.
      Note
      The examples in this section use the AWS CLI. For programming language-specific code samples,
      see Getting Started with DynamoDB (p. 52).
Topics
    • Reserved Words (p. 341)
    • Attribute Names Containing Dots (p. 342)
    • Nested Attributes (p. 342)
    • Repeating Attribute Names (p. 343)
Reserved Words
On some occasions, you might need to write an expression containing an attribute name that conflicts
with a DynamoDB reserved word. (For a complete list of reserved words, see Reserved Words in
DynamoDB (p. 820).)
For example, the following AWS CLI example would fail because COMMENT is a reserved word:
To work around this, you can replace Comment with an expression attribute name such as #c. The #
(pound sign) is required and indicates that this is a placeholder for an attribute name. The AWS CLI
example would now look like this:
    Note
    If an attribute name begins with a number or contains a space, a special character, or a reserved
    word, then you must use an expression attribute name to replace that attribute's name in the
    expression.
DynamoDB would return an empty result, rather than the expected string ("Always wear a helmet").
This is because DynamoDB interprets a dot in an expression as a document path separator. In this
case, you would need to define an expression attribute name (such as #sw) as a substitute for
Safety.Warning. You could then use the following projection expression:
Nested Attributes
Suppose that you wanted to access the nested attribute ProductReviews.OneStar, using the
following projection expression:
The result would contain all of the one-star product reviews, which is expected.
But what if you decided to use an expression attribute name instead? For example, what would happen if
you were to define #pr1star as a substitute for ProductReviews.OneStar?
    --key '{"Id":{"N":"123"}}' \
    --projection-expression "#pr1star" \
    --expression-attribute-names '{"#pr1star":"ProductReviews.OneStar"}'
DynamoDB would return an empty result, instead of the expected map of one-star reviews. This is
because DynamoDB interprets a dot in an expression attribute value as a character within an attribute's
name. When DynamoDB evaluates the expression attribute name #pr1star, it determines that
ProductReviews.OneStar refers to a scalar attribute—which is not what was intended.
The correct approach would be to define an expression attribute name for each element in the document
path:
• #pr — ProductReviews
• #1star — OneStar
To make this more concise, you can replace ProductReviews with an expression attribute name such as
#pr. The revised expression would now look like this:
If you define an expression attribute name, you must use it consistently throughout the entire
expression. Also, you cannot omit the # symbol.
For example, suppose you wanted to return all of the ProductCatalog items that are available in Black
and cost 500 or less. You could use a Scan operation with a filter expression, as in this AWS CLI example:
{
    ":c": { "S": "Black" },
    ":p": { "N": "500" }
}
    Note
    A Scan operation reads every item in a table; therefore, you should avoid using Scan with large
    tables.
    The filter expression is applied to the Scan results, and items that do not match the filter
    expression are discarded.
If you define an expression attribute value, you must use it consistently throughout the entire expression.
Also, you cannot omit the : symbol.
Expression attribute values are used with condition expressions, update expressions, and filter
expressions.
    Note
    For programming language-specific code samples, see Getting Started with DynamoDB (p. 52).
Condition Expressions
To manipulate data in a DynamoDB table, you use the PutItem, UpdateItem and DeleteItem
operations. (You can also use BatchWriteItem to perform multiple PutItem or DeleteItem
operations in a single call.)
For these data manipulation operations, you can specify a condition expression to determine which items
should be modified. If the condition expression evaluates to true, the operation succeeds; otherwise, the
operation fails.
The following are some AWS CLI examples of using condition expressions. These examples are based
on the ProductCatalog table, which was introduced in Specifying Item Attributes (p. 337). The
partition key for this table is Id; there is no sort key. The following PutItem operation creates a sample
ProductCatalog item that we will refer to in the examples:
The arguments for --item are stored in the file item.json. (For simplicity, only a few item attributes
are used.)
{
      "Id": {"N": "456" },
      "ProductCategory": {"S": "Sporting Goods" },
      "Price": {"N": "650" }
}
Topics
    • Preventing Overwrites of an Existing Item (p. 345)
    • Checking for Attributes in an Item (p. 345)
    • Conditional Deletes (p. 346)
    • Conditional Updates (p. 347)
    • Comparison Operator and Function Reference (p. 347)
If the condition expression evaluates to false, DynamoDB returns the following error message: The
conditional request failed
      Note
      For more information about attribute_not_exists and other functions, see Comparison
      Operator and Function Reference (p. 347).
The following example uses attribute_not_exists to delete a product only if it does not have a
Price attribute:
DynamoDB also provides an attribute_exists function. The following example will delete a product
only if it has received poor reviews:
    Note
    For more information about attribute_not_exists, attribute_exists, and other
    functions, see Comparison Operator and Function Reference (p. 347).
Conditional Deletes
To perform a conditional delete, you use a DeleteItem operation with a condition expression. The
condition expression must evaluate to true in order for the operation to succeed; otherwise, the
operation fails.
{
    "Id": {
        "N": "456"
    },
    "Price": {
        "N": "650"
    },
    "ProductCategory": {
        "S": "Sporting Goods"
    }
}
Now suppose that you wanted to delete the item, but only under the following conditions:
{
    ":cat1": {"S": "Sporting Goods"},
    ":cat2": {"S": "Gardening Supplies"},
    ":lo": {"N": "500"},
    ":hi": {"N": "600"}
}
    Note
    In the condition expression, the : (colon character) indicates an expression attribute value—
    placeholder for an actual value. For more information, see Expression Attribute Values (p. 344).
    For more information about IN, AND, and other keywords, , see Comparison Operator and
    Function Reference (p. 347).
In this example, the ProductCategory comparison evaluates to true, but the Price comparison
evaluates to false. This causes the condition expression to evaluate to false, and the DeleteItem
operation to fail.
Conditional Updates
To perform a conditional update, you use an UpdateItem operation with a condition expression.
The condition expression must evaluate to true in order for the operation to succeed; otherwise, the
operation fails.
    Note
    UpdateItem also supports update expressions, where you specify the modifications you
    specify the changes you want to make to an item. For more information, see Update
    Expressions (p. 352).
Suppose that you started with the item shown in Condition Expressions (p. 344):
{
    "Id": { "N": "456"},
    "Price": {"N": "650"},
    "ProductCategory": {"S": "Sporting Goods"}
}
The following example performs an UpdateItem operation. It attempts to reduce the Price of a
product by 75—but the condition expression prevents the update if the current Price is below 500:
{
    ":discount": { "N": "75"},
    ":limit": {"N": "500"}
}
If the starting Price is 650, then the UpdateItem operation reduces the Price to 575. If you run the
UpdateItem operation again, the Price is reduced to 500. If you run it a third time, the condition
expression evaluates to false, and the update fails.
    Note
    In the condition expression, the : (colon character) indicates an expression attribute value—
    placeholder for an actual value. For more information, see Expression Attribute Values (p. 344).
    For more information about ">" and other operators, see Comparison Operator and Function
    Reference (p. 347).
Topics
 • Syntax for Condition Expressions (p. 348)
 • Making Comparisons (p. 348)
condition-expression ::=
      operand comparator operand
    | operand BETWEEN operand AND operand
    | operand IN ( operand (',' operand (, ...) ))
    | function
    | condition AND condition
    | condition OR condition
    | NOT condition
    | ( condition )
comparator ::=
    =
    | <>
    | <
    | <=
    | >
    | >=
function ::=
    attribute_exists (path)
    | attribute_not_exists (path)
    | attribute_type (path, type)
    | begins_with (path, substr)
    | contains (path, operand)
    | size (path)
Making Comparisons
Use these comparators to compare an operand against a range of values, or an enumerated list of values:
• a = b — true if a is equal to b
• a <> b — true if a is not equal to b
•   a   < b — true if a is less than b
•   a   <= b — true if a is less than or equal to b
•   a   > b — true if a is greater than b
•   a   >= b — true if a is greater than or equal to b
Use the BETWEEN and IN keywords to compare an operand against a range of values, or an enumerated
list of values:
• a BETWEEN b AND c - true if a is greater than or equal to b, and less than or equal to c.
• a IN (b, c, d) — true if a is equal to any value in the list — for example, any of b, c or d. The list
  can contain up to 100 values, separated by commas.
Functions
Use the following functions to determine whether an attribute exists in an item, or to evaluate the value
of an attribute. These function names are case-sensitive. For a nested attribute, you must provide its full
document path.
Function Description
• attribute_exists (Pictures.SideView)
• attribute_not_exists (Manufacturer)
                                                       • S — String
                                                       • SS — String Set
                                                       • N — Number
                                                       • NS — Number Set
                                                       • B — Binary
                                                       • BS — Binary Set
                                                       • BOOL — Boolean
                                                       • NULL — Null
                                                       • L — List
                                                       • M — Map
                                                       • attribute_type
                                                         (ProductReviews.FiveStar, :v_sub)
 begins_with (path, substr)                            True if the attribute specified by path begins with
                                                       a particular substring.
Function                                    Description
                                            Example: Check whether the first few characters
                                            of the front view picture URL are http://.
                                            • begins_with
                                              (Pictures.FrontView, :v_sub)
Function Description
                                                       • size(ProductReviews.OneStar)
                                                         > :v_sub
Logical Evaluations
Use the AND, OR and NOT keywords to perform logical evaluations. In the list following, a and b represent
conditions to be evaluated.
Parentheses
Use parentheses to change the precedence of a logical evaluation. For example, suppose that conditions
a and b are true, and that condition c is false. The following expression evaluates to true:
• a OR b AND c
However, if you enclose a condition in parentheses, it is evaluated first. For example, the following
evaluates to false:
• (a OR b) AND c
    Note
    You can nest parentheses in an expression. The innermost ones are evaluated first.
Precedence in Conditions
DynamoDB evaluates conditions from left to right using the following precedence rules:
Update Expressions
To update an existing item in a table, you use the UpdateItem operation. You must provide the key of
the item you want to update. You must also provide an update expression, indicating the attributes you
want to modify and the values you want to assign to them.
An update expression specifies how UpdateItem will modify the attributes of an item—for example,
setting a scalar value, or removing elements from a list or a map.
update-expression ::=
    [ SET action [, action] ... ]
    [ REMOVE action [, action] ...]
    [ ADD action [, action] ... ]
    [ DELETE action [, action] ...]
An update expression consists of one or more clauses. Each clause begins with a SET, REMOVE, ADD or
DELETE keyword. You can include any of these clauses in an update expression, in any order. However,
each action keyword can appear only once.
Within each clause are one or more actions, separated by commas. Each action represents a data
modification.
The examples in this section are based on the ProductCatalog item shown in Projection
Expressions (p. 340).
Topics
    • SET—Modifying or Adding Item Attributes (p. 353)
    • REMOVE—Deleting Attributes From An Item (p. 358)
    • ADD—Updating Numbers and Sets (p. 359)
    • DELETE—Removing Elements From A Set (p. 360)
You can also use SET to add or subtract from an attribute that is of type Number. To perform multiple
SET actions, separate them by commas.
set-action ::=
    path = value
value ::=
    operand
    | operand '+' operand
    | operand '-' operand
operand ::=
    path | function
The following PutItem operation creates a sample item that we will refer to in the examples:
The arguments for --item are stored in the file item.json. (For simplicity, only a few item attributes
are used.)
{
      "Id": {"N": "789"},
      "ProductCategory": {"S": "Home Improvement"},
      "Price": {"N": "52"},
      "InStock": {"BOOL": true},
      "Brand": {"S": "Acme"}
}
Topics
    • Modifying Attributes (p. 354)
Modifying Attributes
Example
{
      ":c": { "S": "Hardware" },
      ":p": { "N": "60" }
}
      Note
      In the UpdateItem operation, --return-values ALL_NEW causes DynamoDB to return the
      item as it appears after the update.
Example
{
      ":ri": {
          "L": [
               { "S": "Hammer" }
          ]
    },
    ":pr": {
        "M": {
             "FiveStar": {
                 "L": [
                     { "S": "Best product ever!" }
                 ]
             }
        }
    }
}
Example
Add a new attribute to the RelatedItems list. (Remember that list elements are zero-based, so [0]
represents the first element in the list, [1] represents the second, and so on.)
{
    ":ri": { "S": "Nails" }
}
    Note
    When you use SET to update a list element, the contents of that element are replaced with
    the new data that you specify. If the element does not already exist, SET will append the new
    element at the end of the list.
    If you add multiple elements in a single SET operation, the elements are sorted in order by
    element number.
Example
{
    "#pr": "ProductReviews",
    "#5star": "FiveStar",
    "#3star": "ThreeStar"
}
{
    ":r5": { "S": "Very happy with my purchase" },
    ":r3": {
        "L": [
             { "S": "Just OK - not that great" }
        ]
    }
}
You can add to or subtract from an existing numeric attribute. To do this, use the + (plus) and - (minus)
operators.
Example
To increase the Price, you would use the + operator in the update expression.
You can add elements to the end of a list,. To do this, use SET with the list_append function. (The
function name is case-sensitive.) The list_append function is specific to the SET action, and can only
be used in an update expression. The syntax is:
The function takes two lists as input, and appends list2 to list1.
Example
In Adding Elements To a List (p. 355), we created the RelatedItems list and populated it with two
elements: Hammer and Nails. Now we will append two more elements to the end of RelatedItems:
{
    ":vals": {
        "L": [
            { "S": "Screwdriver" },
            {"S": "Hacksaw" }
        ]
    }
}
Finally, we will append one more element to the beginning of RelatedItems. To do this, we will swap
the order of the list_append elements. (Remember that list_append takes two lists as input, and
appends the second list to the first.)
The resulting RelatedItems attribute now contains five elements, in the following order: Chisel,
Hammer, Nails, Screwdriver, Hacksaw.
If you want to avoid overwriting an existing attribute, you can use SET with the if_not_exists
function. (The function name is case-sensitive.) The if_not_exists function is specific to the SET
action, and can only be used in an update expression. The syntax is:
If the item does not contain an attribute at the specified path, then if_not_exists evaluates to
value; otherwise, it evaluates to path.
Example
Set the Price of an item, but only if the item does not already have a Price attribute. (If Price already
exists, nothing happens.)
The following is a syntax summary for REMOVE in an update expression. The only operand is the
document path for the attribute you want to remove:
remove-action ::=
    path
Example
Remove some attributes from an item. (If the attributes do not exist, nothing happens.)
Example
In Appending Elements To a List (p. 356), we modified a list attribute (RelatedItems) so that it
contained five elements:
• [0]—Chisel
• [1]—Hammer
• [2]—Nails
• [3]—Screwdriver
• [4]—Hacksaw
The following AWS CLI example deletes Hammer and Nails from the list.
After removing Hammer and Nails, the remaining elements are shifted. The list now contains the
following:
• [0]—Chisel
• [1]—Screwdriver
• [2]—Hacksaw
Use the ADD action in an update expression to add a new attribute and its value(s) to an item.
If the attribute already exists, then the behavior of ADD depends on the attribute's data type:
• If the attribute is a number, and the value you are adding is also a number, then the value is
  mathematically added to the existing attribute. (If the value is a negative number, then it is subtracted
  from the existing attribute.)
• If the attribute is a set, and the value you are adding is also a set, then the value is appended to the
  existing set.
    Note
    The ADD action only supports number and set data types.
• The path element is the document path to an attribute. The attribute must be either a Number or a
  set data type.
• The value element is a number that you want to add to the attribute (for Number data types), or a set
  to append to the attribute (for set types).
add-action ::=
    path value
Adding a Number
Assume that the QuantityOnHand attribute does not exist. The following AWS CLI example sets
QuantityOnHand to 5:
Now that QuantityOnHand exists, you can re-run the example to increment QuantityOnHand by 5
each time.
Assume that the Color attribute does not exist. The following AWS CLI example sets Color to a string
set with two elements:
Use the DELETE action in an update expression to remove one or more elements from a set. To perform
multiple DELETE actions, separate them by commas.
• The path element is the document path to an attribute. The attribute must be a set data type.
• The subset is one or more elements that you want to delete from path. that you want to delete. You
  must specify subset as a set type.
delete-action ::=
    path value
Example
In Adding Elements To A Set (p. 359), we created the Colors string set. This example removes some of
the elements from that set:
Time To Live
Time To Live (TTL) for DynamoDB allows you to define when items in a table expire so that they can be
automatically deleted from the database.
TTL is provided at no extra cost as a way to reduce storage usage and reduce the cost of storing
irrelevant data without using provisioned throughput. With TTL enabled on a table, you can set a
timestamp for deletion on a per-item basis, allowing you to limit storage usage to only those records
that are relevant.
TTL is useful if you have continuously accumulating data that loses relevance after a specific time period.
For example: session data, event logs, usage patterns, and other temporary data. If you have sensitive
data that must be retained only for a certain amount of time according to contractual or regulatory
obligations, TTL helps you ensure that it is removed promptly and as scheduled.
TTL compares the current time in epoch time format to the time stored in the Time To Live attribute of
an item. If the epoch time value stored in the attribute is less than the current time, the item is marked
as expired and subsequently deleted.
    Note
    The epoch time format is the number of seconds elapsed since 12:00:00 AM January 1st, 1970
    UTC.
DynamoDB deletes expired items on a best-effort basis to ensure availability of throughput for other
data operations.
    Important
    DynamoDB typically deletes expired items within 48 hours of expiration. The exact duration
    within which an item truly gets deleted after expiration is specific to the nature of the workload
    and the size of the table. Items that have expired and not been deleted will still show up in
    reads, queries, and scans. These items can still be updated and successful updates to change or
    remove the expiration attribute will be honored.
As items are deleted, they are removed from any Local Secondary Index and Global Secondary Index
immediately in the same eventually consistent way as a standard delete operation.
For example, consider a table named SessionData that tracks the session history of users. Each item in
SessionData is identified by a partition key (UserName) and a sort key (SessionId). Additional attributes
like UserName,SessionId, CreationTime and ExpirationTime track the session information.
The following diagram shows how the items in the table would be organized. The ExpirationTime
attribute is set as the Time To Live (TTL) attribute. (Not all of the attributes are shown)
SessionData
In this example each item has an ExpirationTime attribute value set when it is created. Consider the first
record:
SessionData
In this example, the item CreationTime is set to Friday, April 29 12:00 PM UTC 2016 and the
ExpirationTime is set 2 hours later at Friday, April 29 2:00 PM UTC 2016. The item will expire when the
current time, in epoch format, is greater than the time in the ExpirationTime attribute. In this case, the
item with the key { Username: user1, SessionId: 74686572652773 } will expire after 2:00 PM
(1461938400).
    Note
    Due to the potential delay between expiration and deletion time, you might get expired items
    when you query for items. If you don’t need to view expired items when you issue a read
    request, you should filter out the expired items using the expiration attribute that you have
    defined
    You can do this by using a filter expression that returns only items where the Time To Live
    expiration value is greater than the current time in epoch format. For more information, see
    Filter Expressions for Query (p. 410) and Filter Expressions for Scan (p. 425).
• Ensure that any existing timestamp values in the specified Time To Live attribute are correct and in the
  right format.
• Items with an expiration time greater than 5 years in the past are not deleted.
• If data recovery is a concern, we recommend that you back up your table.
  • For a 24-hour recovery window, you can use Amazon DynamoDB Streams. For more information, see
    DynamoDB Streams and Time To Live (p. 521).
  • For a full backup, you can use AWS Data Pipeline. For more information, see Exporting and
    Importing DynamoDB Data Using AWS Data Pipeline (p. 760).
• You can use IAM policies to prevent unauthorized updates to the TTL attribute or configuration of
  the Time To Live feature. If you only allow access to specified actions in your existing IAM policies,
  ensure that your policies are updated to allow dynamodb:UpdateTimeToLive for roles that need to
  enable or disable Time To Live on tables. For more information, see Using Identity-Based Policies (IAM
  Policies) for Amazon DynamoDB (p. 649).
• Consider whether you need to do any post-processing of deleted items. The Streams records of TTL
  deletes are marked and you can monitor them using an AWS Lambda function. For more information
  on the additions to the Streams record, see DynamoDB Streams and Time To Live (p. 521).
Topics
 • Enable Time To Live (console) (p. 363)
 • Enable Time To Live (CLI) (p. 365)
4. In the Manage TTL dialog box, choose Enable TTL and then type the TTL attribute name.
     • Enable TTL – Choose this to either enable or disable TTL on the table. It may take up to one hour
       for the change to fully process.
     • TTL Attribute – The name of the DynamoDB attribute to store the TTL timestamp for items.
     • 24-hour backup streams – Choose this to enable Amazon DynamoDB Streams on the table. For
       more information about how you can use DynamoDB Streams for backup, see DynamoDB Streams
       and Time To Live (p. 521).
5.   (Optional) To preview some of the items that will be deleted when TTL is enabled, choose Run
     preview.
         Warning
         This provides you with a sample list of items. It does not provide you with a complete list of
         items that will be deleted by TTL.
6.   Choose Continue to save the settings and enable TTL.
Now that TTL is enabled, the TTL attribute is marked TTL when you view items in the DynamoDB
console.
You can view the date and time that an item will expire by hovering your mouse over the attribute.
To add an item to the "TTLExample" table with the Time To Live attribute set using the BASH shell and
CLI:
aws dynamodb put-item --table-name "TTLExample" --item '{"id": {"N": "1"}, "ttl": {"N":
 "'$EXP'"}}'
This example started with the current date and added five days to it to create an expiration time. Then, it
converts the expiration time to epoch time format to finally add an item to the "TTLExample" table.
    Note
    One way to set expiration values for Time To Live is to calculate the number of seconds to add
    to the expiration time. For example, five days is 432000 seconds. However, it is often preferable
    to start with a date and work from there.
It is fairly simple to get the current time in epoch time format. For example:
You can use the AWS SDK for Java Document API to perform typical create, read, update, and delete
(CRUD) operations on items in a table.
    Note
    The SDK for Java also provides an object persistence model, allowing you to map your client-
    side classes to DynamoDB tables. This approach can reduce the amount of code you have to
    write. For more information, see Java: DynamoDBMapper (p. 195).
The following sections describe Java snippets to perform several Java Document API item actions. To run
complete working examples instead, see:
• Example: CRUD Operations Using the AWS SDK for Java Document API (p. 375)
• Example: Batch Operations Using AWS SDK for Java Document API (p. 379)
• Example: Handling Binary Type Attributes Using the AWS SDK for Java Document API (p. 383)
Putting an Item
The putItem method stores an item in a table. If the item exists, it replaces the entire item. Instead of
replacing the entire item, if you want to update only specific attributes, you can use the updateItem
method. For more information, see Updating an Item (p. 373).
The following Java code snippet demonstrates the preceding tasks. The snippet writes a new item to the
ProductCatalog table.
Example
In the preceding example, the item has attributes that are scalars (String, Number, Boolean, Null), sets
(String Set), and document types (List, Map).
• A ConditionExpression that defines the conditions for the request. The snippet defines the
  condition that the existing item that has the same primary key is replaced only if it has an ISBN
  attribute that equals a specific value.
• A map for ExpressionAttributeValues that will be used in the condition. In this case, there is
  only one substitution required: The placeholder :val in the condition expression will be replaced at
  runtime with the actual ISBN value to be checked.
The following example adds a new book item using these optional parameters.
Example
Suppose that you wanted to store the following JSON document, containing vendors that can fulfill
orders for a particular product:
Example
{
    "V01": {
        "Name": "Acme Books",
        "Offices": [ "Seattle" ]
    },
    "V02": {
        "Name": "New Publishers, Inc.",
        "Offices": ["London", "New York"
        ]
    },
    "V03": {
You can use the withJSON method to store this in the ProductCatalog table, in a Map attribute
named VendorInfo. The following Java code snippet demonstrates how to do this.
Getting an Item
To retrieve a single item, use the getItem method of a Table object. Follow these steps:
The following Java code snippet demonstrates the preceding steps. The code snippet gets the item that
has the specified partition key.
You can use a ProjectionExpression to retrieve only specific attributes or elements, rather than
an entire item. A ProjectionExpression can specify top-level or nested attributes, using document
paths. For more information, see Projection Expressions (p. 340).
The parameters of the getItem method do not let you specify read consistency; however, you can create
a GetItemSpec, which provides full access to all of the inputs to the low-level GetItem operation. The
code example below creates a GetItemSpec, and uses that spec as input to the getItem method.
Example
System.out.println(item.toJSONPretty());
To print an Item in a human-readable format, use the toJSONPretty method. The output from the
example above looks like this:
{
  "RelatedItems" : [ 341 ],
  "Reviews" : {
     "FiveStar" : [ "Excellent! Can't recommend it highly enough!        Buy it!", "Do yourself a
 favor and buy this" ]
  },
  "Id" : 123,
  "Title" : "20-Bicycle 123"
}
    Note
    You can use the toJSON method to convert any item (or its attributes) to a JSON-formatted
    string. The following code snippet retrieves several top-level and nested attributes, and prints
    the results as JSON:
    {"VendorInfo":{"V01":{"Name":"Acme Books","Offices":
    ["Seattle"]}},"Price":30,"Title":"Book 210 Title"}
The following Java code snippet demonstrates the preceding steps. The example performs a
batchWriteItem operation on two tables - Forum and Thread. The corresponding TableWriteItems
objects define the following actions:
For a working example, see Example: Batch Write Operation Using the AWS SDK for Java Document
API (p. 379).
The following Java code snippet demonstrates the preceding steps. The example retrieves two items
from the Forum table and three items from the Thread table.
The following code snippet retrieves two items from the Forum table. The
withProjectionExpression parameter specifies that only the Threads attribute is to be retrieved.
Example
forumTableKeysAndAttributes.addHashOnlyPrimaryKeys("Name",
    "Amazon S3",
    "Amazon DynamoDB");
Updating an Item
The updateItem method of a Table object can update existing attribute values, add new attributes, or
delete attributes from an existing item.
• If an item does not exist (no item in the table with the specified primary key), updateItem adds a new
  item to the table
• If an item exists, updateItem performs the update as specified by the UpdateExpression
  parameter:
    Note
    It is also possible to "update" an item using putItem. For example, if you call putItem to add
    an item to the table, but there is already an item with the specified primary key, putItem will
    replace the entire item. If there are attributes in the existing item that are not specified in the
    input, putItem will remove those attributes from the item.
    In general, we recommend that you use updateItem whenever you want to modify any item
    attributes. The updateItem method will only modify the item attributes that you specify in the
    input, and the other attributes in the item will remain unchanged.
1. Create an instance of the Table class to represent the table you want to work with.
2. Call the updateTable method of the Table instance. You must specify the primary key of the item
   that you want to retrieve, along with an UpdateExpression that describes the attributes to modify
   and how to modify them.
The following Java code snippet demonstrates the preceding tasks. The snippet updates a book item
in the ProductCatalog table. It adds a new author to the set of Authors and deletes the existing ISBN
attribute. It also reduces the price by one.
Example
Atomic Counter
You can use updateItem to implement an atomic counter, where you increment or decrement the value
of an existing attribute without interfering with other write requests. To increment an atomic counter,
use an UpdateExpression with a set action to add a numeric value to an existing attribute of type
Number.
The following code snippet demonstrates this, incrementing the Quantity attribute by one. It also
demonstrates the use of the ExpressionAttributeNames parameter in an UpdateExpression.
Deleting an Item
The deleteItem method deletes an item from a table. You must provide the primary key of the item
you want to delete.
Example
Example
   Note
   The SDK for Java also provides an object persistence model, allowing you to map your client-
   side classes to DynamoDB tables. This approach can reduce the amount of code you have to
   write. For more information, see Java: DynamoDBMapper (p. 195).
   Note
   This code sample assumes that you have already loaded data into DynamoDB for your account
   by following the instructions in the Creating Tables and Loading Sample Data (p. 281) section.
   For step-by-step instructions to run the following example, see Java Code Samples (p. 286).
import   java.io.IOException;
import   java.util.Arrays;
import   java.util.HashMap;
import   java.util.HashSet;
import   java.util.Map;
import   com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
import   com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder;
import   com.amazonaws.services.dynamodbv2.document.DeleteItemOutcome;
import   com.amazonaws.services.dynamodbv2.document.DynamoDB;
import   com.amazonaws.services.dynamodbv2.document.Item;
import   com.amazonaws.services.dynamodbv2.document.Table;
import   com.amazonaws.services.dynamodbv2.document.UpdateItemOutcome;
import   com.amazonaws.services.dynamodbv2.document.spec.DeleteItemSpec;
import   com.amazonaws.services.dynamodbv2.document.spec.UpdateItemSpec;
import   com.amazonaws.services.dynamodbv2.document.utils.NameMap;
import   com.amazonaws.services.dynamodbv2.document.utils.ValueMap;
import   com.amazonaws.services.dynamodbv2.model.ReturnValue;
createItems();
retrieveItem();
       }
       catch (Exception e) {
           System.err.println("Create items failed.");
           System.err.println(e.getMessage());
       }
   }
try {
       }
       catch (Exception e) {
           System.err.println("GetItem failed.");
           System.err.println(e.getMessage());
       }
try {
       }
       catch (Exception e) {
           System.err.println("Failed to add new attribute in " + tableName);
           System.err.println(e.getMessage());
       }
   }
try {
        }
        catch (Exception e) {
            System.err.println("Failed to update multiple attributes in " + tableName);
            System.err.println(e.getMessage());
        }
   }
try {
            // Specify the desired price (25.00) and also the condition (price =
            // 20.00)
        }
        catch (Exception e) {
            System.err.println("Error updating item in " + tableName);
            System.err.println(e.getMessage());
        }
   }
try {
           }
           catch (Exception e) {
               System.err.println("Error deleting item in " + tableName);
               System.err.println(e.getMessage());
           }
      }
}
This section provides examples of batch write and batch get operations using the AWS SDK for Java
Document API.
      Note
      The SDK for Java also provides an object persistence model, allowing you to map your client-
      side classes to DynamoDB tables. This approach can reduce the amount of code you have to
      write. For more information, see Java: DynamoDBMapper (p. 195).
Example: Batch Write Operation Using the AWS SDK for Java Document API
The following Java code example uses the batchWriteItem method to perform the following put and
delete operations:
You can specify any number of put and delete requests against one or more tables when creating your
batch write request. However, batchWriteItem limits the size of a batch write request and the number
of put and delete operations in a single batch write operation. If your request exceeds these limits, your
request is rejected. If your table does not have sufficient provisioned throughput to serve this request,
the unprocessed request items are returned in the response.
The following example checks the response to see if it has any unprocessed request items. If it does,
it loops back and resends the batchWriteItem request with unprocessed items in the request.
If you followed the Creating Tables and Loading Sample Data (p. 281) section, you should already
have created the Forum and Thread tables. You can also create these tables and upload sample data
programmatically. For more information, see Creating Example Tables and Uploading Data Using the
AWS SDK for Java (p. 788).
For step-by-step instructions to test the following sample, see Java Code Samples (p. 286).
Example
import   java.io.IOException;
import   java.util.Arrays;
import   java.util.HashSet;
import   java.util.List;
import   java.util.Map;
import   com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
import   com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder;
import   com.amazonaws.services.dynamodbv2.document.BatchWriteItemOutcome;
import   com.amazonaws.services.dynamodbv2.document.DynamoDB;
import   com.amazonaws.services.dynamodbv2.document.Item;
import   com.amazonaws.services.dynamodbv2.document.TableWriteItems;
import   com.amazonaws.services.dynamodbv2.model.WriteRequest;
writeMultipleItemsBatchWrite();
do {
                  if (outcome.getUnprocessedItems().size() == 0) {
                      System.out.println("No unprocessed items found");
                  }
                  else {
                      System.out.println("Retrieving the unprocessed items");
                      outcome = dynamoDB.batchWriteItemUnprocessed(unprocessedItems);
                  }
          }
          catch (Exception e) {
              System.err.println("Failed to retrieve items: ");
              e.printStackTrace(System.err);
          }
Example: Batch Get Operation Using the AWS SDK for Java Document API
The following Java code example uses the batchGetItem method to retrieve multiple items from the
Forum and the Thread tables. The BatchGetItemRequest specifies the table names and a list of keys
for each item to get. The example processes the response by printing the items retrieved.
    Note
    This code sample assumes that you have already loaded data into DynamoDB for your account
    by following the instructions in the Creating Tables and Loading Sample Data (p. 281) section.
    For step-by-step instructions to run the following example, see Java Code Samples (p. 286).
Example
import java.io.IOException;
import java.util.List;
import java.util.Map;
import   com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
import   com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder;
import   com.amazonaws.services.dynamodbv2.document.BatchGetItemOutcome;
import   com.amazonaws.services.dynamodbv2.document.DynamoDB;
import   com.amazonaws.services.dynamodbv2.document.Item;
import   com.amazonaws.services.dynamodbv2.document.TableKeysAndAttributes;
import   com.amazonaws.services.dynamodbv2.model.KeysAndAttributes;
try {
            BatchGetItemOutcome outcome =
 dynamoDB.batchGetItem(forumTableKeysAndAttributes,
                threadTableKeysAndAttributes);
            do {
                for (String tableName : outcome.getTableItems().keySet()) {
                    System.out.println("Items in table " + tableName);
                    List<Item> items = outcome.getTableItems().get(tableName);
                    for (Item item : items) {
                        System.out.println(item.toJSONPretty());
                    }
                }
                if (unprocessed.isEmpty()) {
                    System.out.println("No unprocessed keys found");
                }
                else {
                    System.out.println("Retrieving the unprocessed keys");
                    outcome = dynamoDB.batchGetItemUnprocessed(unprocessed);
                }
} while (!unprocessed.isEmpty());
        }
        catch (Exception e) {
            System.err.println("Failed to retrieve items.");
            System.err.println(e.getMessage());
        }
If you followed the Creating Tables and Loading Sample Data (p. 281) section, you should already have
created the Reply table. You can also create this tables programmatically. For more information, see
Creating Example Tables and Uploading Data Using the AWS SDK for Java (p. 788).
For step-by-step instructions to test the following sample, see Java Code Samples (p. 286).
Example
import   java.io.ByteArrayInputStream;
import   java.io.ByteArrayOutputStream;
import   java.io.IOException;
import   java.nio.ByteBuffer;
import   java.text.SimpleDateFormat;
import   java.util.Date;
import   java.util.TimeZone;
import   java.util.zip.GZIPInputStream;
import   java.util.zip.GZIPOutputStream;
import   com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
import   com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder;
import   com.amazonaws.services.dynamodbv2.document.DynamoDB;
import   com.amazonaws.services.dynamodbv2.document.Item;
import   com.amazonaws.services.dynamodbv2.document.Table;
import   com.amazonaws.services.dynamodbv2.document.spec.GetItemSpec;
              dateFormatter.setTimeZone(TimeZone.getTimeZone("UTC"));
              String replyDateTime = dateFormatter.format(new Date());
           os.write(input.getBytes("UTF-8"));
           os.close();
           baos.close();
           byte[] compressedBytes = baos.toByteArray();
           is.close();
           baos.close();
           bais.close();
           return result;
      }
}
You can use the AWS SDK for .NET low-level API to perform typical create, read, update, and delete
(CRUD) operations on an item in a table.
The following are the common steps you follow to perform data CRUD operations using the .NET low-
level API.
  For example, use the PutItemRequest request object when uploading an item and use the
  GetItemRequest request object when retrieving an existing item.
  You can use the request object to provide both the required and optional parameters.
3. Execute the appropriate method provided by the client by passing in the request object that you
   created in the preceding step.
Putting an Item
The PutItem method uploads an item to a table. If the item exists, it replaces the entire item.
    Note
    Instead of replacing the entire item, if you want to update only specific attributes, you can use
    the UpdateItem method. For more information, see Updating an Item (p. 388).
The following are the steps to upload an item using the low-level .NET SDK API.
   To put an item, you must provide the table name and the item.
3. Execute the PutItem method by providing the PutItemRequest object that you created in the
   preceding step.
The following C# code snippet demonstrates the preceding steps. The example uploads an item to the
ProductCatalog table.
Example
};
client.PutItem(request);
In the preceding example, you upload a book item that has the Id, Title, ISBN, and Authors attributes.
Note that Id is a numeric type attribute and all other attributes are of the string type. Authors is a String
set.
Example
};
var response = client.PutItem(request);
Getting an Item
The GetItem method retrieves an item.
    Note
    To retrieve multiple items you can use the BatchGetItem method. For more information, see
    Batch Get: Getting Multiple Items (p. 393).
The following are the steps to retrieve an existing item using the low-level .NET SDK API.
To get an item, you must provide the table name and primary key of the item.
3. Execute the GetItem method by providing the GetItemRequest object that you created in the
   preceding step.
The following C# code snippet demonstrates the preceding steps. The example retrieves an item from
the ProductCatalog table.
Example
Updating an Item
The UpdateItem method updates an existing item if it is present. You can use the UpdateItem
operation to update existing attribute values, add new attributes, or delete attributes from the existing
collection. If the item that has the specified primary key is not found, it adds a new item.
• If the item does not exist, UpdateItem adds a new item using the primary key that is specified in the
  input.
• If the item exists, UpdateItem applies the updates as follows:
  • Replaces the existing attribute values by the values in the update
  • If the attribute that you provide in the input does not exist, it adds a new attribute to the item.
  • If the input attribute is null, it deletes the attribute, if it is present.
  • If you use ADD for the Action, you can add values to an existing set (string or number set), or
    mathematically add (use a positive number) or subtract (use a negative number) from the existing
    numeric attribute value.
    Note
    The PutItem operation also can perform an update. For more information, see Putting an
    Item (p. 386). For example, if you call PutItem to upload an item and the primary key exists,
    the PutItem operation replaces the entire item. Note that, if there are attributes in the existing
    item and those attributes are not specified in the input, the PutItem operation deletes those
    attributes. However, UpdateItem only updates the specified input attributes, any other existing
    attributes of that item remain unchanged.
The following are the steps to update an existing item using the low-level .NET SDK API.
  This is the request object in which you describe all the updates, such as add attributes, update existing
  attributes, or delete attributes. To delete an existing attribute, specify the attribute name with null
  value.
3. Execute the UpdateItem method by providing the UpdateItemRequest object that you created in
   the preceding step.
The following C# code snippet demonstrates the preceding steps. The example updates a book item in
the ProductCatalog table. It adds a new author to the Authors collection, and deletes the existing ISBN
attribute. It also reduces the price by one.
Example
Atomic Counter
You can use updateItem to implement an atomic counter, where you increment or decrement the value
of an existing attribute without interfering with other write requests. To update an atomic counter,
use updateItem with an attribute of type Number in the UpdateExpression parameter, and ADD as the
Action.
The following code snippet demonstrates this, incrementing the Quantity attribute by one.
Deleting an Item
The DeleteItem method deletes an item from a table.
The following are the steps to delete an item using the low-level .NET SDK API.
   To delete an item, the table name and item's primary key are required.
3. Execute the DeleteItem method by providing the DeleteItemRequest object that you created in
   the preceding step.
Example
Example
{
    TableName = tableName,
    Key = new Dictionary<string,AttributeValue>() { { "Id", new AttributeValue { N =
 "201" } } },
     // Optional parameters.
     ReturnValues = "ALL_OLD",
     ExpressionAttributeNames = new Dictionary<string, string>()
     {
         {"#IP", "InPublication"}
     },
     ExpressionAttributeValues = new Dictionary<string, AttributeValue>()
     {
         {":inpub",new AttributeValue {BOOL = false}}
     },
     ConditionExpression = "#IP = :inpub"
};
The following C# code snippet demonstrates the preceding steps. The example creates a
BatchWriteItemRequest to perform the following write operations:
         {
             new WriteRequest
             {
                PutRequest = new PutRequest
                {
                   Item = new Dictionary<string,AttributeValue>
                   {
                     { "Name", new AttributeValue { S = "Amazon S3 forum" } },
                     { "Threads", new AttributeValue { N = "0" }}
                   }
                }
             }
        }
      } ,
      {
        table2Name, new List<WriteRequest>
        {
          new WriteRequest
          {
             PutRequest = new PutRequest
             {
                 Item = new Dictionary<string,AttributeValue>
                 {
                     { "ForumName", new AttributeValue { S = "Amazon S3 forum" } },
                     { "Subject", new AttributeValue { S = "My sample question" } },
                     { "Message", new AttributeValue { S = "Message Text." } },
                     { "KeywordTags", new AttributeValue { SS = new List<string> { "Amazon S3",
 "Bucket" } } }
                 }
             }
          },
          new WriteRequest
          {
               DeleteRequest = new DeleteRequest
               {
                   Key = new Dictionary<string,AttributeValue>()
                   {
                       { "ForumName", new AttributeValue { S = "Some forum name" } },
                       { "Subject", new AttributeValue { S = "Some subject" } }
                   }
               }
          }
        }
      }
    }
 };
response = client.BatchWriteItem(request);
For a working example, see Example: Batch Operations Using AWS SDK for .NET Low-Level API (p. 399).
The following are the steps to retrieve multiple items using the low-level .NET SDK API.
To retrieve multiple items, the table name and a list of primary key values are required.
3. Execute the BatchGetItem method by providing the BatchGetItemRequest object that you
   created in the preceding step.
4. Process the response. You should check if there were any unprocessed keys, which could happen if you
   reach the provisioned throughput limit or some other transient error.
The following C# code snippet demonstrates the preceding steps. The example retrieves items from two
tables, Forum and Thread. The request specifies two items in the Forum and three items in the Thread
table. The response includes items from both of the tables. The code shows how you can process the
response.
Example
};
Example: CRUD Operations Using the AWS SDK for .NET Low-
Level API
The following C# code example illustrates CRUD operations on an item. The example adds an item to the
ProductCatalog table, retrieves it, performs various updates, and finally deletes the item. If you followed
the steps in Creating Tables and Loading Sample Data (p. 281), you already have the ProductCatalog
table created. You can also create these sample tables programmatically. For more information, see
Creating Example Tables and Uploading Data Using the AWS SDK for .NET (p. 795).
For step-by-step instructions to test the following sample, see .NET Code Samples (p. 288).
Example
using   System;
using   System.Collections.Generic;
using   Amazon.DynamoDBv2;
using   Amazon.DynamoDBv2.Model;
using   Amazon.Runtime;
using   Amazon.SecurityToken;
namespace com.amazonaws.codesamples
{
    class LowLevelItemCRUDExample
    {
        private static string tableName = "ProductCatalog";
        private static AmazonDynamoDBClient client = new AmazonDynamoDBClient();
                  // Delete item.
                  DeleteItem();
                  Console.WriteLine("To continue, press Enter");
                  Console.ReadLine();
              }
              catch (Exception e)
              {
                  Console.WriteLine(e.Message);
                  Console.WriteLine("To continue, press Enter");
                  Console.ReadLine();
              }
          }
    {
        { "Id", new AttributeValue {
              N = "1000"
          }},
        { "Title", new AttributeValue {
              S = "Book 201 Title"
          }},
        { "ISBN", new AttributeValue {
              S = "11-11-11-11"
          }},
        { "Authors", new AttributeValue {
              SS = new List<string>{"Author1", "Author2" }
          }},
        { "Price", new AttributeValue {
              N = "20.00"
          }},
        { "Dimensions", new AttributeValue {
              S = "8.5x11.0x.75"
          }},
        { "InPublication", new AttributeValue {
              BOOL = false
          } }
    }
    };
    client.PutItem(request);
}
                {"#NA","NewAttribute"},
                {"#I","ISBN"}
           },
                ExpressionAttributeValues = new Dictionary<string, AttributeValue>()
           {
                {":auth",new AttributeValue {
                     SS = {"Author YY", "Author ZZ"}
                 }},
                {":new",new AttributeValue {
                     S = "New Value"
                 }}
           },
                TableName = tableName,
                ReturnValues = "ALL_NEW" // Give me all attributes of the updated item.
           };
           var response = client.UpdateItem(request);
                TableName = tableName,
                ReturnValues = "ALL_NEW" // Give me all attributes of the updated item.
           };
           var response = client.UpdateItem(request);
                   Console.WriteLine(
                       attributeName + " " +
                       (value.S == null ? "" : "S=[" + value.S + "]") +
                       (value.N == null ? "" : "N=[" + value.N + "]") +
                       (value.SS == null ? "" : "SS=[" + string.Join(",", value.SS.ToArray())
 + "]") +
                       (value.NS == null ? "" : "NS=[" + string.Join(",", value.NS.ToArray())
 + "]")
                      );
              }
              Console.WriteLine("************************************************");
          }
    }
}
 • Example: Batch Write Operation Using the AWS SDK for .NET Low-Level API (p. 400)
 • Example: Batch Get Operation Using the AWS SDK for .NET Low-Level API (p. 402)
This section provides examples of batch operations, batch write and batch get, that DynamoDB supports.
Example: Batch Write Operation Using the AWS SDK for .NET Low-Level API
The following C# code example uses the BatchWriteItem method to perform the following put and
delete operations:
You can specify any number of put and delete requests against one or more tables when creating your
batch write request. However, DynamoDB BatchWriteItem limits the size of a batch write request and
the number of put and delete operations in a single batch write operation. For more information, see
BatchWriteItem. If your request exceeds these limits, your request is rejected. If your table does not have
sufficient provisioned throughput to serve this request, the unprocessed request items are returned in
the response.
The following example checks the response to see if it has any unprocessed request items. If it does,
it loops back and resends the BatchWriteItem request with unprocessed items in the request. If
you followed the steps in Creating Tables and Loading Sample Data (p. 281), you already have the
Forum and Thread tables created. You can also create these sample tables and upload sample data
programmatically. For more information, see Creating Example Tables and Uploading Data Using the
AWS SDK for .NET (p. 795).
For step-by-step instructions to test the following sample, see .NET Code Samples (p. 288).
Example
using   System;
using   System.Collections.Generic;
using   Amazon.DynamoDBv2;
using   Amazon.DynamoDBv2.Model;
using   Amazon.Runtime;
namespace com.amazonaws.codesamples
{
    class LowLevelBatchWrite
    {
        private static string table1Name = "Forum";
        private static string table2Name = "Thread";
        private static AmazonDynamoDBClient client = new AmazonDynamoDBClient();
                                }
                           }
                       }
                  }
             }
             };
             CallBatchWriteTillCompletion(request);
         }
             int callCount = 0;
             do
             {
                 Console.WriteLine("Making request");
                 response = client.BatchWriteItem(request);
                 callCount++;
                  Console.WriteLine("Unprocessed");
                  foreach (var unp in unprocessed)
                  {
                      Console.WriteLine("{0} - {1}", unp.Key, unp.Value.Count);
                  }
                  Console.WriteLine();
                 // For the next iteration, the request will have unprocessed items.
                 request.RequestItems = unprocessed;
             } while (response.UnprocessedItems.Count > 0);
Example: Batch Get Operation Using the AWS SDK for .NET Low-Level API
The following C# code example uses the BatchGetItem method to retrieve multiple items from the
Forum and the Thread tables. The BatchGetItemRequest specifies the table names and a list of
primary keys for each table. The example processes the response by printing the items retrieved.
If you followed the steps in Creating Tables and Loading Sample Data (p. 281), you already have these
tables created with sample data. You can also create these sample tables and upload sample data
programmatically. For more information, see Creating Example Tables and Uploading Data Using the
AWS SDK for .NET (p. 795).
For step-by-step instructions to test the following sample, see .NET Code Samples (p. 288).
Example
using   System;
using   System.Collections.Generic;
using   Amazon.DynamoDBv2;
using   Amazon.DynamoDBv2.Model;
using   Amazon.Runtime;
namespace com.amazonaws.codesamples
{
    class LowLevelBatchGet
    {
        private static string table1Name = "Forum";
        private static string table2Name = "Thread";
        private static AmazonDynamoDBClient client = new AmazonDynamoDBClient();
           BatchGetItemResponse response;
           do
           {
               Console.WriteLine("Making request");
               response = client.BatchGetItem(request);
               request.RequestItems = unprocessedKeys;
           } while (response.UnprocessedKeys.Count > 0);
       }
                    Console.WriteLine(
                        attributeName + " " +
                        (value.S == null ? "" : "S=[" + value.S + "]") +
                        (value.N == null ? "" : "N=[" + value.N + "]") +
                        (value.SS == null ? "" : "SS=[" + string.Join(",", value.SS.ToArray())
 + "]") +
                        (value.NS == null ? "" : "NS=[" + string.Join(",", value.NS.ToArray())
 + "]")
                      );
              }
              Console.WriteLine("************************************************");
          }
    }
}
If you followed the steps in Creating Tables and Loading Sample Data (p. 281), you already have the
Reply table created. You can also create these sample tables programmatically. For more information,
see Creating Example Tables and Uploading Data Using the AWS SDK for .NET (p. 795).
For step-by-step instructions to test the following sample, see .NET Code Samples (p. 288).
Example
using   System;
using   System.Collections.Generic;
using   System.IO;
using   System.IO.Compression;
using   Amazon.DynamoDBv2;
using   Amazon.DynamoDBv2.Model;
using   Amazon.Runtime;
namespace com.amazonaws.codesamples
{
    class LowLevelItemBinaryExample
    {
        private static string tableName = "Reply";
        private static AmazonDynamoDBClient client = new AmazonDynamoDBClient();
try
           {
               CreateItem(replyIdPartitionKey, replyDateTimeSortKey);
               RetrieveItem(replyIdPartitionKey, replyDateTimeSortKey);
               // Delete item.
               DeleteItem(replyIdPartitionKey, replyDateTimeSortKey);
               Console.WriteLine("To continue, press Enter");
               Console.ReadLine();
           }
           catch (AmazonDynamoDBException e) { Console.WriteLine(e.Message); }
           catch (AmazonServiceException e) { Console.WriteLine(e.Message); }
           catch (Exception e) { Console.WriteLine(e.Message); }
       }
           PrintItem(attributeList);
       }
                  Console.WriteLine(
                      attributeName + " " +
                      (value.S == null ? "" : "S=[" + value.S + "]") +
                      (value.N == null ? "" : "N=[" + value.N + "]") +
                      (value.SS == null ? "" : "SS=[" + string.Join(",", value.SS.ToArray())
 + "]") +
                      (value.NS == null ? "" : "NS=[" + string.Join(",", value.NS.ToArray())
 + "]") +
                      (value.B == null ? "" : "B=[" + FromGzipMemoryStream(value.B) + "]")
                      );
              }
              Console.WriteLine("************************************************");
          }
    The Query operation finds items based on primary key values. You can query any table or secondary
    index that has a composite primary key (a partition key and a sort key).
    You must provide the name of the partition key attribute, and a single value for that attribute.Query will
    return all of the items with that partition key value. You can optionally provide a sort key attribute, and
    use a comparison operator to refine the search results.
You must specify the partition key name and value as an equality condition.
    You can optionally provide a second condition for the sort key (if present). The sort key condition must
    use one of the following comparison operators:
• begins_with (a, substr)— true if the value of attribute a begins with a particular substring.
    The following AWS CLI examples demonstrate the use of key condition expressions. Note that these
    expressions use placeholders (such as :name and :sub) instead of actual values. For more information,
    see Expression Attribute Names (p. 341) and Expression Attribute Values (p. 344).
    Example
    Query the Thread table for a particular ForumName (partition key). All of the items with that
    ForumName value will be read by the query, because the sort key (Subject) is not included in
    KeyConditionExpression.
Example
Query the Thread table for a particular ForumName (partition key), but this time return only the items
with a given Subject (sort key).
{
    ":name":{"S":"Amazon DynamoDB"},
    ":sub":{"S":"DynamoDB Thread 1"}
}
Example
Query the Reply table for a particular Id (partition key), but return only those items whose ReplyDateTime
(sort key) begins with certain characters.
{
    ":id":{"S":"Amazon DynamoDB#DynamoDB Thread 1"},
    ":dt":{"S":"2015-09"}
}
You can use any attribute name in a key condition expression, provided that the first character
is a-z or A-Z and the second character (if present) is a-z, A-Z, or 0-9. In addition, the attribute
name must not be a DynamoDB reserved word. (For a complete list of these, see Reserved Words
in DynamoDB (p. 820).) If an attribute name does not meet these requirements, you will need to
define an expression attribute name as a placeholder. For more information, see Expression Attribute
Names (p. 341).
For items with a given partition key value, DynamoDB stores these items close together, in sorted
order by sort key value. In a Query operation, DynamoDB retrieves the items in sorted order, and then
processes the items using KeyConditionExpression and any FilterExpression that might be
present. Only then are the Query results sent back to the client.
A Query operation always returns a result set. If no matching items are found, the result set will be
empty.
Query results are always sorted by the sort key value. If the data type of the sort key is Number, the
results are returned in numeric order; otherwise, the results are returned in order of UTF-8 bytes. By
default, the sort order is ascending. To reverse the order, set the ScanIndexForward parameter to
false.
A single Query operation can retrieve a maximum of 1 MB of data. This limit applies before any
FilterExpression is applied to the results. If LastEvaluatedKey is present in the response and is
non-null, you will need to paginate the result set (see Paginating the Results (p. 411)).
A filter expression is applied after a Query finishes, but before the results are returned. Therefore, a
Query will consume the same amount of read capacity, regardless of whether a filter expression is
present.
A Query operation can retrieve a maximum of 1 MB of data. This limit applies before the filter
expression is evaluated.
A filter expression cannot contain partition key or sort key attributes. You need to specify those
attributes in the key condition expression, not the filter expression.
The syntax for a filter expression is identical to that of a condition expression. Filter expressions can use
the same comparators, functions, and logical operators as a condition expression. For more information,
Condition Expressions (p. 344).
Example
The following AWS CLI example queries the Thread table for a particular ForumName (partition key) and
Subject (sort key). Of the items that are found, only the most popular discussion threads are returned—in
other words, only those threads with more than a certain number of Views.
{
    ":fn":{"S":"Amazon DynamoDB#DynamoDB Thread 1"},
    ":num":{"N":"3"}
}
Note that Views is a reserved word in DynamoDB (see Reserved Words in DynamoDB (p. 820)), so this
example uses #v as a placeholder. For more information, see Expression Attribute Names (p. 341).
    Note
    A filter expression removes items from the Query result set. If possible, avoid using Query
    where you expect to retrieve a large number of items, but also need to discard most of those
    items.
For example, suppose you Query a table, with a Limit value of 6, and without a filter expression. The
Query result will contain the first six items from the table that match the key condition expression from
the request.
Now suppose you add a filter expression to the Query. In this case, DynamoDB will apply the filter
expression to the six items that were returned, discarding those that do not match. The final Query
result will contain 6 items or fewer, depending on the number of items that were filtered.
A single Query will only return a result set that fits within the 1 MB size limit. To determine whether
there are more results, and to retrieve them one page at a time, applications should do the following:
In other words, the LastEvaluatedKey from a Query response should be used as the
ExclusiveStartKey for the next Query request. If there is not a LastEvaluatedKey element in a
Query response, then you have retrieved the final page of results. (The absence of LastEvaluatedKey
is the only way to know that you have reached the end of the result set.)
You can use the AWS CLI to view this behavior. The CLI sends low-level Query requests to DynamoDB,
repeatedly, until LastEvaluatedKey is no longer present in the results. Consider the following AWS CLI
example that retrieves movie titles from a particular year:
Ordinarily, the AWS CLI handles pagination automatically; however, in this example, the CLI's --
page-size parameter limits the number of items per page. The --debug parameter prints low-level
information about requests and responses.
If you run the example, the first response from DynamoDB looks similar to this:
The LastEvaluatedKey in the response indicates that not all of the items have been retrieved. The
AWS CLI will then issue another Query request to DynamoDB. This request and response pattern
continues, until the final response:
The absence of LastEvaluatedKey indicates that there are no more items to retrieve.
    Note
    The AWS SDKs handle the low-level DynamoDB responses (including the presence or absence
    of LastEvaluatedKey), and provide various abstractions for paginating Query results. For
    example, the SDK for Java document interface provides java.util.Iterator support, so that
    you can walk through the results one at a time.
    For code samples in various programming languages, see the Amazon DynamoDB Getting
    Started Guide and the AWS SDK documentation for your language.
• ScannedCount — the number of items that matched the key condition expression, before a filter
  expression (if present) was applied.
• Count — the number of items that remain, after a filter expression (if present) was applied.
    Note
    If you do not use a filter expression, then ScannedCount and Count will have the same value.
If the size of the Query result set is larger than 1 MB, then ScannedCount and Count will represent
only a partial count of the total items. You will need to perform multiple Query operations in order to
retrieve all of the results (see Paginating the Results (p. 411)).
Each Query response will contain the ScannedCount and Count for the items that were processed by
that particular Query request. To obtain grand totals for all of the Query requests, you could keep a
running tally of both ScannedCount and Count.
By default, a Query operation does not return any data on how much read capacity it consumes.
However, you can specify the ReturnConsumedCapacity parameter in a Query request to obtain this
information. The following are the valid settings for ReturnConsumedCapacity:
DynamoDB calculates the number of read capacity units consumed based on item size, not on the
amount of data that is returned to an application. For this reason, the number of capacity units
consumed will be the same whether you request all of the attributes (the default behavior) or just some
of them (using a projection expression). The number will also be the same whether or not you use a filter
expression.
If you require strongly consistent reads, set the ConsistentRead parameter to true in the Query
request.
The following are the steps to retrieve an item using the AWS SDK for Java Document API.
The response includes an ItemCollection object that provides all items returned by the query.
The following Java code snippet demonstrates the preceding tasks. The snippet assumes you have a
Reply table that stores replies for forum threads. For more information, see Creating Tables and Loading
Sample Data (p. 281).
Each forum thread has a unique ID and can have zero or more replies. Therefore, the Id attribute
of the Reply table is composed of both the forum name and forum subject. Id (partition key) and
ReplyDateTime (sort key) make up the composite primary key for the table.
The following query retrieves all replies for a specific thread subject. The query requires both the table
name and the Subject value.
Example
The following Java code snippet retrieves forum thread replies posted in the past 15 days. The snippet
specifies optional parameters using:
• A KeyConditionExpression to retrieve the replies from a specific discussion forum (partition key)
  and, within that set of items, replies that were posted within the last 15 days (sort key).
• A FilterExpression to return only the replies from a specific user. The filter is applied after the
  query is processed, but before the results are returned to the user.
• A ValueMap to define the actual values for the KeyConditionExpression placeholders.
• A ConsistentRead setting of true, to request a strongly consistent read.
This snippet uses a QuerySpec object which gives access to all of the low-level Query input parameters.
Example
        .withString(":v_reply_dt_tm", twoWeeksAgoStr)
        .withString(":v_posted_by", "User B"))
    .withConsistentRead(true);
You can also optionally limit the number of items per page by using the withMaxPageSize method.
When time you call the query method, you get an ItemCollection that contains the resulting items.
You can then step through the results, processing one page at a time, until there are no more pages.
The following Java code snippet modifies the query specification shown above. This time, the query spec
uses the withMaxPageSize method. The Page class provides an Iterator that allows the code to process
the items on each page.
Example
spec.withMaxPageSize(10);
Example
In this Java code example, you execute variations of finding replies for a thread 'DynamoDB Thread 1' in
forum 'DynamoDB'.
• Find replies for a thread, specifying a limit on the number of items per page of results. If the number
  of items in the result set exceeds the page size, you get only the first page of results. This coding
  pattern ensures your code processes all the pages in the query result.
• Find replies in the last 15 days.
• Find replies in a specific date range.
  Both the preceding two queries shows how you can specify sort key conditions to narrow the query
  results and use other optional query parameters.
    Note
    This code sample assumes that you have already loaded data into DynamoDB for your account
    by following the instructions in the Creating Tables and Loading Sample Data (p. 281) section.
    For step-by-step instructions to run the following example, see Java Code Samples (p. 286).
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import   com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
import   com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder;
import   com.amazonaws.services.dynamodbv2.document.DynamoDB;
import   com.amazonaws.services.dynamodbv2.document.Item;
import   com.amazonaws.services.dynamodbv2.document.ItemCollection;
import   com.amazonaws.services.dynamodbv2.document.Page;
import   com.amazonaws.services.dynamodbv2.document.QueryOutcome;
import   com.amazonaws.services.dynamodbv2.document.Table;
import   com.amazonaws.services.dynamodbv2.document.spec.QuerySpec;
import   com.amazonaws.services.dynamodbv2.document.utils.ValueMap;
          findRepliesForAThread(forumName, threadSubject);
          findRepliesForAThreadSpecifyOptionalLimit(forumName, threadSubject);
          findRepliesInLast15DaysWithConfig(forumName, threadSubject);
          findRepliesPostedWithinTimePeriod(forumName, threadSubject);
          findRepliesUsingAFilterExpression(forumName, threadSubject);
    }
System.out.println("\nfindRepliesForAThread results:");
System.out.println("\nfindRepliesForAThreadSpecifyOptionalLimit results:");
       System.out.println("\nfindRepliesInLast15DaysWithConfig results:");
       Iterator<Item> iterator = items.iterator();
       while (iterator.hasNext()) {
           System.out.println(iterator.next().toJSONPretty());
       }
         System.out.println("\nfindRepliesPostedWithinTimePeriod results:");
         Iterator<Item> iterator = items.iterator();
         while (iterator.hasNext()) {
             System.out.println(iterator.next().toJSONPretty());
         }
    }
         System.out.println("\nfindRepliesUsingAFilterExpression results:");
         Iterator<Item> iterator = items.iterator();
         while (iterator.hasNext()) {
             System.out.println(iterator.next().toJSONPretty());
         }
    }
The following are the steps to query a table using low-level .NET SDK API.
The response includes the QueryResult object that provides all items returned by the query.
The following C# code snippet demonstrates the preceding tasks. The snippet assumes you have a Reply
table stores replies for forum threads. For more information, see Creating Tables and Loading Sample
Data (p. 281).
Example
Each forum thread has a unique ID and can have zero or more replies. Therefore, the primary key is
composed of both the Id (partition key) and ReplyDateTime (sort key).
The following query retrieves all replies for a specific thread subject. The query requires both the table
name and the Subject value.
Example
The following C# code snippet retrieves forum thread replies posted in the past 15 days. The snippet
specifies the following optional parameters:
Example
You can also optionally limit the page size, or the number of items per page, by adding the optional
Limit parameter. Each time you execute the Query method, you get one page of results that has the
specified number of items. To fetch the next page, you execute the Query method again by providing
the primary key value of the last item in the previous page so that the method can return the next set
of items. You provide this information in the request by setting the ExclusiveStartKey property.
Initially, this property can be null. To retrieve subsequent pages, you must update this property value to
the primary key of the last item in the preceding page.
The following C# code snippet queries the Reply table. In the request, it specifies the Limit and
ExclusiveStartKey optional parameters. The do/while loop continues to scan one page at time
until the LastEvaluatedKey returns a null value.
Example
do
{
     var request = new QueryRequest
     {
         TableName = "Reply",
         KeyConditionExpression = "Id = :v_Id",
         ExpressionAttributeValues = new Dictionary<string, AttributeValue> {
             {":v_Id", new AttributeValue { S = "Amazon DynamoDB#DynamoDB Thread 2" }}
         },
          // Optional parameters.
          Limit = 1,
          ExclusiveStartKey = lastKeyEvaluated
     };
lastKeyEvaluated = response.LastEvaluatedKey;
Example
In this C# code example, you execute variations of "Find replies for a thread "DynamoDB Thread 1" in
forum "DynamoDB".
  This function illustrate the use of pagination to process multipage result. Amazon DynamoDB has
  a page size limit and if your result exceeds the page size, you get only the first page of results. This
  coding pattern ensures your code processes all the pages in the query result.
• Find replies in the last 15 days.
• Find replies in a specific date range.
  Both of the preceding two queries shows how you can specify sort key conditions to narrow query
  results and use other optional query parameters.
For step-by-step instructions to test the following sample, see .NET Code Samples (p. 288).
using   System;
using   System.Collections.Generic;
using   Amazon.DynamoDBv2;
using   Amazon.DynamoDBv2.Model;
using   Amazon.Runtime;
using   Amazon.Util;
namespace com.amazonaws.codesamples
{
    class LowLevelQuery
    {
        private static AmazonDynamoDBClient client = new AmazonDynamoDBClient();
               FindRepliesForAThread(forumName, threadSubject);
               FindRepliesForAThreadSpecifyOptionalLimit(forumName, threadSubject);
               FindRepliesInLast15DaysWithConfig(forumName, threadSubject);
               FindRepliesPostedWithinTimePeriod(forumName, threadSubject);
                // Optional parameter.
                ProjectionExpression = "Id, ReplyDateTime, PostedBy",
                // Optional parameter.
                ConsistentRead = true
           };
             Console.ReadLine();
         }
                 Console.WriteLine(
                     attributeName + " " +
                     (value.S == null ? "" : "S=[" + value.S + "]") +
                     (value.N == null ? "" : "N=[" + value.N + "]") +
                     (value.SS == null ? "" : "SS=[" + string.Join(",", value.SS.ToArray())
+ "]") +
                     (value.NS == null ? "" : "NS=[" + string.Join(",", value.NS.ToArray())
+ "]")
                     );
                    }
                    Console.WriteLine("************************************************");
               }
          }
    }
    A Scan operation reads every item in a table or a secondary index. By default, a Scan operation returns
    all of the data attributes for every item in the table or index. You can use the ProjectionExpression
    parameter so that Scan only returns some of the attributes, rather than all of them.
Scan always returns a result set. If no matching items are found, the result set will be empty.
    A single Scan request can retrieve a maximum of 1 MB of data; DynamoDB can optionally apply a filter
    expression to this data, narrowing the results before they are returned to the user.
    A filter expression is applied after a Scan finishes, but before the results are returned. Therefore, a Scan
    will consume the same amount of read capacity, regardless of whether a filter expression is present.
    A Scan operation can retrieve a maximum of 1 MB of data. This limit applies before the filter expression
    is evaluated.
    With Scan, you can specify any attributes in a filter expression—including partition key and sort key
    attributes.
    The syntax for a filter expression is identical to that of a condition expression. Filter expressions can use
    the same comparators, functions, and logical operators as a condition expression. For more information,
    Condition Expressions (p. 344).
    Example
    The following AWS CLI example scans the Thread table and returns only the items that were last posted
    to by a particular user.
      --table-name Thread \
      --filter-expression "LastPostedBy = :name" \
      --expression-attribute-values '{":name":{"S":"User A"}}'
For example, suppose you Scan a table, with a Limit value of 6, and without a filter expression. The
Scan result will contain the first six items from the table that match the key condition expression from
the request.
Now suppose you add a filter expression to the Scan. In this case, DynamoDB will apply the filter
expression to the six items that were returned, discarding those that do not match. The final Scan result
will contain 6 items or fewer, depending on the number of items that were filtered.
A single Scan will only return a result set that fits within the 1 MB size limit. To determine whether there
are more results, and to retrieve them one page at a time, applications should do the following:
In other words, the LastEvaluatedKey from a Scan response should be used as the
ExclusiveStartKey for the next Scan request. If there is not a LastEvaluatedKey element in a
Scan response, then you have retrieved the final page of results. (The absence of LastEvaluatedKey is
the only way to know that you have reached the end of the result set.)
You can use the AWS CLI to view this behavior. The CLI sends low-level Scan requests to DynamoDB,
repeatedly, until LastEvaluatedKey is no longer present in the results. Consider the following AWS CLI
example that scans the entire Movies table but returns only the movies from a particular genre:
Ordinarily, the AWS CLI handles pagination automatically; however, in this example, the CLI's --
page-size parameter limits the number of items per page. The --debug parameter prints low-level
information about requests and responses.
If you run the example, the first response from DynamoDB looks similar to this:
The LastEvaluatedKey in the response indicates that not all of the items have been retrieved.
The AWS CLI will then issue another Scan request to DynamoDB. This request and response pattern
continues, until the final response:
The absence of LastEvaluatedKey indicates that there are no more items to retrieve.
    Note
    The AWS SDKs handle the low-level DynamoDB responses (including the presence or absence
    of LastEvaluatedKey), and provide various abstractions for paginating Scan results. For
    example, the SDK for Java document interface provides java.util.Iterator support, so that
    you can walk through the results one at a time.
    For code samples in various programming languages, see the Amazon DynamoDB Getting
    Started Guide and the AWS SDK documentation for your language.
• ScannedCount — the number of items that matched the key condition expression, before a filter
  expression (if present) was applied.
• Count — the number of items that remain, after a filter expression (if present) was applied.
    Note
    If you do not use a filter expression, then ScannedCount and Count will have the same value.
If the size of the Scan result set is larger than 1 MB, then ScannedCount and Count will represent only
a partial count of the total items. You will need to perform multiple Scan operations in order to retrieve
all of the results (see Paginating the Results (p. 426)).
Each Scan response will contain the ScannedCount and Count for the items that were processed
by that particular Scan request. To obtain grand totals for all of the Scan requests, you could keep a
running tally of both ScannedCount and Count.
By default, a Scan operation does not return any data on how much read capacity it consumes. However,
you can specify the ReturnConsumedCapacity parameter in a Scan request to obtain this information.
The following are the valid settings for ReturnConsumedCapacity:
DynamoDB calculates the number of read capacity units consumed based on item size, not on the
amount of data that is returned to an application. For this reason, the number of capacity units
consumed will be the same whether you request all of the attributes (the default behavior) or just some
of them (using a projection expression). The number will also be the same whether or not you use a filter
expression.
If you require strongly consistent reads, as of the time that the Scan begins, set the ConsistentRead
parameter to true in the Scan request. This will ensure that all of the write operations that completed
before the Scan began will be included in the Scan response.
Setting ConsistentRead to true can be useful in table backup or replication scenarios, in conjunction
with DynamoDB Streams: You first use Scan with ConsistentRead set to true, in order to obtain a
consistent copy of the data in the table. During the Scan, DynamoDB Streams records any additional
write activity that occurs on the table. After the Scan completes, you can apply the write activity from
the stream to the table.
    Note
    Note that a Scan operation with ConsistentRead set to true will consume twice as many
    read capacity units, as compared to leaving ConsistentRead at its default value (false).
Parallel Scan
By default, the Scan operation processes data sequentially. DynamoDB returns data to the application in
1 MB increments, and an application performs additional Scan operations to retrieve the next 1 MB of
data.
The larger the table or index being scanned, the more time the Scan will take to complete. In addition, a
sequential Scan might not always be able to fully utilize the provisioned read throughput capacity: Even
though DynamoDB distributes a large table's data across multiple physical partitions, a Scan operation
can only read one partition at a time. For this reason, the throughput of a Scan is constrained by the
maximum throughput of a single partition.
To address these issues, the Scan operation can logically divide a table or secondary index into multiple
segments, with multiple application workers scanning the segments in parallel. Each worker can be a
thread (in programming languages that support multithreading) or an operating system process. To
perform a parallel scan, each worker issues its own Scan request with the following parameters:
• Segment — A segment to be scanned by a particular worker. Each worker should use a different value
  for Segment.
• TotalSegments — The total number of segments for the parallel scan. This value must be the same
  as the number of workers that your application will use.
The following diagram shows how a multithreaded application performs a parallel Scan with three
degrees of parallelism:
In this diagram, the application spawns three threads and assigns each thread a number. (Segments
are zero-based, so the first number is always 0.) Each thread issues a Scan request, setting Segment
to its designated number and setting TotalSegments to 3. Each thread scans its designated segment,
retrieving data 1 MB at a time, and returns the data to the application's main thread.
The values for Segment and TotalSegments apply to individual Scan requests, and you can use
different values at any time. You might need to experiment with these values, and the number of
workers you use, until your application achieves its best performance.
    Note
    A parallel scan with a large number of workers can easily consume all of the provisioned
    throughput for the table or index being scanned. It is best to avoid such scans if the table or
    index is also incurring heavy read or write activity from other applications.
    To control the amount of data returned per request, use the Limit parameter. This can help
    prevent situations where one worker consumes all of the provisioned throughput, at the
    expense of all other workers.
The following are the steps to scan a table using the AWS SDK for Java Document API.
Example
The table maintains all the replies for various forum threads. Therefore, the primary key is composed
of both the Id (partition key) and ReplyDateTime (sort key). The following Java code snippet scans the
entire table. The ScanRequest instance specifies the name of the table to scan.
Example
The following Java snippet scans the ProductCatalog table to find items that are priced less than 0. The
snippet specifies the following optional parameters:
• A filter expression to retrieve only the items priced less than 0 (error condition).
• A list of attributes to retrieve for items in the query results.
Example
You can also optionally limit the page size, or the number of items per page, by using the withLimit
method of the scan request. Each time you execute the scan method, you get one page of results
that has the specified number of items. To fetch the next page, you execute the scan method
again by providing the primary key value of the last item in the previous page so that the scan
method can return the next set of items. You provide this information in the request by using the
withExclusiveStartKey method. Initially, the parameter of this method can be null. To retrieve
subsequent pages, you must update this property value to the primary key of the last item in the
preceding page.
The following Java code snippet scans the ProductCatalog table. In the request, the withLimit and
withExclusiveStartKey methods are used. The do/while loop continues to scan one page at time
until the getLastEvaluatedKey method of the result returns a value of null.
Example
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder;
import com.amazonaws.services.dynamodbv2.document.DynamoDB;
import   com.amazonaws.services.dynamodbv2.document.Item;
import   com.amazonaws.services.dynamodbv2.document.ItemCollection;
import   com.amazonaws.services.dynamodbv2.document.ScanOutcome;
import   com.amazonaws.services.dynamodbv2.document.Table;
          findProductsForPriceLessThanOneHundred();
    }
          System.out.println("Scan of " + tableName + " for items with a price less than
 100.");
          Iterator<Item> iterator = items.iterator();
          while (iterator.hasNext()) {
              System.out.println(iterator.next().toJSONPretty());
          }
    }
import java.util.ArrayList;
import java.util.Arrays;
import   java.util.HashSet;
import   java.util.Iterator;
import   java.util.List;
import   java.util.concurrent.ExecutorService;
import   java.util.concurrent.Executors;
import   java.util.concurrent.TimeUnit;
import   com.amazonaws.AmazonServiceException;
import   com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
import   com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder;
import   com.amazonaws.services.dynamodbv2.document.DynamoDB;
import   com.amazonaws.services.dynamodbv2.document.Item;
import   com.amazonaws.services.dynamodbv2.document.ItemCollection;
import   com.amazonaws.services.dynamodbv2.document.ScanOutcome;
import   com.amazonaws.services.dynamodbv2.document.Table;
import   com.amazonaws.services.dynamodbv2.document.spec.ScanSpec;
import   com.amazonaws.services.dynamodbv2.model.AttributeDefinition;
import   com.amazonaws.services.dynamodbv2.model.KeySchemaElement;
import   com.amazonaws.services.dynamodbv2.model.KeyType;
import   com.amazonaws.services.dynamodbv2.model.ProvisionedThroughput;
       shutDownExecutorService(executor);
   }
       @Override
       public void run() {
           System.out.println("Scanning " + tableName + " segment " + segment + " out of "
+ totalSegments
                + " segments " + itemLimit + " items at a time...");
           int totalScannedItemCount = 0;
           try {
               ScanSpec spec = new
ScanSpec().withMaxResultSize(itemLimit).withTotalSegments(totalSegments)
                   .withSegment(segment);
           }
           catch (Exception e) {
               System.err.println(e.getMessage());
           }
           finally {
       try {
           System.out.println("Processing record #" + productIndex);
       }
       catch (Exception e) {
            System.err.println("Failed to create item " + productIndex + " in " +
tableName);
            System.err.println(e.getMessage());
       }
   }
       }
       catch (Exception e) {
           System.err.println("Failed to delete table " + tableName);
           e.printStackTrace(System.err);
       }
   }
         try {
             System.out.println("Creating table " + tableName);
// key
            if (sortKeyName != null) {
                keySchema.add(new
 KeySchemaElement().withAttributeName(sortKeyName).withKeyType(KeyType.RANGE)); // Sort
                      // key
                attributeDefinitions
                    .add(new
 AttributeDefinition().withAttributeName(sortKeyName).withAttributeType(sortKeyType));
            }
 .withReadCapacityUnits(readCapacityUnits).withWriteCapacityUnits(writeCapacityUnits));
            System.out.println("Waiting for " + tableName + " to be created...this may take
 a while...");
            table.waitForActive();
         }
         catch (Exception e) {
             System.err.println("Failed to create table " + tableName);
             e.printStackTrace(System.err);
         }
    }
The following are the steps to scan a table using the AWS SDK for NET low-level API:
Example
The table maintains all the replies for various forum threads. Therefore, the primary key is composed of
both the Id (partition key) and ReplyDateTime (sort key). The following C# code snippet scans the entire
table. The ScanRequest instance specifies the name of the table to scan.
Example
The following C# code scans the ProductCatalog table to find items that are priced less than 0. The
sample specifies the following optional parameters:
• A FilterExpression parameter to retrieve only the items priced less than 0 (error condition).
• A ProjectionExpression parameter to specify the attributes to retrieve for items in the query
  results.
The following C# code snippet scans the ProductCatalog table to find all items priced less than 0.
Example
   TableName = "ProductCatalog",
   // Optional parameters.
   ExpressionAttributeValues = new Dictionary<string,AttributeValue> {
        {":val", new AttributeValue { N = "0" }}
   },
   FilterExpression = "Price < :val",
   ProjectionExpression = "Id"
 };
You can also optionally limit the page size, or the number of items per page, by adding the optional
Limit parameter. Each time you execute the Scan method, you get one page of results that has the
specified number of items. To fetch the next page, you execute the Scan method again by providing
the primary key value of the last item in the previous page so that the Scan method can return the next
set of items. You provide this information in the request by setting the ExclusiveStartKey property.
Initially, this property can be null. To retrieve subsequent pages, you must update this property value to
the primary key of the last item in the preceding page.
The following C# code snippet scans the ProductCatalog table. In the request, it specifies the Limit and
ExclusiveStartKey optional parameters. The do/while loop continues to scan one page at time
until the LastEvaluatedKey returns a null value.
Example
For step-by-step instructions to test the following sample, see .NET Code Samples (p. 288).
using   System;
using   System.Collections.Generic;
using   Amazon.DynamoDBv2;
using   Amazon.DynamoDBv2.Model;
using   Amazon.Runtime;
namespace com.amazonaws.codesamples
{
    class LowLevelScan
    {
        Console.WriteLine(
            attributeName + " " +
            (value.S == null ? "" : "S=[" + value.S + "]") +
            (value.N == null ? "" : "N=[" + value.N + "]") +
For step-by-step instructions to test the following sample, see .NET Code Samples (p. 288).
using   System;
using   System.Collections.Generic;
using   System.Threading;
using   System.Threading.Tasks;
using   Amazon.DynamoDBv2;
using   Amazon.DynamoDBv2.Model;
using   Amazon.Runtime;
namespace com.amazonaws.codesamples
{
    class LowLevelParallelScan
    {
        private static AmazonDynamoDBClient client = new AmazonDynamoDBClient();
        private static string tableName = "ProductCatalog";
        private static int exampleItemCount = 100;
        private static int scanItemLimit = 10;
        private static int totalSegments = 5;
               tasks[segment] = task;
           }
        TableName = tableName,
        Item = new Dictionary<string, AttributeValue>()
    {
        { "Id", new AttributeValue {
              N = itemIndex
          }},
        { "Title", new AttributeValue {
              S = "Book " + itemIndex + " Title"
          }},
        { "ISBN", new AttributeValue {
              S = "11-11-11-11"
          }},
        { "Authors", new AttributeValue {
              SS = new List<string>{"Author1", "Author2" }
          }},
        { "Price", new AttributeValue {
              N = "20.00"
          }},
        { "Dimensions", new AttributeValue {
              S = "8.5x11.0x.75"
          }},
        { "InPublication", new AttributeValue {
              BOOL = false
          } }
    }
    };
    client.PutItem(request);
}
tableDescription.ProvisionedThroughput.WriteCapacityUnits);
           WaitUntilTableReady(tableName);
       }
    Amazon DynamoDB provides fast access to items in a table by specifying primary key values. However,
    many applications might benefit from having one or more secondary (or alternate) keys available, to
    allow efficient access to data with attributes other than the primary key. To address this, you can create
    one or more secondary indexes on a table, and issue Query or Scan requests against these indexes.
    A secondary index is a data structure that contains a subset of attributes from a table, along with an
    alternate key to support Query operations. You can retrieve data from the index using a Query, in much
    the same way as you use Query with a table. A table can have multiple secondary indexes, which gives
    your applications access to many different query patterns.
          Note
          You can also Scan an index, in much the same way as you would Scan a table.
    Every secondary index is associated with exactly one table, from which it obtains its data. This is called
    the base table for the index. When you create an index, you define an alternate key for the index
    (partition key and sort key). You also define the attributes that you want to be projected, or copied, from
    the base table into the index. DynamoDB copies these attributes into the index, along with the primary
    key attributes from the base table. You can then query or scan the index just as you would query or scan
    a table.
    Every secondary index is automatically maintained by DynamoDB. When you add, modify, or delete
    items in the base table, any indexes on that table are also updated to reflect these changes.
    • Global secondary index — an index with a partition key and a sort key that can be different from
      those on the base table. A global secondary index is considered "global" because queries on the index
      can span all of the data in the base table, across all partitions.
    • Local secondary index — an index that has the same partition key as the base table, but a different
      sort key. A local secondary index is "local" in the sense that every partition of a local secondary index is
      scoped to a base table partition that has the same partition key value.
You should consider your application's requirements when you determine which type of index to use.
The following table shows the main differences between a global secondary index and a local secondary
index:
Key Schema                        The primary key of a global          The primary key of a local
                                  secondary index can be either        secondary index must be
                                  simple (partition key) or            composite (partition key and
                                  composite (partition key and         sort key).
                                  sort key).
Key Attributes                    The index partition key and sort     The partition key of the index
                                  key (if present) can be any base     is the same attribute as the
                                  table attributes of type string,     partition key of the base table.
                                  number, or binary.                   The sort key can be any base
                                                                       table attribute of type string,
                                                                       number, or binary.
Size Restrictions Per Partition   There are no size restrictions for   For each partition key value, the
Key Value                         global secondary indexes.            total size of all indexed items
                                                                       must be 10 GB or less.
Online Index Operations           Global secondary indexes can         Local secondary indexes are
                                  be created at the same time          created at the same time that
                                  that you create a table. You         you create a table. You cannot
                                  can also add a new global            add a local secondary index
                                  secondary index to an existing       to an existing table, nor can
                                  table, or delete an existing         you delete any local secondary
                                  global secondary index.              indexes that currently exist.
                                  For more information, see
                                  Managing Global Secondary
                                  Indexes (p. 453).
Queries and Partitions            A global secondary index lets        A local secondary index lets you
                                  you query over the entire table,     query over a single partition, as
                                  across all partitions.               specified by the partition key
                                                                       value in the query.
Projected Attributes              With global secondary index          If you query or scan a local
                                  queries or scans, you can only       secondary index, you can
                                  request the attributes that          request attributes that are
If you want to create more than one table with secondary indexes, you must do so sequentially. For
example, you would create the first table and wait for it to become ACTIVE, create the next table and
wait for it to become ACTIVE, and so on. If you attempt to concurrently create more than one table with
a secondary index, DynamoDB will return a LimitExceededException.
• The type of index to be created – either a global secondary index or a local secondary index.
• A name for the index. The naming rules for indexes are the same as those for tables, as listed in Limits
  in DynamoDB (p. 769). The name must be unique for the base table it is associated with, but you can
  use the same name for indexes that are associated with different base tables.
• The key schema for the index. Every attribute in the index key schema must be a top-level attribute of
  type String, Number, or Binary. Other data types, including documents and sets, are not allowed. Other
  requirements for the key schema depend on the type of index:
  • For a global secondary index, the partition key can be any scalar attribute of the base table. A sort
    key is optional, and it too can be any scalar attribute of the base table.
  • For a local secondary index, the partition key must be the same as the base table's partition key, and
    the sort key must be a non-key base table attribute.
• Additional attributes, if any, to project from the base table into the index. These attributes are in
  addition to the table's key attributes, which are automatically projected into every index. You can
  project attributes of any data type, including scalars, documents, and sets.
• The provisioned throughput settings for the index, if necessary:
  • For a global secondary index, you must specify read and write capacity unit settings. These
    provisioned throughput settings are independent of the base table's settings.
  • For a local secondary index, you do not need to specify read and write capacity unit settings. Any
    read and write operations on a local secondary index draw from the provisioned throughput settings
    of its base table.
For maximum query flexibility, you can create up to 5 global secondary indexes and up to 5 local
secondary indexes per table.
To get a detailed listing of secondary indexes on a table, use the DescribeTable operation.
DescribeTable will return the name, storage size and item counts for every secondary index on the
table. These values are not updated in real time, but they are refreshed approximately every six hours.
You can access the data in a secondary index using either the Query or Scan operation. You must specify
the name of the base table and the name of the index that you want to use, the attributes to be returned
in the results, and any condition expressions or filters that you want to apply. DynamoDB can return the
results in ascending or descending order.
When you delete a table, all of the indexes associated with that table are also deleted.
For best practices, see Best Practices for Local Secondary Indexes (p. 719) and Best Practices for Global
Secondary Indexes (p. 721), respectively.
Some applications might need to perform many kinds of queries, using a variety of different attributes
as query criteria. To support these requirements, you can create one or more global secondary indexes
and issue Query requests against these indexes. To illustrate, consider a table named GameScores that
keeps track of users and scores for a mobile gaming application. Each item in GameScores is identified
by a partition key (UserId) and a sort key (GameTitle). The following diagram shows how the items in the
table would be organized. (Not all of the attributes are shown)
Now suppose that you wanted to write a leaderboard application to display top scores for each game.
A query that specified the key attributes (UserId and GameTitle) would be very efficient; however, if the
application needed to retrieve data from GameScores based on GameTitle only, it would need to use a
Scan operation. As more items are added to the table, scans of all the data would become slow and
inefficient, making it difficult to answer questions such as these:
• What is the top score ever recorded for the game Meteor Blasters?
• Which user had the highest score for Galaxy Invaders?
• What was the highest ratio of wins vs. losses?
To speed up queries on non-key attributes, you can create a global secondary index. A global secondary
index contains a selection of attributes from the base table, but they are organized by a primary key that
is different from that of the table. The index key does not need to have any of the key attributes from
the table; it doesn't even need to have the same key schema as a table.
For example, you could create a global secondary index named GameTitleIndex, with a partition key of
GameTitle and a sort key of TopScore. Since the base table's primary key attributes are always projected
into an index, the UserId attribute is also present. The following diagram shows what GameTitleIndex
index would look like:
Now you can query GameTitleIndex and easily obtain the scores for Meteor Blasters. The results are
ordered by the sort key values, TopScore. If you set the ScanIndexForward parameter to false, the
results are returned in descending order, so the highest score is returned first.
Every global secondary index must have a partition key, and can have an optional sort key. The index
key schema can be different from the base table schema; you could have a table with a simple primary
key (partition key), and create a global secondary index with a composite primary key (partition key and
sort key) — or vice-versa. The index key attributes can consist of any top-level String, Number, or Binary
attributes from the base table; other scalar types, document types, and set types are not allowed.
You can project other base table attributes into the index if you want. When you query the index,
DynamoDB can retrieve these projected attributes efficiently; however, global secondary index queries
cannot fetch attributes from the base table. For example, if you queried GameTitleIndex, as shown in
the diagram above, the query would not be able to access any non-key attributes other than TopScore
(though the key attributes GameTitle and UserId would automatically be projected).
In a DynamoDB table, each key value must be unique. However, the key values in a global secondary
index do not need to be unique. To illustrate, suppose that a game named Comet Quest is especially
difficult, with many new users trying but failing to get a score above zero. Here is some data that we
could use to represent this:
When this data is added to the GameScores table, DynamoDB will propagate it to GameTitleIndex. If
we then query the index using Comet Quest for GameTitle and 0 for TopScore, the following data is
returned:
Only the items with the specified key values appear in the response; within that set of data, the items are
in no particular order.
A global secondary index only keeps track of data items where its key attribute(s) actually exist. For
example, suppose that you added another new item to the GameScores table, but only provided the
required primary key attributes:
UserId GameTitle
Because you didn't specify the TopScore attribute, DynamoDB would not propagate this item to
GameTitleIndex. Thus, if you queried GameScores for all the Comet Quest items, you would get the
following four items:
A similar query on GameTitleIndex would still return three items, rather than four. This is because the
item with the nonexistent TopScore is not propagated to the index:
Attribute Projections
A projection is the set of attributes that is copied from a table into a secondary index. The partition key
and sort key of the table are always projected into the index; you can project other attributes to support
your application's query requirements. When you query an index, Amazon DynamoDB can access any
attribute in the projection as if those attributes were in a table of their own.
When you create a secondary index, you need to specify the attributes that will be projected into the
index. DynamoDB provides three different options for this:
• KEYS_ONLY – Each item in the index consists only of the table partition key and sort key values, plus
  the index key values. The KEYS_ONLY option results in the smallest possible secondary index.
• INCLUDE – In addition to the attributes described in KEYS_ONLY, the secondary index will include
  other non-key attributes that you specify.
• ALL – The secondary index includes all of the attributes from the source table. Because all of the table
  data is duplicated in the index, an ALL projection results in the largest possible secondary index.
In the diagram above, GameTitleIndex does not have any additional projected attributes. An application
can use GameTitle and TopScore in queries; however, it is not possible to efficiently determine which user
has the highest score for a particular game, or the highest ratio of wins vs. losses. The most efficient way
to support queries on this data would be to project these attributes from the base table into the global
secondary index, as shown in this diagram:
Because the non-key attributes Wins and Losses are projected into the index, an application can
determine the wins vs. losses ratio for any game, or for any combination of game and user ID.
When you choose the attributes to project into a global secondary index, you must consider the tradeoff
between provisioned throughput costs and storage costs:
• If you need to access just a few attributes with the lowest possible latency, consider projecting only
  those attributes into a global secondary index. The smaller the index, the less that it will cost to store
  it, and the less your write costs will be.
• If your application will frequently access some non-key attributes, you should consider projecting
  those attributes into a global secondary index. The additional storage costs for the global secondary
  index will offset the cost of performing frequent table scans.
• If you need to access most of the non-key attributes on a frequent basis, you can project these
  attributes—or even the entire base table— into a global secondary index. This will give you maximum
  flexibility; however, your storage cost would increase, or even double.
• If your application needs to query a table infrequently, but must perform many writes or updates
  against the data in the table, consider projecting KEYS_ONLY. The global secondary index would be of
  minimal size, but would still be available when needed for query activity.
Consider the following data returned from a Query that requests gaming data for a leaderboard
application:
{
    "TableName": "GameScores",
    "IndexName": "GameTitleIndex",
    "KeyConditionExpression": "GameTitle = :v_title",
    "ExpressionAttributeValues": {
        ":v_title": {"S": "Meteor Blasters"}
    },
    "ProjectionExpression": "UserId, TopScore",
    "ScanIndexForward": false
}
In this query:
• DynamoDB accesses GameTitleIndex, using the GameTitle partition key to locate the index items
  for Meteor Blasters. All of the index items with this key are stored adjacent to each other for rapid
  retrieval.
• Within this game, DynamoDB uses the index to access all of the user IDs and top scores for this game.
• The results are returned, sorted in descending order because the ScanIndexForward parameter is set
  to false.
When you create a global secondary index, you specify one or more index key attributes and their data
types. This means that whenever you write an item to the base table, the data types for those attributes
must match the index key schema's data types. In the case of GameTitleIndex, the GameTitle partition key
in the index is defined as a String data type, and the TopScore sort key in the index is of type Number.
If you attempt to add an item to the GameScores table and specify a different data type for either
GameTitle or TopScore, DynamoDB will return a ValidationException because of the data type
mismatch.
When you put or delete items in a table, the global secondary indexes on that table are updated in an
eventually consistent fashion. Changes to the table data are propagated to the global secondary indexes
within a fraction of a second, under normal conditions. However, in some unlikely failure scenarios,
longer propagation delays might occur. Because of this, your applications need to anticipate and handle
situations where a query on a global secondary index returns results that are not up-to-date.
If you write an item to a table, you don't have to specify the attributes for any global secondary index
sort key. Using GameTitleIndex as an example, you would not need to specify a value for the TopScore
attribute in order to write a new item to the GameScores table. In this case, Amazon DynamoDB does not
write any data to the index for this particular item.
A table with many global secondary indexes will incur higher costs for write activity than tables with
fewer indexes. For more information, see Provisioned Throughput Considerations for Global Secondary
Indexes (p. 452).
For example, if you Query a global secondary index and exceed its provisioned read capacity, your
request will be throttled. If you perform heavy write activity on the table, but a global secondary index
on that table has insufficient write capacity, then the write activity on the table will be throttled.
To view the provisioned throughput settings for a global secondary index, use the DescribeTable
operation; detailed information about all of the table's global secondary indexes will be returned.
For global secondary index queries, DynamoDB calculates the provisioned read activity in the same way
as it does for queries against tables. The only difference is that the calculation is based on the sizes of
the index entries, rather than the size of the item in the base table. The number of read capacity units
is the sum of all projected attribute sizes across all of the items returned; the result is then rounded up
to the next 4 KB boundary. For more information on how DynamoDB calculates provisioned throughput
usage, see Throughput Settings for Reads and Writes (p. 294).
The maximum size of the results returned by a Query operation is 1 MB; this includes the sizes of all the
attribute names and values across all of the items returned.
For example, consider a global secondary index where each item contains 2000 bytes of data. Now
suppose that you Query this index and that the query returns 8 items. The total size of the matching
items is 2000 bytes × 8 items = 16,000 bytes; this is then rounded up to the nearest 4 KB boundary.
Since global secondary index queries are eventually consistent, the total cost is 0.5 × (16 KB / 4 KB), or 2
read capacity units.
In order for a table write to succeed, the provisioned throughput settings for the table and all of its
global secondary indexes must have enough write capacity to accommodate the write; otherwise, the
write to the table will be throttled.
The cost of writing an item to a global secondary index depends on several factors:
• If you write a new item to the table that defines an indexed attribute, or you update an existing item
  to define a previously undefined indexed attribute, one write operation is required to put the item into
  the index.
• If an update to the table changes the value of an indexed key attribute (from A to B), two writes are
  required, one to delete the previous item from the index and another write to put the new item into
  the index. 
• If an item was present in the index, but a write to the table caused the indexed attribute to be deleted,
  one write is required to delete the old item projection from the index.
• If an item is not present in the index before or after the item is updated, there is no additional write
  cost for the index.
• If an update to the table only changes the value of projected attributes in the index key schema, but
  does not change the value of any indexed key attribute, then one write is required to update the values
  of the projected attributes into the index.
All of these factors assume that the size of each item in the index is less than or equal to the 1 KB item
size for calculating write capacity units. Larger index entries will require additional write capacity units.
You can minimize your write costs by considering which attributes your queries will need to return and
projecting only those attributes into the index.
The amount of space used by an index item is the sum of the following:
• The size in bytes of the base table primary key (partition key and sort key)
• The size in bytes of the index key attribute
• The size in bytes of the projected attributes (if any)
• 100 bytes of overhead per index item
To estimate the storage requirements for a global secondary index, you can estimate the average size
of an item in the index and then multiply by the number of items in the base table that have the global
secondary index key attributes.
If a table contains an item where a particular attribute is not defined, but that attribute is defined as an
index partition key or sort key, then DynamoDB does not write any data for that item to the index.
Topics
 • Creating a Table With Global Secondary Indexes (p. 454)
 • Describing the Global Secondary Indexes on a Table (p. 454)
 • Adding a Global Secondary Index To an Existing Table (p. 454)
 • Modifying an Index Creation (p. 456)
 • Deleting a Global Secondary Index From a Table (p. 457)
 • Detecting and Correcting Index Key Violations (p. 457)
You must specify one attribute to act as the index partition key; you can optionally specify another
attribute for the index sort key. It is not necessary for either of these key attributes to be the
same as a key attribute in the table. For example, in the GameScores table (see Global Secondary
Indexes (p. 446)), neither TopScore nor TopScoreDateTime are key attributes; you could create a global
secondary index with a partition key of TopScore and a sort key of TopScoreDateTime. You might use such
an index to determine whether there is a correlation between high scores and the time of day a game is
played.
Each index key attribute must be a scalar of type String, Number, or Binary. (It cannot be a document
or a set.) You can project attributes of any data type into a global secondary index; this includes scalars,
documents, and sets. For a complete list of data types, see Data Types (p. 12).
You must provide ProvisionedThroughput settings for the index, consisting of ReadCapacityUnits
and WriteCapacityUnits. These provisioned throughput settings are separate from those of the
table, but behave in similar ways. For more information, see Provisioned Throughput Considerations for
Global Secondary Indexes (p. 452).
The IndexStatus for a global secondary index will be one of the following:
• CREATING—The index is currently being created, and is not yet available for use.
• ACTIVE—The index is ready for use, and applications can perform Query operations on the index
• UPDATING—The provisioned throughput settings of the index are being changed.
• DELETING—The index is currently being deleted, and can no longer be used.
When DynamoDB has finished building a global secondary index, the index status changes from
CREATING to ACTIVE.
• An index name. The name must be unique among all the indexes on the table.
• The key schema of the index. You must specify one attribute for the index partition key; you can
  optionally specify another attribute for the index sort key. It is not necessary for either of these key
  attributes to be the same as a key attribute in the table. The data types for each schema attribute
  must be scalar: String, Number, or Binary.
• The attributes to be projected from the table into the index:
  • KEYS_ONLY – Each item in the index consists only of the table partition key and sort key values, plus
    the index key values.
  • INCLUDE – In addition to the attributes described in KEYS_ONLY, the secondary index will include
    other non-key attributes that you specify.
  • ALL – The index includes all of the attributes from the source table.
You can only create one global secondary index per UpdateTable operation.
    Note
    You cannot cancel an in-flight global secondary index creation.
Resource Allocation
DynamoDB allocates the compute and storage resources that will be needed for building the index.
    During the resource allocation phase, the IndexStatus attribute is CREATING and the
    Backfilling attribute is false. Use the DescribeTable operation to retrieve the status of a table
    and all of its secondary indexes.
    While the index is in the resource allocation phase, you cannot delete its parent table; nor can you
    modify the provisioned throughput of the index or the table. You cannot add or delete other indexes
    on the table; however, you can modify the provisioned throughput of these other indexes.
Backfilling
    For each item in the table, DynamoDB determines which set of attributes to write to the index
    based on its projection (KEYS_ONLY, INCLUDE, or ALL). It then writes these attributes to the index.
    During the backfill phase, DynamoDB keeps track of items that are being added, deleted, or updated
    in the table. The attributes from these items are also added, deleted, or updated in the index as
    appropriate.
    During the backfilling phase, the IndexStatus attribute is CREATING and the Backfilling
    attribute is true. Use the DescribeTable operation to retrieve the status of a table and all of its
    secondary indexes.
    While the index is backfilling, you cannot delete its parent table. However, you can still modify the
    provisioned throughput of the table and any of its global secondary indexes.
        Note
        During the backfilling phase, some writes of violating items may succeed while others will
        be rejected. After backfilling, all writes to items that violate the new index's key schema
        will be rejected. We recommend that you run the Violation Detector tool after the backfill
        phase completes, to detect and resolve any key violations that may have occurred. For more
        information, see Detecting and Correcting Index Key Violations (p. 457).
While the resource allocation and backfilling phases are in progress, the index is in the CREATING state.
During this time, DynamoDB performs read operations on the table; you will not be charged for this read
activity.
When the index build is complete, its status changes to ACTIVE. You will not be able to Query or Scan
the index until it is ACTIVE.
    Note
    In some cases, DynamoDB will not be able to write data from the table to the index due to index
    key violations. This can occur if the data type of an attribute value does not match the data type
    of an index key schema data type, or if the size of an attribute exceeds the maximum length
    for an index key attribute. Index key violations do not interfere with global secondary index
    creation; however, when the index becomes ACTIVE, the violating keys will not be present in the
    index.
    DynamoDB provides a standalone tool for finding and resolving these issues. For more
    information, see Detecting and Correcting Index Key Violations (p. 457).
If you are adding a global secondary index to a very large table, it might take a long time for the creation
process to complete. To monitor progress and determine whether the index has sufficient write capacity,
consult the following Amazon CloudWatch metrics:
• OnlineIndexPercentageProgress
• OnlineIndexConsumedWriteCapacity
• OnlineIndexThrottleEvents
    Note
    For more information on CloudWatch metrics related to DynamoDB, see DynamoDB
    Metrics (p. 680).
If the provisioned write throughput setting on the index is too low, the index build will take longer
to complete. To shorten the time it takes to build a new global secondary index, you can increase its
provisioned write capacity temporarily.
    Note
    As a general rule, we recommend setting the provisioned write capacity of the index to 1.5 times
    the write capacity of the table. This is a good setting for many use cases; however, your actual
    requirements may be higher or lower.
While an index is being backfilled, DynamoDB uses internal system capacity to read from the table. This
is to minimize the impact of the index creation and to assure that your table does not run out of read
capacity.
However, it is possible that the volume of incoming write activity might exceed the provisioned write
capacity of the index. This is a bottleneck scenario, in which the index creation takes more time because
the write activity to the index is throttled. During the index build, we recommend that you monitor
the Amazon CloudWatch metrics for the index to determine whether its consumed write capacity is
exceeding its provisioned capacity. In a bottleneck scenario, you should increase the provisioned write
capacity on the index to avoid write throttling during the backfill phase.
After the index has been created, you should set its provisioned write capacity to reflect the normal
usage of your application.
While the backfill is proceeding, you can update the provisioned throughput parameters for the index.
You might decide to do this in order to speed up the index build: You can increase the write capacity of
the index while it is being built, and then decrease it afterward. To modify the provisioned throughput
settings of the index, use the UpdateTable operation. The index status will change to UPDATING, and
Backfilling will be true until the index is ready for use.
During the backfilling phase, you cannot add or delete other indexes on the table.
    Note
    For indexes that were created as part of a CreateTable operation, the Backfilling attribute
    does not appear in the DescribeTable output. For more information, see Phases of Index
    Creation (p. 455).
You can only delete one global secondary index per UpdateTable operation.
While the global secondary index, is being deleted, there is no effect on any read or write activity in the
parent table. While the deletion is in progress, you can still modify the provisioned throughput on other
indexes
    Note
    When you delete a table using the DeleteTable action, all of the global secondary indexes on
    that table are also deleted.  
• There is a data type mismatch between an attribute value and the index key schema data type. For
  example, suppose one of the items in the GameScores table had a TopScore value of type String. If you
  added a global secondary index with a partition key of TopScore, of type Number, the item from the
  table would violate the index key.
• An attribute value from the table exceeds the maximum length for an index key attribute. The
  maximum length of a partition key is 2048 bytes, and the maximum length of a sort key is 1024 bytes.
  If any of the corresponding attribute values in the table exceed these limits, the item from the table
  would violate the index key.
If an index key violation occurs, the backfill phase continues without interruption; however, any violating
items are not included in the index. After the backfill phase completes, all writes to items that violate the
new index's key schema will be rejected.
To identify and fix attribute values in a table that violate an index key, use the Violation Detector tool. To
run Violation Detector, you create a configuration file that specifies the name of a table to be scanned,
the names and data types of the global secondary index partition key and sort key, and what actions to
take if any index key violations are found. Violation Detector can run in one of two different modes:
• Detection mode—detect index key violations. Use detection mode to report the items in the table that
  would cause key violations in a global secondary index. (You can optionally request that these violating
  table items be deleted immediately when they are found.) The output from detection mode is written
  to a file, which you can use for further analysis.
• Correction mode— correct index key violations. In correction mode, Violation Detector reads an input
  file with the same format as the output file from detection mode. Correction mode reads the records
  from the input file and, for each record, it either deletes or updates the corresponding items in the
  table. (Note that if you choose to update the items, you must edit the input file and set appropriate
  values for these updates.)
• https://github.com/awslabs/dynamodb-online-index-violation-detector
Follow the instructions in the README.md file to download and install Violation Detector using Maven
To start Violation Detector, go to the directory where you have built ViolationDetector.java and
enter the following command:
                                     accessKey
                                      = access_key_id_goes_here
                                     secretKey
                                      = secret_key_goes_here
S | N | B
S | N | B
                            detectionOutputPath
                            = //local/path/
                            filename.csv
                            detectionOutputPath =
                            s3://bucket/filename.csv
                                   correctionOutputPath
                                   = //local/path/
                                   filename.csv
                                   correctionOutputPath =
                                   s3://bucket/filename.csv
Detection
To detect index key violations, use Violation Detector with the --detect command line option. To show
how this option works, consider the ProductCatalog table shown in Creating Tables and Loading
Sample Data (p. 281). The following is a list of items in the table; only the primary key (Id) and the
Price attribute are shown.
101 -2
102 20
103 200
201 100
202 200
203 300
204 400
205 500
Note that all of the values for Price are of type Number. However, because DynamoDB is schemaless, it
is possible to add an item with a non-numeric Price. For example, suppose that we add another item to
the ProductCatalog table:
999 "Hello"
Now we will add a new global secondary index to the table: PriceIndex. The primary key for this index
is a partition key, Price, which is of type Number. After the index has been built, it will contain eight
items—but the ProductCatalog table has nine items. The reason for this discrepancy is that the value
"Hello" is of type String, but PriceIndex has a primary key of type Number. The String value violates
the global secondary index key, so it is not present in the index.
To use Violation Detector in this scenario, you first create a configuration file such as the following:
awsCredentialsFile = /home/alice/credentials.txt
dynamoDBRegion = us-west-2
tableName = ProductCatalog
gsiHashKeyName = Price
gsiHashKeyType = N
recordDetails = true
recordGsiValueInViolationRecord = true
detectionOutputPath = ./gsi_violation_check.csv
correctionInputPath = ./gsi_violation_check.csv
numOfSegments = 1
readWriteIOPSPercent = 40
Violation detection started: sequential scan, Table name: ProductCatalog, GSI name:
 PriceIndex
Progress: Items scanned in total: 9,    Items scanned by this thread: 9,    Violations
 found by this thread: 1, Violations deleted by this thread: 0
Violation detection finished: Records scanned: 9, Violations found: 1, Violations deleted:
 0, see results at: ./gsi_violation_check.csv
If the recordDetails config parameter is set to true, then Violation Detector writes details of each
violation to the output file, as in the following example:
Table Hash Key,GSI Hash Key Value,GSI Hash Key Violation Type,GSI Hash Key Violation
 Description,GSI Hash Key Update Value(FOR USER),Delete Blank Attributes When Updating?(Y/
N)
The output file is in comma-separated value format (CSV). The first line in the file is a header, followed
by one record per item that violates the index key. The fields of these violation records are as follows:
• Table Hash Key—the partition key value of the item in the table.
• Table Range Key—the sort key value of the item in the table.
• GSI Hash Key Value—the partition key value of the global secondary index
• GSI Hash Key Violation Type—either Type Violation or Size Violation.
    If either of these fields are non-blank, then Delete Blank Attribute When Updating(Y/N) has
    no effect.
     Note
     The output format might vary, depending on the configuration file and command line options.
     For example, if the table has a simple primary key (without a sort key), no sort key fields will be
     present in the output.
     The violation records in the file might not be in sorted order.
Correction
To correct index key violations, use Violation Detector with the --correct command line option.
In correction mode, Violation Detector reads the input file specified by the correctionInputPath
parameter. This file has the same format as the detectionOutputPath file, so that you can use the
output from detection as input for correction.
Violation Detector provides two different ways to correct index key violations:
• Delete violations—delete the table items that have violating attribute values.
• Update violations—update the table items, replacing the violating attributes with non-violating
  values.
In either case, you can use the output file from detection mode as input for correction mode.
Continuing with our ProductCatalog example, suppose that we want to delete the violating item from
the table. To do this, we use the following command line:
At this point, you are asked to confirm whether you want to delete the violating items.
Now both ProductCatalog and PriceIndex have the same number of items.
The following are the steps to create a table with a global secondary index, using the DynamoDB
document API.
  You must provide the table name, its primary key, and the provisioned throughput values. For the
  global secondary index, you must provide the index name, its provisioned throughput settings, the
  attribute definitions for the index sort key, the key schema for the index, and the attribute projection.
3. Call the createTable method by providing the request object as a parameter.
The following Java code snippet demonstrates the preceding steps. The snippet creates a table
(WeatherData) with a global secondary index (PrecipIndex). The index partition key is Date and its sort
key is Precipitation. All of the table attributes are projected into the index. Users can query this index to
obtain weather data for a particular date, optionally sorting the data by precipitation amount.
Note that since Precipitation is not a key attribute for the table, it is not required; however, WeatherData
items without Precipitation will not appear in PrecipIndex.
// Attribute definitions
ArrayList<AttributeDefinition> attributeDefinitions = new ArrayList<AttributeDefinition>();
attributeDefinitions.add(new AttributeDefinition()
    .withAttributeName("Location")
    .withAttributeType("S"));
attributeDefinitions.add(new AttributeDefinition()
    .withAttributeName("Date")
    .withAttributeType("S"));
attributeDefinitions.add(new AttributeDefinition()
    .withAttributeName("Precipitation")
    .withAttributeType("N"));
// PrecipIndex
GlobalSecondaryIndex precipIndex = new GlobalSecondaryIndex()
    .withIndexName("PrecipIndex")
    .withProvisionedThroughput(new ProvisionedThroughput()
        .withReadCapacityUnits((long) 10)
        .withWriteCapacityUnits((long) 1))
        .withProjection(new Projection().withProjectionType(ProjectionType.ALL));
indexKeySchema.add(new KeySchemaElement()
    .withAttributeName("Date")
    .withKeyType(KeyType.HASH)); //Partition key
indexKeySchema.add(new KeySchemaElement()
    .withAttributeName("Precipitation")
    .withKeyType(KeyType.RANGE)); //Sort key
precipIndex.setKeySchema(indexKeySchema);
You must wait until DynamoDB creates the table and sets the table status to ACTIVE. After that, you can
begin putting data items into the table.
The following are the steps to access global secondary index information a table.
Example
Iterator<GlobalSecondaryIndexDescription> gsiIter =
 tableDesc.getGlobalSecondaryIndexes().iterator();
while (gsiIter.hasNext()) {
    GlobalSecondaryIndexDescription gsiDesc = gsiIter.next();
    System.out.println("Info for index "
         + gsiDesc.getIndexName() + ":");
The following are the steps to query a global secondary index using the AWS SDK for Java Document
API.
The attribute name Date is a DynamoDB reserved word. Therefore, we must use an expression attribute
name as a placeholder in the KeyConditionExpression.
Example
Example: Global Secondary Indexes Using the AWS SDK for Java Document API
The following Java code example shows how to work with global secondary indexes. The example
creates a table named Issues, which might be used in a simple bug tracking system for software
development. The partition key is IssueId and the sort key is Title. There are three global secondary
indexes on this table:
• CreateDateIndex—the partition key is CreateDate and the sort key is IssueId. In addition to the
  table keys, the attributes Description and Status are projected into the index.
• TitleIndex—the partition key is IssueId and the sort key is Title. No attributes other than the table
  keys are projected into the index.
• DueDateIndex—the partition key is DueDate, and there is no sort key. All of the table attributes are
  projected into the index.
After the Issues table is created, the program loads the table with data representing software bug
reports, and then queries the data using the global secondary indexes. Finally, the program deletes the
Issues table.
For step-by-step instructions to test the following sample, see Java Code Samples (p. 286).
Example
import java.util.ArrayList;
import java.util.Iterator;
import   com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
import   com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder;
import   com.amazonaws.services.dynamodbv2.document.DynamoDB;
import   com.amazonaws.services.dynamodbv2.document.Index;
import   com.amazonaws.services.dynamodbv2.document.Item;
import   com.amazonaws.services.dynamodbv2.document.ItemCollection;
import   com.amazonaws.services.dynamodbv2.document.QueryOutcome;
import   com.amazonaws.services.dynamodbv2.document.Table;
import   com.amazonaws.services.dynamodbv2.document.spec.QuerySpec;
import   com.amazonaws.services.dynamodbv2.document.utils.ValueMap;
import   com.amazonaws.services.dynamodbv2.model.AttributeDefinition;
import   com.amazonaws.services.dynamodbv2.model.CreateTableRequest;
import   com.amazonaws.services.dynamodbv2.model.GlobalSecondaryIndex;
import   com.amazonaws.services.dynamodbv2.model.KeySchemaElement;
import   com.amazonaws.services.dynamodbv2.model.KeyType;
import   com.amazonaws.services.dynamodbv2.model.Projection;
import   com.amazonaws.services.dynamodbv2.model.ProvisionedThroughput;
       createTable();
       loadData();
       queryIndex("CreateDateIndex");
       queryIndex("TitleIndex");
       queryIndex("DueDateIndex");
deleteTable(tableName);
       // Attribute definitions
       ArrayList<AttributeDefinition> attributeDefinitions = new
ArrayList<AttributeDefinition>();
       attributeDefinitions.add(new
AttributeDefinition().withAttributeName("IssueId").withAttributeType("S"));
       attributeDefinitions.add(new
AttributeDefinition().withAttributeName("Title").withAttributeType("S"));
       attributeDefinitions.add(new
AttributeDefinition().withAttributeName("CreateDate").withAttributeType("S"));
       attributeDefinitions.add(new
AttributeDefinition().withAttributeName("DueDate").withAttributeType("S"));
               // key
       tableKeySchema.add(new
KeySchemaElement().withAttributeName("Title").withKeyType(KeyType.RANGE)); // Sort
// key
       // CreateDateIndex
       GlobalSecondaryIndex createDateIndex = new
GlobalSecondaryIndex().withIndexName("CreateDateIndex")
           .withProvisionedThroughput(ptIndex)
           .withKeySchema(new
KeySchemaElement().withAttributeName("CreateDate").withKeyType(KeyType.HASH), // Partition
                 // key
               new
KeySchemaElement().withAttributeName("IssueId").withKeyType(KeyType.RANGE)) // Sort
    // key
           .withProjection(
               new
Projection().withProjectionType("INCLUDE").withNonKeyAttributes("Description", "Status"));
       // TitleIndex
       GlobalSecondaryIndex titleIndex = new
GlobalSecondaryIndex().withIndexName("TitleIndex")
           .withProvisionedThroughput(ptIndex)
           .withKeySchema(new
KeySchemaElement().withAttributeName("Title").withKeyType(KeyType.HASH), // Partition
             // key
                new
 KeySchemaElement().withAttributeName("IssueId").withKeyType(KeyType.RANGE)) // Sort
     // key
              .withProjection(new Projection().withProjectionType("KEYS_ONLY"));
        // DueDateIndex
        GlobalSecondaryIndex dueDateIndex = new
 GlobalSecondaryIndex().withIndexName("DueDateIndex")
            .withProvisionedThroughput(ptIndex)
            .withKeySchema(new
 KeySchemaElement().withAttributeName("DueDate").withKeyType(KeyType.HASH)) // Partition
                 // key
              .withProjection(new Projection().withProjectionType("ALL"));
        System.out.println("\n***********************************************************
\n");
        System.out.print("Querying index " + indexName + "...");
        if (indexName == "CreateDateIndex") {
            System.out.println("Issues filed on 2013-11-01");
            querySpec.withKeyConditionExpression("CreateDate = :v_date and
 begins_with(IssueId, :v_issue)")
                .withValueMap(new ValueMap().withString(":v_date",
 "2013-11-01").withString(":v_issue", "A-"));
            items = index.query(querySpec);
        }
        else if (indexName == "TitleIndex") {
            System.out.println("Compilation errors");
            querySpec.withKeyConditionExpression("Title = :v_title and
 begins_with(IssueId, :v_issue)")
       while (iterator.hasNext()) {
           System.out.println(iterator.next().toJSONPretty());
       }
       //   IssueId, Title,
       //   Description,
       //   CreateDate, LastUpdateDate, DueDate,
       //   Priority, Status
       putItem("A-102", "Can't read data file", "The main data file is missing, or the
permissions are incorrect",
           "2013-11-01", "2013-11-04", "2013-11-30", 2, "In progress");
           table.putItem(item);
      }
You can use the AWS SDK for .NET low-level API to create a table with one or more global secondary
indexes, describe the indexes on the table, and perform queries using the indexes. These operations map
to the corresponding DynamoDB operations. For more information, see the Amazon DynamoDB API
Reference.
The following are the common steps for table operations using the .NET low-level API.
   For example, create a CreateTableRequest object to create a table and QueryRequest object to
   query a table or an index.
3. Execute the appropriate method provided by the client that you created in the preceding step.
The following are the steps to create a table with a global secondary index, using the .NET low-level API.
  You must provide the table name, its primary key, and the provisioned throughput values. For the
  global secondary index, you must provide the index name, its provisioned throughput settings, the
  attribute definitions for the index sort key, the key schema for the index, and the attribute projection.
3. Execute the CreateTable method by providing the request object as a parameter.
The following C# code snippet demonstrates the preceding steps. The snippet creates a table
(WeatherData) with a global secondary index (PrecipIndex). The index partition key is Date and its sort
key is Precipitation. All of the table attributes are projected into the index. Users can query this index to
obtain weather data for a particular date, optionally sorting the data by precipitation amount.
Note that since Precipitation is not a key attribute for the table, it is not required; however, WeatherData
items without Precipitation will not appear in PrecipIndex.
// Attribute definitions
var attributeDefinitions = new List<AttributeDefinition>()
{
    {new AttributeDefinition{
        AttributeName = "Location",
        AttributeType = "S"}},
    {new AttributeDefinition{
        AttributeName = "Date",
        AttributeType = "S"}},
    {new AttributeDefinition(){
        AttributeName = "Precipitation",
        AttributeType = "N"}
    }
};
// PrecipIndex
var precipIndex = new GlobalSecondaryIndex
{
    IndexName = "PrecipIndex",
    ProvisionedThroughput = new ProvisionedThroughput
    {
        ReadCapacityUnits = (long)10,
        WriteCapacityUnits = (long)1
    },
    Projection = new Projection { ProjectionType = "ALL" }
};
precipIndex.KeySchema = indexKeySchema;
You must wait until DynamoDB creates the table and sets the table status to ACTIVE. After that, you can
begin putting data items into the table.
The following are the steps to access global secondary index information a table using the .NET low-level
API.
Example
List<GlobalSecondaryIndexDescription> globalSecondaryIndexes =
response.DescribeTableResult.Table.GlobalSecondaryIndexes;
// This code snippet will work for multiple indexes, even though
// there is only one index in this example.
       if (projection.ProjectionType.ToString().Equals("INCLUDE")) {
            Console.WriteLine("\t\tThe non-key projected attributes are: "
                 + projection.NonKeyAttributes);
       }
}
The following are the steps to query a global secondary index using the .NET low-level API.
The attribute name Date is a DynamoDB reserved word. Therefore, we must use an expression attribute
name as a placeholder in the KeyConditionExpression.
Example
          Console.WriteLine(currentItem[attr].S);
    }
         }
     Console.WriteLine();
}
Example: Global Secondary Indexes Using the AWS SDK for .NET Low-Level API
The following C# code example shows how to work with global secondary indexes. The example creates
a table named Issues, which might be used in a simple bug tracking system for software development.
The partition key is IssueId and the sort key is Title. There are three global secondary indexes on this
table:
• CreateDateIndex—the partition key is CreateDate and the sort key is IssueId. In addition to the table
  keys, the attributes Description and Status are projected into the index.
• TitleIndex—the partition key is IssueId and the sort key is Title. No attributes other than the table keys
  are projected into the index.
• DueDateIndex—the partition key is DueDate, and there is no sort key. All of the table attributes are
  projected into the index.
After the Issues table is created, the program loads the table with data representing software bug
reports, and then queries the data using the global secondary indexes. Finally, the program deletes the
Issues table.
For step-by-step instructions to test the following sample, see .NET Code Samples (p. 288).
Example
using   System;
using   System.Collections.Generic;
using   System.Linq;
using   Amazon.DynamoDBv2;
using   Amazon.DynamoDBv2.DataModel;
using   Amazon.DynamoDBv2.DocumentModel;
using   Amazon.DynamoDBv2.Model;
using   Amazon.Runtime;
using   Amazon.SecurityToken;
namespace com.amazonaws.codesamples
{
    class LowLevelGlobalSecondaryIndexExample
    {
        private static AmazonDynamoDBClient client = new AmazonDynamoDBClient();
        public static String tableName = "Issues";
              QueryIndex("CreateDateIndex");
              QueryIndex("TitleIndex");
              QueryIndex("DueDateIndex");
DeleteTable(tableName);
     // CreateDateIndex
     var createDateIndex = new GlobalSecondaryIndex()
     {
         IndexName = "CreateDateIndex",
         ProvisionedThroughput = ptIndex,
         KeySchema = {
         new KeySchemaElement {
             AttributeName = "CreateDate", KeyType = "HASH" //Partition key
         },
         new KeySchemaElement {
             AttributeName = "IssueId", KeyType = "RANGE" //Sort key
         }
     },
         Projection = new Projection
         {
             ProjectionType = "INCLUDE",
             NonKeyAttributes = {
             "Description", "Status"
         }
         }
     };
// TitleIndex
    // DueDateIndex
    var dueDateIndex = new GlobalSecondaryIndex()
    {
        IndexName = "DueDateIndex",
        ProvisionedThroughput = ptIndex,
        KeySchema = {
        new KeySchemaElement {
            AttributeName = "DueDate",
            KeyType = "HASH" //Partition key
        }
    },
        Projection = new Projection
        {
            ProjectionType = "ALL"
        }
    };
    WaitUntilTableReady(tableName);
}
    //   IssueId, Title,
    //   Description,
    //   CreateDate, LastUpdateDate, DueDate,
    //   Priority, Status
                 S = status
           });
           try
           {
                 client.PutItem(new PutItemRequest
                 {
                     TableName = tableName,
                     Item = item
                 });
           }
           catch (Exception e)
           {
               Console.WriteLine(e.ToString());
           }
       }
           String keyConditionExpression;
           Dictionary<string, AttributeValue> expressionAttributeValues = new
Dictionary<string, AttributeValue>();
           if (indexName == "CreateDateIndex")
           {
               Console.WriteLine("Issues filed on 2013-11-01\n");
// Select
        queryRequest.Select = "ALL_PROJECTED_ATTRIBUTES";
    }
    else if (indexName == "DueDateIndex")
    {
        Console.WriteLine("Items that are due on 2013-11-30\n");
        // Select
        queryRequest.Select = "ALL_PROJECTED_ATTRIBUTES";
    }
    else
    {
        Console.WriteLine("\nNo valid index name provided");
        return;
    }
    queryRequest.KeyConditionExpression = keyConditionExpression;
    queryRequest.ExpressionAttributeValues = expressionAttributeValues;
                while (tablePresent)
                {
                    System.Threading.Thread.Sleep(5000); // Wait 5 seconds.
                    try
                    {
                        var res = client.DescribeTable(new DescribeTableRequest
                        {
                            TableName = tableName
                        });
Some applications only need to query data using the base table's primary key; however, there may be
situations where an alternate sort key would be helpful. To give your application a choice of sort keys,
you can create one or more local secondary indexes on a table and issue Query or Scan requests against
these indexes.
For example, consider the Thread table that is defined in Creating Tables and Loading Sample
Data (p. 281). This table is useful for an application such as the AWS Discussion Forums. The following
diagram shows how the items in the table would be organized. (Not all of the attributes are shown.)
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".
Some requests might require more complex data access patterns. For example:
To answer these questions, the Query action would not be sufficient. Instead, you would have to Scan
the entire table. For a table with millions of items, this would consume a large amount of provisioned
read throughput and take a long time to complete.
However, you can specify one or more local secondary indexes on non-key attributes, such as Replies or
LastPostDateTime.
A local secondary index maintains an alternate sort key for a given partition key value. A local secondary
index also contains a copy of some or all of the attributes from its base table; you specify which
attributes are projected into the local secondary index when you create the table. The data in a local
secondary index is organized by the same partition key as the base table, but with a different sort key.
This lets you access data items efficiently across this different dimension. For greater query or scan
flexibility, you can create up to five local secondary indexes per table.
Suppose that an application needs to find all of the threads that have been posted within the last three
months. Without a local secondary index, the application would have to Scan the entire Thread table
and discard any posts that were not within the specified time frame. With a local secondary index, a
Query operation could use LastPostDateTime as a sort key and find the data quickly.
The following diagram shows a local secondary index named LastPostIndex. Note that the partition key is
the same as that of the Thread table, but the sort key is LastPostDateTime.
In this example, the partition key is ForumName and the sort key of the local secondary index is
LastPostDateTime. In addition, the sort key value from the base table (in this example, Subject) is
projected into the index, but it is not a part of the index key. If an application needs a list that is based on
ForumName and LastPostDateTime, it can issue a Query request against LastPostIndex. The query results
are sorted by LastPostDateTime, and can be returned in ascending or descending order. The query can
also apply key conditions, such as returning only items that have a LastPostDateTime within a particular
time span.
Every local secondary index automatically contains the partition and sort keys from its base table; you
can optionally project non-key attributes into the index. When you query the index, DynamoDB can
retrieve these projected attributes efficiently. When you query a local secondary index, the query can
also retrieve attributes that are not projected into the index. DynamoDB will automatically fetch these
attributes from the base table, but at a greater latency and with higher provisioned throughput costs.
For any local secondary index, you can store up to 10 GB of data per distinct partition key value. This
figure includes all of the items in the base table, plus all of the items in the indexes, that have the same
partition key value. For more information, see Item Collections (p. 490).
Attribute Projections
With LastPostIndex, an application could use ForumName and LastPostDateTime as query criteria;
however, to retrieve any additional attributes, DynamoDB would need to perform additional read
operations against the Thread table. These extra reads are known as fetches, and they can increase the
total amount of provisioned throughput required for a query.
Suppose that you wanted to populate a web page with a list of all the threads in "S3" and the number
of replies for each thread, sorted by the last reply date/time beginning with the most recent reply. To
populate this list, you would need the following attributes:
• Subject
• Replies
• LastPostDateTime
The most efficient way to query this data, and to avoid fetch operations, would be to project the Replies
attribute from the table into the local secondary index, as shown in this diagram:
A projection is the set of attributes that is copied from a table into a secondary index. The partition key
and sort key of the table are always projected into the index; you can project other attributes to support
your application's query requirements. When you query an index, Amazon DynamoDB can access any
attribute in the projection as if those attributes were in a table of their own.
When you create a secondary index, you need to specify the attributes that will be projected into the
index. DynamoDB provides three different options for this:
• KEYS_ONLY – Each item in the index consists only of the table partition key and sort key values, plus
  the index key values. The KEYS_ONLY option results in the smallest possible secondary index.
• INCLUDE – In addition to the attributes described in KEYS_ONLY, the secondary index will include
  other non-key attributes that you specify.
• ALL – The secondary index includes all of the attributes from the source table. Because all of the table
  data is duplicated in the index, an ALL projection results in the largest possible secondary index.
In the previous diagram, the non-key attribute Replies is projected into LastPostIndex. An application can
query LastPostIndex instead of the full Thread table to populate a web page with Subject, Replies and
LastPostDateTime. If any other non-key attributes are requested, DynamoDB would need to fetch those
attributes from the Thread table.
From an application's point of view, fetching additional attributes from the base table is automatic and
transparent, so there is no need to rewrite any application logic. However, note that such fetching can
greatly reduce the performance advantage of using a local secondary index.
When you choose the attributes to project into a local secondary index, you must consider the tradeoff
between provisioned throughput costs and storage costs:
• If you need to access just a few attributes with the lowest possible latency, consider projecting only
  those attributes into a local secondary index. The smaller the index, the less that it will cost to store it,
  and the less your write costs will be. If there are attributes that you occasionally need to fetch, the cost
  for provisioned throughput may well outweigh the longer-term cost of storing those attributes.
• If your application will frequently access some non-key attributes, you should consider projecting
  those attributes into a local secondary index. The additional storage costs for the local secondary
  index will offset the cost of performing frequent table scans.
• If you need to access most of the non-key attributes on a frequent basis, you can project these
  attributes—or even the entire base table— into a local secondary index. This will give you maximum
  flexibility and lowest provisioned throughput consumption, because no fetching would be required;
  however, your storage cost would increase, or even double if you are projecting all attributes.
• If your application needs to query a table infrequently, but must perform many writes or updates
  against the data in the table, consider projecting KEYS_ONLY. The local secondary index would be of
  minimal size, but would still be available when needed for query activity.
You must specify one non-key attribute to act as the sort key of the local secondary index. The attribute
that you choose must be a scalar String, Number, or Binary; other scalar types, document types, and set
types are not allowed. For a complete list of data types, see Data Types (p. 12).
    Important
    For tables with local secondary indexes, there is a 10 GB size limit per partition key value. A
    table with local secondary indexes can store any number of items, as long as the total size for
    any one partition key value does not exceed 10 GB. For more information, see Item Collection
    Size Limit (p. 492).
You can project attributes of any data type into a local secondary index. This includes scalars, documents,
and sets. For a complete list of data types, see Data Types (p. 12).
You can query a local secondary index using either eventually consistent or strongly consistent reads. To
specify which type of consistency you want, use the ConsistentRead parameter of the Query operation. A
strongly consistent read from a local secondary index will always return the latest updated values. If the
query needs to fetch additional attributes from the base table, then those attributes will be consistent
with respect to the index.
Example
Consider the following data returned from a Query that requests data from the discussion threads in a
particular forum:
{
    "TableName": "Thread",
    "IndexName": "LastPostIndex",
    "ConsistentRead": false,
    "ProjectionExpression": "Subject, LastPostDateTime, Replies, Tags",
    "KeyConditionExpression":
        "ForumName = :v_forum and LastPostDateTime between :v_start and :v_end",
    "ExpressionAttributeValues": {
        ":v_start": {"S": "2015-08-31T00:00:00.000Z"},
        ":v_end": {"S": "2015-11-31T00:00:00.000Z"},
        ":v_forum": {"S": "EC2"}
    }
}
In this query:
• DynamoDB accesses LastPostIndex, using the ForumName partition key to locate the index items for
  "EC2". All of the index items with this key are stored adjacent to each other for rapid retrieval.
• Within this forum, DynamoDB uses the index to look up the keys that match the specified
  LastPostDateTime condition.
• Because the Replies attribute is projected into the index, DynamoDB can retrieve this attribute without
  consuming any additional provisioned throughput.
• The Tags attribute is not projected into the index, so DynamoDB must access the Thread table and
  fetch this attribute.
• The results are returned, sorted by LastPostDateTime. The index entries are sorted by partition key
  value and then by sort key value, and Query returns them in the order they are stored. (You can use
  the ScanIndexForward parameter to return the results in descending order.)
Because the Tags attribute is not projected into the local secondary index, DynamoDB must consume
additional read capacity units to fetch this attribute from the base table. If you need to run this query
often, you should project Tags into LastPostIndex to avoid fetching from the base table; however, if you
needed to access Tags only occasionally, then the additional storage cost for projecting Tags into the
index might not be worthwhile.
When you create a local secondary index, you specify an attribute to serve as the sort key for the index.
You also specify a data type for that attribute. This means that whenever you write an item to the base
table, if the item defines an index key attribute, its type must match the index key schema's data type. In
the case of LastPostIndex, the LastPostDateTime sort key in the index is defined as a String data type. If
you attempt to add an item to the Thread table and specify a different data type for LastPostDateTime
(such as Number), DynamoDB will return a ValidationException because of the data type mismatch.
If you write an item to a table, you don't have to specify the attributes for any local secondary index sort
key. Using LastPostIndex as an example, you would not need to specify a value for the LastPostDateTime
attribute in order to write a new item to the Thread table. In this case, DynamoDB does not write any
data to the index for this particular item.
There is no requirement for a one-to-one relationship between the items in a base table and the items
in a local secondary index; in fact, this behavior can be advantageous for many applications. For more
information, see Take Advantage of Sparse Indexes (p. 721).
A table with many local secondary indexes will incur higher costs for write activity than tables with
fewer indexes. For more information, see Provisioned Throughput Considerations for Local Secondary
Indexes (p. 488).
    Important
    For tables with local secondary indexes, there is a 10 GB size limit per partition key value. A
    table with local secondary indexes can store any number of items, as long as the total size for
    any one partition key value does not exceed 10 GB. For more information, see Item Collection
    Size Limit (p. 492).
As with table queries, an index query can use either eventually consistent or strongly consistent reads
depending on the value of ConsistentRead. One strongly consistent read consumes one read capacity
unit; an eventually consistent read consumes only half of that. Thus, by choosing eventually consistent
reads, you can reduce your read capacity unit charges.
For index queries that request only index keys and projected attributes, DynamoDB calculates the
provisioned read activity in the same way as it does for queries against tables. The only difference is
that the calculation is based on the sizes of the index entries, rather than the size of the item in the
base table. The number of read capacity units is the sum of all projected attribute sizes across all of
the items returned; the result is then rounded up to the next 4 KB boundary. For more information
on how DynamoDB calculates provisioned throughput usage, see Throughput Settings for Reads and
Writes (p. 294).
For index queries that read attributes that are not projected into the local secondary index, DynamoDB
will need to fetch those attributes from the base table, in addition to reading the projected attributes
from the index. These fetches occur when you include any non-projected attributes in the Select or
ProjectionExpression parameters of the Query operation. Fetching causes additional latency in
query responses, and it also incurs a higher provisioned throughput cost: In addition to the reads from
the local secondary index described above, you are charged for read capacity units for every base table
item fetched. This charge is for reading each entire item from the table, not just the requested attributes.
The maximum size of the results returned by a Query operation is 1 MB; this includes the sizes of all
the attribute names and values across all of the items returned. However, if a Query against a local
secondary index causes DynamoDB to fetch item attributes from the base table, the maximum size of the
data in the results might be lower. In this case, the result size is the sum of:
• The size of the matching items in the index, rounded up to the next 4 KB.
• The size of each matching item in the base table, with each item individually rounded up to the next 4
  KB.
Using this formula, the maximum size of the results returned by a Query operation is still 1 MB.
For example, consider a table where the size of each item is 300 bytes. There is a local secondary index
on that table, but only 200 bytes of each item is projected into the index. Now suppose that you Query
this index, that the query requires table fetches for each item, and that the query returns 4 items.
DynamoDB sums up the following:
• The size of the matching items in the index: 200 bytes × 4 items = 800 bytes; this is then rounded up
  to 4 KB.
• The size of each matching item in the base table: (300 bytes, rounded up to 4 KB) × 4 items = 16 KB.
The cost of writing an item to a local secondary index depends on several factors:
• If you write a new item to the table that defines an indexed attribute, or you update an existing item
  to define a previously undefined indexed attribute, one write operation is required to put the item into
  the index.
• If an update to the table changes the value of an indexed key attribute (from A to B), two writes are
  required, one to delete the previous item from the index and another write to put the new item into
  the index. 
• If an item was present in the index, but a write to the table caused the indexed attribute to be deleted,
  one write is required to delete the old item projection from the index.
• If an item is not present in the index before or after the item is updated, there is no additional write
  cost for the index.
All of these factors assume that the size of each item in the index is less than or equal to the 1 KB item
size for calculating write capacity units. Larger index entries will require additional write capacity units.
You can minimize your write costs by considering which attributes your queries will need to return and
projecting only those attributes into the index.
The amount of space used by an index item is the sum of the following:
• The size in bytes of the base table primary key (partition key and sort key)
• The size in bytes of the index key attribute
• The size in bytes of the projected attributes (if any)
• 100 bytes of overhead per index item
To estimate the storage requirements for a local secondary index, you can estimate the average size of
an item in the index and then multiply by the number of items in the base table.
If a table contains an item where a particular attribute is not defined, but that attribute is defined as an
index sort key, then DynamoDB does not write any data for that item to the index. For more information
about this behavior, see Take Advantage of Sparse Indexes (p. 721).
Item Collections
    Note
    The following section pertains only to tables that have local secondary indexes.
In DynamoDB, an item collection is any group of items that have the same partition key value in a table
and all of its local secondary indexes. In the examples used throughout this section, the partition key
for the Thread table is ForumName, and the partition key for LastPostIndex is also ForumName. All the
table and index items with the same ForumName are part of the same item collection. For example, in
the Thread table and the LastPostIndex local secondary index, there is an item collection for forum EC2
and a different item collection for forum RDS.
The following diagram shows the item collection for forum S3:
In this diagram, the item collection consists of all the items in Thread and LastPostIndex where the
ForumName partition key value is "S3". If there were other local secondary indexes on the table, then any
items in those indexes with ForumName equal to "S3" would also be part of the item collection.
You can use any of the following operations in DynamoDB to return information about item collections:
•   BatchWriteItem
•   DeleteItem
•   PutItem
•   UpdateItem
Each of these operations support the ReturnItemCollectionMetrics parameter. When you set this
parameter to SIZE, you can view information about the size of each item collection in the index.
Example
Here is a snippet from the output of an UpdateItem operation on the Thread table, with
ReturnItemCollectionMetrics set to SIZE. The item that was updated had a ForumName value of
"EC2", so the output includes information about that item collection.
{
    ItemCollectionMetrics: {
        ItemCollectionKey: {
            ForumName: "EC2"
        },
        SizeEstimateRangeGB: [0.0, 1.0]
    }
}
The SizeEstimateRangeGB object shows that the size of this item collection is between 0 and 1
gigabyte. DynamoDB periodically updates this size estimate, so the numbers might be different next
time the item is modified.
To reduce the size of an item collection, you can do one of the following:
• Delete any unnecessary items with the partition key value in question. When you delete these items
  from the base table, DynamoDB will also remove any index entries that have the same partition key
  value.
• Update the items by removing attributes or by reducing the size of the attributes. If these
  attributes are projected into any local secondary indexes, DynamoDB will also reduce the size of the
  corresponding index entries.
• Create a new table with the same partition key and sort key, and then move items from the old table
  to the new table. This might be a good approach if a table has historical data that is infrequently
  accessed. You might also consider archiving this historical data to Amazon Simple Storage Service
  (Amazon S3).
When the total size of the item collection drops below 10 GB, you will once again be able to add items
with the same partition key value.
We recommend as a best practice that you instrument your application to monitor the sizes of your
item collections. One way to do so is to set the ReturnItemCollectionMetrics parameter to
SIZE whenever you use BatchWriteItem, DeleteItem, PutItem or UpdateItem. Your application
should examine the ReturnItemCollectionMetrics object in the output and log an error message
whenever an item collection exceeds a user-defined limit (8 GB, for example). Setting a limit that is less
than 10 GB would provide an early warning system so you know that an item collection is approaching
the limit in time to do something about it.
stored in the same partition. The "S3" item collection would be stored on one partition, "EC2" in another
partition, and "RDS" in a third partition.
You should design your applications so that table data is evenly distributed across distinct partition key
values. For tables with local secondary indexes, your applications should not create "hot spots" of read
and write activity within a single item collection on a single partition. For more information see Best
Practices for Tables (p. 704).
You can use the AWS SDK for Java Document API to create a table with one or more local secondary
indexes, describe the indexes on the table, and perform queries using the indexes.
The following are the common steps for table operations using the AWS SDK for Java Document API.
The following are the steps to create a table with a local secondary index, using the DynamoDB
document API.
  You must provide the table name, its primary key, and the provisioned throughput values. For the
  local secondary index, you must provide the index name, the name and data type for the index sort
  key, the key schema for the index, and the attribute projection.
3. Call the createTable method by providing the request object as a parameter.
The following Java code snippet demonstrates the preceding steps. The snippet creates a table (Music)
with a secondary index on the AlbumTitle attribute. The table partition key and sort key, plus the index
sort key, are the only attributes projected into the index.
//ProvisionedThroughput
createTableRequest.setProvisionedThroughput(new
 ProvisionedThroughput().withReadCapacityUnits((long)5).withWriteCapacityUnits((long)5));
//AttributeDefinitions
ArrayList<AttributeDefinition> attributeDefinitions= new ArrayList<AttributeDefinition>();
attributeDefinitions.add(new
 AttributeDefinition().withAttributeName("Artist").withAttributeType("S"));
attributeDefinitions.add(new
 AttributeDefinition().withAttributeName("SongTitle").withAttributeType("S"));
attributeDefinitions.add(new
 AttributeDefinition().withAttributeName("AlbumTitle").withAttributeType("S"));
createTableRequest.setAttributeDefinitions(attributeDefinitions);
//KeySchema
ArrayList<KeySchemaElement> tableKeySchema = new ArrayList<KeySchemaElement>();
tableKeySchema.add(new
 KeySchemaElement().withAttributeName("Artist").withKeyType(KeyType.HASH)); //Partition
 key
tableKeySchema.add(new
 KeySchemaElement().withAttributeName("SongTitle").withKeyType(KeyType.RANGE)); //Sort key
createTableRequest.setKeySchema(tableKeySchema);
.withIndexName("AlbumTitleIndex").withKeySchema(indexKeySchema).withProjection(projection);
You must wait until DynamoDB creates the table and sets the table status to ACTIVE. After that, you can
begin putting data items into the table.
The following are the steps to access local secondary index information a table using the AWS SDK for
Java Document API
Example
List<LocalSecondaryIndexDescription> localSecondaryIndexes
    = tableDescription.getLocalSecondaryIndexes();
// This code snippet will work for multiple indexes, even though
// there is only one index in this example.
The only attributes returned are those that have been projected into the index. You could modify this
query to select non-key attributes too, but this would require table fetch activity that is relatively
expensive. For more information about table fetches, see Attribute Projections (p. 485).
The following are the steps to query a local secondary index using the AWS SDK for Java Document API.
Example
while (itemsIter.hasNext()) {
    Item item = itemsIter.next();
    System.out.println(item.toJSONPretty());
}
• OrderCreationDateIndex—the sort key is OrderCreationDate, and the following attributes are projected
  into the index:
  • ProductCategory
  • ProductName
  • OrderStatus
  • ShipmentTrackingId
• IsOpenIndex—the sort key is IsOpen, and all of the table attributes are projected into the index.
After the CustomerOrders table is created, the program loads the table with data representing customer
orders, and then queries the data using the local secondary indexes. Finally, the program deletes the
CustomerOrders table.
For step-by-step instructions to test the following sample, see Java Code Samples (p. 286).
Example
import java.util.ArrayList;
import java.util.Iterator;
import   com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
import   com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder;
import   com.amazonaws.services.dynamodbv2.document.DynamoDB;
import   com.amazonaws.services.dynamodbv2.document.Index;
import   com.amazonaws.services.dynamodbv2.document.Item;
import   com.amazonaws.services.dynamodbv2.document.ItemCollection;
import   com.amazonaws.services.dynamodbv2.document.PutItemOutcome;
import   com.amazonaws.services.dynamodbv2.document.QueryOutcome;
import   com.amazonaws.services.dynamodbv2.document.Table;
import   com.amazonaws.services.dynamodbv2.document.spec.QuerySpec;
import   com.amazonaws.services.dynamodbv2.document.utils.ValueMap;
import   com.amazonaws.services.dynamodbv2.model.AttributeDefinition;
import   com.amazonaws.services.dynamodbv2.model.CreateTableRequest;
import   com.amazonaws.services.dynamodbv2.model.KeySchemaElement;
import   com.amazonaws.services.dynamodbv2.model.KeyType;
import   com.amazonaws.services.dynamodbv2.model.LocalSecondaryIndex;
import   com.amazonaws.services.dynamodbv2.model.Projection;
import   com.amazonaws.services.dynamodbv2.model.ProjectionType;
import   com.amazonaws.services.dynamodbv2.model.ProvisionedThroughput;
import   com.amazonaws.services.dynamodbv2.model.ReturnConsumedCapacity;
import   com.amazonaws.services.dynamodbv2.model.Select;
          createTable();
          loadData();
          query(null);
          query("IsOpenIndex");
          query("OrderCreationDateIndex");
deleteTable(tableName);
createTableRequest.setAttributeDefinitions(attributeDefinitions);
                  // key
       tableKeySchema.add(new
KeySchemaElement().withAttributeName("OrderId").withKeyType(KeyType.RANGE)); // Sort
// key
createTableRequest.setKeySchema(tableKeySchema);
       // OrderCreationDateIndex
       LocalSecondaryIndex orderCreationDateIndex = new
LocalSecondaryIndex().withIndexName("OrderCreationDateIndex");
                  // key
       indexKeySchema.add(new
KeySchemaElement().withAttributeName("OrderCreationDate").withKeyType(KeyType.RANGE)); //
Sort
// key
orderCreationDateIndex.setKeySchema(indexKeySchema);
orderCreationDateIndex.setProjection(projection);
localSecondaryIndexes.add(orderCreationDateIndex);
       // IsOpenIndex
       LocalSecondaryIndex isOpenIndex = new
LocalSecondaryIndex().withIndexName("IsOpenIndex");
                  // key
       indexKeySchema.add(new
KeySchemaElement().withAttributeName("IsOpen").withKeyType(KeyType.RANGE)); // Sort
// key
       isOpenIndex.setKeySchema(indexKeySchema);
       isOpenIndex.setProjection(projection);
localSecondaryIndexes.add(isOpenIndex);
        System.out.println("\n***********************************************************
\n");
        System.out.println("Querying table " + tableName + "...");
if (indexName == "IsOpenIndex") {
            querySpec.withProjectionExpression("OrderCreationDate, ProductCategory,
 ProductName, OrderStatus");
            while (iterator.hasNext()) {
                System.out.println(iterator.next().toJSONPretty());
            }
        }
        else if (indexName == "OrderCreationDateIndex") {
            System.out.println("\nUsing index: '" + indexName + "': Bob's orders that were
 placed after 01/31/2015.");
            System.out.println("Only the projected attributes are returned\n");
            Index index = table.getIndex(indexName);
                   new ValueMap().withString(":v_custid",
"bob@example.com").withNumber(":v_orddate", 20150131));
querySpec.withSelect(Select.ALL_PROJECTED_ATTRIBUTES);
           while (iterator.hasNext()) {
               System.out.println(iterator.next().toJSONPretty());
           }
       }
       else {
           System.out.println("\nNo index: All of Bob's orders, by OrderId:\n");
           querySpec.withKeyConditionExpression("CustomerId = :v_custid")
               .withValueMap(new ValueMap().withString(":v_custid", "bob@example.com"));
           while (iterator.hasNext()) {
               System.out.println(iterator.next().toJSONPretty());
           }
putItemOutcome = table.putItem(item);
putItemOutcome = table.putItem(item);
putItemOutcome = table.putItem(item);
putItemOutcome = table.putItem(item);
putItemOutcome = table.putItem(item);
putItemOutcome = table.putItem(item);
               .withNumber("OrderCreationDate", 20150309).withString("ProductCategory",
 "Book")
               .withString("ProductName", "How To Cook").withString("OrderStatus", "IN
 TRANSIT")
               .withString("ShipmentTrackingId", "440185");
putItemOutcome = table.putItem(item);
putItemOutcome = table.putItem(item);
           putItemOutcome = table.putItem(item);
           assert putItemOutcome != null;
      }
You can use the AWS SDK for .NET low-level API to create a table with one or more local secondary
indexes, describe the indexes on the table, and perform queries using the indexes. These operations
map to the corresponding low-level DynamoDB API actions. For more information, see .NET Code
Samples (p. 288).
The following are the common steps for table operations using the .NET low-level API.
    For example, create a CreateTableRequest object to create a table and an QueryRequest object
    to query a table or an index.
3. Execute the appropriate method provided by the client that you created in the preceding step.
The following are the steps to create a table with a local secondary index, using the .NET low-level API.
  You must provide the table name, its primary key, and the provisioned throughput values. For the
  local secondary index, you must provide the index name, the name and data type of the index sort
  key, the key schema for the index, and the attribute projection.
3. Execute the CreateTable method by providing the request object as a parameter.
The following C# code snippet demonstrates the preceding steps. The snippet creates a table (Music)
with a secondary index on the AlbumTitle attribute. The table partition key and sort key, plus the index
sort key, are the only attributes projected into the index.
//ProvisionedThroughput
createTableRequest.ProvisionedThroughput = new ProvisionedThroughput()
{
    ReadCapacityUnits = (long)5,
    WriteCapacityUnits = (long)5
};
//AttributeDefinitions
List<AttributeDefinition> attributeDefinitions = new List<AttributeDefinition>();
attributeDefinitions.Add(new AttributeDefinition()
{
    AttributeName = "Artist",
    AttributeType = "S"
});
attributeDefinitions.Add(new AttributeDefinition()
 {
     AttributeName = "SongTitle",
     AttributeType = "S"
 });
attributeDefinitions.Add(new AttributeDefinition()
 {
     AttributeName = "AlbumTitle",
     AttributeType = "S"
 });
createTableRequest.AttributeDefinitions = attributeDefinitions;
//KeySchema
List<KeySchemaElement> tableKeySchema = new List<KeySchemaElement>();
createTableRequest.KeySchema = tableKeySchema;
You must wait until DynamoDB creates the table and sets the table status to ACTIVE. After that, you can
begin putting data items into the table.
The following are the steps to access local secondary index information a table using the .NET low-level
API.
Example
response.DescribeTableResult.Table.LocalSecondaryIndexes;
// This code snippet will work for multiple indexes, even though
// there is only one index in this example.
foreach (LocalSecondaryIndexDescription lsiDescription in localSecondaryIndexes)
{
    Console.WriteLine("Info for index " + lsiDescription.IndexName + ":");
    if (projection.ProjectionType.ToString().Equals("INCLUDE"))
    {
        Console.WriteLine("\t\tThe non-key projected attributes are:");
    }
}
The only attributes returned are those that have been projected into the index. You could modify this
query to select non-key attributes too, but this would require table fetch activity that is relatively
expensive. For more information about table fetches, see Attribute Projections (p. 485)
The following are the steps to query a local secondary index using the .NET low-level API.
Example
};
Example: Local Secondary Indexes Using the AWS SDK for .NET Low-Level API
The following C# code example shows how to work with local secondary indexes. The example creates a
table named CustomerOrders with a partition key of CustomerId and a sort key of OrderId. There are two
local secondary indexes on this table:
• OrderCreationDateIndex—the sort key is OrderCreationDate, and the following attributes are projected
  into the index:
  • ProductCategory
  • ProductName
  • OrderStatus
  • ShipmentTrackingId
• IsOpenIndex—the sort key is IsOpen, and all of the table attributes are projected into the index.
After the CustomerOrders table is created, the program loads the table with data representing customer
orders, and then queries the data using the local secondary indexes. Finally, the program deletes the
CustomerOrders table.
For step-by-step instructions to test the following sample, see .NET Code Samples (p. 288).
Example
using   System;
using   System.Collections.Generic;
using   System.Linq;
using   Amazon.DynamoDBv2;
using   Amazon.DynamoDBv2.DataModel;
using   Amazon.DynamoDBv2.DocumentModel;
using   Amazon.DynamoDBv2.Model;
using   Amazon.Runtime;
using   Amazon.SecurityToken;
namespace com.amazonaws.codesamples
{
    class LowLevelLocalSecondaryIndexExample
    {
        private static AmazonDynamoDBClient client = new AmazonDynamoDBClient();
        private static string tableName = "CustomerOrders";
Query(null);
         Query("IsOpenIndex");
         Query("OrderCreationDateIndex");
DeleteTable(tableName);
createTableRequest.AttributeDefinitions = attributeDefinitions;
createTableRequest.KeySchema = tableKeySchema;
     // OrderCreationDateIndex
     LocalSecondaryIndex orderCreationDateIndex = new LocalSecondaryIndex()
     {
         IndexName = "OrderCreationDateIndex"
};
orderCreationDateIndex.KeySchema = indexKeySchema;
orderCreationDateIndex.Projection = projection;
localSecondaryIndexes.Add(orderCreationDateIndex);
     // IsOpenIndex
     LocalSecondaryIndex isOpenIndex
         = new LocalSecondaryIndex()
         {
             IndexName = "IsOpenIndex"
         };
     isOpenIndex.KeySchema = indexKeySchema;
     isOpenIndex.Projection = projection;
localSecondaryIndexes.Add(isOpenIndex);
            client.CreateTable(createTableRequest);
            WaitUntilTableReady(tableName);
        }
 Console.WriteLine("\n***********************************************************\n");
            Console.WriteLine("Querying table " + tableName + "...");
            if (indexName == "IsOpenIndex")
            {
                Console.WriteLine("\nUsing index: '" + indexName
                          + "': Bob's orders that are open.");
                Console.WriteLine("Only a user-specified list of attributes are returned
\n");
                queryRequest.IndexName = indexName;
                // ProjectionExpression
                queryRequest.ProjectionExpression = "OrderCreationDate, ProductCategory,
 ProductName, OrderStatus";
            }
            else if (indexName == "OrderCreationDateIndex")
            {
                Console.WriteLine("\nUsing index: '" + indexName
                          + "': Bob's orders that were placed after 01/31/2013.");
                Console.WriteLine("Only the projected attributes are returned\n");
                queryRequest.IndexName = indexName;
                // Select
                queryRequest.Select = "ALL_PROJECTED_ATTRIBUTES";
            }
            else
            {
                Console.WriteLine("\nNo index: All of Bob's orders, by OrderId:\n");
            }
           queryRequest.KeyConditionExpression = keyConditionExpression;
           queryRequest.ExpressionAttributeValues = expressionAttributeValues;
{
    S = "Music"
};
item["ProductName"] = new AttributeValue
{
    S = "A Quiet Interlude"
};
item["OrderStatus"] = new AttributeValue
{
    S = "IN TRANSIT"
};
item["ShipmentTrackingId"] = new AttributeValue
{
    S = "176493"
};
putItemRequest = new PutItemRequest
{
    TableName = tableName,
    Item = item,
    ReturnItemCollectionMetrics = "SIZE"
};
client.PutItem(putItemRequest);
    Item = item,
    ReturnItemCollectionMetrics = "SIZE"
};
client.PutItem(putItemRequest);
{
    S = "IN TRANSIT"
};
item["ShipmentTrackingId"] = new AttributeValue
{
    S = "440185"
};
putItemRequest = new PutItemRequest
{
    TableName = tableName,
    Item = item,
    ReturnItemCollectionMetrics = "SIZE"
};
client.PutItem(putItemRequest);
    };
    item["ProductCategory"] = new AttributeValue
    {
        S = "Golf"
    };
    item["ProductName"] = new AttributeValue
    {
        S = "PGA Pro II"
    };
    item["OrderStatus"] = new AttributeValue
    {
        S = "OUT FOR DELIVERY"
    };
    item["ShipmentTrackingId"] = new AttributeValue
    {
        S = "383283"
    };
    putItemRequest = new PutItemRequest
    {
        TableName = tableName,
        Item = item,
        ReturnItemCollectionMetrics = "SIZE"
    };
    client.PutItem(putItemRequest);
}
    while (tablePresent)
    {
        System.Threading.Thread.Sleep(5000); // Wait 5 seconds.
        try
        {
            var res = client.DescribeTable(new DescribeTableRequest
            {
                TableName = tableName
            });
    • An application in one AWS region modifies the data in a DynamoDB table. A second application in
      another AWS region reads these data modifications and writes the data to another table, creating a
      replica that stays in sync with the original table.
    • A popular mobile app modifies data in a DynamoDB table, at the rate of thousands of updates per
      second. Another application captures and stores data about these updates, providing near real time
      usage metrics for the mobile app.
    • A global multi-player game has a multi-master topology, storing data in multiple AWS regions. Each
      master stays in sync by consuming and replaying the changes that occur in the remote regions.
    • An application automatically sends notifications to the mobile devices of all friends in a group as soon
      as one friend uploads a new picture.
    • A new customer adds data to a DynamoDB table. This event invokes another application that sends a
      welcome email to the new customer.
    DynamoDB Streams enables solutions such as these, and many others. DynamoDB Streams captures a
    time-ordered sequence of item-level modifications in any DynamoDB table, and stores this information
    in a log for up to 24 hours. Applications can access this log and view the data items as they appeared
    before and after they were modified, in near real time.
    A DynamoDB stream is an ordered flow of information about changes to items in an Amazon DynamoDB
    table. When you enable a stream on a table, DynamoDB captures information about every modification
    to data items in the table.
    Whenever an application creates, updates, or deletes items in the table, DynamoDB Streams writes a
    stream record with the primary key attribute(s) of the items that were modified. A stream record contains
    information about a data modification to a single item in a DynamoDB table. You can configure the
    stream so that the stream records capture additional information, such as the "before" and "after"
    images of modified items.
    DynamoDB Streams writes stream records in near real time, so that you can build applications that
    consume these streams and take action based on the contents.
AWS maintains separate endpoints for DynamoDB and DynamoDB Streams. To work with database
tables and indexes, your application will need to access a DynamoDB endpoint. To read and process
DynamoDB Streams records, your application will need to access a DynamoDB Streams endpoint in the
same region.
The AWS SDKs provide separate clients for DynamoDB and DynamoDB Streams. Depending on your
requirements, your application can access a DynamoDB endpoint, a DynamoDB Streams endpoint, or
both at the same time. To connect to both endpoints, your application will need to instantiate two
clients - one for DynamoDB, and one for DynamoDB Streams.
Enabling a Stream
You can enable a stream on a new table when you create it. You can also enable or disable a stream on
an existing table, or change the settings of a stream. DynamoDB Streams operates asynchronously, so
there is no performance impact on a table if you enable a stream.
The easiest way to manage DynamoDB Streams is by using the AWS Management Console.
You can also use the CreateTable or UpdateTable APIs to enable or modify a stream. The
StreamSpecification parameter determines how the stream is configured:
• StreamEnabled—specifies whether a stream is enabled (true) or disabled (false) for the table.
• StreamViewType—specifies the information that will be written to the stream whenever data in the
  table is modified:
     • KEYS_ONLY—only the key attributes of the modified item.
     • NEW_IMAGE—the entire item, as it appears after it was modified.
     • OLD_IMAGE—the entire item, as it appeared before it was modified.
     • NEW_AND_OLD_IMAGES—both the new and the old images of the item.
You can enable or disable a stream at any time. However, note that you will receive a
ResourceInUseException if you attempt to enable a stream on a table that already has a stream, and you
will receive a ValidationException if you attempt to disable a stream on a table which does not have a
stream.
When you set StreamEnabled to true, DynamoDB creates a new stream with a unique stream
descriptor assigned to it. If you disable and then re-enable a stream on the table, a new stream will be
created with a different stream descriptor.
Every stream is uniquely identified by an Amazon Resource Name (ARN). Here is an example ARN for a
stream on a DynamoDB table named TestTable:
arn:aws:dynamodb:us-west-2:111122223333:table/TestTable/stream/2015-05-11T21:21:33.291
To determine the latest stream descriptor for a table, issue a DynamoDB DescribeTable request and
look for the LatestStreamArn element in the response.
A stream consists of stream records. Each stream record represents a single data modification in the
DynamoDB table to which the stream belongs. Each stream record is assigned a sequence number,
reflecting the order in which the record was published to the stream.
                                    API Version 2012-08-10
                                              519
                              Amazon DynamoDB Developer Guide
                               Reading and Processing a Stream
Stream records are organized into groups, or shards. Each shard acts as a container for multiple stream
records, and contains information required for accessing and iterating through these records. The stream
records within a shard are removed automatically after 24 hours.
Shards are ephemeral: They are created and deleted automatically, as needed. Any shard can also split
into multiple new shards; this also occurs automatically. (Note that it is also possible for a parent shard
to have just one child shard.) A shard might split in response to high levels of write activity on its parent
table, so that applications can process records from multiple shards in parallel.
If you disable a stream, any shards that are open will be closed.
Because shards have a lineage (parent and children), applications must always process a parent shard
before it processes a child shard. This will ensure that the stream records are also processed in the correct
order. (If you use the DynamoDB Streams Kinesis Adapter, this is handled for you: Your application will
process the shards and stream records in the correct order, and automatically handle new or expired
shards, as well as shards that split while the application is running. For more information, see Using the
DynamoDB Streams Kinesis Adapter to Process Stream Records (p. 522).)
The following diagram shows the relationship between a stream, shards in the stream, and stream
records in the shards.
    Note
    If you perform a PutItem or UpdateItem operation that does not change any data in an item,
    then DynamoDB Streams will not write a stream record for that operation.
To access a stream and process the stream records within, you must do the following:
• Determine the unique Amazon Resource Name (ARN) of the stream that you want to access.
• Determine which shard(s) in the stream contain the stream records that you are interested in.
• Access the shard(s) and retrieve the stream records that you want.
    Note
    No more than 2 processes at most should be reading from the same Streams shard at the same
    time. Having more than 2 readers per shard may result in throttling.
The DynamoDB Streams API provides the following actions for use by application programs:
• ListStreams—returns a list of stream descriptors for the current account and endpoint. You can
  optionally request just the stream descriptors for a particular table name.
• DescribeStream—returns detailed information about a given stream. The output includes a list of
  shards associated with the stream, including the shard IDs.
• GetShardIterator—returns a shard iterator, which describes a location within a shard. You can
  request that the iterator provide access to the oldest point, the newest point, or a particular point in
  the stream.
• GetRecords—returns the stream records from within a given shard. You must provide the shard
  iterator returned from a GetShardIterator request.
For complete descriptions of these API actions, including example requests and responses, go to the
Amazon DynamoDB Streams API Reference.
If you disable a stream on a table, the data in the stream will continue to be readable for 24 hours.
After this time, the data expires and the stream records are automatically deleted. Note that there is
no mechanism for manually deleting an existing stream; you just need to wait until the retention limit
expires (24 hours), and all the stream records will be deleted.
Items that are deleted by the Time To Live process after expiration have the following fields:
• Records[<index>].userIdentity.type
  "Service"
• Records[<index>].userIdentity.principalId
"dynamodb.amazonaws.com"
The following JSON shows the relevant portion of a single Streams record.
"Records":[
    {
        ...
         "userIdentity":{
             "type":"Service",
             "principalId":"dynamodb.amazonaws.com"
         }
...
]}
Items deleted by other users will show the principalId of the account used to delete the items.
The DynamoDB Streams API is intentionally similar to that of Kinesis Streams, a service for real-time
processing of streaming data at massive scale. In both services, data streams are composed of shards,
which are containers for stream records. Both services' APIs contain ListStreams, DescribeStream,
GetShards, and GetShardIterator actions. (Even though these DynamoDB Streams actions are
similar to their counterparts in Kinesis Streams, they are not 100% identical.)
You can write applications for Kinesis Streams using the Kinesis Client Library (KCL). The KCL simplifies
coding by providing useful abstractions above the low-level Kinesis Streams API. For more information
on the KCL, go to the Amazon Kinesis Developer Guide.
As a DynamoDB Streams user, you can leverage the design patterns found within the KCL to process
DynamoDB Streams shards and stream records. To do this, you use the DynamoDB Streams Kinesis
Adapter. The Kinesis Adapter implements the Kinesis Streams interface, so that the KCL can be used for
consuming and processing records from DynamoDB Streams.
The following diagram shows how these libraries interact with one another.
With the DynamoDB Streams Kinesis Adapter in place, you can begin developing against the KCL
interface, with the API calls seamlessly directed at the DynamoDB Streams endpoint.
When your application starts, it calls the KCL to instantiate a worker. You must provide the worker with
configuration information for the application, such as the stream descriptor and AWS credentials, and
the name of a record processor class that you provide. As it runs the code in the record processor, the
worker performs the following tasks:
    Note
    For a description of the KCL concepts listed above, go to Developing Amazon Kinesis Consumers
    Using the Amazon Kinesis Client Library in the Amazon Kinesis Developer Guide.
1. Creates two DynamoDB tables named KCL-Demo-src and KCL-Demo-dst. Each of these tables has a
   stream enabled on it.
2. Generates update activity in the source table by adding, updating, and deleting items. This causes
   data to be written to the table's stream.
3. Reads the records from the stream, reconstructs them as DynamoDB requests, and applies the
   requests to the destination table.
4. Scans the source and destination tables to ensure their contents are identical.
5. Cleans up by deleting the tables.
These steps are described in the following sections, and the complete application is shown at the end of
the walkthrough.
Topics
 • Step 1: Create DynamoDB Tables (p. 524)
 • Step 2: Generate Update Activity in Source Table (p. 525)
 • Step 3: Process the Stream (p. 525)
 • Step 4: Ensure Both Tables Have Identical Contents (p. 526)
 • Step 5: Clean Up (p. 526)
 • Complete Program: DynamoDB Streams Kinesis Adapter (p. 526)
The following code snippet shows the code used for creating both tables.
                                                                                                             //
 key
.withProvisionedThroughput(provisionedThroughput).withStreamSpecification(streamSpecification);
The application defines a helper class with methods that call the PutItem, UpdateItem, and
DeleteItem API actions for writing the data. The following code snippet shows how these methods are
used.
• It defines a record processor class, StreamsRecordProcessor, with methods that comply with the
  KCL interface definition: initialize, processRecords, and shutdown. The processRecords
  method contains the logic required for reading from the source table's stream and writing to the
  destination table.
• It defines a class factory for the record processor class (StreamsRecordProcessorFactory). This is
  required for Java programs that use the KCL.
• It instantiates a new KCL Worker, which is associated with the class factory.
• It shuts down the Worker when record processing is complete.
To learn more about the KCL interface definition, go to Developing Amazon Kinesis Consumers Using the
Amazon Kinesis Client Library in the Amazon Kinesis Developer Guide.
The following code snippet shows the main loop in StreamsRecordProcessor. The case statement
determines what action to perform, based on the OperationType that appears in the stream record.
                  switch (streamRecord.getEventName()) {
                      case "INSERT":
                      case "MODIFY":
                          StreamsAdapterDemoHelper.putItem(dynamoDBClient, tableName,
                              streamRecord.getDynamodb().getNewImage());
                          break;
                      case "REMOVE":
                          StreamsAdapterDemoHelper.deleteItem(dynamoDBClient, tableName,
                              streamRecord.getDynamodb().getKeys().get("Id").getN());
                  }
    }
    checkpointCounter += 1;
    if (checkpointCounter % 10 == 0) {
        try {
            checkpointer.checkpoint();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
}
The DemoHelper class contains a ScanTable method that calls the low-level Scan API. The following
code snippet shows how this is used.
if (StreamsAdapterDemoHelper.scanTable(dynamoDBClient, srcTable).getItems()
    .equals(StreamsAdapterDemoHelper.scanTable(dynamoDBClient, destTable).getItems())) {
    System.out.println("Scan result is equal.");
}
else {
    System.out.println("Tables are different!");
}
Step 5: Clean Up
The demo is complete, so the application deletes the source and destination tables. See the following
code snippet.
Even after the tables are deleted, their streams remain available for up to 24 hours, after which they are
automatically deleted.
dynamoDBClient.deleteTable(new DeleteTableRequest().withTableName(srcTable));
dynamoDBClient.deleteTable(new DeleteTableRequest().withTableName(destTable));
    Important
    To run this program, make sure the client application has access to DynamoDB and CloudWatch
    using policies. For more information, see Using Identity-Based Policies (IAM Policies) for Amazon
    DynamoDB (p. 649).
• StreamsAdapterDemo.java
• StreamsRecordProcessor.java
• StreamsRecordProcessorFactory.java
• StreamsAdapterDemoHelper.java
StreamsAdapterDemo.java
import com.amazonaws.ClientConfiguration;
import com.amazonaws.auth.AWSCredentialsProvider;
import com.amazonaws.auth.profile.ProfileCredentialsProvider;
import com.amazonaws.services.cloudwatch.AmazonCloudWatch;
import com.amazonaws.services.cloudwatch.AmazonCloudWatchClientBuilder;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder;
import com.amazonaws.services.dynamodbv2.model.DeleteTableRequest;
import com.amazonaws.services.dynamodbv2.model.DescribeTableResult;
import com.amazonaws.services.dynamodbv2.streamsadapter.AmazonDynamoDBStreamsAdapterClient;
import com.amazonaws.services.kinesis.clientlibrary.interfaces.IRecordProcessorFactory;
import com.amazonaws.services.kinesis.clientlibrary.lib.worker.InitialPositionInStream;
import
 com.amazonaws.services.kinesis.clientlibrary.lib.worker.KinesisClientLibConfiguration;
import com.amazonaws.services.kinesis.clientlibrary.lib.worker.Worker;
    /**
     * @param args
     */
    public static void main(String[] args) throws Exception {
        System.out.println("Starting demo...");
dynamoDBClient = AmazonDynamoDBClientBuilder.standard().build();
cloudWatchClient = AmazonCloudWatchClientBuilder.standard().build();
setUpTables();
        Thread.sleep(25000);
        worker.shutdown();
        t.join();
        if (StreamsAdapterDemoHelper.scanTable(dynamoDBClient, srcTable).getItems()
            .equals(StreamsAdapterDemoHelper.scanTable(dynamoDBClient,
 destTable).getItems())) {
            System.out.println("Scan result is equal.");
        }
        else {
            System.out.println("Tables are different!");
        }
        System.out.println("Done.");
        cleanupAndExit(0);
    }
awaitTableCreation(srcTable);
        performOps(srcTable);
    }
        Integer retries = 0;
        Boolean created = false;
        while (!created && retries < 100) {
            DescribeTableResult result =
 StreamsAdapterDemoHelper.describeTable(dynamoDBClient, tableName);
            created = result.getTable().getTableStatus().equals("ACTIVE");
            if (created) {
                System.out.println("Table is active.");
                return;
            }
            else {
                retries++;
                try {
                    Thread.sleep(1000);
                }
                catch (InterruptedException e) {
                    // do nothing
                }
            }
        }
        System.out.println("Timeout after table creation. Exiting...");
        cleanupAndExit(1);
    }
StreamsRecordProcessor.java
import java.nio.charset.Charset;
import java.util.List;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
import com.amazonaws.services.dynamodbv2.streamsadapter.model.RecordAdapter;
import com.amazonaws.services.kinesis.clientlibrary.interfaces.IRecordProcessor;
import
 com.amazonaws.services.kinesis.clientlibrary.interfaces.IRecordProcessorCheckpointer;
import com.amazonaws.services.kinesis.clientlibrary.lib.worker.ShutdownReason;
import com.amazonaws.services.kinesis.model.Record;
     @Override
     public void initialize(String shardId) {
         checkpointCounter = 0;
     }
    @Override
    public void processRecords(List<Record> records, IRecordProcessorCheckpointer
 checkpointer) {
        for (Record record : records) {
            String data = new String(record.getData().array(), Charset.forName("UTF-8"));
            System.out.println(data);
            if (record instanceof RecordAdapter) {
                 com.amazonaws.services.dynamodbv2.model.Record streamRecord =
 ((RecordAdapter) record)
                     .getInternalObject();
                 switch (streamRecord.getEventName()) {
                     case "INSERT":
                     case "MODIFY":
                         StreamsAdapterDemoHelper.putItem(dynamoDBClient, tableName,
                             streamRecord.getDynamodb().getNewImage());
                         break;
                     case "REMOVE":
                         StreamsAdapterDemoHelper.deleteItem(dynamoDBClient, tableName,
                             streamRecord.getDynamodb().getKeys().get("Id").getN());
                 }
             }
             checkpointCounter += 1;
             if (checkpointCounter % 10 == 0) {
                 try {
                     checkpointer.checkpoint();
                 }
                 catch (Exception e) {
                     e.printStackTrace();
                 }
             }
         }
     @Override
     public void shutdown(IRecordProcessorCheckpointer checkpointer, ShutdownReason reason)
 {
         if (reason == ShutdownReason.TERMINATE) {
             try {
                 checkpointer.checkpoint();
             }
             catch (Exception e) {
                 e.printStackTrace();
             }
         }
     }
StreamsRecordProcessorFactory.java
import   com.amazonaws.auth.AWSCredentialsProvider;
import   com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
import   com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder;
import   com.amazonaws.services.kinesis.clientlibrary.interfaces.IRecordProcessor;
import   com.amazonaws.services.kinesis.clientlibrary.interfaces.IRecordProcessorFactory;
    @Override
    public IRecordProcessor createProcessor() {
 AmazonDynamoDB dynamoDBClient = AmazonDynamoDBClientBuilder.standard().build();
        return new StreamsRecordProcessor(dynamoDBClient, tableName);
    }
StreamsAdapterDemoHelper.java
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import   com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
import   com.amazonaws.services.dynamodbv2.model.AttributeAction;
import   com.amazonaws.services.dynamodbv2.model.AttributeDefinition;
import   com.amazonaws.services.dynamodbv2.model.AttributeValue;
import   com.amazonaws.services.dynamodbv2.model.AttributeValueUpdate;
import   com.amazonaws.services.dynamodbv2.model.CreateTableRequest;
import   com.amazonaws.services.dynamodbv2.model.CreateTableResult;
import   com.amazonaws.services.dynamodbv2.model.DeleteItemRequest;
import   com.amazonaws.services.dynamodbv2.model.DescribeTableRequest;
import   com.amazonaws.services.dynamodbv2.model.DescribeTableResult;
import   com.amazonaws.services.dynamodbv2.model.KeySchemaElement;
import   com.amazonaws.services.dynamodbv2.model.KeyType;
import   com.amazonaws.services.dynamodbv2.model.ProvisionedThroughput;
import   com.amazonaws.services.dynamodbv2.model.PutItemRequest;
import   com.amazonaws.services.dynamodbv2.model.ResourceInUseException;
import   com.amazonaws.services.dynamodbv2.model.ScanRequest;
import   com.amazonaws.services.dynamodbv2.model.ScanResult;
import   com.amazonaws.services.dynamodbv2.model.StreamSpecification;
import   com.amazonaws.services.dynamodbv2.model.StreamViewType;
import   com.amazonaws.services.dynamodbv2.model.UpdateItemRequest;
/**
    * @return StreamArn
    */
   public static String createTable(AmazonDynamoDB client, String tableName) {
       java.util.List<AttributeDefinition> attributeDefinitions = new
ArrayList<AttributeDefinition>();
       attributeDefinitions.add(new
AttributeDefinition().withAttributeName("Id").withAttributeType("N"));
// key
.withProvisionedThroughput(provisionedThroughput).withStreamSpecification(streamSpecification);
         try {
             System.out.println("Creating table " + tableName);
             CreateTableResult result = client.createTable(createTableRequest);
             return result.getTableDescription().getLatestStreamArn();
         }
         catch (ResourceInUseException e) {
             System.out.println("Table already exists.");
             return describeTable(client, tableName).getTable().getLatestStreamArn();
         }
   }
    Note
    This code does not handle all exceptions, and will not work reliably under high-traffic
    conditions. The recommended way to consume Stream records from DynamoDB is through
    the Kinesis Adapter using the Kinesis Client Library (KCL), as described in Using the DynamoDB
    Streams Kinesis Adapter to Process Stream Records (p. 522).
When you run the program, you will see output similar to the following:
Example
import   java.util.ArrayList;
import   java.util.Arrays;
import   java.util.HashMap;
import   java.util.List;
import   java.util.Map;
import   com.amazonaws.AmazonClientException;
import   com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
import   com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder;
import   com.amazonaws.services.dynamodbv2.AmazonDynamoDBStreams;
import   com.amazonaws.services.dynamodbv2.AmazonDynamoDBStreamsClientBuilder;
import   com.amazonaws.services.dynamodbv2.model.AttributeAction;
import   com.amazonaws.services.dynamodbv2.model.AttributeDefinition;
import   com.amazonaws.services.dynamodbv2.model.AttributeValue;
import   com.amazonaws.services.dynamodbv2.model.AttributeValueUpdate;
import   com.amazonaws.services.dynamodbv2.model.CreateTableRequest;
import   com.amazonaws.services.dynamodbv2.model.DescribeStreamRequest;
import   com.amazonaws.services.dynamodbv2.model.DescribeStreamResult;
import   com.amazonaws.services.dynamodbv2.model.DescribeTableResult;
import   com.amazonaws.services.dynamodbv2.model.GetRecordsRequest;
import   com.amazonaws.services.dynamodbv2.model.GetRecordsResult;
import   com.amazonaws.services.dynamodbv2.model.GetShardIteratorRequest;
import   com.amazonaws.services.dynamodbv2.model.GetShardIteratorResult;
import   com.amazonaws.services.dynamodbv2.model.KeySchemaElement;
import   com.amazonaws.services.dynamodbv2.model.KeyType;
import   com.amazonaws.services.dynamodbv2.model.ProvisionedThroughput;
import   com.amazonaws.services.dynamodbv2.model.Record;
import   com.amazonaws.services.dynamodbv2.model.Shard;
import   com.amazonaws.services.dynamodbv2.model.ShardIteratorType;
import   com.amazonaws.services.dynamodbv2.model.StreamSpecification;
import   com.amazonaws.services.dynamodbv2.model.StreamViewType;
import   com.amazonaws.services.dynamodbv2.util.TableUtils;
       AmazonDynamoDBStreams streamsClient =
AmazonDynamoDBStreamsClientBuilder.standard().build();
       try {
           TableUtils.waitUntilActive(dynamoDBClient, tableName);
       } catch (AmazonClientException e) {
           e.printStackTrace();
       }
dynamoDBClient.putItem(tableName, item);
System.out.println();
       // Get all the shard IDs from the stream.        Note that DescribeStream returns
       // the shard IDs one page at a time.
       do {
           DescribeStreamResult describeStreamResult = streamsClient.describeStream(
               new DescribeStreamRequest()
                   .withStreamArn(streamArn)
                   .withExclusiveStartShardId(lastEvaluatedShardId));
           List<Shard> shards = describeStreamResult.getStreamDescription().getShards();
               }
           }
System.out.println("Demo complete");
      }
}
Cross-Region Replication
      Important
      AWS previously provided a cross-region replication solution based on AWS CloudFormation.
      This solution has now been deprecated in favor of an open source command line tool. For more
      information, please refer to the detailed instructions on GitHub:
• https://github.com/awslabs/dynamodb-cross-region-library/blob/master/README.md
The DynamoDB cross-region replication solution uses the Amazon DynamoDB Cross-Region Replication
Library. This library uses DynamoDB Streams to keep DynamoDB tables in sync across multiple regions
in near real time. When you write to a DynamoDB table in one region, those changes are automatically
propagated by the Cross-Region Replication Library to your tables in other regions.
You can leverage the Cross-Region Replication Library in your own applications, to build your own
replication solutions with DynamoDB Streams. For more information, and to download the source code,
go to the following GitHub repository:
• https://github.com/awslabs/dynamodb-cross-region-library
Amazon DynamoDB is integrated with AWS Lambda so that you can create triggers—pieces of code that
automatically respond to events in DynamoDB Streams. With triggers, you can build applications that
react to data modifications in DynamoDB tables.
If you enable DynamoDB Streams on a table, you can associate the stream ARN with a Lambda function
that you write. Immediately after an item in the table is modified, a new record appears in the table's
stream. AWS Lambda polls the stream and invokes your Lambda function synchronously when it detects
new stream records.
The Lambda function can perform any actions you specify, such as sending a notification or initiating a
workflow. For example, you can write a Lambda function to simply copy each stream record to persistent
storage, such as Amazon Simple Storage Service (Amazon S3), to create a permanent audit trail of
write activity in your table. Or suppose you have a mobile gaming app that writes to a GameScores
table. Whenever the TopScore attribute of the GameScores table is updated, a corresponding stream
record is written to the table's stream. This event could then trigger a Lambda function that posts a
congratulatory message on a social media network. (The function would simply ignore any stream
records that are not updates to GameScores or that do not modify the TopScore attribute.)
For more information about AWS Lambda, see the AWS Lambda Developer Guide.
In this tutorial, you will create an AWS Lambda trigger to process a stream from a DynamoDB table.
The scenario for this tutorial is Woofer, a simple social network. Woofer users communicate using barks
(short text messages) that are sent to other Woofer users. The following diagram shows the components
and workflow for this application:
1. A user writes an item to a DynamoDB table (BarkTable). Each item in the table represents a bark.
2. A new stream record is written to reflect that a new item has been added to BarkTable.
3. The new stream record triggers an AWS Lambda function (publishNewBark).
4. If the stream record indicates that a new item was added to BarkTable, the Lambda function reads the
   data from the stream record and publishes a message to a topic in Amazon Simple Notification Service
   (Amazon SNS).
5. The message is received by subscribers to the Amazon SNS topic. (In this tutorial, the only subscriber is
   an email address.)
This tutorial uses the AWS Command Line Interface. If you have not done so already, follow the
instructions in the AWS Command Line Interface User Guide install and configure the AWS CLI.
BarkTable will have a stream enabled. Later in this tutorial, you will create a trigger by associating an
AWS Lambda function with the stream.
      ...
      "LatestStreamArn": "arn:aws:dynamodb:region:accountID:table/BarkTable/stream/timestamp
      ...
     Make a note of the region and the accountID, because you will need them for the other steps in
     this tutorial.
You will also create a policy for the role. The policy will contain all of the permissions that the Lambda
function will need at runtime.
     {
          "Version": "2012-10-17",
          "Statement": [
            {
              "Effect": "Allow",
              "Principal": {
                 "Service": "lambda.amazonaws.com"
              },
              "Action": "sts:AssumeRole"
            }
          ]
      }
3.   Create a file named role-policy.json with the following contents. (Replace region and
     accountID with your AWS region and account ID.)
     {
         "Version": "2012-10-17",
         "Statement": [
             {
                 "Effect": "Allow",
                 "Action": "lambda:InvokeFunction",
                 "Resource": "arn:aws:lambda:region:accountID:function:publishNewBark*"
             },
             {
                 "Effect": "Allow",
                 "Action": [
                     "logs:CreateLogGroup",
                     "logs:CreateLogStream",
                     "logs:PutLogEvents"
                 ],
                 "Resource": "arn:aws:logs:region:accountID:*"
             },
             {
                 "Effect": "Allow",
                 "Action": [
                     "dynamodb:DescribeStream",
                     "dynamodb:GetRecords",
                     "dynamodb:GetShardIterator",
                     "dynamodb:ListStreams"
                 ],
                 "Resource": "arn:aws:dynamodb:region:accountID:table/BarkTable/stream/*"
             },
             {
                 "Effect": "Allow",
                 "Action": [
                     "sns:Publish"
                 ],
                 "Resource": [
                     "*"
                 ]
             }
         ]
     }
     • Execute a Lambda function (publishNewBark). You will create the function later in this tutorial.
     • Access CloudWatch Logs. The Lambda function will write diagnostics to CloudWatch Logs at
       runtime.
     • Read data from the DynamoDB stream for BarkTable.
     • Publish messages to Amazon SNS.
4.   Type the following command to attach the policy to WooferLambdaRole:
         --policy-name WooferLambdaRolePolicy \
         --policy-document file://role-policy.json
2.   Type the following command to subscribe an email address to wooferTopic. (Replace region and
     accountID with your AWS region and account ID, and replace example@example.com with a valid
     email address.)
3.   Amazon SNS will send a confirmation message to your email address. Click the Confirm
     subscription link in that message to complete the subscription process.
The publishNewBark function processes only the stream events that correspond to new items in
BarkTable. The function reads data from such an event, and then invokes Amazon SNS to publish it.
1.   Create a file named publishNewBark.js with the following contents:. (Replace region and
     accountID with your AWS region and account ID.)
     'use strict';
     var AWS = require("aws-sdk");
     var sns = new AWS.SNS();
         event.Records.forEach((record) => {
             console.log('Stream record: ', JSON.stringify(record, null, 2));
             if (record.eventName == 'INSERT') {
                 var who = JSON.stringify(record.dynamodb.NewImage.Username.S);
                 var when = JSON.stringify(record.dynamodb.NewImage.Timestamp.S);
                 var what = JSON.stringify(record.dynamodb.NewImage.Message.S);
                 var params = {
                     Subject: 'A new bark from ' + who,
                     Message: 'Woofer user ' + who + ' barked the following at ' + when + ':
     \n\n ' + what,
                     TopicArn: 'arn:aws:sns:region:accountID:wooferTopic'
                 };
                 sns.publish(params, function(err, data) {
                     if (err) {
                         console.error("Unable to send message. Error JSON:",
      JSON.stringify(err, null, 2));
                         } else {
                             console.log("Results from sending message: ", JSON.stringify(data,
      null, 2));
                         }
                   });
              }
          });
          callback(null, `Successfully processed ${event.Records.length} records.`);
     };
2.   Create a zip file to contain publishNewBark.js. If you have the zip command-line utility you can
     type the following command to do this:
3.   When you create the Lambda function, you specify the ARN for WooferLambdaRole, which you
     created in Step 2: Create a Lambda Execution Role (p. 539). Type the following command to
     retrieve this ARN:
     ...
     "Arn": "arn:aws:iam::region:role/service-role/WooferLambdaRole"
     ...
     Now type the following command to create the Lambda function. (Replace roleARN with the ARN
     for WooferLambdaRole.)
4.   Now you will test publishNewBark to verify that it works. To do this, you will provide input that
     resembles a real record from DynamoDB Streams.
     {
          "Records": [
              {
                  "eventID": "7de3041dd709b024af6f29e4fa13d34c",
                  "eventName": "INSERT",
                  "eventVersion": "1.1",
                  "eventSource": "aws:dynamodb",
                  "awsRegion": "us-west-2",
                  "dynamodb": {
                       "ApproximateCreationDateTime": 1479499740,
                       "Keys": {
                           "Timestamp": {
                               "S": "2016-11-18:12:09:36"
                                    API Version 2012-08-10
                                              542
                        Amazon DynamoDB Developer Guide
                    DynamoDB Streams and AWS Lambda Triggers
                        },
                        "Username": {
                            "S": "John Doe"
                        }
                   },
                   "NewImage": {
                       "Timestamp": {
                           "S": "2016-11-18:12:09:36"
                       },
                       "Message": {
                           "S": "This is a bark from the Woofer social network"
                       },
                       "Username": {
                           "S": "John Doe"
                       }
                   },
                   "SequenceNumber": "13021600000000001596893679",
                   "SizeBytes": 112,
                   "StreamViewType": "NEW_IMAGE"
            },
            "eventSourceARN": "arn:aws:dynamodb:us-east-1:123456789012:table/BarkTable/
stream/2016-11-16T20:42:48.104"
        }
    ]
}
If the test was successful, you will see the following output:
{
    "StatusCode": 200
}
You will also receive a new email message within a few minutes.
    Note
    AWS Lambda writes diagnostic information to Amazon CloudWatch Logs. If you encounter
    errors with your Lambda function, you can use these diagnostics for troubleshooting
    purposes:
1.   When you create the trigger, you will need to specify the ARN for the BarkTable stream. Type the
     following command to retrieve this ARN:
     ...
      "LatestStreamArn": "arn:aws:dynamodb:region:accountID:table/BarkTable/stream/timestamp
     ...
2.   Type the following command to create the trigger. (Replace streamARN with the actual stream
     ARN.)
3. You will now test the trigger. Type the following command to add an item to BarkTable:
     The Lambda function processes only new items that you add to BarkTable. If you update or delete an
     item in the table, the function does nothing.
     Note
     AWS Lambda writes diagnostic information to Amazon CloudWatch Logs. If you encounter
     errors with your Lambda function, you can use these diagnostics for troubleshooting purposes.
Best Practices
An AWS Lambda function runs within a container—an execution environment that is isolated from other
functions. When you run a function for the first time, AWS Lambda creates a new container and begins
executing the function's code.
A Lambda function has a handler that is executed once per invocation. The handler contains the main
business logic for the function. For example, the Lambda function shown in Step 4: Create and Test a
Lambda Function (p. 541) has a handler that can process records in a DynamoDB stream.
You can also provide initialization code that runs one time only—after the container is created, but
before AWS Lambda executes the handler for the first time. The Lambda function shown in Step 4:
Create and Test a Lambda Function (p. 541) has initialization code that imports the SDK for JavaScript
in Node.js, and creates a client for Amazon SNS. These objects should only be defined once, outside of
the handler.
After the function executes, AWS Lambda may opt to reuse the container for subsequent invocations of
the function. In this case, your function handler might be able to reuse the resources that you defined in
your initialization code. (Note that you cannot control how long AWS Lambda will retain the container, or
whether the container will be reused at all.)
• AWS service clients should be instantiated in the initialization code, not in the handler. This will allow
  AWS Lambda to reuse existing connections, for the duration of the container's lifetime.
• In general, you do not need to explicitly manage connections or implement connection pooling
  because AWS Lambda manages this for you.
For more information, see Best Practices for Working with AWS Lambda Functions in the AWS Lambda
Developer Guide.
    On-demand backup and restore scales without degrading the performance or availability of your
    applications. It uses a new and unique distributed technology that allows you to complete backups
    in seconds regardless of table size. You can create backups that are consistent within seconds across
    thousands of partitions without worrying about schedules or long-running backup processes. All backups
    are cataloged, easily discoverable, and retained until explicitly deleted.
    In addition, on-demand backup and restore operations don't affect performance or API latencies.
    Backups are preserved regardless of table deletion. For more information, see Backup and Restore: How
    It Works (p. 546).
    You can create table backups using the console, the AWS Command Line Interface (AWS CLI), or the
    DynamoDB API. For more information, see Backing Up a DynamoDB Table (p. 548).
    For information about restoring a table from a backup, see Restoring a DynamoDB Table from a
    Backup (p. 549).
        Note
        Currently, on-demand backup and restore functionality is supported in the following AWS
        Regions:
    Currently, the backup and restore functionality works in the same Region as the source table. DynamoDB
    on-demand backups are available at no additional cost beyond the normal pricing that's associated with
    backup storage size. For more information, see Amazon DynamoDB Pricing.
    Backups
    When you create an on-demand backup, a time marker of the request is cataloged. The backup is created
    asynchronously by applying all changes until the time of the request to the last full table snapshot.
    Backup requests are processed instantaneously and become available for restore within minutes.
        Note
        Each time you create an on-demand backup, the entire table data is backed up. There is no limit
        to the number of on-demand backups that can be taken.
All backups in DynamoDB work without consuming any provisioned throughput on the table.
DynamoDB backups do not guarantee causal consistency across items; however, the skew between
updates in a backup is usually much less than a second.
Along with data, the following are also included on the backups:
For DynamoDB tables that have GSIs, the GSI data is automatically restored at table restore time.
    Note
    Currently, backup and restore works only in the same AWS Region as the source table.
To learn how to perform a backup, see Backing Up a DynamoDB Table (p. 548).
Restores
A table is restored without consuming any provisioned throughput on the table. The destination table
is set with the same provisioned read capacity units and write capacity units as the source table, as
recorded at the time the backup was requested. The restore process also restores the local secondary
indexes and the global secondary indexes.
You can only restore the entire table data to a new table from a backup. Restore times vary based on
the size of the DynamoDB table that's being restored. You can write to the restored table only after it
becomes active.
    Note
    You can't overwrite an existing table during a restore operation.
To learn how to perform a restore, see Restoring a DynamoDB Table from a Backup (p. 549).
You can use IAM policies for access control. For more information, see Using IAM with DynamoDB Backup
and Restore (p. 554).
You can schedule periodic or future backups by using AWS Lambda functions. Also, all backup and
restore console and API actions are captured and recorded in AWS CloudTrail for logging, continuous
monitoring, and auditing.
    Topics
     • Creating a Table Backup (Console) (p. 548)
     • Creating a Table Backup (AWS CLI) (p. 549)
    1.   Sign in to the AWS Management Console and open the DynamoDB console at https://
         console.aws.amazon.com/dynamodb/.
    2.   You can create a backup by doing one of the following:
             Note
             If you create backups using the Backups section in the navigation pane, the table isn't
             preselected for you. You have to manually choose the source table name for the backup.
         While the backup is being created, the backup status is set to Creating. After the backup is finalized,
         the backup status changes to Available.
• Create a backup with the name MusicCollectionBackup for the MusicCollection table:
While the backup is being created, the backup status is set to CREATING:
          {
              "BackupDetails": {
                  "BackupName": "MusicCollectionBackup",
                  "BackupArn": "arn:aws:dynamodb:us-east-1:123456789012:table/MusicCollection/
          backup/01489602797149-73d8d5bc",
                  "BackupStatus": "CREATING",
                  "BackupCreationDateTime": 1489602797.149
              }
          }
    After the backup is finalized, its BackupStatus should change to AVAILABLE. To confirm this, use
    the describe-backup command. You can get the input value of backup-arn from the output of the
    previous step or by using the list-backups command.
    To keep track of your backups, you can use the list-backups command. It lists all your backups that
    are in CREATING or AVAILABLE status:
    The list-backups command and the describe-backup command are useful to check information
    about the source table of the backup.
    Topics
        • Restoring a Table from a Backup (Console) (p. 549)
        • Restoring a Table from a Backup (AWS CLI) (p. 551)
     Note
     This procedure assumes the MusicCollection table no longer exists before restoring it using
     the MusicCollectionBackup.
1.   Sign in to the AWS Management Console and open the DynamoDB console at https://
     console.aws.amazon.com/dynamodb/.
2.   In the navigation pane on the left side of the console, choose Backups.
3.   In the list of backups, choose MusicCollectionBackup.
5.   Type MusicCollection as the new table name. Confirm the backup name and other backup
     details. Then choose Restore table to start the restore process.
    The table that is being restored is shown with the status Creating. After the restore process is
    finished, the status of the MusicCollection table changes to Active.
    1. Confirm the backup that you want to restore by using the list-backups command. This example
       uses MusicCollectionBackup.
         To get additional details for the backup, use the describe-backup command. You can get the input
         backup-arn from the previous step:
    2. Restore the table from the backup. In this case, the MusicCollectionBackup restores the
       MusicCollection table:
To verify the restore, use the describe-table command to describe the MusicCollection table:
    The table that is being restored from the backup is shown with the status Creating. After the restore
    process is finished, the status of the MusicCollection table changes to Active.
          Important
          While a restore is in progress, do not modify or delete your IAM role policy; otherwise,
          unexpected behavior can result. For example, suppose that you removed write permissions for a
          table while that table is being restored. In this case, the underlying RestoreTableFromBackup
          operation would not be able to write any of the restored data to the table.
          After the restore operation is complete, you can modify or delete your IAM role policy.
    Topics
     • Deleting a Table Backup (Console) (p. 552)
     • Deleting a Table Backup (AWS CLI) (p. 553)
    1.    Sign in to the AWS Management Console and open the DynamoDB console at https://
          console.aws.amazon.com/dynamodb/.
2.   In the navigation pane on the left side of the console, choose Backups.
3.   In the list of backups, choose MusicCollectionBackup.
    For more information about using IAM policies in DynamoDB, see Using Identity-Based Policies (IAM
    Policies) for Amazon DynamoDB (p. 649).
    The following are examples of IAM policies that you can use to configure specific backup and restore
    functionality in DynamoDB.
    {
        "Version": "2012-10-17",
        "Statement": [
            {
                "Effect": "Allow",
                "Action": [
                    "dynamodb:CreateBackup",
                    "dynamodb:RestoreTableFromBackup",
                    "dynamodb:PutItem",
                    "dynamodb:UpdateItem",
                    "dynamodb:DeleteItem",
                    "dynamodb:GetItem",
                    "dynamodb:Query",
                    "dynamodb:Scan",
                    "dynamodb:BatchWriteItem"
                ],
                "Resource": "*"
            }
        ]
    }
        Important
        DynamoDB write permissions are necessary for restore functionality.
    {
        "Version": "2012-10-17",
        "Statement": [
            {
                "Effect": "Allow",
                "Action": ["dynamodb:CreateBackup"],
                "Resource": "*"
            },
        {
            "Effect": "Deny",
            "Action": ["dynamodb:RestoreTableFromBackup"],
            "Resource": "*"
        }
    ]
}
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": ["dynamodb:ListBackups"],
            "Resource": "*"
        },
        {
            "Effect": "Deny",
            "Action": [
                "dynamodb:CreateBackup",
                "dynamodb:RestoreTableFromBackup"
            ],
            "Resource": "*"
        }
    ]
}
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": ["dynamodb:ListBackups"],
            "Resource": "*"
        },
        {
            "Effect": "Deny",
            "Action": ["dynamodb:DeleteBackup"],
            "Resource": "*"
        }
    ]
}
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "dynamodb:DescribeBackup",
                "dynamodb:RestoreTableFromBackup",
                "dynamodb:PutItem",
                "dynamodb:UpdateItem",
                "dynamodb:DeleteItem",
                "dynamodb:GetItem",
                "dynamodb:Query",
                "dynamodb:Scan",
                "dynamodb:BatchWriteItem"
            ],
            "Resource": "*"
        },
        {
            "Effect": "Deny",
            "Action": [
                "dynamodb:DeleteBackup"
            ],
            "Resource": "arn:aws:dynamodb:us-east-1:123456789012:table/MusicCollection/
backup/01489173575360-b308cd7d"
        }
    ]
}
    Important
    DynamoDB write permissions are necessary for restore functionality.
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": ["dynamodb:CreateBackup"],
            "Resource": [
                "arn:aws:dynamodb:us-east-1:123456789012:table/Movies"
            ]
        }
    ]
}
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": ["dynamodb:ListBackups"],
            "Resource": "*"
        }
    ]
}
    Important
    You cannot grant permissions for the ListBackups action on a specific table.
Global Tables
    Amazon DynamoDB global tables provide a fully managed solution for deploying a multi-region, multi-
    master database, without having to build and maintain your own replication solution. When you create a
    global table, you specify the AWS regions where you want the table to be available. DynamoDB performs
    all of the necessary tasks to create identical tables in these regions, and propagate ongoing data changes
    to all of them.
    To illustrate one use case for a global table, suppose that you have a large customer base spread across
    three geographic areas—the US east coast, the US west coast, and western Europe. Customers would
    need to update their profile information while using your application. To address these requirements,
    you could create three identical DynamoDB tables named CustomerProfiles, in three different AWS
    regions. These three tables would be entirely separate from each other, and changes to the data in one
    table would not be reflected in the other tables. Without a managed replication solution, you could write
    code to replicate data changes among these tables; however, this would be a time-consuming and labor-
    intensive effort.
    Instead of writing your own code, you could create a global table consisting of your three region-specific
    CustomerProfiles tables. DynamoDB would then automatically replicate data changes among those
    tables, so that changes to CustomerProfiles data in one region would be seamlessly propagated to the
    other regions. In addition, if one of the AWS regions were to become temporarily unavailable, your
    customers could still access the same CustomerProfiles data in the other regions.
    DynamoDB global tables are ideal for massively scaled applications, with globally dispersed users. In
    such an environment, users expect very fast application performance. Global tables provide automatic
    multi-master replication to AWS regions world-wide, so you can deliver low-latency data access to your
    users no matter where they are located.
    Topics
     • Supported Regions (p. 558)
     • Global Tables: How It Works (p. 559)
     • Requirements and Best Practices (p. 560)
     • Creating a Global Table (p. 561)
     • Monitoring Global Tables (p. 564)
     • Using IAM with Global Tables (p. 565)
Supported Regions
    The DynamoDB global tables feature is available in the following AWS regions:
• EU (Frankfurt)
    A replica table (or replica, for short) is a single DynamoDB table that functions as a part of a global table.
    Each replica stores the same set of data items. Any given global table can only have one replica table per
    region.
    1. Create an ordinary DynamoDB table, with DynamoDB Streams enabled, in an AWS region.
    2. Repeat step 1 for every other AWS region where you want to replicate your data.
    3. Define a DynamoDB global table, based upon the tables that you have created.
    The AWS Management Console automates these tasks, so you can create a global table quickly and
    easily. (For detailed instructions, see Creating a Global Table (p. 561).)
    The resulting DynamoDB global table consists of multiple replica tables, one per region, that DynamoDB
    treats as a single unit. Every replica has the same table name and the same primary key schema. When
    an application writes data to a replica table in one region, DynamoDB automatically propagates the write
    to the other replica tables in the other AWS regions.
        Important
        Global Tables automatically creates the following attributes for every item to keep your table
        data in sync:
        • aws:rep:updatetime
        • aws:rep:updateregion
You should not alter these attributes or create attributes with the same name.
    You can add replica tables to the global table, so that it can be available in additional AWS regions. (In
    order to do this, the global table must be empty. In other words, none of the replica tables can have any
    data in them.)
    You can also remove a replica table from a global table. If you do this, then the table is completely
    disassociated from the global table. This newly-independent table no longer interacts with the global
    table, and data is no longer propagated to or from the global table.
    With a global table, each replica table stores the same set of data items. DynamoDB does not support
    partial replication of only some of the items.
    An application can read and write data to any replica table. If your application only uses eventually
    consistent reads, and only issues reads against one AWS region, then it will work without any
    modification.
    However, if your application requires strongly consistent reads, then it must perform all of its strongly
    consistent reads and writes in the same region. DynamoDB does not support strongly consistent
    reads across AWS regions; therefore, if you write to one region and read from another region, the read
    response might include stale data that doesn't reflect the results of recently-completed writes in the
    other region.
    Conflicts can arise if applications update the same item in different regions at about the same time. To
    ensure eventual consistency, DynamoDB global tables use a “last writer wins” reconciliation between
    concurrent updates, where DynamoDB makes a best effort to determine the last writer. With this conflict
    resolution mechanism, all of the replicas will agree on the latest update, and converge toward a state in
    which they all have identical data.
    If a region becomes isolated or degraded, DynamoDB keeps track of any writes that have been
    performed, but have not yet been propagated to all of the replica tables. When the region comes back
    online, DynamoDB will resume propagating any pending writes from that region to the replica tables in
    other regions. It will also resume propagating writes from other replica tables to the region that is now
    back online.
    Topics
     • Requirements for Adding a New Replica Table (p. 560)
     • Best Practices for Managing Capacity (p. 561)
     • Best Practices for Global Secondary Indexes (p. 561)
    • The table must have the same primary key as all of the other replicas.
    • The table must have the same name as all of the other replicas.
    • The table must have DynamoDB Streams enabled, with the stream containing both the new and the
      old images of the item.
    • None of the replica tables in the global table can contain any data.
    You must also have appropriate IAM permissions. For more information, see Using IAM with Global
    Tables (p. 565).
    If you create your replica tables using the AWS Management Console, then auto scaling will be enabled
    by default for each replica table, with default auto scaling settings for managing RCUs and WCUs.
    If you decide to modify the auto scaling settings on one replica table, you should ensure that you also
    apply the same auto scaling settings on all of the other replica tables. This will ensure that capacity unit
    management is consistent across all of the replica tables.
    To illustrate the importance of consistent capacity unit settings, suppose that you create a global table
    consisting of replicas in US West (Oregon) and EU (Ireland). Now suppose that you set the write capacity
    units for the US West (Oregon) replica to 1000, but only 500 write capacity units for the EU (Ireland)
    replica. If your application performs more than 500 writes per second to the US West (Oregon) replica,
    then the EU (Ireland) replica will not be able to keep up. The data in these two replica tables will diverge,
    as EU (Ireland) replica falls further behind—perhaps indefinitely.
    Provisioned replicated write capacity units (rWCUs) in each region for every replica table should be
    the total number of rWCUs needed for application writes in all regions multiplied by two. This will
    accommodate application writes that occur in the local region; replicated application writes coming from
    other regions; and at least one additional system write for each application write. (DynamoDB performs
    these system writes on your behalf, to support the "last writer wins" conflict resolution mechanism
    provided by global tables.)
Topics
     For Primary key type Artist. Choose Add sort key, and type SongTitle. (Artist and SongTitle should
     both be strings.)
     To create the table, choose Create. This table will serve as the first replica table in a new global
     table, and will be the prototype for other replica tables that you add later.
4.   Choose the Global Tables tab, and then choose Enable streams. Leave the View type at its default
     value (New and old images).
5.   Choose Add region, and then choose another region where you want to deploy another replica
     table. In this case, choose US West (Oregon), and then choose Continue. This will start the table
     creation process in US West (Oregon).
     The console will check to ensure that there is no table with the same name in the selected region. (If
     a table with the same name does exist, then you must delete the existing table before you can create
     a new replica table in that region.)
         Note
         We recommend reviewing the settings of every replica table that you add to a global
         table, to verify that the provisioning settings are consistent across every replica. For more
         information, see Keep Capacity Settings Consistent (p. 561).
     The Global Table tab for the selected table (and for any other replica tables) will show that the table
     is replicated in multiple regions.
6.   You will now add another region, so that your global table is replicated and synchronized across the
     United States and Europe. To do this, repeat Step 5, but this time specify EU (Frankfurt) instead of
     US West (Oregon).
7.   You should still be using the AWS Management console in the us-east-2 (US East Ohio) region. For
     the Music table, choose the Items tab, and then choose Create Item. For Artist, type item_1. For
     SongTitle, type Song Value 1. To write the item, choose Save.
     After a short time, the item is replicated across all three regions of your global table. To verify this,
     in the AWS Management Console, go to the region selector in the upper right-hand corner and
     choose EU (Frankfurt). The Music table in EU (Frankfurt) should contain the new item.
1.   Create a new table (Music) in US East (Ohio), with DynamoDB Streams enabled
     (NEW_AND_OLD_IMAGES):
3. Create a global table (Music) consisting of replica tables in the us-east-2 and us-east-1 regions.
         Note
         The global table name (Music) must match the name of each of the replica tables (Music).
         For more information, see Requirements and Best Practices (p. 560).
4.   Create another table in EU (Ireland), with the same settings as those you created in Step 1 and Step
     2:
After you have done this, add this new table to the Music global table:
5. To verify that replication is working, add a new item to the Music table in US East (Ohio):
    6.     Wait for a few seconds, and then check to see if the item has been successfully replicated to US East
           (N. Virginia) and EU (Ireland):
         During normal operation, ReplicationLatency should be fairly constant. An elevated value for
         ReplicationLatency could indicate that updates from one replica are not propagating to other
         replica tables in a timely manner. Over time, this could result in other replica tables "falling behind",
         as they no longer receive updates consistently. In this case, you should verify that the read capacity
         units (RCUs) and write capacity units (WCUs) are identical for each of the replica tables. In addition,
         the WCU settings you choose should follow the recommendations in Best Practices for Managing
         Capacity (p. 561).
         ReplicationLatency can increase if an AWS region becomes degraded, and you have a replica table
         in that region. In this case, you can temporarily redirect your application's read and write activity to a
         different AWS region.
    • PendingReplicationCount—the number of item updates that are written to one replica table, but
      that have not yet been written to another replica in the global table. PendingReplicationCount is
      expressed in number of items, and is emitted for every source- and destination-region pair.
        PendingReplicationCount can increase if an AWS region becomes degraded, and you have a
        replica table in that region. In this case, you can temporarily redirect your application's read and write
        activity to a different AWS region.
For more information, see Amazon DynamoDB Metrics and Dimensions (p. 680).
Do not delete this service-linked role. If you do, then all of your global tables will no longer function.
(For more information about service-linked roles, see Using Service-Linked Roles in the IAM User Guide.)
    To create and maintain global tables in DynamoDB, you must have the
    dynamodb:CreateGlobalTable permission to access each of the following:
    If you use an IAM policy to manage access to one replica table, then you should apply an identical policy
    to all of the other replicas within that global table. This practice will help you maintain a consistent
    permissions model across all of the replica tables.
    By using identical IAM policies on all replicas in a global table, you can also avoid granting unintended
    read and write access to your global table data. For example, consider a user who has access to only one
    replica in a global table. If that user can write to this replica, then DynamoDB will propagate the write
    to all of the other replica tables. In effect, the user can (indirectly) write to all of the other replicas in the
    global table. This scenario can be avoided by using consistent IAM policies on all of the replica tables.
The following IAM policy grants permissions to allow the CreateGlobalTable action on all tables:
    {
           "Version": "2012-10-17",
           "Statement": [
               {
                   "Effect": "Allow",
                   "Action": ["dynamodb:CreateGlobalTable"],
                   "Resource": "*"
               }
           ]
     }
    To get started with encryption at rest, you need to create a table with encryption at rest enabled.
        Important
        Encryption at rest can be enabled only when you are creating a new DynamoDB table. Currently,
        you can't enable encryption at rest on an existing table. After encryption at rest is enabled,
        it can't be disabled. We recommend that you enable encryption for any tables that contain
        sensitive data.
    For more information, see Encryption at Rest: How It Works (p. 567). You can enable encryption at
    rest using the AWS Management Console, AWS Command Line Interface (AWS CLI), or the Amazon
    DynamoDB API. For more information, see Enabling Encryption at Rest (p. 568).
        Note
        Currently, encryption at rest is supported in the following AWS Regions:
        • US East (Ohio)
        • US East (N. Virginia)
        • US West (Oregon)
        • EU (Ireland)
    Encryption at rest is offered at no additional cost (KMS encryption key usage charges apply). For
    information on pricing, see Amazon DynamoDB Pricing.
    With encryption at rest, you can enable encryption for all your DynamoDB data at rest: the data that is
    persisted in your DynamoDB tables, local secondary indexes, and global secondary indexes. Encryption at
    rest encrypts your data using 256-bit AES encryption, also known as AES-256 encryption. It works at the
    table level and encrypts not only the base table, but also its indexes.
    Encryption at rest automatically integrates with AWS Key Management Service (AWS KMS) for managing
    the single service default key that is used to encrypt your tables. If a service default key doesn't exist
    when you create your encrypted DynamoDB table, AWS KMS automatically creates a new key for you.
    This key is used with encrypted tables that are created in the future. AWS KMS combines secure, highly
    available hardware and software to provide a key management system scaled for the cloud.
        Note
        Amazon DynamoDB can't read your table data unless it has access to the service default key
        stored in your AWS KMS account. DynamoDB uses envelope encryption and key hierarchy to
        encrypt data. Your AWS KMS encryption key is used to encrypt the root key of this key hierarchy.
        For more information, see How Envelope Encryption Works with Supported AWS Services.
    Using the same AWS KMS single service default key that is used to encrypt the table, the following are
    encrypted:
    After your data is encrypted, DynamoDB handles decryption of your data transparently with minimal
    impact on performance. You don't need to modify your applications to use encryption.
        Note
        DynamoDB does not call KMS for every single DynamoDB operation. The key is refreshed once
        every five minutes per client connection with active traffic.
    • When encryption is enabled for a table, all the data stored in that table is encrypted. You can't encrypt
      only a subset of items in a table.
    • Amazon DynamoDB uses a single service default key for encrypting all of your tables. If this key
      doesn’t exist, it is created for you. Service default keys can't be disabled.
    • Encryption at rest only encrypts data while it is static (at rest) on a persistent storage media. If data
      security is a concern for data in transit or data in use, you need to take additional measures:
      • Data-in-transit: Protect your data while it is actively moving over a public or a private network by
        encrypting sensitive data on the client side or using encrypted connections (HTTPS, SSL, TLS, FTPS,
        and so on).
      • Data-in-use: Protect your data before sending it to DynamoDB using client-side encryption.
          Important
          Currently, you cannot enable Encryption at Rest for DynamoDB Streams. If Encryption at Rest
          is a compliance/regulatory requirement, we recommend turning off DynamoDB Streams for
          encrypted tables.
    • You can use on-demand backup and restore with encrypted tables, and you can create a backup of
      an encrypted table. The table that is restored with this backup has encryption enabled. For more
      information, see On-Demand Backup and Restore for DynamoDB (p. 546).
Topics
1.    Sign in to the AWS Management Console and open the DynamoDB console at https://
      console.aws.amazon.com/dynamodb/.
2.    In the navigation pane on the left side of the console, choose Tables.
3.    Choose Create Table. For the Table name, type Music. For the primary key, use Artist, and for the
      sort key, use SongTitle, both as strings. In Table settings, make sure that Use default settings is
      not selected.
4.    Under Encryption at rest, choose Enable encryption. Then choose Create to create the encrypted
      table.
           Important
           Encryption at rest can be enabled only when you are creating a new DynamoDB table.
           Currently, you can't enable encryption at rest on an existing table. Once encryption at rest is
           enabled, it can't be disabled. We recommend that you enable encryption for any tables that
           contain sensitive data.
To confirm that the table is encrypted, check the table details under the Overview tab. Encryption
should be ENABLED.
      {
          "SSEDescription": {
              "Status": "ENABLED"
          }
    Amazon DynamoDB is designed for scale and performance. In most cases, the DynamoDB response times
    can be measured in single-digit milliseconds. However, there are certain use cases that require response
    times in microseconds. For these use cases, DynamoDB Accelerator (DAX) delivers fast response times for
    accessing eventually consistent data.
    DAX is a DynamoDB-compatible caching service that enables you to benefit from fast in-memory
    performance for demanding applications. DAX addresses three core scenarios:
    1. As an in-memory cache, DAX reduces the response times of eventually-consistent read workloads by
       an order of magnitude, from single-digit milliseconds to microseconds.
    2. DAX reduces operational and application complexity by providing a managed service that is API-
       compatible with Amazon DynamoDB, and thus requires only minimal functional changes to use with
       an existing application.
    3. For read-heavy or bursty workloads, DAX provides increased throughput and potential operational
       cost savings by reducing the need to over-provision read capacity units. This is especially beneficial for
       applications that require repeated reads for individual keys.
    • Applications that require the fastest possible response time for reads. Some examples include real-
      time bidding, social gaming, and trading applications. DAX delivers fast, in-memory read performance
      for these use cases.
    • Applications that read a small number of items more frequently than others. For example, consider an
      e-commerce system that has a one-day sale on a popular product. During the sale, demand for that
      product (and its data in DynamoDB) would sharply increase, compared to all of the other products. To
      mitigate the impacts of a "hot" key and a non-uniform data distribution, you could offload the read
      activity to a DAX cache until the one-day sale is over.
    • Applications that are read-intensive, but are also cost-sensitive. With DynamoDB, you provision the
      number of reads per second that your application requires. If read activity increases, you can increase
      your tables' provisioned read throughput (at an additional cost). Alternatively, you can offload the
      activity from your application to a DAX cluster, and reduce the amount of read capacity units you'd
      need to purchase otherwise.
    • Applications that require repeated reads against a large set of data. Such an application could
      potentially divert database resources from other applications. For example, a long-running analysis of
      regional weather data could temporarily consume all of the read capacity in a DynamoDB table, which
      would negatively impact other applications that need to access the same data. With DAX, the weather
      analysis could be performed against cached data instead.
    • Applications that require strongly consistent reads (or cannot tolerate eventually consistent reads).
    • Applications that do not require microsecond response times for reads, or that need to offload
      repeated read activity from underlying tables.
    • Applications that are write-intensive, or that do not perform much read activity.
    • Applications that are already using a different caching solution with DynamoDB, and are using their
      own client-side logic for working with that caching solution.
Usage Notes
    • For a list of AWS regions where DAX is available, refer to https://aws.amazon.com/dynamodb/pricing.
    • DAX supports applications written in Java, Node.js, .Python and .NET, using AWS-provided clients for
      those programming languages.
    • DAX does not support Transport Layer Security (TLS).
    • DAX is only available for the EC2-VPC platform. (There is no support for the EC2-Classic platform.)
    • DAX clusters maintain metadata about the attribute names of items they store, and that metadata is
      maintained indefinitely (even after the item has expired or been evicted from the cache). Applications
      that use an unbounded number of attribute names can, over time, cause memory exhaustion in the
      DAX cluster. This limitation applies only to top-level attribute names, not nested attribute names.
      Examples of problematic top-level attribute names include timestamps, UUIDs, and session IDs.
      Note that this limitation only applies to attribute names, not their values. Items like this are not a
      problem:
      {
           “Id”: 123,
           “Title”: “Bicycle 123”,
           “CreationDate”: “2017-10-24T01:02:03+00:00”
      }
But items like this are, if there are enough of them and they each have a different timestamp:
      {
           “Id”: 123,
           “Title”: “Bicycle 123”,
           “2017-10-24T01:02:03+00:00”: “created”
      }
Concepts
    DAX is designed to run within an Amazon Virtual Private Cloud environment (Amazon VPC). The Amazon
    VPC service defines a virtual network that closely resembles a traditional data center. With an Amazon
VPC, you have control over its IP address range, subnets, routing tables, network gateways, and security
settings. You can launch a DAX cluster in your virtual network, and control access to the cluster by using
Amazon VPC security groups.
    Note
    If you created your AWS account after 2013-12-04, then you already have a default VPC in each
    AWS region. A default VPC is ready for you to use—you can immediately start using your default
    VPC without having to perform any additional configuration steps.
    For more information, see Your Default VPC and Subnets in the Amazon VPC User Guide.
To create a DAX cluster, you use the AWS Management Console. Unless you specify otherwise, your DAX
cluster will run within your default VPC.
To run your application, you launch an Amazon EC2 instance into your Amazon VPC, and then deploy
your application (with the DAX client) on the EC2 instance. At runtime, the DAX client directs all of your
application's DynamoDB API requests to the DAX cluster. If DAX can process one of these API requests
directly, it does so; otherwise, it passes the request through to DynamoDB. Finally, the DAX cluster
returns the results to your application.
Your application can access DAX by specifying the endpoint for the DAX cluster. The DAX client software
works with the cluster endpoint to perform intelligent load-balancing and routing, so that incoming
requests are evenly distributed across all of the nodes in the cluster.
Read Operations
DAX can respond to the following API calls:
• GetItem
• BatchGetItem
• Query
• Scan
If the request specifies eventually consistent reads (the default behavior), it attempts to read the item
from DAX:
• If DAX has the item available (a cache hit), DAX returns the item to the application without accessing
  DynamoDB.
• If DAX does not have the item available (a cache miss), DAX passes the request through to DynamoDB.
  When it receives the response from DynamoDB, DAX returns the results to the application—but it also
  writes the results to the cache on the primary node.
    Note
    If there are any read replicas in the cluster, DAX automatically keeps the replicas in sync with the
    primary node. For more information, see Clusters (p. 576).
If the request specifies strongly consistent reads, DAX passes the request through to DynamoDB. The
results from DynamoDB are not cached in DAX; instead, they are simply returned to the application.
Write Operations
The following DAX API operations are considered "write-through":
• BatchWriteItem
• UpdateItem
• DeleteItem
• PutItem
With these operations, data is first written to the DynamoDB table, and then to the DAX cluster. The
operation is successful only if the data is successfully written to both the table and to DAX.
Other Operations
DAX does not recognize any DynamoDB operations for managing tables (such as CreateTable,
UpdateTable, and so on). If your application needs to perform these operations, it will need to access
DynamoDB directly rather than using DAX.
Item Cache
DAX maintains an item cache to store the results from GetItem and BatchGetItem operations. The
items in the cache represent eventually consistent data from DynamoDB, and are stored by their primary
key values.
When an application sends a GetItem or BatchGetItem request, DAX attempts to read the items
directly from the item cache using the specified key values. If the items are found (cache hit), DAX returns
them to the application immediately. If the items are not found (cache miss), DAX sends the request to
DynamoDB. DynamoDB processes the requests using eventually consistent reads, and returns the items
to DAX. DAX stores them in the item cache, and then returns them to the application.
The item cache has a time-to-live setting (TTL), which is 5 minutes by default. DAX assigns a timestamp
to every item that it writes to the item cache. An item expires if it has remained in the cache for longer
    than the TTL setting. If you issue a GetItem request on an expired item, this is considered a cache miss,
    so DAX will send the GetItem request to DynamoDB.
        Note
        You can specify the TTL setting for the item cache when you create a new DAX cluster. For more
        information, see Managing DAX Clusters (p. 625).)
    DAX also maintains a least recently used list (LRU) for the item cache. The LRU list keeps track of when
    an item was first written to the cache, and when the item was last read from the cache. If the item cache
    becomes full, DAX will evict older items (even if they have not expired yet) to make room for new items.
    The LRU algorithm is always enabled for the item cache, and is not user-configurable.
    Query Cache
    DAX also maintains a query cache to store the results from Query and Scan operations. The items in this
    cache represent result sets from queries and scans on DynamoDB tables. These result sets are stored by
    their parameter values.
    When an application sends a Query or Scan request, DAX attempts to read a matching result set from
    the query cache using the specified parameter values. If the result set is found (cache hit), DAX returns
    it to the application immediately. If the result set is not found (cache miss), DAX sends the request to
    DynamoDB. DynamoDB processes the requests using eventually consistent reads, and returns the result
    set to DAX. DAX stores it in the item cache, and then returns it to the application.
        Note
        You can specify the TTL setting for the query cache when you create a new DAX cluster. For
        more information, see Managing DAX Clusters (p. 625).)
    DAX also maintains a least recently used list (LRU) for the query cache. The LRU list keeps track of when
    a result set was first written to the cache, and when the result was last read from the cache. If the query
    cache becomes full, DAX will evict older result sets (even if they have not expired yet) to make room for
    new result sets. The LRU algorithm is always enabled for the query cache, and is not user-configurable.
    Topics
     • Nodes (p. 576)
     • Clusters (p. 576)
     • Regions and Availability Zones (p. 577)
     • Parameter Groups (p. 577)
     • Security Groups (p. 577)
     • Cluster ARN (p. 577)
     • Cluster Endpoint (p. 577)
     • Node Endpoints (p. 578)
     • Subnet Groups (p. 578)
     • Events (p. 578)
     • Maintenance Window (p. 578)
Nodes
A node is the smallest building block of a DAX cluster. Each node runs an instance of the DAX software,
and maintains a single replica of the cached data.
• By adding more nodes to the cluster. This will increase the overall read throughput of the cluster.
• By using a larger node type. Larger node types provide more capacity and can increase throughput.
  (Note that you must create a new cluster with the new node type.)
Every node within a cluster is of the same node type, and runs the same DAX caching software. For a list
of available node types, see https://aws.amazon.com/dynamodb/pricing.
Clusters
A cluster is a logical grouping of one or more nodes that DAX manages as a unit. One of the nodes in the
cluster is designated as the primary node, and the other nodes (if any) are read replicas.
When changes are made to cached data on the primary node, DAX propagates the changes to all of the
read replica nodes.
However, unlike the primary node, read replicas do not write to DynamoDB.
• Scalability. If you have a large number of application clients that need to access DAX concurrently, you
  can add more replicas for read-scaling. DAX will spread the load evenly across all of the nodes in the
  cluster. (Another way to increase throughput is to use larger cache node types.)
• High availability. In the event of a primary node failure, DAX automatically fails over to a read replica
  and designates it as the new primary. If a replica node fails, other nodes in the DAX cluster will still be
  able to serve requests until the failed node can be recovered. For maximum fault tolerance, you should
  deploy read replicas in separate Availability Zones. This configuration ensures that your DAX cluster
  can continue to function, even if an entire Availability Zone should become unavailable.
A DAX cluster can support up to ten nodes per cluster (the primary node, plus a maximum of nine read
replicas).
    Important
    A DAX cluster can be deployed with a single node. While this configuration might be acceptable
    for development or test workloads, a one-node cluster is not fault-tolerant and we do not
    recommend a one-node cluster for production use. If this single node encounters software or
    hardware errors, the cluster can become unavailable or lose cached data.
    For production usage, we strongly recommend using DAX with at least three nodes (the primary
    node and at least two read replicas), where the nodes are placed in different Availability Zones.
Each region is designed to be completely isolated from the other regions. Within each region are
multiple Availability Zones. By launching your nodes in different Availability Zones, you are able to
achieve the greatest possible fault tolerance.
    Important
    Do not place all of your cluster's nodes in a single Availability Zone. In this configuration, your
    DAX cluster will become unavailable in case of an Availability Zone failure.
    For production usage, we strongly recommend using DAX with at least three nodes (the primary
    node and at least two read replicas), where the nodes are placed in different Availability Zones.
Parameter Groups
Parameter groups are used to manage runtime settings for DAX clusters. DAX has several parameters that
you can use to optimize performance (such as defining a TTL policy for cached data). A parameter group
is a named set of parameters that you can apply to a cluster, thereby guaranteeing that all of the nodes
in that cluster are configured in exactly the same way.
Security Groups
A DAX cluster runs in an Amazon VPC environment—a virtual network that is dedicated to your AWS
account, and is isolated from other Amazon VPCs. A security group acts as a virtual firewall for your VPC,
allowing you to control inbound and outbound network traffic.
When you launch a cluster in your VPC, you add an ingress rule to your security group to allow incoming
network traffic. The ingress rule specifies the protocol (TCP) and port number (8111) for your cluster.
After you add this ingress rule to your security group, your applications running within your VPC can
access the DAX cluster.
Cluster ARN
Every DAX cluster is assigned an Amazon Resource Identifier (ARN). The ARN format is:
arn:aws:dax:region:accountID:cache/clusterName
You use the cluster ARN in an IAM policy to define permissions for DAX API actions. For more
information, see DAX Access Control (p. 631).
Cluster Endpoint
Every DAX cluster provides a cluster endpoint for use by your application. By accessing the cluster using
its endpoint, your application does not need to know the host names and port numbers of individual
nodes in the cluster. Your application automatically "knows" all of the nodes in the cluster, even if you
add or remove read replicas.
myDAXcluster.2cmrwl.clustercfg.dax.use1.cache.amazonaws.com:8111
Node Endpoints
Each of the individual nodes in a DAX cluster has its own host name and point number. Here is an
example of a node endpoint:
myDAXcluster-a.2cmrwl.clustercfg.dax.use1.cache.amazonaws.com:8111
Your application can access a node directly, using its endpoint; however, we recommend that you treat
the DAX cluster as a single unit, and access it using the cluster endpoint instead. The cluster endpoint
insulates your application from having to maintain a list of nodes, and keeping that list up to date when
you add or remove nodes from the cluster.
Subnet Groups
Access to DAX cluster nodes is restricted to applications running on Amazon EC2 instances within an
Amazon Virtual Private Cloud (Amazon VPC) environment. You can use subnet groups to grant cluster
access from Amazon EC2 instances running on specific subnets. A subnet group is a collection of subnets
(typically private) that you can designate for your clusters running in an Amazon Virtual Private Cloud
(VPC) environment.
When you create a DAX cluster, you must specify a subnet group. DAX uses that subnet group to select a
subnet and IP addresses within that subnet to associate with your nodes.
Events
DAX records significant events within your clusters, such as a failure to add a node, success in adding a
node, or changes to security groups. By monitoring key events, you can know the current state of your
clusters and, depending upon the event, be able to take corrective action. You can access these events
using the AWS Management Console, or the DescribeEvents action in the DAX management API.
You can also request that notifications be sent to a specific Amazon SNS topic, so that you will know
immediately when an event occurs in your DAX cluster.
Maintenance Window
Every cluster has a weekly maintenance window during which any system changes are applied. If you
don't specify a preferred maintenance window when you create or modify a cache cluster, DAX assigns a
60-minute maintenance window on a randomly selected day of the week.
The 60-minute maintenance window is selected at random from an 8-hour block of time per region. The
following table lists the time blocks for each region from which the default maintenance windows are
assigned.
    The maintenance window should fall at the time of lowest usage and thus might need modification from
    time to time. You can specify a time range of up to 24 hours in duration during which any maintenance
    activities you have requested should occur.
    After you have created your DAX cluster, you will be able to access it from an Amazon EC2 instance
    running in the same Amazon VPC. You will then be able to use your DAX cluster with an application
    program. (For more information, see Using the DAX Client in an Application (p. 592).)
    Topics
        • Creating a DAX Service Role (p. 579)
        • AWS CLI (p. 580)
        • AWS Management Console (p. 584)
    If you are using the AWS Management Console, the workflow for creating a cluster checks for the
    presence of a pre-existing DAX service role; if none is found, the console creates a new service role
    for you. For more information, see Step 2: Create a DAX Cluster (p. 585) in AWS Management
    Console (p. 584).
    If you are using the AWS CLI, you will need to specify a DAX service role that you have created previously.
    Otherwise, you will need to create a new service role beforehand. For more information, see Step 1:
    Create a Service Role for DAX (p. 581) in AWS CLI (p. 580).
    Otherwise, you will need to add the following permissions to your IAM policy so that your IAM user can
    create the service role:
    •    iam:CreateRole
    •    iam:CreatePolicy
    •    iam:AttachRolePolicy
    •    iam:PassRole
You should attach these permissions to the user that is attempting to perform the action.
    Note
    The iam:CreateRole, iam:CreatePolicy, iam:AttachPolicy and iam:PassRole
    permissions are not included in the AWS-managed policies for DynamoDB. This is by design,
    because these permissions provide the possibility of privilege escalation: A user could use these
    permissions to create a new administrator policy, and then attach that policy to an existing
    role. For this reason, you (the administrator of your DAX cluster) must explicitly add these
    permissions to your policy.
Troubleshooting
If your user policy is missing the iam:CreateRole, iam:CreatePolicy, and iam:AttachPolicy
permissions, you will encounter error messages. The following table shows these messages, and how to
correct the problems.
For more information about IAM policies required for DAX cluster administration, see DAX Access
Control (p. 631).
AWS CLI
Topics
 • Step 1: Create a Service Role for DAX (p. 581)
 • Step 2: Create a Subnet Group (p. 582)
 • Step 3: Create a DAX Cluster (p. 583)
 • Step 4: Configure Security Group Inbound Rules (p. 583)
This section describes how to create a DAX cluster using the AWS Command Line Interface (AWS CLI). If
you have not already done so, you will need to install and configure the AWS CLI. To do this, go to the
AWS Command Line Interface User Guide and follow these instructions:
     Important
     To manage DAX clusters with the AWS CLI, please install or upgrade to version 1.11.110 or
     higher.
     Note
     All of the AWS CLI examples use the us-west-2 region and fictitious account IDs.
In this step, you will create an IAM policy, and then attach that policy to an IAM role. This will enable you
to assign the role to a DAX cluster so that it can perform DynamoDB operations on your behalf.
     {
         "Version": "2012-10-17",
         "Statement": [
            {
                 "Effect": "Allow",
                 "Principal": {
                     "Service": "dax.amazonaws.com"
                 },
                 "Action": "sts:AssumeRole"
              }
         ]
     }
     {
         "Version": "2012-10-17",
         "Statement": [
             {
                 "Action": [
                     "dynamodb:*"
                 ],
                 "Effect": "Allow",
                 "Resource": [
                     "arn:aws:dynamodb:us-west-2:accountID:*"
                 ]
             }
         ]
     }
     Replace accountID with your AWS account ID. To find your AWS account ID, go to the upper right-
     hand portion of the AWS Management Console and choose your login ID. Your AWS account ID
     appears in the drop-down menu. (In the ARN, accountID must be a twelve-digit number. Do not
     use hyphens or any other punctuation.)
In the output, take note of the ARN for the policy you created. For example:
     arn:aws:iam::123456789012:policy/DAXServicePolicy
5.   Attach the policy to the service role:
Replace arn with the actual role ARN from the previous step.
DAX is designed to run within an Amazon Virtual Private Cloud environment (Amazon VPC). If you
created your AWS account after 2013-12-04, then you already have a default VPC in each AWS region.
For more information, see Your Default VPC and Subnets in the Amazon VPC User Guide.
As part of the creation process for a DAX cluster, you must specify a subnet group. A subnet group is a
collection of one or more subnets within your VPC. When you create your DAX cluster, the nodes will be
deployed to the subnets within the subnet group.
1. To determine the identifier for your default VPC, type the following command:
In the output, take note of the identifier for your default VPC. For example:
     vpc-12345678
2.   Determine the subnets IDs associated with your default VPC:
Replace vpcID with your actual VPC ID. For example: vpc-12345678
     In the output, take note of the subnet identifiers. For example: subnet-11111111
3.   Create the subnet group. Ensure that you specify at least one subnet ID in the --subnet-ids
     parameter:
1. Get the Amazon Resource Name (ARN) for your service role:
In the output, take note of the service role ARN. For example:
     arn:aws:iam::123456789012:role/DaxServiceRole
2.   You are now ready to create your DAX cluster:
     All of the nodes in the cluster will be of type dax.r3.large (--node-type). There will be three nodes
     (--replication-factor)—one primary node and two replicas.
1. To determine the default security group identifier, type the following command:
Replace vpcID with your actual VPC ID (from Step 2: Create a Subnet Group (p. 582)).
     In the output, take note of the security group identifier. For example: sg-01234567
2.   Now do this:
Topics
 • Step 1: Create a Subnet Group (p. 584)
 • Step 2: Create a DAX Cluster (p. 585)
 • Step 3: Configure Security Group Inbound Rules (p. 585)
DAX is designed to run within an Amazon Virtual Private Cloud environment (Amazon VPC). If you
created your AWS account after 2013-12-04, then you already have a default VPC in each AWS region.
For more information, see Your Default VPC and Subnets in the Amazon VPC User Guide.
As part of the creation process for a DAX cluster, you must specify a subnet group. A subnet group is a
collection of one or more subnets within your VPC. When you create your DAX cluster, the nodes will be
deployed to the subnets within the subnet group.
              that you choose multiple subnet IDs, so that DAX can deploy the cluster nodes into
              multiple Availability Zones. If an Availability Zone becomes unavailable, DAX will
              automatically fail over to a surviving Availability Zone, and your DAX cluster will
              continue to function without interruption.
When the settings are as you want them, click Create subnet group.
          • IAM role name—type a name for an IAM role. For example: DAXServiceRole. The console will
            create a new IAM role , and your DAX cluster assume this role at runtime.
          • IAM policy name—type a name for an IAM policy. For example: DAXServicePolicy. The console
            will create a new IAM policy, and attach the policy to the IAM role.
          • IAM role policy—choose Read/Write. This will allow the DAX cluster to perform read and
            write operations in DynamoDB.
        • Target DynamoDB table—choose All tables.
     f. Subnet group—choose the subnet group that you created in Step 1: Create a Subnet
        Group (p. 584).
     g. Security Groups—choose default.
5.   When the settings are as you want them, choose Launch cluster.
On the Clusters screen, your DAX cluster will be listed with a status of Creating.
     Note
     Creating the cluster will take several minutes. When the cluster is ready, its status changes to
     Available.
     In the meantime, you can proceed to Step 3: Configure Security Group Inbound Rules (p. 585)
     and follow the instructions there.
         Note
         If you launched your DAX cluster with a different security group (other than default), you will
         need to perform this procedure for that group instead.
Source—type default, and then choose the identifier for your default security group.
    In many use cases, the way that your application uses DAX will affect the consistency of data within the
    DAX cluster, as well as the consistency of data between DAX and DynamoDB.
    Topics
     • Consistency Among DAX Cluster Nodes (p. 586)
     • DAX Item Cache Behavior (p. 587)
     • DAX Query Cache Behavior (p. 588)
     • Strongly Consistent Reads (p. 589)
     • Negative Caching (p. 589)
     • Strategies for Writes (p. 590)
    When your DAX cluster is running, it will replicate the data among all of the nodes in the cluster
    (assuming that you have provisioned more than one node). Consider an application that performs a
    successful UpdateItem using DAX. This causes the item cache in the primary node to be modified with
    the new value; that value will then be replicated to all of the other nodes in the cluster. This replication is
    eventually consistent, and usually takes less than one second to complete.
    In this scenario, it is possible for two clients to read the same key from the same DAX cluster but receive
    different values, depending on the node that each client accessed. The nodes will all be consistent when
    the update has been fully replicated throughout all of the nodes in the cluster. (Note that this behavior is
    similar to the eventually consistent nature of DynamoDB.)
    If you are building an application that uses DAX, that application should be designed in such a way that it
    can tolerate eventually consistent data.
Consistency of Reads
With Amazon DynamoDB, the GetItem operation performs an eventually consistent read by default. If
you use UpdateItem with the DynamoDB client, and then attempt to read the same item immediately
afterward, you might see the data as it appeared prior to the update. This is due to propagation delay
across all of the DynamoDB storage locations. Consistency is usually reached within seconds, so if you
retry the read, you will likely see the updated item.
When you use GetItem with the DAX client, the operation proceeds as follows:
1. The DAX client issues a GetItem request. DAX attempts to read the requested item from the item
   cache. If the item is in the cache (cache hit), DAX returns it to the application.
2. If the item is not available (cache miss), DAX performs an eventually consistent GetItem operation
   against DynamoDB.
3. DynamoDB returns the requested item, and DAX stores it in the item cache.
4. DAX returns the item to the application.
5. (not shown) If the DAX cluster contains more than one node, the item is replicated to all of the other
   nodes in the cluster.
The item will remain in the DAX item cache, subject to the TTL setting and LRU algorithm for the cache
(see Concepts (p. 572)). However, during this period, DAX will not re-read the item from DynamoDB.
If someone else updates the item using a DynamoDB client, bypassing DAX entirely, then a GetItem
request using the DAX client will yield different results from the same GetItem request using the
DynamoDB client. In this scenario, DAX and DynamoDB will hold inconsistent values for the same key
until the TTL for the DAX item has expired.
If an application modifies data in an underlying DynamoDB table, bypassing DAX, the application will
need to anticipate and tolerate data inconsistencies that might arise.
    Note
    In addition to GetItem, the DAX client also supports BatchGetItem requests. BatchGetItem
    is essentially a wrapper around one or more GetItem requests, so DAX treats each of these as
    an individual GetItem operation.
Consistency of Writes
DAX is a write-through cache, which simplifies the process of keeping the DAX item cache consistent with
the underlying DynamoDB tables.
The DAX client supports the same write API operations as DynamoDB (PutItem, UpdateItem,
DeleteItem, and BatchWriteItem). When you use these operations with the DAX client, the items are
modified in both DAX and DynamoDB. DAX will update the items in its item cache, regardless of the TTL
value for these items.
For example, suppose you issue a GetItem request from the DAX client to read an item from the
ProductCatalog table. (The partition key is Id; there is no sort key.) You retrieve the item whose Id is 101;
the QuantityOnHand value for that item is 42. DAX stores the item in its item cache with a specific TTL;
for this example, let us assume that the TTL is ten minutes. Three minutes later, another application
uses the DAX client to update the same item, so that its QuantityOnHand value is now 41. Assuming that
the item is not updated again, any subsequent reads of the same item during the next ten minutes will
return the cached value for QuantityOnHand (41).
DAX supports the following write operations: PutItem, UpdateItem, DeleteItem, and
BatchWriteItem. When you send one of these requests to DAX, it does the following:
If a write to DynamoDB fails for any reason, including throttling, then the item will not be cached in DAX
and the exception for the failure will be returned to the requester. This ensures that data is not written to
the DAX cache unless it is first written successfully to DynamoDB.
    Note
    Every write to DAX alters the state of the item cache; however, writes to the item cache do not
    affect the query cache. (The DAX item cache and query cache serve different purposes, and
    operate independently from one another.)
result set is saved in the query cache—not in the item cache. You cannot "warm up" the item cache by
performing a Scan operation, because the item cache and query cache are separate entities.
Consistency of Query-Update-Query
Updates to the item cache, or to the underlying DynamoDB table, do not invalidate or modify the results
stored in the query cache.
To illustrate, consider the following scenario where an application is working with a table named
DocumentRevisions, which has DocId as its partition key and RevisionNumber as its sort key.
1. A client issues a Query for DocId 101, for all items with RevisionNumber is greater than or equal to 5.
   DAX stores the result set in the query cache, and returns the result set to the user.
2. The client issues a PutItem request for DocId 101 with a RevisionNumber value of 20.
3. The client issues the same Query as described in step 1 (DocId 101 and RevisionNumber >= 5).
In this scenario, the cached result set for the Query issued in step 3 will be identical to the result set that
was cached in step 1. The reason is that DAX does not invalidate Query or Scan result sets based upon
updates to individual items. The PutItem operation from step 2 will only be reflected in the DAX query
cache when the TTL for the Query expires.
Your application should consider the TTL value for the query cache, and how long your application is
able to tolerate inconsistent results between the query cache and the item cache.
Negative Caching
DAX supports negative cache entries, in both the item cache and the query cache. A negative cache entry
occurs when DAX cannot find requested items in an underlying DynamoDB table. Instead of generating
an error, DAX caches an empty result and returns that result to the user.
For example, suppose that an application sends a GetItem request to a DAX cluster, and that there is
no matching item in the DAX item cache. This will cause DAX to read the corresponding item from the
underlying DynamoDB table. If the item does not exist in DynamoDB, then DAX will store an empty item
in its item cache, and then return the empty item to the application. Now suppose the application sends
another GetItem request for the same item. DAX will find the empty item in the item cache, and return
it to the application immediately. It does not consult DynamoDB at all.
A negative cache entry will remain in the DAX item cache until its item TTL has expired, LRU is invoked,
or until the item is modified using PutItem, UpdateItem or DeleteItem.
The DAX query cache handles negative cache results in a similar way. If an application performs a Query
or Scan, and the DAX query cache does not contain a cached result, then DAX sends the request to
DynamoDB. If there are no matching items in the result set, then DAX stores an empty result set in the
query cache, and returns the empty result set to the application. Subsequent Query or Scan requests
will yield the same (empty) result set, until the TTL for that result set has expired.
For applications that are sensitive to latency, writing through DAX incurs an extra network hop, so a write
to DAX will be a little slower than a write directly to DynamoDB. If your application is sensitive to write
latency, you can reduce the latency by writing directly to DynamoDB instead. (For more information,
see Write-Around (p. 591).)
For write-intensive applications (such as those that perform bulk data loading), it might not be desirable
to write all of the data through DAX because only a very small percentage of that data is ever read by
the application. When you write large amounts of data through DAX, it must invoke its LRU algorithm to
make room in the cache for the new items to be read. This diminishes the effectiveness of DAX as a read
cache.
When you write an item to DAX, the item cache state is altered to accommodate the new item. (For
example, DAX might need to evict older data from the item cache to make room for the new item.) The
new item remains in the item cache, subject to the cache's LRU algorithm and the TTL setting for the
cache. As long as the item persists in the item cache, DAX will not re-read the item from DynamoDB.
Write-Through
The DAX item cache implements a write-through policy (see How DAX Processes Writes (p. 588)).
When you write an item, DAX ensures that the cached item is synchronized with the item as it exists in
DynamoDB. This is helpful for applications that need to re-read an item immediately after writing it.
However, if other applications write directly to a DynamoDB table, the item in the DAX item cache will no
longer be in sync with DynamoDB.
To illustrate, consider two users (Alice and Bob) who are working with the ProductCatalog table. Alice
accesses the table using DAX, but Bob bypasses DAX and accesses the table directly in DynamoDB.
1. Alice updates an item in the ProductCatalog table. DAX forwards the request to DynamoDB, and the
   update succeeds. DAX then writes the item to its item cache, and returns a successful response to
   Alice. From that point on, until the item is ultimately evicted from the cache, any user who reads the
   item from DAX will see the item with Alice's update.
2. A short time later, Bob updates the same ProductCatalog item that Alice wrote—however, Bob updates
   the item directly in DynamoDB. DAX does not automatically refresh its item cache in response to
   updates via DynamoDB; therefore, DAX users do not see Bob's update.
3. Alice reads the item from DAX again. The item is in the item cache, so DAX returns it to Alice without
   accessing the DynamoDB table.
In this scenario, Alice and Bob will see different representations of the same ProductCatalog item. This
will be the case until DAX evicts the item from the item cache, or until another user updates the same
item again using DAX.
Write-Around
If your application needs to write large quantities of data (such as a bulk data load), it might make sense
to bypass DAX and write the data directly to DynamoDB. Such a write-around strategy will reduce write
latency; however, the item cache will not remain in sync with the data in DynamoDB.
If you decide to use a write-around strategy, remember that DAX will populate its item cache whenever
applications use the DAX client to read data. This can be advantageous in some cases, because it ensures
that only the most frequently-read data is cached (as opposed to the most-frequently written data).
Consider a user (Charlie) who wants to work with the GameScores table using DAX. The partition key for
GameScores is UserId, so all of Charlie's scores would have the same UserId.
1. Charlie wants to retrieve all of his scores, so he sends a Query to DAX. Assuming that this query has
   not been issued before, DAX forwards the query to DynamoDB for processing, stores the results in the
   DAX query cache, and then returns the results to Charlie. The result set will remain available in the
   query cache until it is evicted.
2. Now suppose that Charlie plays the Meteor Blasters game and achieves a high score. Charlie sends an
   UpdateItem request to DynamoDB, modifying an item in the GameScores table.
3. Finally, Charlie decides to rerun his earlier Query to retrieve all of his data from GameScores. Charlie
   does not see his high score for Meteor Blasters in the results. This is because the query results come
   from the query cache, not the item cache. (The two caches are independent from one another, so a
   change in one cache does not affect the other cache.)
DAX does not refresh result sets in the query cache with the most current data from DynamoDB. Each
result set in the query cache is current as of the time that the Query or Scan operation was performed.
Thus, Charlie's Query results do not reflect his PutItem operation. This will be the case until DAX evicts
the result set from the query cache.
     This section demonstrates how to launch an Amazon EC2 instance in your default Amazon VPC, connect
     to the instance, and run a sample application.
     This section also provides information on how to modify your existing application so that it can leverage
     your DAX cluster.
          Note
          DAX clients for various programmling languages are available at the following URL:
• http://dax-sdk.s3-website-us-west-2.amazonaws.com
          Note
          In order to complete this tutorial, you must have a DAX cluster running in your default VPC. If
          you have not yet created a DAX cluster, see Creating a DAX Cluster (p. 579) for instructions.
• At the top of the list of AMIs, go to Amazon Linux AMI and choose Select.
     • Choose Launch.
3.   In the Select an existing key pair or create a new key pair window, do one of the following:
     • If you do not have an Amazon EC2 key pair, choose Create a new key pair and follow the
       instructions. You will be asked to download a private key file (.pem file); you will need this file later
       when you log in to your Amazon EC2 instance.
     • If you already have an existing Amazon EC2 key pair, go to Select a key pair and choose your
       key pair from the list. Note that you must already have the private key file ( .pem file) available in
       order to log in to your Amazon EC2 instance.
4.   When you have configured your key pair, choose Launch Instances.
5.   In the console navigation pane, choose EC2 Dashboard and then choose the instance that you
     launched. In the lower pane, on the Description tab, find the Public DNS for your instance. For
     example: ec2-11-22-33-44.us-west-2.compute.amazonaws.com. Make a note of this
     public DNS name, because you will need it for the next step (Step 3: Configure Your Amazon EC2
     Instance (p. 594)).
     Note
     It will take a few minutes for your Amazon EC2 instance to become available. In the meantime,
     you can proceed to Step 2: Create an IAM User and Policy (p. 593) and follow the instructions
     there.
       {
            "Version": "2012-10-17",
            "Statement": [
                {
                    "Action": [
                        "dax:*"
                    ],
                    "Effect": "Allow",
                    "Resource": [
                        "*"
                    ]
                },
                {
                    "Action": [
                        "dynamodb:*"
                    ],
                    "Effect": "Allow",
                    "Resource": [
                        "*"
                    ]
                }
            ]
       }
1.   If you haven't already done so, open the Amazon EC2 console at https://console.aws.amazon.com/
     ec2/.
2.   Use the ssh command to log in to your Amazon EC2 instance. For example:
     You will need to specify your private key file (.pem file) and the public DNS name of your instance.
     (See Step 1: Launch an Amazon EC2 Instance (p. 592)).
aws configure
Topics
 • Java and DAX (p. 595)
 • Node.js and DAX (p. 603)
 • .NET and DAX (p. 609)
 • Python and DAX (p. 617)
2. Download the AWS SDK for Java (.zip file), and then extract it:
wget http://sdk-for-java.amazonwebservices.com/latest/aws-java-sdk.zip
unzip aws-java-sdk.zip
3. Download the latest version of the DAX Java client (.jar file):
wget http://dax-sdk.s3-website-us-west-2.amazonaws.com/java/DaxJavaClient-latest.jar
export SDKVERSION=sdkVersion
     export CLASSPATH=.:./DaxJavaClient-latest.jar:aws-java-sdk-$SDKVERSION/lib/aws-java-
     sdk-$SDKVERSION.jar:aws-java-sdk-$SDKVERSION/third-party/lib/*
     Replace sdkVersion with the actual version number of the AWS SDK for Java. For example:
     1.11.112
5.   Download the sample program source code (.zip file):
wget http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/samples/TryDax.zip
unzip TryDax.zip
javac TryDax*.java
java TryDax
     Take note of the timing information—the number of milliseconds required for the GetItem, Query
     and Scan tests.
8.   In the previous step, you ran the program against the DynamoDB endpoint. You will now run the
     program again, but this time the GetItem, Query and Scan operations will be processed by your
     DAX cluster.
To determine the endpoint for your DAX cluster, choose one of the following:
     • Using the DynamoDB console—choose your DAX cluster. The cluster endpoint is shown in the
       console. For example:
mycluster.frfx8h.clustercfg.dax.amazonaws.com:8111
The cluster endpoint port and address are shown in the output. For example:
       {
           "Port": 8111,
           "Address":"mycluster.frfx8h.clustercfg.dax.amazonaws.com"
       }
     Now run the program again—but this time, specify the cluster endpoint as a command line
     parameter:
     Look at the rest of the output, and take note of the timing information. The elapsed times for
     GetItem, Query and Scan should be significantly lower with DAX than with DynamoDB.
For more information about this program, see the following sections:
TryDax.java
The TryDax.java file contains the main method. If you run the program with no command line
parameters, it creates a DynamoDB client and uses that client for all API operations. If you specify a DAX
cluster endpoint on the command line, the program also creates a DAX client and uses it for GetItem,
Query and Scan operations.
• Use the DAX client instead of the DynamoDB client (see Java and DAX (p. 595)).
• Choose a different name for the test table.
• Modify the number of items written by changing the helper.writeData parameters. The second
  parameter is the number of partition keys, and the third parameter is the number of sort keys. By
  default, the program uses 1-10 for partition key values, and 1-10 for sort key values, for a total of 100
  items written to the table. (For more information, see TryDaxHelper.java (p. 599))
• Modify the number of GetItem, Query and Scan tests, and modify their parameters.
• Comment out the lines containing helper.createTable and helper.deleteTable (if you do not
  want to create and delete the table each time you run the program).
    Note
    To run this program, you must include both the DAX Java client and the AWS SDK for Java in
    your classpath. See Java and DAX (p. 595) for an example of setting your CLASSPATH variable.
import com.amazonaws.services.dynamodbv2.document.DynamoDB;
         System.out.println("Creating table...");
         helper.createTable(tableName, ddbClient);
         System.out.println("Populating table...");
         helper.writeData(tableName, ddbClient, 10, 10);
         // GetItem
         tests.getItemTest(tableName, testClient, 1, 10, 5);
// Query
          // Scan
          tests.scanTest(tableName, testClient, 5);
          helper.deleteTable(tableName, ddbClient);
    }
TryDaxHelper.java
The getDynamoDBClient and getDaxClient methods provide DynamoDB and DAX clients. For
control plane operations (CreateTable, DeleteTable) and write operations, the program uses the
DynamoDB client. If you specify a DAX cluster endpoint, the main program creates a DAX client for
performing read operations (GetItem, Query, Scan).
The other TryDaxHelper methods (createTable, writeData, deleteTable) are for setting up and
tearing down the DynamoDB table and its data.
    Note
    To run this program, you must include both the DAX Java client and the AWS SDK for Java in
    your classpath. See Java and DAX (p. 595) for an example of setting your CLASSPATH variable.
import java.util.Arrays;
import   com.amazon.dax.client.dynamodbv2.AmazonDaxClientBuilder;
import   com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
import   com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder;
import   com.amazonaws.services.dynamodbv2.document.DynamoDB;
import   com.amazonaws.services.dynamodbv2.document.Item;
import   com.amazonaws.services.dynamodbv2.document.Table;
import   com.amazonaws.services.dynamodbv2.model.AttributeDefinition;
import   com.amazonaws.services.dynamodbv2.model.KeySchemaElement;
import   com.amazonaws.services.dynamodbv2.model.KeyType;
import   com.amazonaws.services.dynamodbv2.model.ProvisionedThroughput;
import   com.amazonaws.services.dynamodbv2.model.ScalarAttributeType;
import   com.amazonaws.util.EC2MetadataUtils;
    DynamoDB getDynamoDBClient() {
        System.out.println("Creating a DynamoDB client");
        AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard()
                .withRegion(region)
                .build();
             table = client.createTable(tableName,
                     Arrays.asList(
                             new KeySchemaElement("pk", KeyType.HASH),   // Partition key
                             new KeySchemaElement("sk", KeyType.RANGE)), // Sort key
                     Arrays.asList(
                             new AttributeDefinition("pk", ScalarAttributeType.N),
                             new AttributeDefinition("sk", ScalarAttributeType.N)),
                     new ProvisionedThroughput(10L, 10L));
             table.waitForActive();
             System.out.println("Successfully created table. Table status: " +
                     table.getDescription().getTableStatus());
         } catch (Exception e) {
             System.err.println("Unable to create table: ");
             e.printStackTrace();
         }
   }
         try {
             for (Integer ipk = 1; ipk <= pkmax; ipk++) {
                 System.out.println(("Writing " + skmax + " items for partition key: " +
ipk));
                 for (Integer isk = 1; isk <= skmax; isk++) {
                      table.putItem(new Item()
                              .withPrimaryKey("pk", ipk, "sk", isk)
                              .withString("someData", someData));
                 }
             }
         } catch (Exception e) {
             System.err.println("Unable to write item:");
             e.printStackTrace();
         }
   }
              table.waitForDelete();
              System.out.println("Successfully deleted table.");
          } catch (Exception e) {
              System.err.println("Unable to delete table: ");
              e.printStackTrace();
          }
    }
TryDaxTests.java
The TryDaxTests.java file contains methods that perform read operations against a test table
in DynamoDB. These methods are not concerned with how they access the data (using either the
DynamoDB client or the DAX client), so there is no need to modify the application logic.
    Note
    To run this program, you must include both the DAX Java client and the AWS SDK for Java in
    your classpath. See Java and DAX (p. 595) for an example of setting your CLASSPATH variable.
import java.util.HashMap;
import java.util.Iterator;
import   com.amazonaws.services.dynamodbv2.document.DynamoDB;
import   com.amazonaws.services.dynamodbv2.document.Item;
import   com.amazonaws.services.dynamodbv2.document.ItemCollection;
import   com.amazonaws.services.dynamodbv2.document.QueryOutcome;
import   com.amazonaws.services.dynamodbv2.document.ScanOutcome;
import   com.amazonaws.services.dynamodbv2.document.Table;
import   com.amazonaws.services.dynamodbv2.document.spec.QuerySpec;
    void getItemTest(String tableName, DynamoDB client, int pk, int sk, int iterations) {
        long startTime, endTime;
        System.out.println("GetItem test - partition key " + pk + " and sort keys 1-" +
 sk);
        Table table = client.getTable(tableName);
    void queryTest(String tableName, DynamoDB client, int pk, int sk1, int sk2, int
 iterations) {
        long startTime, endTime;
        System.out.println("Query test - partition key " + pk + " and sort keys between " +
 sk1 + " and " + sk2);
        Table table = client.getTable(tableName);
            try {
                Iterator<Item> iter = items.iterator();
                while (iter.hasNext()) {
                    iter.next();
                }
            } catch (Exception e) {
                System.err.println("Unable to query table:");
                e.printStackTrace();
            }
            endTime = System.nanoTime();
            printTime(startTime, endTime, iterations);
        }
    }
b. Activate nvm:
. ~/.nvm/nvm.sh
wget http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/samples/TryDax.zip
unzip TryDax.zip
     node 01-create-table.js
     node 02-write-data.js
     The first program creates a DynamoDB table named TryDaxTable. The second program writes data to
     the table.
5.   Run the following Node.js programs:
     node 03-getitem-test.js
     node 04-query-test.js
     node 05-scan-test.js
     Take note of the timing information—the number of milliseconds required for the GetItem, Query
     and Scan tests.
6.   In the previous step, you ran the programs against the DynamoDB endpoint. You will now run the
     programs again, but this time the GetItem, Query and Scan operations will be processed by your
     DAX cluster.
To determine the endpoint for your DAX cluster, choose one of the following:
     • Using the DynamoDB console—choose your DAX cluster. The cluster endpoint is shown in the
       console. For example:
mycluster.frfx8h.clustercfg.dax.amazonaws.com:8111
The cluster endpoint port and address are shown in the output. For example:
       {
           "Port": 8111,
           "Address":"mycluster.frfx8h.clustercfg.dax.amazonaws.com"
       }
     Now run the programs again—but this time, specify the cluster endpoint as a command line
     parameter:
     Look at the rest of the output, and take note of the timing information. The elapsed times for
     GetItem, Query and Scan should be significantly lower with DAX than with DynamoDB.
7.   Run the following Node.js program to delete TryDaxTable:
node 06-delete-table
For more information about these programs, see the following sections:
01-create-table.js
The 01-create-table.js program creates a table (TryDaxTable). The remaining Node.js programs in
this section depend on this table.
AWS.config.update({
  region: region
});
var params = {
    TableName : tableName,
    KeySchema: [
        { AttributeName: "pk",   KeyType: "HASH"}, //Partition key
        { AttributeName: "sk",   KeyType: "RANGE" } //Sort key
    ],
    AttributeDefinitions: [
        { AttributeName: "pk",   AttributeType: "N" },
        { AttributeName: "sk",   AttributeType: "N" }
    ],
    ProvisionedThroughput: {
        ReadCapacityUnits: 10,
        WriteCapacityUnits: 10
    }
};
02-write-data.js
The 02-write-data.js program writes test data to TryDaxTable.
AWS.config.update({
  region: region
});
        //
        //put item
    }
}
03-getitem-test.js
AWS.config.update({
  region: region
});
if (process.argv.length > 2) {
    var dax = new AmazonDaxClient({endpoints: [process.argv[2]], region: region})
    daxClient = new AWS.DynamoDB.DocumentClient({service: dax });
}
var pk = 1;
var sk = 10;
var iterations = 5;
            var params = {
                TableName: tableName,
                    Key:{
                        "pk": ipk,
                        "sk": isk
                    }
              };
04-query-test.js
AWS.config.update({
  region: region
});
if (process.argv.length > 2) {
    var dax = new AmazonDaxClient({endpoints: [process.argv[2]], region: region})
    daxClient = new AWS.DynamoDB.DocumentClient({service: dax });
}
var tableName = "TryDaxTable";
var   pk = 5;
var   sk1 = 2;
var   sk2 = 9;
var   iterations = 5;
var params = {
    TableName: tableName,
    KeyConditionExpression: "pk = :pkval and sk between :skval1 and :skval2",
    ExpressionAttributeValues: {
        ":pkval":pk,
        ":skval1":sk1,
        ":skval2":sk2
    }
};
05-scan-test.js
The 05-scan-test.js program performs Scan operations on TryDaxTable.
AWS.config.update({
  region: region
});
if (process.argv.length > 2) {
    var dax = new AmazonDaxClient({endpoints: [process.argv[2]], region: region})
    client = new AWS.DynamoDB.DocumentClient({service: dax });
}
var iterations = 5;
var params = {
    TableName: tableName
};
06-delete-table.js
The 06-delete-table.js program deletes TryDaxTable. Run this program after you are done testing.
AWS.config.update({
  region: region
});
var params = {
    TableName : tableName
};
To run the .NET sample on your Amazon EC2 instance, follow this proceedure:
     mkdir dotnet
     tar zxvf dotnet-sdk-N.N.N-linux-x64.tar.gz -C dotnet
     Replace N.N.N with the actual version number of the .NET Core SDK. For example: 2.1.4
3.   Verify the installation:
     alias dotnet=$HOME/dotnet/dotnet
     dotnet --version
     This should print the version number of the .NET Core SDK.
         Note
         Instead of the versioun number, you might receive the following error:
         error: libunwind.so.8: cannot open shared object file: No such file
         or directory
         After you do this, you should be able to run the dotnet --version command without
         any errors.
4.   Create a new .NET project:
     This will require a few minutes to perform a one-time-only setup. When it completes, run the
     sample project:
<Project Sdk="Microsoft.NET.Sdk">
       <PropertyGroup>
         <OutputType>Exe</OutputType>
         <TargetFramework>netcoreapp2.0</TargetFramework>
       </PropertyGroup>
       <ItemGroup>
         <PackageReference Include="AWSSDK.DAX.Client" Version="1.0.0" />
       </ItemGroup>
</Project>
wget http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/samples/TryDax.zip
unzip TryDax.zip
7.   You will now run the sample programs, one at a time. For each program, you will copy its contents
     into the myApp/Program.cs, and then run the MyApp project..
     cp 01-CreateTable.cs myApp/Program.cs
     dotnet run --project myApp
     cp 02-Write-Data.cs myApp/Program.cs
     dotnet run --project myApp
     The first program creates a DynamoDB table named TryDaxTable. The second program writes data
     to the table.
8.   You will now some programs to perform GetItem, Query and Scan operations on your DAX cluster.
     To determine the endpoint for your DAX cluster, choose one of the following:
                                   API Version 2012-08-10
                                             610
                               Amazon DynamoDB Developer Guide
                              Tutorial: Running a Sample Application
     • Using the DynamoDB console—choose your DAX cluster. The cluster endpoint is shown in the
       console. For example:
mycluster.frfx8h.clustercfg.dax.amazonaws.com:8111
The cluster endpoint port and address are shown in the output. For example:
       {
           "Port": 8111,
           "Address":"mycluster.frfx8h.clustercfg.dax.amazonaws.com"
       }
     Now run the following programs, specifying your cluster endpoint as a command line parameter.
     (Replace the sample endpoint with your actual DAX cluster endpoint.)
     cp 03-GetItem-Test.cs myApp/Program.cs
     dotnet run --project myApp mycluster.frfx8h.clustercfg.dax.amazonaws.com:8111
     cp 04-Query-Test.cs myApp/Program.cs
     dotnet run --project myApp mycluster.frfx8h.clustercfg.dax.amazonaws.com:8111
     cp 05-Scan-Test.cs myApp/Program.cs
     dotnet run --project myApp mycluster.frfx8h.clustercfg.dax.amazonaws.com:8111
     Take note of the timing information—the number of milliseconds required for the GetItem, Query
     and Scan tests.
9.   Run the following .NET program to delete TryDaxTable:
     cp 06-DeleteTable.cs myApp/Program.cs
     dotnet run --project myApp
For more information about these programs, see the following sections:
01-CreateTable.cs
The 01-CreateTable.cs program creates a table (TryDaxTable). The remaining .NET programs in this
section depend on this table.
using Amazon.DynamoDBv2.Model;
using System.Collections.Generic;
using System;
using Amazon.DynamoDBv2;
namespace ClientTest
{
    class Program
    {
        static void Main(string[] args)
        {
02-Write-Data.cs
The 02-Write-Data.cs program writes test data to TryDaxTable.
using   Amazon.DynamoDBv2.Model;
using   System.Collections.Generic;
using   System;
using   Amazon.DynamoDBv2;
namespace ClientTest
{
    class Program
    {
        static void Main(string[] args)
        {
              {
                  Console.WriteLine("Writing " + skmax + " items for partition key: " + ipk);
                  for (var isk = 1; isk <= skmax; isk++)
                  {
                      var request = new PutItemRequest()
                      {
                           TableName = tableName,
                           Item = new Dictionary<string, AttributeValue>()
                         {
                              { "pk", new AttributeValue{N = ipk.ToString() } },
                             { "sk", new AttributeValue{N = isk.ToString() } },
                             { "someData", new AttributeValue{S = someData } }
                         }
                      };
                  }
              }
03-GetItem-Test.cs
The 03-GetItem-Test.cs program performs GetItem operations on TryDaxTable.
using   Amazon.Runtime;
using   Amazon.DAX;
using   Amazon.DynamoDBv2.Model;
using   System.Collections.Generic;
using   System;
using   Amazon.DynamoDBv2;
using   Amazon;
namespace ClientTest
{
    class Program
    {
        static void Main(string[] args)
        {
              };
              var client = new ClusterDaxClient(clientConfig);
              var pk = 1;
              var sk = 10;
              var iterations = 5;
04-Query-Test.cs
The 04-Query-Test.cs program performs Query operations on TryDaxTable.
using   Amazon.Runtime;
using   Amazon.DAX;
using   Amazon.DynamoDBv2.Model;
using   System.Collections.Generic;
using   System;
using   Amazon.DynamoDBv2;
using   Amazon;
namespace ClientTest
{
    class Program
    {
        static void Main(string[] args)
        {
};
              var   pk = 5;
              var   sk1 = 2;
              var   sk2 = 9;
              var   iterations = 5;
05-Scan-Test.cs
The 05-Scan-Test.cs program performs Scan operations on TryDaxTable.
using   Amazon.Runtime;
using   Amazon.DAX;
using   Amazon.DynamoDBv2.Model;
using   System.Collections.Generic;
using   System;
using   Amazon.DynamoDBv2;
using   Amazon;
namespace ClientTest
{
    class Program
    {
        static void Main(string[] args)
        {
            };
            var client = new ClusterDaxClient(clientConfig);
var iterations = 5;
06-DeleteTable.cs
The 06-DeleteTable.cs program deletes TryDaxTable. Run this program after you are done testing.
using Amazon.DynamoDBv2.Model;
using System;
using Amazon.DynamoDBv2;
namespace ClientTest
{
    class Program
    {
        static void Main(string[] args)
        {
wget http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/samples/TryDax.zip
unzip TryDax.zip
     python 01-create-table.py
     python 02-write-data.py
     The first program creates a DynamoDB table named TryDaxTable. The second program writes data to
     the table.
4.   Run the following Python programs:
     python 03-getitem-test.py
     python 04-query-test.py
     python 05-scan-test.py
     Take note of the timing information—the number of milliseconds required for the GetItem, Query
     and Scan tests.
5.   In the previous step, you ran the programs against the DynamoDB endpoint. You will now run the
     programs again, but this time the GetItem, Query and Scan operations will be processed by your
     DAX cluster.
To determine the endpoint for your DAX cluster, choose one of the following:
     • Using the DynamoDB console—choose your DAX cluster. The cluster endpoint is shown in the
       console. For example:
mycluster.frfx8h.clustercfg.dax.amazonaws.com:8111
The cluster endpoint port and address are shown in the output. For example:
       {
           "Port": 8111,
           "Address":"mycluster.frfx8h.clustercfg.dax.amazonaws.com"
       }
     Now run the programs again—but this time, specify the cluster endpoint as a command line
     parameter:
     Look at the rest of the output, and take note of the timing information. The elapsed times for
     GetItem, Query and Scan should be significantly lower with DAX than with DynamoDB.
6.   Run the following Python program to delete TryDaxTable:
python 06-delete-table.py
For more information about these programs, see the following sections:
01-create-table.py
The 01-create-table.py program creates a table (TryDaxTable). The remaining Python programs in
this section depend on this table.
#!/usr/bin/env python3
from __future__ import print_function
import os
import amazondax
import botocore.session
session = botocore.session.get_session()
dynamodb = session.create_client('dynamodb', region_name=region) # low-level client
table_name = "TryDaxTable"
params = {
    'TableName' : table_name,
    'KeySchema': [
        { 'AttributeName': "pk", 'KeyType': "HASH"},              # Partition key
        { 'AttributeName': "sk", 'KeyType': "RANGE" }             # Sort key
    ],
    'AttributeDefinitions': [
02-write-data.py
#!/usr/bin/env python3
from __future__ import print_function
import os
import amazondax
import botocore.session
session = botocore.session.get_session()
dynamodb = session.create_client('dynamodb', region_name=region) # low-level client
table_name = "TryDaxTable"
        dynamodb.put_item(**params)
        print("PutItem ({}, {}) suceeded".format(ipk, isk))
03-getitem-test.py
#!/usr/bin/env python
from __future__ import print_function
session = botocore.session.get_session()
dynamodb = session.create_client('dynamodb', region_name=region) # low-level client
table_name = "TryDaxTable"
if len(sys.argv) > 1:
    endpoint = sys.argv[1]
    dax = amazondax.AmazonDaxClient(session, region_name=region, endpoints=[endpoint])
    client = dax
else:
    client = dynamodb
pk = 10
sk = 10
iterations = 50
start = time.time()
for i in range(iterations):
    for ipk in range(1, pk+1):
        for isk in range(1, sk+1):
            params = {
                'TableName': table_name,
                'Key': {
                    "pk": {'N': str(ipk)},
                    "sk": {'N': str(isk)}
                }
            }
            result = client.get_item(**params)
            print('.', end='', file=sys.stdout); sys.stdout.flush()
print()
end = time.time()
print('Total time: {} ms - Avg time: {} ms'.format(end - start, (end-start)/iterations))
04-query-test.py
The 04-query-test.py program performs Query operations on TryDaxTable.
#!/usr/bin/env python
from __future__ import print_function
session = botocore.session.get_session()
dynamodb = session.create_client('dynamodb', region_name=region) # low-level client
table_name = "TryDaxTable"
if len(sys.argv) > 1:
    endpoint = sys.argv[1]
    dax = amazondax.AmazonDaxClient(session, region_name=region, endpoints=[endpoint])
    client = dax
else:
    client = dynamodb
pk = 5
sk1 = 2
sk2 = 9
iterations = 5
params = {
    'TableName': table_name,
    'KeyConditionExpression': 'pk = :pkval and sk between :skval1 and :skval2',
    'ExpressionAttributeValues': {
        ":pkval": {'N': str(pk)},
        ":skval1": {'N': str(sk1)},
        ":skval2": {'N': str(sk2)}
    }
}
for i in range(iterations):
    start = time.time()
    result = client.query(**params)
end = time.time()
print('Total time: {} ms - Avg time: {} ms'.format(end - start, (end-start)/iterations))
05-scan-test.py
#!/usr/bin/env python
from __future__ import print_function
session = botocore.session.get_session()
dynamodb = session.create_client('dynamodb', region_name=region) # low-level client
table_name = "TryDaxTable"
if len(sys.argv) > 1:
    endpoint = sys.argv[1]
    dax = amazondax.AmazonDaxClient(session, region_name=region, endpoints=[endpoint])
    client = dax
else:
    client = dynamodb
iterations = 5
params = {
    'TableName': table_name
}
for i in range(iterations):
    start = time.time()
    result = client.scan(**params)
end = time.time()
print('Total time: {} ms - Avg time: {} ms'.format(end - start, (end-start)/iterations))
06-delete-table.py
The 06-delete-table.py program deletes TryDaxTable. Run this program after you are done testing.
#!/usr/bin/env python3
import os
import amazondax
import botocore.session
session = botocore.session.get_session()
dynamodb = session.create_client('dynamodb', region_name=region) # low-level client
table_name = "TryDaxTable"
params = {
    'TableName' : table_name
}
Suppose that you have a DynamoDB table named Music. The partition key for the table is Artist, and its
sort key is SongTitle. The following program reads an item directly from the Music table:
import java.util.HashMap;
import   com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
import   com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder;
import   com.amazonaws.services.dynamodbv2.model.AttributeValue;
import   com.amazonaws.services.dynamodbv2.model.GetItemRequest;
import   com.amazonaws.services.dynamodbv2.model.GetItemResult;
          try {
              System.out.println("Attempting to read the item...");
              GetItemResult result = client.getItem(request);
              System.out.println("GetItem succeeded: " + result);
          } catch (Exception e) {
              System.err.println("Unable to read item");
              System.err.println(e.getMessage());
          }
    }
}
To modify the program, you replace the DynamoDB client with a DAX client:
import java.util.HashMap;
import   com.amazon.dax.client.dynamodbv2.AmazonDaxClient;
import   com.amazon.dax.client.dynamodbv2.ClientConfig;
import   com.amazon.dax.client.dynamodbv2.ClusterDaxClient;
import   com.amazonaws.services.dynamodbv2.model.AttributeValue;
import   com.amazonaws.services.dynamodbv2.model.GetItemRequest;
import   com.amazonaws.services.dynamodbv2.model.GetItemResult;
 .withEndpoints("mydaxcluster.2cmrwl.clustercfg.dax.use1.cache.amazonaws.com:8111");
        AmazonDaxClient client = new ClusterDaxClient(daxConfig);
         /*
         ** ...
         ** Remaining code omitted (it is identical)
         ** ...
         */
    }
}
The document interface can also be used with the low-level DAX client, as shown below:
import   com.amazon.dax.client.dynamodbv2.AmazonDaxClient;
import   com.amazon.dax.client.dynamodbv2.ClientConfig;
import   com.amazon.dax.client.dynamodbv2.ClusterDaxClient;
import   com.amazonaws.services.dynamodbv2.document.DynamoDB;
import   com.amazonaws.services.dynamodbv2.document.GetItemOutcome;
import   com.amazonaws.services.dynamodbv2.document.Table;
 .withEndpoints("mydaxcluster.2cmrwl.clustercfg.dax.use1.cache.amazonaws.com:8111")
                .withRegion("us-east-1");
        AmazonDaxClient client = new ClusterDaxClient(daxConfig);
          try {
              System.out.println("Attempting to read the item...");
              GetItemOutcome outcome = table.tgetItemOutcome(
                  "Artist", "No One You Know",
                  "SongTitle", "Scared of My Shadow");
              System.out.println(outcome.getItem());
              System.out.println("GetItem succeeded: " + outcome);
          } catch (Exception e) {
              System.err.println("Unable to read item");
              System.err.println(e.getMessage());
          }
    }
}
The following program shows how to use ClusterDaxAsyncClient, along with Java Future, to
implement a non-blocking solution.
import java.util.HashMap;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import   com.amazon.dax.client.dynamodbv2.ClientConfig;
import   com.amazon.dax.client.dynamodbv2.ClusterDaxAsyncClient;
import   com.amazonaws.auth.profile.ProfileCredentialsProvider;
import   com.amazonaws.handlers.AsyncHandler;
import   com.amazonaws.services.dynamodbv2.AmazonDynamoDBAsync;
import   com.amazonaws.services.dynamodbv2.model.AttributeValue;
import   com.amazonaws.services.dynamodbv2.model.GetItemRequest;
import   com.amazonaws.services.dynamodbv2.model.GetItemResult;
 // Java Futures
 Future<GetItemResult> call = client.getItemAsync(request);
 while (!call.isDone()) {
     // Do other processing while you're waiting for the response
     System.out.println("Doing something else for a few seconds...");
         Thread.sleep(3000);
     }
     // The results should be ready by now
     try {
         call.get();
     // Async callbacks
     call = client.getItemAsync(request, new AsyncHandler<GetItemRequest, GetItemResult>() {
           @Override
           public void onSuccess(GetItemRequest request, GetItemResult getItemResult) {
        System.out.println("Result: " + getItemResult);
           }
           @Override
           public void onError(Exception e) {
        System.out.println("Unable to read item");
        System.err.println(e.getMessage());
        // Callers can also test if exception is an instance of
        // AmazonServiceException or AmazonClientException and cast
        // it to get additional information
           }
     });
     call.get();
          }
    }
    Topics
        • IAM Permissions for Managing a DAX Cluster (p. 625)
        • Customizing DAX Cluster Settings (p. 627)
        • Configuring TTL Settings (p. 629)
        • Tagging Support for DAX (p. 630)
        • AWS CloudTrail Integration (p. 631)
        • Deleting a DAX Cluster (p. 631)
The following discussion focuses on access control for the DAX management APIs (see Amazon
DynamoDB Accelerator in the Amazon DynamoDB API Reference).
    Note
    For more detailed information about managing IAM permissions, see the following:
    • IAM and creating DAX clusters: Creating a DAX Cluster (p. 579).
    • IAM and DAX data plane operations:DAX Access Control (p. 631).
For the DAX management APIs, you cannot scope API actions to a specific resource. The Resource
element must be set to "*". (Note that this is different from DAX data plane API operations, such as
GetItem, Query, and Scan. Data plane operations are exposed through the DAX client, and those
operations can be scoped to specific resources.)
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Action": [
                "dax:*"
            ],
            "Effect": "Allow",
            "Resource": [
                "arn:aws:dax:us-west-2:123456789012:cache/DAXCluster01"
            ]
        }
    ]
}
Suppose that the intent of this policy is to allow DAX management API calls for the cluster
DAXCluster01— and only that cluster.
Now suppose that a user issues the following AWS CLI command:
This command will fail with a "Not Authorized" exception, because the underlying DescribeClusters
API call cannot be scoped to a specific cluster. Even though the policy is syntactically valid, the command
fails because the Resource element must be set to "*". However, if the user runs a program that
sends DAX data plane calls (such as GetItem or Query) to DAXCluster01, those calls will succeed. This is
because DAX data plane APIs can be scoped to specific resources (in this case, DAXCluster01).
If you want to write a single comprehensive IAM policy, encompassing both DAX management APIs and
DAX data plane APIs, we suggest that you include two distinct statements in the policy document. One
of these statements should address the DAX data plane APIs, while the other statement addresses the
DAX management APIs.
The following example policy shows this approach. Note how the DAXDataAPIs statement is scoped to
the DAXCluster01 resource, but the resource for DAXManagementAPIs must be "*". (The actions shown
in each statement are for illustration only; you can customize them as needed for your application.)
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "DAXDataAPIs",
            "Action": [
                "dax:GetItem",
                 "dax:BatchGetItem",
                 "dax:Query",
                 "dax:Scan",
                 "dax:PutItem",
                 "dax:UpdateItem",
                 "dax:DeleteItem",
                 "dax:BatchWriteItem",
                 "dax:DefineAttributeList",
                 "dax:DefineAttributeListId",
                 "dax:DefineKeySchema",
                 "dax:Endpoints"
                 ],
             "Effect": "Allow",
             "Resource": [
                 "arn:aws:dax:us-west-2:123456789012:cache/DAXCluster01"
             ]},
             {
             "Sid": "DAXManagementAPIs",
             "Action": [
                 "dax:CreateParameterGroup",
                 "dax:CreateSubnetGroup",
                 "dax:DecreaseReplicationFactor",
                 "dax:DeleteCluster",
                 "dax:DeleteParameterGroup",
                 "dax:DeleteSubnetGroup",
                 "dax:DescribeClusters",
                 "dax:DescribeDefaultParameters",
                 "dax:DescribeEvents",
                 "dax:DescribeParameterGroups",
                 "dax:DescribeParameters",
                 "dax:DescribeSubnetGroups",
                 "dax:IncreaseReplicationFactor",
                 "dax:ListTags",
                 "dax:RebootNode",
                 "dax:TagResource",
                 "dax:UntagResource",
                 "dax:UpdateCluster",
                 "dax:UpdateParameterGroup",
                 "dax:UpdateSubnetGroup"
                 ],
             "Effect": "Allow",
             "Resource": [
                 "*"
             ]
         }
    ]
}
You cannot change these settings on a DAX cluster that is currently running. However, for new clusters,
you can customize the settings at creation time. To do this in the AWS Management Console, deselect
Use default settings to modify the following settings:
• Network and Security—allows you to run individual DAX cluster nodes in different Availability Zones
  (AZs) within the current AWS region. If you choose No Preference, the nodes will be distributed among
  AZs automatically.
• Parameter Group—a named set of parameters that are applied to every node in the cluster. You can
  use a parameter group to specify cache time-to-live (TTL) behavior.
• Maintenance Window—a weekly time period during which software upgrades and patches are applied
  to the nodes in the cluster. You can choose the start day, start time, and duration of the maintenance
  window. If you choose No Preference, the maintenance window will be selected at random from an 8-
  hour block of time per region. (For more information, see Maintenance Window (p. 578).)
When a maintenance event occurs, DAX can notify you using Amazon Simple Notification Service
(Amazon SNS). To configure notifications, choose an option from the Topic for SNS notification selector.
You can create a new Amazon SNS topic, or use an existing topic. (For more information on setting up
and subscribing to an Amazon SNS topic, see Getting Started with Amazon Simple Notification Service in
the Amazon Simple Notification Service Developer Guide.)
Read Scaling
With read scaling, you can improve throughput by adding more read replicas to the cluster. A single DAX
cluster supports up to 9 read replicas, and you can add or remove replicas while the cluster is running.
The following AWS CLI examples show how to increase or decrease the number of nodes. The --new-
replication-factor argument specifies the total number of nodes in the cluster—one of the nodes is
the primary node, and the other nodes are read replicas.
    Note
    The cluster status changes to modifying when you modify the replication factor. When the
    status changes to available, the cluster is ready for use again.
Node Types
If you have a large working set of data, your application might benefit from using larger node types.
Larger nodes can enable the cluster to store more data in memory, reducing cache misses and improving
overall application performance of the application. (Note that all of the nodes in a DAX cluster must be
of the same type.)
You cannot modify the node types on a running DAX cluster. Instead, you must create a new cluster with
the desired node type. For a list of supported node types, see Nodes (p. 576).
You can create a new DAX cluster using the AWS Management Console or the AWS CLI. (For the latter,
use the --node-type parameter to specify the node type.)
For more information, see Item Cache (p. 574) and Query Cache (p. 575)
The default time to live (TTL) for each of these caches is 5 minutes. If you want to use different
TTL settings, you can launch a DAX cluster using a custom parameter group. To do this in the AWS
Management Console, choose DAX | Parameter groups in the navigation pane.
You can also perform these tasks using the AWS CLI. The following example shows how to launch a
new DAX cluster using a custom parameter group. In this example, the item cache TTL will be set to 10
minutes and the query cache TTL will be set to 3 minutes.
You can now launch a new DAX cluster with this parameter group:
     Note
     You cannot modify a parameter group that is being used by a running DAX instance.
When the settings are as you want them, choose Apply Changes.
AWS CLI
When you use the AWS CLI to manage DAX cluster tags, you must first determine the Amazon Resource
Name (ARN) for the cluster. The following example shows how to determine the ARN for a cluster named
MyDAXCluster:
    You can delete a DAX cluster from the AWS Management Console or from the AWS CLI. Here is an
    example:
    We highly recommend that you understand both security models, so that you can implement proper
    security measures for your applications that use DAX.
    In this section, we describe the access control mechanisms provided by DAX, and provide sample IAM
    policies that you can tailor to your needs.
    With DynamoDB, you can create IAM policies that limit the actions a user can perform on individual
    DynamoDB resources. For example, you can create a user role that only allows the user to perform read-
    only actions on a particular DynamoDB table. (For more information, see Authentication and Access
    Control for Amazon DynamoDB.) By comparison, the DAX security model focuses on cluster security, and
    the ability of the cluster to perform DynamoDB API actions on your behalf.
        Warning
        If you are currently using IAM roles and policies to restrict access to DynamoDB tables data, then
        the use of DAX can subvert those policies. For example, a user could have access to a DynamoDB
        table via DAX but not have explicit access to the same table accessing DynamoDB directly. (For
        more information, see Authentication and Access Control for Amazon DynamoDB.)
        DAX does not enforce user-level separation on data in DynamoDB. Instead, users inherit the
        permissions of the DAX cluster's IAM policy when they access that cluster. Thus, when accessing
        DynamoDB tables via DAX, the only access controls that are in effect are the permissions in the
        DAX cluster's IAM policy. No other permissions are recognized.
        If you require isolation, we recommend that you create additional DAX clusters and scope the
        IAM policy for each cluster accordingly. For example, you could create multiple DAX clusters and
        allow each cluster to access only a single table.
Suppose that you wanted to create a new DAX cluster named DAXCluster01. You could create
a service role named DAXServiceRole, and associate the role with DAXCluster01. The policy
for DAXServiceRole would define the DynamoDB actions that DAXCluster01 could perform, on behalf of
the users who interact with DAXCluster01.
When you create a service role, you must specify a trust relationship between DAXServiceRole and the
DAX service itself. A trust relationship determines which entities can assume a role and make use of its
permissions. The following is an example trust relationship document for DAXServiceRole:
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Principal": {
                "Service": "dax.amazonaws.com"
            },
            "Action": "sts:AssumeRole"
        }
    ]
}
This trust relationship allows a DAX cluster to assume DAXServiceRole and perform DynamoDB API calls
on your behalf.
The DynamoDB API actions that are allowed are described in an IAM policy document, which you attach
to DAXServiceRole. The following is an example policy document:
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "DaxAccessPolicy",
            "Effect": "Allow",
            "Action": [
                "dynamodb:*"
            ],
            "Resource": [
                "arn:aws:dynamodb:us-west-2:123456789012:table/Books"
            ]
        }
    ]
}
This policy allows DAX to perform all DynamoDB API actions on a DynamoDB table named Books, which
is in the us-west-2 region and is owned by AWS account ID 123456789012.
For example, suppose you want to grant access to DAXCluster01 to an IAM user named Alice. You would
first create an IAM policy (AliceAccessPolicy) that defines the DAX clusters and DAX API actions that the
recipient can access. You would then confer access by attaching this policy to user Alice.
The following policy document gives the recipient full access on DAXCluster01:
    "Version": "2012-10-17",
    "Statement": [
        {
            "Action": [
                "dax:*"
            ],
            "Effect": "Allow",
            "Resource": [
                "arn:aws:dax:us-west-2:123456789012:cache/DAXCluster01"
            ]
        }
    ]
}
The policy document allows access to the DAX cluster, but it does not grant any DynamoDB permissions.
(The DynamoDB permissions are conferred by the DAX service role.)
For user Alice, you would first create AliceAccessPolicy with the policy document shown above. You would
then attach the policy to Alice.
    Note
    Instead of attaching the policy to an IAM user, you could attach it to an IAM role. That way, all of
    the users who assume that role would have the permissions that you defined in the policy.
The user policy, in conjunction with the DAX service role, determine the DynamoDB resources and API
actions that the recipient can access via DAX.
The combination of policy statements in BobAccessPolicy and DAXAccessPolicy determine what Bob can
do with the Books table. For example, Bob might be able to access Books directly (using the DynamoDB
endpoint), indirectly (using the DAX cluster), or both. Bob might also be able to read data from Books,
write data to Books, or both.
It is possible to allow direct access to a DynamoDB table, while preventing indirect access using a DAX
cluster. For direct access to DynamoDB, the privileges for BobUserRole are determined by BobAccessPolicy
(which is attached to the role).
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "DynamoDBAccessStmt",
            "Effect": "Allow",
            "Action": [
                "dynamodb:GetItem",
                "dynamodb:BatchGetItem",
                "dynamodb:Query",
                "dynamodb:Scan"
            ],
            "Resource": "arn:aws:dynamodb:us-west-2:123456789012:table/Books"
        }
    ]
}
When this document is attached to BobAccessPolicy, it allows BobUserRole to access the DynamoDB
endpoint and perform read-only operations on the Books table.
DAX does not appear in this policy, so access via DAX is denied.
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "DynamoDBAccessStmt",
            "Effect": "Allow",
            "Action": [
                "dynamodb:GetItem",
                "dynamodb:BatchGetItem",
                "dynamodb:Query",
                "dynamodb:Scan",
                "dynamodb:PutItem",
                "dynamodb:UpdateItem",
                "dynamodb:DeleteItem",
                "dynamodb:BatchWriteItem"
            ],
            "Resource": "arn:aws:dynamodb:us-west-2:123456789012:table/Books"
        }
    ]
}
Again, DAX does not appear in this policy, so access via DAX is denied.
To allow access to a DAX cluster, you must include DAX-specific actions in an IAM policy.
The following DAX-specific actions correspond to their similarly-named counterparts in the DynamoDB
API:
• dax:GetItem
• dax:BatchGetItem
• dax:Query
• dax:Scan
• dax:PutItem
• dax:UpdateItem
• dax:DeleteItem
• dax:BatchWriteItem
In addition, there are four other DAX-specific actions that do not correspond to any DynamoDB APIs:
• dax:DefineAttributeList
• dax:DefineAttributeListId
• dax:DefineKeySchema
• dax:Endpoints
You must specify all four of these actions in any IAM policy that allows access to a DAX cluster. These
actions are specific to the low-level DAX data transport protocol. Your application does not need to
concern itself with these actions—they are only used in IAM policies.
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "DAXAccessStmt",
            "Effect": "Allow",
            "Action": [
                "dax:GetItem",
                "dax:BatchGetItem",
                "dax:Query",
                "dax:Scan",
                "dax:DefineAttributeList",
                "dax:DefineAttributeListId",
                "dax:DefineKeySchema",
                "dax:Endpoints"
            ],
            "Resource": "arn:aws:dax:us-west-2:123456789012:cache/DAXCluster01"
        },
        {
            "Sid": "DynamoDBAccessStmt",
            "Effect": "Allow",
            "Action": [
                "dynamodb:GetItem",
                "dynamodb:BatchGetItem",
                "dynamodb:Query",
                "dynamodb:Scan"
            ],
            "Resource": "arn:aws:dynamodb:us-west-2:123456789012:table/Books"
        }
    ]
}
The policy has a statement for DAX access (DAXAccessStmt) and another statement
for DynamoDBaccess (DynamoDBAccessStmt). These statements would allow Bob to
send GetItem, BatchGetItem, Query, and Scan requests to DAXCluster01.
However, the service role for DAXCluster01 would also require read-only access to the Books table in
DynamoDB. The following IAM policy, attached to DAXServiceRole, would fulfill this requirement:
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "DynamoDBAccessStmt",
            "Effect": "Allow",
            "Action": [
                "dynamodb:GetItem",
                "dynamodb:BatchGetItem",
                "dynamodb:Query",
                "dynamodb:Scan"
            ],
            "Resource": "arn:aws:dynamodb:us-west-2:123456789012:table/Books"
        }
    ]
}
For Bob, the IAM policy for BobUserRole would need to allow DynamoDB read and write actions on
the Books table, while also supporting read-only actions via DAXCluster01.
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "DAXAccessStmt",
            "Effect": "Allow",
            "Action": [
                "dax:GetItem",
                "dax:BatchGetItem",
                "dax:Query",
                "dax:Scan",
                "dax:DefineKeySchema",
                "dax:DefineAttributeList",
                "dax:DefineAttributeListId",
                "dax:Endpoints"
            ],
            "Resource": "arn:aws:dax:us-west-2:123456789012:cache/DAXCluster01"
        },
        {
            "Sid": "DynamoDBAccessStmt",
            "Effect": "Allow",
            "Action": [
                "dynamodb:GetItem",
                "dynamodb:BatchGetItem",
                "dynamodb:Query",
                "dynamodb:Scan",
                "dynamodb:PutItem",
                "dynamodb:UpdateItem",
                "dynamodb:DeleteItem",
                "dynamodb:BatchWriteItem",
                "dynamodb:DescribeTable"
            ],
            "Resource": "arn:aws:dynamodb:us-west-2:123456789012:table/Books"
        }
    ]
}
In addition, DAXServiceRole would require an IAM policy that allows DAXCluster01 to perform read-only
actions on the Books table:
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "DynamoDBAccessStmt",
            "Effect": "Allow",
            "Action": [
                "dynamodb:GetItem",
                "dynamodb:BatchGetItem",
                "dynamodb:Query",
                "dynamodb:Scan",
                "dynamodb:DescribeTable"
             ],
              "Resource": "arn:aws:dynamodb:us-west-2:123456789012:table/Books"
         }
    ]
}
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "DAXAccessStmt",
            "Effect": "Allow",
            "Action": [
                "dax:GetItem",
                "dax:BatchGetItem",
                "dax:Query",
                "dax:Scan",
                "dax:PutItem",
                "dax:UpdateItem",
                "dax:DeleteItem",
                "dax:BatchWriteItem",
                "dax:DefineKeySchema",
                "dax:DefineAttributeList",
                "dax:DefineAttributeListId",
                "dax:Endpoints"
            ],
            "Resource": "arn:aws:dax:us-west-2:123456789012:cache/DAXCluster01"
        },
        {
            "Sid": "DynamoDBAccessStmt",
            "Effect": "Allow",
            "Action": [
                "dynamodb:GetItem",
                "dynamodb:BatchGetItem",
                "dynamodb:Query",
                "dynamodb:Scan",
                "dynamodb:PutItem",
                "dynamodb:UpdateItem",
                "dynamodb:DeleteItem",
                "dynamodb:BatchWriteItem",
                "dynamodb:DescribeTable"
            ],
            "Resource": "arn:aws:dynamodb:us-west-2:123456789012:table/Books"
        }
    ]
}
In addition, DAXServiceRole would require an IAM policy that allows DAXCluster01 to perform read-write
actions on the Books table:
{
    "Version": "2012-10-17",
    "Statement": [
        {
              "Sid": "DynamoDBAccessStmt",
              "Effect": "Allow",
              "Action": [
                  "dynamodb:GetItem",
                  "dynamodb:BatchGetItem",
                  "dynamodb:Query",
                  "dynamodb:Scan",
                  "dynamodb:PutItem",
                  "dynamodb:UpdateItem",
                  "dynamodb:DeleteItem",
                  "dynamodb:BatchWriteItem",
                  "dynamodb:DescribeTable"
              ],
              "Resource": "arn:aws:dynamodb:us-west-2:123456789012:table/Books"
         }
    ]
}
If you are currently using IAM roles and policies to restrict access to DynamoDB tables and data, then the
use of DAX can subvert those policies. In the policy below, Bob has access to a DynamoDB table via DAX
but does not have explicit direct access to the same table in DynamoDB.
The following policy document (BobAccessPolicy), attached to BobUserRole, would confer this access:
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "DAXAccessStmt",
            "Effect": "Allow",
            "Action": [
                "dax:GetItem",
                "dax:BatchGetItem",
                "dax:Query",
                "dax:Scan",
                "dax:PutItem",
                "dax:UpdateItem",
                "dax:DeleteItem",
                "dax:BatchWriteItem",
                "dax:DefineKeySchema",
                "dax:DefineAttributeList",
                "dax:DefineAttributeListId",
                "dax:Endpoints"
            ],
            "Resource": "arn:aws:dax:us-west-2:123456789012:cache/DAXCluster01"
        }
    ]
}
Note that in this access policy, there are no permissions to access DynamoDB directly.
Together with BobAccessPolicy, the following DAXAccessPolicy would give BobUserRole access to the
DynamoDB table Books even though BobUserRole cannot directly access the Books table:
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "DynamoDBAccessStmt",
            "Effect": "Allow",
            "Action": [
                "dynamodb:GetItem",
                "dynamodb:BatchGetItem",
                "dynamodb:Query",
                "dynamodb:Scan",
                "dynamodb:PutItem",
                "dynamodb:UpdateItem",
                "dynamodb:DeleteItem",
                "dynamodb:BatchWriteItem",
                "dynamodb:DescribeTable"
            ],
            "Resource": "arn:aws:dynamodb:us-west-2:123456789012:table/Books"
        }
    ]
}
As shown in this example, when you configure access control for the user access policy and the DAX
cluster access policy, you must fully-understand the end-to-end access to ensure that the principle of
least privilege is observed. You must also ensure that giving a user access to a DAX cluster does not
subvert previously established access control policies.
    A service-linked role makes setting up DAX easier because you don’t have to manually add the necessary
    permissions. DAX defines the permissions of its service-linked roles, and unless defined otherwise, only
    DAX can assume its roles. The defined permissions include the trust policy and the permissions policy,
    and that permissions policy cannot be attached to any other IAM entity.
    You can delete the roles only after first deleting their related resources. This protects your DAX resources
    because you can't inadvertently remove permission to access the resources.
    For information about other services that support service-linked roles, see AWS Services That Work with
    IAM and look for the services that have Yes in the Service-Linked Role column. Choose a Yes with a link
    to view the service-linked role documentation for that service.
The AWSServiceRoleForDAX service-linked role trusts the following services to assume the role:
• dax.amazonaws.com
The role permissions policy allows DAX to complete the following actions on the specified resources:
    • Actions on ec2:
      • AuthorizeSecurityGroupIngress
      • CreateNetworkInterface
      • CreateSecurityGroup
      • DeleteNetworkInterface
      • DeleteSecurityGroup
      • DescribeAvailabilityZones
      • DescribeNetworkInterfaces
      • DescribeSecurityGroups
      • DescribeSubnets
      • DescribeVpcs
      • ModifyNetworkInterfaceAttribute
      • RevokeSecurityGroupIngress
    You must configure permissions to allow an IAM entity (such as a user, group, or role) to create, edit, or
    delete a service-linked role. For more information, see Service-Linked Role Permissions in the IAM User
    Guide.
                                        API Version 2012-08-10
                                                  642
                              Amazon DynamoDB Developer Guide
                             Creating a Service-Linked Role for DAX
If you delete this service-linked role, and then need to create it again, you can use the same process to
recreate the role in your account. When you create an instance or a cluster, DAX creates the service-linked
role for you again.
To check whether the service-linked role has an active session in the IAM console
1.   Sign in to the AWS Management Console and open the IAM console at https://
     console.aws.amazon.com/iam/.
2.   In the navigation pane of the IAM console, choose Roles. Then choose the name (not the check box)
     of the AWSServiceRoleForDAX role.
3.   On the Summary page for the selected role, choose the Access Advisor tab.
4.   On the Access Advisor tab, review recent activity for the service-linked role.
         Note
         If you are unsure whether DAX is using the AWSServiceRoleForDAX role, you can try to
         delete the role. If the service is using the role, then the deletion fails and you can view the
         regions where the role is being used. If the role is being used, then you must delete your
         DAX clusters before you can delete the role. You cannot revoke the session for a service-
         linked role.
If you want to remove the AWSServiceRoleForDAX role, you must first delete all of your DAX clusters.
    Use the IAM console, the IAM CLI, or the IAM API to delete the AWSServiceRoleForDAX service-linked
    role. For more information, see Deleting a Service-Linked Role in the IAM User Guide.
Authentication
    You can access AWS as any of the following types of identities:
    • AWS account root user – When you first create an AWS account, you begin with a single sign-in
      identity that has complete access to all AWS services and resources in the account. This identity is
      called the AWS account root user and is accessed by signing in with the email address and password
      that you used to create the account. We strongly recommend that you do not use the root user for
      your everyday tasks, even the administrative ones. Instead, adhere to the best practice of using the
      root user only to create your first IAM user. Then securely lock away the root user credentials and use
      them to perform only a few account and service management tasks.
    • IAM user – An IAM user is an identity within your AWS account that has specific custom permissions
      (for example, permissions to create a table in DynamoDB). You can use an IAM user name and
      password to sign in to secure AWS webpages like the AWS Management Console, AWS Discussion
      Forums, or the AWS Support Center.
      In addition to a user name and password, you can also generate access keys for each user. You can
      use these keys when you access AWS services programmatically, either through one of the several
      SDKs or by using the AWS Command Line Interface (CLI). The SDK and CLI tools use the access keys
      to cryptographically sign your request. If you don’t use AWS tools, you must sign the request yourself.
      DynamoDB supports Signature Version 4, a protocol for authenticating inbound API requests. For more
      information about authenticating requests, see Signature Version 4 Signing Process in the AWS General
      Reference.
       
    • IAM role – An IAM role is an IAM identity that you can create in your account that has specific
      permissions. It is similar to an IAM user, but it is not associated with a specific person. An IAM role
      enables you to obtain temporary access keys that can be used to access AWS services and resources.
      IAM roles with temporary credentials are useful in the following situations:
       
      • Federated user access – Instead of creating an IAM user, you can use existing user identities from
        AWS Directory Service, your enterprise user directory, or a web identity provider. These are known as
        federated users. AWS assigns a role to a federated user when access is requested through an identity
        provider. For more information about federated users, see Federated Users and Roles in the IAM User
        Guide.
         
      • AWS service access – You can use an IAM role in your account to grant an AWS service permissions
        to access your account’s resources. For example, you can create a role that allows Amazon Redshift
        to access an Amazon S3 bucket on your behalf and then load data from that bucket into an Amazon
        Redshift cluster. For more information, see Creating a Role to Delegate Permissions to an AWS
        Service in the IAM User Guide.
         
      • Applications running on Amazon EC2 – You can use an IAM role to manage temporary credentials
        for applications that are running on an EC2 instance and making AWS API requests. This is preferable
        to storing access keys within the EC2 instance. To assign an AWS role to an EC2 instance and make
        it available to all of its applications, you create an instance profile that is attached to the instance.
        An instance profile contains the role and enables programs that are running on the EC2 instance
        to get temporary credentials. For more information, see Using an IAM Role to Grant Permissions to
        Applications Running on Amazon EC2 Instances in the IAM User Guide.
Access Control
    You can have valid credentials to authenticate your requests, but unless you have permissions you cannot
    create or access Amazon DynamoDB resources. For example, you must have permissions to create an
    Amazon DynamoDB table.
    The following sections describe how to manage permissions for Amazon DynamoDB. We recommend
    that you read the overview first.
    When granting permissions, you decide who is getting the permissions, the resources they get
    permissions for, and the specific actions that you want to allow on those resources.
    Topics
     • Amazon DynamoDB Resources and Operations (p. 647)
     • Understanding Resource Ownership (p. 647)
     • Managing Access to Resources (p. 647)
     • Specifying Policy Elements: Actions, Effects, and Principals (p. 649)
These resources and subresources have unique Amazon Resource Names (ARNs) associated with them, as
shown in the following table.
Table arn:aws:dynamodb:region:account-id:table/table-name
 Index                          arn:aws:dynamodb:region:account-id:table/table-name/
                                index/index-name
 Stream                         arn:aws:dynamodb:region:account-id:table/table-name/
                                stream/stream-label
DynamoDB provides a set of operations to work with DynamoDB resources. For a list of available
operations, see Amazon DynamoDB Actions.
• If you use the root account credentials of your AWS account to create a table, your AWS account is the
  owner of the resource (in DynamoDB, the resource is a table).
• If you create an IAM user in your AWS account and grant permissions to create a table to that user,
  the user can create a table. However, your AWS account, to which the user belongs, owns the table
  resource.
• If you create an IAM role in your AWS account with permissions to create a table, anyone who can
  assume the role can create a table. Your AWS account, to which the user belongs, owns the table
  resource.
Policies attached to an IAM identity are referred to as identity-based policies (IAM polices) and policies
attached to a resource are referred to as resource-based policies. DynamoDB supports only identity-based
policies (IAM policies).
Topics
    • Identity-Based Policies (IAM Policies) (p. 648)
    • Resource-Based Policies (p. 648)
• Attach a permissions policy to a user or a group in your account – To grant a user permissions to
  create an Amazon DynamoDB resource, such as a table, you can attach a permissions policy to a user
  or group that the user belongs to.
• Attach a permissions policy to a role (grant cross-account permissions) – You can attach an
  identity-based permissions policy to an IAM role to grant cross-account permissions. For example,
  the administrator in account A can create a role to grant cross-account permissions to another AWS
  account (for example, account B) or an AWS service as follows:
    1. Account A administrator creates an IAM role and attaches a permissions policy to the role that
       grants permissions on resources in account A.
    2. Account A administrator attaches a trust policy to the role identifying account B as the principal
       who can assume the role.
    3. Account B administrator can then delegate permissions to assume the role to any users in account B.
       Doing this allows users in account B to create or access resources in account A. The principal in the
       trust policy can also be an AWS service principal if you want to grant an AWS service permissions to
       assume the role.
    For more information about using IAM to delegate permissions, see Access Management in the IAM
    User Guide.
The following is an example policy that grants permissions for one DynamoDB action
(dynamodb:ListTables). The wildcard character (*) in the Resource value means that you can use this
action to obtain the names of all the tables owned by the AWS account in the current AWS region.
{
      "Version": "2012-10-17",
      "Statement": [
          {
              "Sid": "ListTables",
              "Effect": "Allow",
              "Action": [
                  "dynamodb:ListTables"
              ],
              "Resource": "*"
          }
      ]
}
For more information about using identity-based policies with DynamoDB, see Using Identity-Based
Policies (IAM Policies) for Amazon DynamoDB (p. 649). For more information about users, groups, roles,
and permissions, see Identities (Users, Groups, and Roles) in the IAM User Guide.
Resource-Based Policies
Other services, such as Amazon S3, also support resource-based permissions policies. For example, you
can attach a policy to an S3 bucket to manage access permissions to that bucket. DynamoDB doesn't
support resource-based policies.
     • Resource – You use an Amazon Resource Name (ARN) to identify the resource that the policy applies
       to. For more information, see Amazon DynamoDB Resources and Operations (p. 647).
     • Action – You use action keywords to identify resource operations that you want to allow or deny. For
       example, you can use dynamodb:Query to allows the user permissions to perform the DynamoDB
       Query operation.
     • Effect – You specify the effect, either allow or deny, when the user requests the specific action. If you
       don't explicitly grant access to (allow) a resource, access is implicitly denied. You can also explicitly
       deny access to a resource, which you might do to make sure that a user cannot access it, even if a
       different policy grants access.
     • Principal – In identity-based policies (IAM policies), the user that the policy is attached to is the
       implicit principal. For resource-based policies, you specify the user, account, service, or other entity
       that you want to receive permissions (applies to resource-based policies only). DynamoDB doesn't
       support resource-based policies.
     To learn more about IAM policy syntax and descriptions, see AWS IAM Policy Reference in the IAM User
     Guide.
     For a list showing all of the Amazon DynamoDB API operations and the resources that they apply to, see
     DynamoDB API Permissions: Actions, Resources, and Conditions Reference (p. 657).
     To express conditions, you use predefined condition keys. There are AWS-wide condition keys and
     DynamoDB–specific keys that you can use as appropriate. For a complete list of AWS-wide keys, see
     Available Keys for Conditions in the IAM User Guide. For a complete list of DynamoDB–specific keys, see
     Using IAM Policy Conditions for Fine-Grained Access Control (p. 661).
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "DescribeQueryScanBooksTable",
            "Effect": "Allow",
            "Action": [
                "dynamodb:DescribeTable",
                "dynamodb:Query",
                "dynamodb:Scan"
            ],
            "Resource": "arn:aws:dynamodb:us-west-2:account-id:table/Books"
        }
    ]
}
The policy has one statement that grants permissions for three DynamoDB actions
(dynamodb:DescribeTable, dynamodb:Query and dynamodb:Scan) on a table in the us-west-2
region, which is owned by the AWS account specified by account-id. The Amazon Resource Name (ARN)
in the Resource value specifies the table to which the permissions apply.
If you create an IAM policy that is more restrictive than the minimum required permissions, the console
won't function as intended for users with that IAM policy. To ensure that those users can still use the
DynamoDB console, also attach the AmazonDynamoDBReadOnlyAccess managed policy to the user, as
described in AWS Managed (Predefined) Policies for Amazon DynamoDB (p. 651).
You don't need to allow minimum console permissions for users that are making calls only to the AWS
CLI or the Amazon DynamoDB API.
The following AWS managed policies, which you can attach to users in your account, are specific to
DynamoDB and are grouped by use case scenario:
    Note
    You can review these permissions policies by signing in to the IAM console and searching for
    specific policies there.
You can also create your own custom IAM policies to allow permissions for DynamoDB actions
and resources. You can attach these custom policies to the IAM users or groups that require those
permissions.
Examples
 • Example 1: Allow a User to Perform Any DynamoDB Actions on a Table (p. 651)
 • Example 2: Allow Read-only Access on Items in a Table (p. 652)
 • Example 3: Allow Put, Update, and Delete Operations on a Specific Table (p. 652)
 • Example 4: Allow Access to a Specific Table and All of Its Indexes (p. 653)
 • Example 5: Set Up Permissions Policies for Separate Test and Production Environments (p. 653)
 • Example 6: Prevent a User from Purchasing Reserved Capacity Offerings (p. 654)
 • Example 7: Allow Read Access for a DynamoDB Stream Only (Not for the Table) (p. 655)
 • Example 8: Allow an AWS Lambda Function to Process DynamoDB Stream Records (p. 656)
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "AllAPIActionsOnBooks",
            "Effect": "Allow",
            "Action": "dynamodb:*",
            "Resource": "arn:aws:dynamodb:us-west-2:123456789012:table/Books"
        }
    ]
}
    Note
    If you replace the table name in the resource ARN (Books) with a wildcard character (*) , you
    allow any DynamoDB actions on all tables in the account. Carefully consider the security
    implications if you decide to do this.
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "ReadOnlyAPIActionsOnBooks",
            "Effect": "Allow",
            "Action": [
                "dynamodb:GetItem",
                "dynamodb:BatchGetItem"
            ],
            "Resource": "arn:aws:dynamodb:us-west-2:123456789012:table/Books"
        }
    ]
}
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "PutUpdateDeleteOnBooks",
            "Effect": "Allow",
            "Action": [
                "dynamodb:PutItem",
                "dynamodb:UpdateItem",
                "dynamodb:DeleteItem"
            ],
            "Resource": "arn:aws:dynamodb:us-west-2:123456789012:table/Books"
        }
    ]
}
{
        "Version": "2012-10-17",
        "Statement": [
            {
                "Sid": "AccessAllIndexesOnBooks",
                "Effect": "Allow",
                "Action": [
                    "dynamodb:*"
                ],
                "Resource": [
                    "arn:aws:dynamodb:us-west-2:123456789012:table/Books",
                    "arn:aws:dynamodb:us-west-2:123456789012:table/Books/index/*"
                ]
            }
        ]
}
Suppose further that you have two developers, Bob and Alice, who are testing the ProductCatalog
table. Instead of creating a separate AWS account for every developer, your developers can share the
same test account. in this test account, you can create a copy of the same table for each developer to
work on, such as Alice_ProductCatalog and Bob_ProductCatalog. In this case, you can create IAM
users Alice and Bob in the AWS account that you created for the test environment. You can then grant
permissions to these users to perform DynamoDB actions on the tables that they own.
• Create a separate policy for each user and then attach each policy to its user separately. For example,
  you can attach the following policy to user Alice to allow her access to all DynamoDB actions on the
  Alice_ProductCatalog table:
    {
        "Version": "2012-10-17",
        "Statement": [
            {
                "Sid": "AllAPIActionsOnAliceTable",
                "Effect": "Allow",
                "Action": [
                    "dynamodb:*"
                ],
                "Resource": "arn:aws:dynamodb:us-west-2:123456789012:table/
    Alice_ProductCatalog"
             }
        ]
  }
  Then, you can create a similar policy with a different resource (Bob_ProductCatalog table) for user
  Bob.
• Instead of attaching policies to individual users, you can use IAM policy variables to write a
  single policy and attach it to a group. You need to create a group and, for this example, add both
  users Alice and user Bob to the group. The following example grants permissions to perform
  all DynamoDB actions on the ${aws:username}_ProductCatalog table. The policy variable
  ${aws:username} is replaced by the requester's user name when the policy is evaluated. For
  example, if Alice sends a request to add an item, the action is allowed only if Alice is adding items to
  the Alice_ProductCatalog table.
  {
      "Version": "2012-10-17",
      "Statement": [
          {
              "Sid": "AllAPIActionsOnUserSpecificTable",
              "Effect": "Allow",
              "Action": [
                  "dynamodb:*"
              ],
              "Resource": "arn:aws:dynamodb:us-west-2:123456789012:table/
  ${aws:username}_ProductCatalog"
          },
          {
              "Sid": "AdditionalPrivileges",
              "Effect": "Allow",
              "Action": [
                  "dynamodb:ListTables",
                  "dynamodb:DescribeTable",
                  "cloudwatch:*",
                  "sns:*"
              ],
              "Resource": "*"
          }
      ]
  }
      Note
      When using IAM policy variables, you must explicitly specify the 2012-10-17 version of
      the access policy language in the policy. The default version of the access policy language
      (2008-10-17) does not support policy variables.
Note that, instead of identifying a specific table as a resource, you can use a wildcard character (*) to
grant permissions on all tables where the name is prefixed with the name of the IAM user that is making
the request, as shown following:
"Resource":"arn:aws:dynamodb:us-west-2:123456789012:table/${aws:username}_*"
purchase reserved capacity. However, you might not want all of the users in your organization to have
the same levels of access.
DynamoDB provides the following API operations for controlling access to reserved capacity
management:
The AWS Management Console uses these API operations to display reserved capacity information and
to make purchases. You cannot call these operations from an application program, because they are
only accessible from the Console. However, you can allow or deny access to these operations in an IAM
permissions policy.
The following policy allows users to view reserved capacity offerings and current purchases using the
AWS Management Console—but new purchases are denied.
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "AllowReservedCapacityDescriptions",
            "Effect": "Allow",
            "Action": [
                "dynamodb:DescribeReservedCapacity",
                "dynamodb:DescribeReservedCapacityOfferings"
            ],
            "Resource": "arn:aws:dynamodb:us-west-2:123456789012:*"
        },
        {
            "Sid": "DenyReservedCapacityPurchases",
            "Effect": "Deny",
            "Action": "dynamodb:PurchaseReservedCapacityOfferings",
            "Resource": "arn:aws:dynamodb:us-west-2:123456789012:*"
        }
    ]
}
In some cases, you might want to prevent an application from reading data from a DynamoDB table,
while still allowing access to that table's stream. For example, you can configure AWS Lambda to poll
the stream and invoke a Lambda function when item updates are detected, and then perform additional
processing.
The following actions are available for controlling access to DynamoDB Streams:
• dynamodb:DescribeStream
• dynamodb:GetRecords
• dynamodb:GetShardIterator
• dynamodb:ListStreams
The following example creates a policy that grants users permissions to access the streams on a table
named GameScores. The final wildcard character (*) in the ARN matches any stream ID associated with
that table.
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "AccessGameScoresStreamOnly",
            "Effect": "Allow",
            "Action": [
                "dynamodb:DescribeStream",
                "dynamodb:GetRecords",
                "dynamodb:GetShardIterator",
                "dynamodb:ListStreams"
            ],
            "Resource": "arn:aws:dynamodb:us-west-2:123456789012:table/GameScores/stream/*"
        }
    ]
}
Note that this policy permits access to the streams on the GameScores table, but not to the table itself.
To grant permissions to Lambda, you use the permissions policy that is associated with the Lambda
function's IAM role (execution role), which you specify when you create the Lambda function.
For example, you can associate the following permissions policy with the execution role to grant Lambda
permissions to perform the DynamoDB Streams actions listed.
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "AllowLambdaFunctionInvocation",
            "Effect": "Allow",
            "Action": [
                "lambda:InvokeFunction"
            ],
            "Resource": [
                "*"
            ]
        },
        {
            "Sid": "AllAPIAccessForDynamoDBStreams",
            "Effect": "Allow",
            "Action": [
                       "dynamodb:GetRecords",
                       "dynamodb:GetShardIterator",
                       "dynamodb:DescribeStream",
                       "dynamodb:ListStreams"
                  ],
                  "Resource": "*"
             }
        ]
    }
For more information, see AWS Lambda Permission Model in the AWS Lambda Developer Guide.
    You can use AWS-wide condition keys in your DynamoDB policies to express conditions. For a complete
    list of AWS-wide keys, see Available Keys in the IAM User Guide.
    In addition to the AWS-wide condition keys, DynamoDB has its own specific keys that you can
    use in conditions. For more information, see Using IAM Policy Conditions for Fine-Grained Access
    Control (p. 661).
        Note
        To specify an action, use the dynamodb: prefix followed by the API operation name (for
        example, dynamodb:CreateTable).
BatchGetItem
Action(s): dynamodb:BatchGetItem
Resource:
arn:aws:dynamodb:region:account-id:table/table-name
or
        arn:aws:dynamodb:region:account-id:table/*
    BatchWriteItem
Action(s): dynamodb:BatchWriteItem
Resource:
arn:aws:dynamodb:region:account-id:table/table-name
or
arn:aws:dynamodb:region:account-id:table/*
CreateTable
Action(s): dynamodb:CreateTable
Resource:
arn:aws:dynamodb:region:account-id:table/table-name
or
   arn:aws:dynamodb:region:account-id:table/*
DeleteItem
Action(s): dynamodb:DeleteItem
Resource:
arn:aws:dynamodb:region:account-id:table/table-name
or
   arn:aws:dynamodb:region:account-id:table/*
DeleteTable
Action(s): dynamodb:DeleteTable
Resource:
arn:aws:dynamodb:region:account-id:table/table-name
or
   arn:aws:dynamodb:region:account-id:table/*
DescribeReservedCapacity
Action(s): dynamodb:DeleteTable
Resource:
   arn:aws:dynamodb:region:account-id:*
DescribeReservedCapacityOfferings
Action(s): dynamodb:DescribeReservedCapacityOfferings
Resource:
   arn:aws:dynamodb:region:account-id:*
DescribeStream
Action(s): dynamodb:DescribeStream
Resource:
arn:aws:dynamodb:region:account-id:table/table-name/stream/stream-label
or
arn:aws:dynamodb:region:account-id:table/table-name/stream/*
DescribeTable
Action(s): dynamodb:DescribeTable
Resource:
arn:aws:dynamodb:region:account-id:table/table-name
or
    arn:aws:dynamodb:region:account-id:table/*
GetItem
Action(s): dynamodb:GetItem
Resource:
arn:aws:dynamodb:region:account-id:table/table-name
or
    arn:aws:dynamodb:region:account-id:table/*
GetRecords
Action(s): dynamodb:GetRecords
Resource:
arn:aws:dynamodb:region:account-id:table/table-name/stream/stream-label
or
    arn:aws:dynamodb:region:account-id:table/table-name/stream/*
GetShardIterator
Action(s): dynamodb:GetShardIterator
Resource:
arn:aws:dynamodb:region:account-id:table/table-name/stream/stream-label
or
    arn:aws:dynamodb:region:account-id:table/table-name/stream/*
ListStreams
Action(s): dynamodb:ListStreams
Resource:
arn:aws:dynamodb:region:account-id:table/table-name/stream/*
or
    arn:aws:dynamodb:region:account-id:table/*/stream/*
ListTables
Action(s): dynamodb:ListTables
Resource:
   *
PurchaseReservedCapacityOfferings
Action(s): dynamodb:PurchaseReservedCapacityOfferings
Resource:
   arn:aws:dynamodb:region:account-id:*
PutItem
Action(s): dynamodb:PutItem
Resource:
arn:aws:dynamodb:region:account-id:table/table-name
or
   arn:aws:dynamodb:region:account-id:table/*
Query
Action(s): dynamodb:Query
Resource:
To query a table:arn:aws:dynamodb:region:account-id:table/table-name
or:
arn:aws:dynamodb:region:account-id:table/table-name
To query an index:
arn:aws:dynamodb:region:account-id:table/table-name/index/index-name
or:
   arn:aws:dynamodb:region:account-id:table/table-name/index/*
Scan
Action(s): dynamodb:Scan
Resource:
To scan a table:
arn:aws:dynamodb:region:account-id:table/table-name
or:
arn:aws:dynamodb:region:account-id:table/table-name
To scan an index:
arn:aws:dynamodb:region:account-id:table/table-name/index/index-name
or:
        arn:aws:dynamodb:region:account-id:table/table-name/index/*
    UpdateItem
Action(s): dynamodb:UpdateItem
Resource:
arn:aws:dynamodb:region:account-id:table/table-name
or
        arn:aws:dynamodb:region:account-id:table/*
    UpdateTable
Action(s): dynamodb:UpdateTable
Resource:
arn:aws:dynamodb:region:account-id:table/table-name
or
arn:aws:dynamodb:region:account-id:table/*
    Related Topics
    • Access Control (p. 646)
    • Using IAM Policy Conditions for Fine-Grained Access Control (p. 661)
    Overview
    In DynamoDB, you have the option to specify conditions when granting permissions using an IAM policy
    (see Access Control (p. 646)). For example, you can:
    • Grant permissions to allow users read-only access to certain items and attributes in a table or a
      secondary index.
    • Grant permissions to allow users to write-only access to certain attributes in a table, based upon the
      identity of that user.
    In DynamoDB, you can specify conditions in an IAM policy using condition keys, as illustrated in the use
    case in the following section.
        Note
        Some AWS services also support tag-based conditions; however, DynamoDB does not.
• Grant permissions on a table, but restrict access to specific items in that table based on certain primary
  key values. An example might be a social networking app for games, where all users' saved game data
  is stored in a single table, but no users can access data items that they do not own, as shown in the
  following illustration:
• Hide information so that only a subset of attributes are visible to the user. An example might be an
  app that displays flight data for nearby airports, based on the user's location. Airline names, arrival and
  departure times, and flight numbers are all displayed. However, attributes such as pilot names or the
  number of passengers are hidden, as shown in the following illustration:
To implement this kind of fine-grained access control, you write an IAM permissions policy that specifies
conditions for accessing security credentials and the associated permissions. You then apply the policy to
IAM users, groups, or roles that you create using the IAM console. Your IAM policy can restrict access to
individual items in a table, access to the attributes in those items, or both at the same time.
You can optionally use web identity federation to control access by users who are authenticated by login
with Amazon, Facebook, or Google. For more information, see Using Web Identity Federation (p. 671).
You use the IAM Condition element to implement a fine-grained access control policy. By adding a
Condition element to a permissions policy, you can allow or deny access to items and attributes in
DynamoDB tables and indexes, based upon your particular business requirements.
As an example, consider a mobile gaming app that lets players select from and play a variety of different
games. The app uses a DynamoDB table named GameScores to keep track of high scores and other user
data. Each item in the table is uniquely identified by a user ID and the name of the game that the user
played. The GameScores table has a primary key consisting of a partition key (UserId) and sort key
(GameTitle). Users only have access to game data associated with their user ID. A user who wants to
play a game must belong to an IAM role named GameRole, which has a security policy attached to it.
To manage user permissions in this app, you could write a permissions policy such as the following:
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "AllowAccessToOnlyItemsMatchingUserID",
            "Effect": "Allow",
            "Action": [
                "dynamodb:GetItem",
                "dynamodb:BatchGetItem",
                "dynamodb:Query",
                "dynamodb:PutItem",
                "dynamodb:UpdateItem",
                "dynamodb:DeleteItem",
                "dynamodb:BatchWriteItem"
            ],
            "Resource": [
                "arn:aws:dynamodb:us-west-2:123456789012:table/GameScores"
            ],
            "Condition": {
                "ForAllValues:StringEquals": {
                    "dynamodb:LeadingKeys": [
                        "${www.amazon.com:user_id}"
                    ],
                    "dynamodb:Attributes": [
                        "UserId",
                        "GameTitle",
                        "Wins",
                        "Losses",
                        "TopScore",
                        "TopScoreDateTime"
                    ]
                },
                "StringEqualsIfExists": {
                    "dynamodb:Select": "SPECIFIC_ATTRIBUTES"
                }
            }
        }
    ]
}
In addition to granting permissions for specific DynamoDB actions (Action element) on the
GameScores table (Resource element), the Condition element uses the following condition keys
specific to DynamoDB that limit the permissions as follows:
• dynamodb:LeadingKeys – This condition key allows users to access only the items where
  the partition key value matches their user ID. This ID, ${www.amazon.com:user_id}, is a
  substitution variable. For more information about substitution variables, see Using Web Identity
  Federation (p. 671).
• dynamodb:Attributes – This condition key limits access to the specified attributes so that only
  the actions listed in the permissions policy can return values for these attributes. In addition, the
  StringEqualsIfExists clause ensures that the app must always provide a list of specific attributes
  to act upon and that the app can't request all attributes.
When an IAM policy is evaluated, the result is always either true (access is allowed) or false (access is
denied). If any part of the Condition element is false, the entire policy evaluates to false and access is
denied.
    Important
    If you use dynamodb:Attributes, you must specify the names of all of the primary key and
    index key attributes for the table and any secondary indexes that are listed the in the policy.
    Otherwise, DynamoDB can't use these key attributes to perform the requested action.
IAM policy documents can contain only the following Unicode characters: horizontal tab (U+0009),
linefeed (U+000A), carriage return (U+000D), and characters in the range U+0020 to U+00FF.
The following table shows the DynamoDB service-specific condition keys that apply to DynamoDB
 dynamodb:LeadingKeysRepresents the first key attribute of a table—in other words, the partition
                     key. Note that the key name LeadingKeys is plural, even if the key is
                     used with single-item actions. In addition, note that you must use the
                     ForAllValues modifier when using LeadingKeys in a condition.
 dynamodb:Select           Represents the Select parameter of a Query or Scan request. Select can
                           be any of the following values:
                           • ALL_ATTRIBUTES
                           • ALL_PROJECTED_ATTRIBUTES
                           • SPECIFIC_ATTRIBUTES
                           • COUNT
 dynamodb:Attributes Represents a list of the attribute names in a request, or the attributes that
                     are returned from a request. Attributes values are named the same way
                     and have the same meaning as the parameters for certain DynamoDB API
                     actions, as shown following:
• AttributesToGet
                           • ALL_OLD
                           • UPDATED_OLD
                            • TOTAL
                            • NONE
1: Grant Permissions that Limit Access to Items with a Specific Partition Key
Value
The following permissions policy grants permissions that allow a set of DynamoDB actions on the
GamesScore table. It uses the dynamodb:LeadingKeys condition key to limit user actions only on the
items whose UserID partition key value matches the Login with Amazon unique user ID for this app.
    Important
    The list of actions does not include permissions for Scan because Scan returns all items
    regardless of the leading keys.
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "FullAccessToUserItems",
            "Effect": "Allow",
            "Action": [
                "dynamodb:GetItem",
                "dynamodb:BatchGetItem",
                "dynamodb:Query",
                "dynamodb:PutItem",
                "dynamodb:UpdateItem",
                "dynamodb:DeleteItem",
                "dynamodb:BatchWriteItem"
            ],
            "Resource": [
                "arn:aws:dynamodb:us-west-2:123456789012:table/GameScores"
            ],
            "Condition": {
                "ForAllValues:StringEquals": {
                    "dynamodb:LeadingKeys": [
                        "${www.amazon.com:user_id}"
                    ]
                }
            }
        }
    ]
}
    Note
    When using policy variables, you must explicitly specify version 2012-10-17 in the policy. The
    default version of the access policy language, 2008-10-17, does not support policy variables.
To implement read-only access, you can remove any actions that can modify the data. In the following
policy, only those actions that provide read-only access are included in the condition.
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "ReadOnlyAccessToUserItems",
            "Effect": "Allow",
            "Action": [
                "dynamodb:GetItem",
                "dynamodb:BatchGetItem",
                "dynamodb:Query"
            ],
            "Resource": [
                "arn:aws:dynamodb:us-west-2:123456789012:table/GameScores"
            ],
            "Condition": {
                "ForAllValues:StringEquals": {
                    "dynamodb:LeadingKeys": [
                        "${www.amazon.com:user_id}"
                    ]
                }
            }
        }
    ]
}
    Important
    If you use dynamodb:Attributes, you must specify the names of all of the primary key
    and index key attributes, for the table and any secondary indexes that are listed in the policy.
    Otherwise, DynamoDB can't use these key attributes to perform the requested action.
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "LimitAccessToSpecificAttributes",
            "Effect": "Allow",
            "Action": [
                "dynamodb:UpdateItem",
                "dynamodb:GetItem",
                "dynamodb:Query",
                "dynamodb:BatchGetItem",
                "dynamodb:Scan"
            ],
            "Resource": [
                "arn:aws:dynamodb:us-west-2:123456789012:table/GameScores"
            ],
            "Condition": {
                "ForAllValues:StringEquals": {
                    "dynamodb:Attributes": [
                        "UserId",
                        "TopScore"
                    ]
                },
                "StringEqualsIfExists": {
                    "dynamodb:Select": "SPECIFIC_ATTRIBUTES",
                    "dynamodb:ReturnValues": [
                        "NONE",
                        "UPDATED_OLD",
                        "UPDATED_NEW"
                    ]
                }
            }
        }
    ]
}
    Note
    The policy takes a whitelist approach, which allows access to a named set of attributes. You
    can write an equivalent policy that denies access to other attributes instead (that is, a blacklist
    approach). We don't recommend this blacklist approach because users can determine the
    names of these blacklisted attributes by repeatedly issuing requests for all possible attribute
    names, eventually finding an attribute that they aren't allowed to access. To avoid this, follow
    the principle of least privilege, as explained in Wikipedia at http://en.wikipedia.org/wiki/
    Principle_of_least_privilege, and use a whitelist approach to enumerate all of the allowed
    values, rather than specifying the denied attributes.
This policy doesn't permit PutItem, DeleteItem, or BatchWriteItem, because these actions always
replace the entire previous item, which would allow users to delete the previous values for attributes
that they are not allowed to access.
• If the user specifies the Select parameter, then its value must be SPECIFIC_ATTRIBUTES. This
  requirement prevents the API action from returning any attributes that aren't allowed, such as from an
  index projection.
• If the user specifies the ReturnValues parameter, then its value must be NONE, UPDATED_OLD or
  UPDATED_NEW. This is required because the UpdateItem action also performs implicit read operations
  to check whether an item exists before replacing it, and so that previous attribute values can be
  returned if requested. Restricting ReturnValues in this way ensures that users can only read or write
  the allowed attributes.
• The StringEqualsIfExists clause assures that only one of these parameters — Select or
  ReturnValues — can be used per request, in the context of the allowed actions.
• To allow only read actions, we can remove UpdateItem from the list of allowed actions. Because none
  of the remaining actions accept ReturnValues, we can remove ReturnValues from the condition.
  We can also change StringEqualsIfExists to StringEquals because the Select parameter
  always has a value (ALL_ATTRIBUTES, unless otherwise specified).
• To allow only write actions, we can remove everything except UpdateItem from the list of allowed
  actions. Because UpdateItem does not use the Select parameter, we can remove Select from
  the condition. We must also change StringEqualsIfExists to StringEquals because the
  ReturnValues parameter always has a value (NONE unless otherwise specified).
• To allow all attributes whose name matches a pattern, use StringLike instead of StringEquals,
  and use a multi-character pattern match wildcard character (*).
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "PreventUpdatesOnCertainAttributes",
            "Effect": "Allow",
            "Action": [
                "dynamodb:UpdateItem"
            ],
            "Resource": "arn:aws:dynamodb:us-west-2:123456789012:table/GameScores",
            "Condition": {
                "ForAllValues:StringNotLike": {
                    "dynamodb:Attributes": [
                        "FreeGamesAvailable",
                        "BossLevelUnlocked"
                    ]
                },
                "StringEquals": {
                    "dynamodb:ReturnValues": [
                        "NONE",
                        "UPDATED_OLD",
                        "UPDATED_NEW"
                    ]
                }
            }
        }
    ]
}
• The UpdateItem action, like other write actions, requires read access to the items so that it can
  return values before and after the update. In the policy, you limit the action to accessing only the
  attributes that are allowed to be updated by specifying the dynamodb:ReturnValues condition
  key. The condition key restricts ReturnValues in the request to specify only NONE, UPDATED_OLD, or
  UPDATED_NEW and doesn't include ALL_OLD or ALL_NEW.
• The PutItem and DeleteItem actions replace an entire item, and thus allows applications to modify
  any attributes. So when limiting an application to updating only specific attributes, you should not
  grant permission for these APIs.
To require the application to specify a list of attributes in the query, the policy also specifies the
dynamodb:Select condition key to require that the Select parameter of the DynamoDB Query action
is SPECIFIC_ATTRIBUTES. The list of attributes is limited to a specific list that is provided using the
dynamodb:Attributes condition key.
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "QueryOnlyProjectedIndexAttributes",
            "Effect": "Allow",
            "Action": [
                "dynamodb:Query"
            ],
            "Resource": [
                "arn:aws:dynamodb:us-west-2:123456789012:table/GameScores/index/
TopScoreDateTimeIndex"
            ],
            "Condition": {
                "ForAllValues:StringEquals": {
                    "dynamodb:Attributes": [
                        "TopScoreDateTime",
                        "GameTitle",
                        "Wins",
                        "Losses",
                        "Attempts"
                    ]
                },
                "StringEquals": {
                    "dynamodb:Select": "SPECIFIC_ATTRIBUTES"
                }
            }
        }
    ]
}
The following permissions policy is similar, but the query must request all of the attributes that have
been projected into the index.
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "QueryAllIndexAttributes",
            "Effect": "Allow",
            "Action": [
                "dynamodb:Query"
            ],
            "Resource": [
                "arn:aws:dynamodb:us-west-2:123456789012:table/GameScores/index/
TopScoreDateTimeIndex"
            ],
            "Condition": {
                "StringEquals": {
                    "dynamodb:Select": "ALL_PROJECTED_ATTRIBUTES"
                }
            }
        }
    ]
}
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "LimitAccessToCertainAttributesAndKeyValues",
            "Effect": "Allow",
            "Action": [
                "dynamodb:UpdateItem",
                "dynamodb:GetItem",
                "dynamodb:Query",
                "dynamodb:BatchGetItem"
            ],
            "Resource": [
                "arn:aws:dynamodb:us-west-2:123456789012:table/GameScores",
                "arn:aws:dynamodb:us-west-2:123456789012:table/GameScores/index/
TopScoreDateTimeIndex"
            ],
            "Condition": {
                "ForAllValues:StringEquals": {
                    "dynamodb:LeadingKeys": [
                        "${graph.facebook.com:id}"
                    ],
                    "dynamodb:Attributes": [
                        "attribute-A",
                        "attribute-B"
                    ]
                },
                "StringEqualsIfExists": {
                    "dynamodb:Select": "SPECIFIC_ATTRIBUTES",
                    "dynamodb:ReturnValues": [
                        "NONE",
                        "UPDATED_OLD",
                        "UPDATED_NEW"
                    ]
                }
            }
        }
    ]
}
    • Write actions allowed by the policy (UpdateItem) can only modify attribute-A or attribute-B.
    • Because the policy allows UpdateItem, an application can insert new items, and the hidden attributes
      will be null in the new items. If these attributes are projected into TopScoreDateTimeIndex , the
      policy has the added benefit of preventing queries that cause fetches from the table.
    • Applications cannot read any attributes other than those listed in dynamodb:Attributes. With this
      policy in place, an application must set the Select parameter to SPECIFIC_ATTRIBUTES in read
      requests, and only whitelisted attributes can be requested. For write requests, the application cannot
      set ReturnValues to ALL_OLD or ALL_NEW and it cannot perform conditional write operations based
      on any other attributes.
    Related Topics
    • Access Control (p. 646)
    • DynamoDB API Permissions: Actions, Resources, and Conditions Reference (p. 657)
    • The Web Identity Federation Playground is an interactive website that lets you walk through the
      process of authenticating via Login with Amazon, Facebook, or Google, getting temporary security
      credentials, and then using those credentials to make a request to AWS.
    • The entry Web Identity Federation using the AWS SDK for .NET on the AWS .NET Development blog
      walks through how to use web identity federation with Facebook and includes code snippets in C# that
      show how to assume an IAM role with web identity and how to use temporary security credentials to
      access an AWS resource.
    • The AWS SDK for iOS and the AWS SDK for Android contain sample apps. These apps include code that
      shows how to invoke the identity providers, and then how to use the information from these providers
      to get and use temporary security credentials.
    • The article Web Identity Federation with Mobile Applications discusses web identity federation and
      shows an example of how to use web identity federation to access an AWS resource.
 Table Name                Primary Key Type           Partition Key Name        Sort Key Name and
                                                      and Type                  Type
Now suppose that a mobile gaming app uses this table, and that app needs to support thousands, or
even millions, of users. At this scale, it becomes very difficult to manage individual app users, and to
guarantee that each user can only access their own data in the GameScores table. Fortunately, many
users already have accounts with a third-party identity provider, such as Facebook, Google, or Login with
Amazon — so it makes sense to leverage one of these providers for authentication tasks.
To do this using web identity federation, the app developer must register the app with an identity
provider (such as Login with Amazon) and obtain a unique app ID. Next, the developer needs to create an
IAM role. (For this example, we will give this role a name of GameRole.) The role must have an IAM policy
document attached to it, specifying the conditions under which the app can access GameScores table.
When a user want to play a game, he signs in to his Login with Amazon account from within the gaming
app. The app then calls AWS Security Token Service (AWS STS), providing the Login with Amazon app ID
and requesting membership in GameRole. AWS STS returns temporary AWS credentials to the app and
allows it to access the GameScores table, subject to the GameRole policy document.
1. The app calls a third-party identity provider to authenticate the user and the app. The identity
   provider returns a web identity token to the app.
2. The app calls AWS STS and passes the web identity token as input. AWS STS authorizes the app and
   gives it temporary AWS access credentials. The app is allowed to assume an IAM role (GameRole) and
   access AWS resources in accordance with the role's security policy.
3. The app calls DynamoDB to access the GameScores table. Because it has assumed the GameRole, the
   app is subject to the security policy associated with that role. The policy document prevents the app
   from accessing data that does not belong to the user.
Once again, here is the security policy for GameRole that was shown in Using IAM Policy Conditions for
Fine-Grained Access Control (p. 661):
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "AllowAccessToOnlyItemsMatchingUserID",
            "Effect": "Allow",
            "Action": [
                "dynamodb:GetItem",
                "dynamodb:BatchGetItem",
                    "dynamodb:Query",
                    "dynamodb:PutItem",
                    "dynamodb:UpdateItem",
                    "dynamodb:DeleteItem",
                    "dynamodb:BatchWriteItem"
               ],
               "Resource": [
                   "arn:aws:dynamodb:us-west-2:123456789012:table/GameScores"
               ],
               "Condition": {
                   "ForAllValues:StringEquals": {
                       "dynamodb:LeadingKeys": [
                           "${www.amazon.com:user_id}"
                       ],
                       "dynamodb:Attributes": [
                           "UserId",
                           "GameTitle",
                           "Wins",
                           "Losses",
                           "TopScore",
                           "TopScoreDateTime"
                       ]
                   },
                   "StringEqualsIfExists": {
                       "dynamodb:Select": "SPECIFIC_ATTRIBUTES"
                   }
               }
          }
      ]
}
The Condition clause determines which items in GameScores are visible to the app. It does this by
comparing the Login with Amazon ID to the UserId partition key values in GameScores. Only the items
belonging to the current user can be processed using one of DynamoDB actions that are listed in this
policy—other items in the table cannot be accessed. Furthermore, only the specific attributes listed in
the policy can be accessed.
1. Sign up as a developer with a third-party identity provider. The following external links provide
   information about signing up with supported identity providers:
    • Login with Amazon Developer Center
    • Registration on the Facebook site
    • Using OAuth 2.0 to Access Google APIs on the Google site
2. Register your app with the identity provider. When you do this, the provider gives you an ID that's
   unique to your app. If you want your app to work with multiple identity providers, you will need to
   obtain an app ID from each provider.
3. Create one or more IAM roles. You will need one role for each identity provider for each app. For
   example, you might create a role that can be assumed by an app where the user signed in using Login
   with Amazon, a second role for the same app where the user has signed in using Facebook, and a third
   role for the app where users sign in using Google.
    As part of the role creation process, you will need to attach an IAM policy to the role. Your policy
    document should define the DynamoDB resources required by your app, and the permissions for
    accessing those resources.
For more information, see About Web Identity Federation in IAM User Guide.
      Note
      As an alternative to AWS Security Token Service, you can use Amazon Cognito. Amazon
      Cognito is the preferred service for managing temporary credentials for mobile apps. For more
      information, see the following pages:
1.    Sign in to the AWS Management Console and open the DynamoDB console at https://
      console.aws.amazon.com/dynamodb/.
2.    In the navigation pane, choose Tables.
3.    In the list of tables, choose the table for which you want to create the IAM policy.
4.    Choose the Access control tab.
5.    Choose the identity provider, actions, and attributes for the policy.
      When the settings are as you want them, click Create policy. The generated policy appears.
6.    Click Attach policy instructions, and follow the steps required to attach the generated policy to an
      IAM role.
At runtime, if your app uses web identity federation, it must follow these steps:
1. Authenticate with a third-party identity provider. Your app must call the identity provider using an
   interface that they provide. The exact way in which you authenticate the user depends on the provider
   and on what platform your app is running. Typically, if the user is not already signed in, the identity
   provider takes care of displaying a sign-in page for that provider.
     After the identity provider authenticates the user, the provider returns a web identity token to your
     app. The format of this token depends on the provider, but is typically a very long string of characters.
2. Obtain temporary AWS security credentials. To do this, your app sends a
   AssumeRoleWithWebIdentity request to AWS Security Token Service (AWS STS). This request
   contains:
     • The web identity token from the previous step
     • The app ID from the identity provider
     • The Amazon Resource Name (ARN) of the IAM role that you created for this identity provider for this
       app
     AWS STS returns a set of AWS security credentials that expire after a certain amount of time (3600
     seconds, by default).
                                      API Version 2012-08-10
                                                675
                             Amazon DynamoDB Developer Guide
                       Writing Your App to Use Web Identity Federation
  GET / HTTP/1.1
  Host: sts.amazonaws.com
  Content-Type: application/json; charset=utf-8
  URL: https://sts.amazonaws.com/?ProviderId=www.amazon.com
  &DurationSeconds=900&Action=AssumeRoleWithWebIdentity
  &Version=2011-06-15&RoleSessionName=web-identity-federation
  &RoleArn=arn:aws:iam::123456789012:role/GameRole
  &WebIdentityToken=Atza|IQEBLjAsAhQluyKqyBiYZ8-kclvGTYM81e...(remaining characters
   omitted)
  <AssumeRoleWithWebIdentityResponse
    xmlns="https://sts.amazonaws.com/doc/2011-06-15/">
    <AssumeRoleWithWebIdentityResult>
      <SubjectFromWebIdentityToken>amzn1.account.AGJZDKHJKAUUSW6C44CHPEXAMPLE</
  SubjectFromWebIdentityToken>
      <Credentials>
        <SessionToken>AQoDYXdzEMf//////////wEa8AP6nNDwcSLnf+cHupC...(remaining characters
   omitted)</SessionToken>
        <SecretAccessKey>8Jhi60+EWUUbbUShTEsjTxqQtM8UKvsM6XAjdA==</SecretAccessKey>
        <Expiration>2013-10-01T22:14:35Z</Expiration>
        <AccessKeyId>06198791C436IEXAMPLE</AccessKeyId>
      </Credentials>
      <AssumedRoleUser>
        <Arn>arn:aws:sts::123456789012:assumed-role/GameRole/web-identity-federation</Arn>
        <AssumedRoleId>AROAJU4SA2VW5SZRF2YMG:web-identity-federation</AssumedRoleId>
      </AssumedRoleUser>
    </AssumeRoleWithWebIdentityResult>
    <ResponseMetadata>
      <RequestId>c265ac8e-2ae4-11e3-8775-6969323a932d</RequestId>
    </ResponseMetadata>
  </AssumeRoleWithWebIdentityResponse>
3. Access AWS resources. The response from AWS STS contains information that your app will require in
   order to access DynamoDB resources:
  • The AccessKeyID, SecretAccessKey and SessionToken fields contain security credentials that
    are valid for this user and this app only.
  • The Expiration field signifies the time limit for these credentials, after which they will no longer
    be valid.
  • The AssumedRoleId field contains the name of a session-specific IAM role that has been assumed
    by the app. The app will honor the access controls in the IAM policy document for the duration of
    this session.
  • The SubjectFromWebIdentityToken field contains the unique ID that appears in an IAM
    policy variable for this particular identity provider. The following are the IAM policy variables for
    supported providers, and some example values for them:
${www.amazon.com:user_id} amzn1.account.AGJZDKHJKAUUSW6C44CHPEXAMPLE
${graph.facebook.com:id} 123456789
${accounts.google.com:sub} 123456789012345678901
For example IAM policies where these policy variables are used, see Example Policies: Using Conditions
for Fine-Grained Access Control (p. 665).
For more information about how AWS Security Token Service generates temporary access credentials,
see Requesting Temporary Security Credentials in IAM User Guide.
Monitoring DynamoDB
    Monitoring is an important part of maintaining the reliability, availability, and performance of Amazon
    DynamoDB and your AWS solutions. You should collect monitoring data from all parts of your AWS
    solution so that you can more easily debug a multi-point failure, if one occurs. Before you start
    monitoring Amazon DynamoDB, you should create a monitoring plan that includes answers to the
    following questions:
    The next step is to establish a baseline for normal Amazon DynamoDB performance in your environment,
    by measuring performance at various times and under different load conditions. As you monitor Amazon
    DynamoDB, you should consider storing historical monitoring data. This stored data will give you a
    baseline from which to compare current performance data, identify normal performance patterns and
    performance anomalies, and devise methods to address issues.
    • The number of read or write capacity units consumed over the specified time period, so you can track
      how much of your provisioned throughput is used.
    • Requests that exceeded a table's provisioned write or read capacity during the specified time period, so
      you can determine which requests exceed the provisioned throughput limits of a table.
    • System errors, so you can determine if any requests resulted in an error.
    Topics
     • Monitoring Tools (p. 678)
     • Monitoring with Amazon CloudWatch (p. 679)
     • Logging DynamoDB Operations by Using AWS CloudTrail (p. 696)
Monitoring Tools
    AWS provides tools that you can use to monitor Amazon DynamoDB. You can configure some of
    these tools to do the monitoring for you; some require manual intervention. We recommend that you
    automate monitoring tasks as much as possible.
    • Amazon CloudWatch Alarms – Watch a single metric over a time period that you specify, and perform
      one or more actions based on the value of the metric relative to a given threshold over a number of
      time periods. The action is a notification sent to an Amazon Simple Notification Service (Amazon SNS)
      topic or Amazon EC2 Auto Scaling policy. CloudWatch alarms do not invoke actions simply because
      they are in a particular state; the state must have changed and been maintained for a specified
      number of periods. For more information, see Monitoring with Amazon CloudWatch (p. 679).
    • Amazon CloudWatch Logs – Monitor, store, and access your log files from AWS CloudTrail or other
      sources. For more information, see Monitoring Log Files in the Amazon CloudWatch User Guide.
    • Amazon CloudWatch Events – Match events and route them to one or more target functions or
      streams to make changes, capture state information, and take corrective action. For more information,
      see What is Amazon CloudWatch Events in the Amazon CloudWatch User Guide.
    • AWS CloudTrail Log Monitoring – Share log files between accounts, monitor CloudTrail log files in real
      time by sending them to CloudWatch Logs, write log processing applications in Java, and validate that
      your log files have not changed after delivery by CloudTrail. For more information, see Working with
      CloudTrail Log Files in the AWS CloudTrail User Guide.
    Topics
     • Amazon DynamoDB Metrics and Dimensions (p. 680)
     • How Do I Use Amazon DynamoDB Metrics? (p. 693)
     • Creating CloudWatch Alarms to Monitor Amazon DynamoDB (p. 694)
                                    API Version 2012-08-10
                                              679
                             Amazon DynamoDB Developer Guide
                                  Metrics and Dimensions
Metrics are grouped first by the service namespace, and then by the various dimension combinations
within each namespace.
DynamoDB Metrics
The following metrics are available from Amazon DynamoDB. Note that DynamoDB only sends metrics
to CloudWatch when they have a non-zero value. For example, the UserErrors metric is incremented
whenever a request generates an HTTP 400 status code. If no HTTP 400 errors were encountered during
a time period, CloudWatch will not provide metrics for UserErrors during that period.
     Note
     Amazon CloudWatch aggregates the following DynamoDB metrics at one-minute intervals:
     • ConditionalCheckFailedRequests
     • ConsumedReadCapacityUnits
     • ConsumedWriteCapacityUnits
     • ReadThrottleEvents
     • ReturnedBytes
     • ReturnedItemCount
     • ReturnedRecordsCount
     • SuccessfulRequestLatency
     • SystemErrors
     • TimeToLiveDeletedItemCount
     • ThrottledRequests
     • UserErrors
     • WriteThrottleEvents
For all other DynamoDB metrics, the aggregation granularity is five minutes.
Not all statistics, such as Average or Sum, are applicable for every metric. However, all of these values are
available through the Amazon DynamoDB console, or by using the CloudWatch console, AWS CLI, or AWS
SDKs for all metrics. In the following table, each metric has a list of Valid Statistics that is applicable to
that metric.
Metric Description
Units: Count
Dimensions: TableName
Valid Statistics:
                                             • Minimum
                                             • Maximum
                                             • Average
                                             • SampleCount
                                             • Sum
Units: Count
Valid Statistics:
Metric                          Description
                                • Minimum – Minimum number of read capacity units
                                  consumed by any individual request to the table or index.
                                • Maximum – Maximum number of read capacity units
                                  consumed by any individual request to the table or index.
                                • Average – Average per-request read capacity consumed.
                                • Sum – Total read capacity units consumed. This is the most
                                  useful statistic for the ConsumedReadCapacityUnits
                                  metric.
                                • SampleCount – Number of requests to DynamoDB that
                                  consumed read capacity.
Units: Count
Valid Statistics:
Metric Description
                                      You can adjust the write capacity of the index using the
                                      UpdateTable operation, even while the index is still being
                                      built.
Units: Count
Valid Statistics:
                                      • Minimum
                                      • Maximum
                                      • Average
                                      • SampleCount
                                      • Sum
Units: Count
Valid Statistics:
                                      • Minimum
                                      • Maximum
                                      • Average
                                      • SampleCount
                                      • Sum
Metric Description
OnlineIndexThrottleEvents          The number of write throttle events that occur when adding
                                   a new global secondary index to a table. These events
                                   indicate that the index creation will take longer to complete,
                                   because incoming write activity is exceeding the provisioned
                                   write throughput of the index.
                                   You can adjust the write capacity of the index using the
                                   UpdateTable operation, even while the index is still being
                                   built.
Units: Count
Valid Statistics:
                                   • Minimum
                                   • Maximum
                                   • Average
                                   • SampleCount
                                   • Sum
Units: Count
Valid Statistics:
                                   • Average
                                   • Sample Count
                                   • Sum
Metric Description
Units: Count
Valid Statistics:
Metric Description
Units: Count
Valid Statistics:
Units: Count
Valid Statistics:
                                 • SampleCount
                                 • Sum
Metric Description
Units: Milliseconds
Valid Statistics:
                                 • Average
                                 • Minimum
                                 • Maximum
Units: Bytes
Valid Statistics:
                                 • Minimum
                                 • Maximum
                                 • Average
                                 • SampleCount
                                 • Sum
Units: Count
Valid Statistics:
                                 • Minimum
                                 • Maximum
                                 • Average
                                 • SampleCount
                                 • Sum
Metric Description
Units: Count
Valid Statistics:
                                   • Minimum
                                   • Maximum
                                   • Average
                                   • SampleCount
                                   • Sum
Units: Milliseconds
Valid Statistics:
                                   • Minimum
                                   • Maximum
                                   • Average
                                   • SampleCount
Units: Count
Valid Statistics:
                                   • Sum
                                   • SampleCount
Metric Description
Units: Count
Dimensions: TableName
Valid Statistics:
• Sum
Metric Description
Units: Count
Valid Statistics:
                                • Sum
                                • SampleCount
Metric Description
Units: Count
Valid Statistics:
                         • Sum
                         • SampleCount
Metric Description
Units: Count
Valid Statistics:
                                         • Sum
                                         • SampleCount
Dimension Description
Operation                         This dimension limits the data to one of the following DynamoDB
                                  operations:
                                  • PutItem
                                  • DeleteItem
                                  • UpdateItem
                                  • GetItem
                                  • BatchGetItem
                                  • Scan
                                  • Query
                                  • BatchWriteItem
Dimension                         Description
                                  In addition, you can limit the data to the following Amazon
                                  DynamoDB Streams operation:
• GetRecords
ReceivingRegion                   This dimension limits the data to a particular AWS region. It is used
                                  with metrics originating from replica tables within a DynamoDB
                                  global table.
TableName                         This dimension limits the data to a specific table. This value can be
                                  any table name in the current region and the current AWS account.
How can I monitor the             You can monitor TimeToLiveDeletedItemCount over the
rate of TTL deletions on          specified time period, to track the rate of TTL deletions on your
my table?                         table. For an example of a server-less application using the
                                  TimeToLiveDeletedItemCount metric, see Automatically
                                  archive items to S3 using DynamoDB Time to Live (TTL) with AWS
                                  Lambda and Amazon Kinesis Firehose.
How can I determine               You can monitor SystemErrors to determine if any requests
if any system errors              resulted in a HTTP 500 (server error) code. Typically, this metric
occurred?                         should be equal to zero. If it isn't, then you might want to
                                  investigate.
                                      Note
                                      You might encounter internal server errors while working
                                      with items. These are expected during the lifetime of a
                                      table. Any failed requests can be retried immediately.
     Note
     The alarm is activated whenever the consumed read capacity is at least 4 units per second (80%
     of provisioned read capacity of 5) for 1 minute (60 seconds). So the threshold is 240 read
     capacity units (4 units/sec * 60 seconds). Any time the read capacity is updated you should
     update the alarm calculations appropriately. You can avoid this process by creating alarms
     through the DynamoDB Console. In this way, the alarms are automatically updated for you.
Amazon DynamoDB
    • CreateBackup
    • CreateGlobalTable
    • CreateTable
    • DeleteBackup
    • DeleteTable
    • DescribeBackup
    • DescribeContinuousBackups
    • DescribeGlobalTable
    • DescribeLimits
    • DescribeTable
    • DescribeTimeToLive
    • ListBackups
    • ListTables
    • ListTagsOfResource
    • ListGlobalTables
    • RestoreTableFromBackup
    • TagResource
    • UntagResource
    • UpdateGlobalTable
    • UpdateTable
    • UpdateTimeToLive
    • DescribeReservedCapacity
    • DescribeReservedCapacityOfferings
    • PurchaseReservedCapacityOfferings
DynamoDB Streams
• DescribeStream
• ListStreams
• CreateCluster
• CreateParameterGroup
• CreateSubnetGroup
• DecreaseReplicationFactor
• DeleteCluster
• DeleteParameterGroup
• DeleteSubnetGroup
• DescribeClusters
• DescribeDefaultParameters
• DescribeEvents
• DescribeParameterGroups
• DescribeParameters
• DescribeSubnetGroups
• IncreaseReplicationFactor
• ListTags
• RebootNode
• TagResource
• UntagResource
• UpdateCluster
• UpdateParameterGroup
• UpdateSubnetGroup
Every log entry contains information about who generated the request. The user identity information
in the log helps you determine whether the request was made with root or IAM user credentials,
with temporary security credentials for a role or federated user, or by another AWS service. For more
information, see the userIdentity field in the CloudTrail Event Reference.
You can store your log files in your bucket for as long as you want, but you can also define Amazon S3
lifecycle rules to archive or delete log files automatically. By default, your log files are encrypted by using
Amazon S3 server-side encryption (SSE).
You can choose to have CloudTrail publish Amazon SNS notifications when new log files are delivered if
you want to take quick action upon log file delivery. For more information, see Configuring Amazon SNS
Notifications.
You can also aggregate DynamoDB log files from multiple AWS regions and multiple AWS accounts
into a single Amazon S3 bucket. For more information, see Receiving CloudTrail Log Files from Multiple
Regions .
are not guaranteed to be in any particular order. That is, they are not an ordered stack trace of the low-
level DynamoDB API calls.
{"Records": [
    {
        "eventVersion": "1.03",
        "userIdentity": {
            "type": "AssumedRole",
            "principalId": "AKIAIOSFODNN7EXAMPLE:bob",
            "arn": "arn:aws:sts::111122223333:assumed-role/users/bob",
            "accountId": "111122223333",
            "accessKeyId": "AKIAIOSFODNN7EXAMPLE",
            "sessionContext": {
                "attributes": {
                    "mfaAuthenticated": "false",
                    "creationDate": "2015-05-28T18:06:01Z"
                },
                "sessionIssuer": {
                    "type": "Role",
                    "principalId": "AKIAI44QH8DHBEXAMPLE",
                    "arn": "arn:aws:iam::444455556666:role/admin-role",
                    "accountId": "444455556666",
                    "userName": "bob"
                }
            }
        },
        "eventTime": "2015-05-01T07:24:55Z",
        "eventSource": "dynamodb.amazonaws.com",
        "eventName": "CreateTable",
        "awsRegion": "us-west-2",
        "sourceIPAddress": "192.0.2.0",
        "userAgent": "console.aws.amazon.com",
        "requestParameters": {
            "provisionedThroughput": {
                "writeCapacityUnits": 10,
                "readCapacityUnits": 10
            },
            "tableName": "Music",
            "keySchema": [
                {
                    "attributeName": "Artist",
                    "keyType": "HASH"
                },
                {
                    "attributeName": "SongTitle",
                    "keyType": "RANGE"
                }
            ],
            "attributeDefinitions": [
                {
                    "attributeType": "S",
                    "attributeName": "Artist"
                },
                {
                    "attributeType": "S",
                    "attributeName": "SongTitle"
                }
            ]
        },
        "responseElements": {"tableDescription": {
            "tableName": "Music",
            "attributeDefinitions": [
                {
                  "attributeType": "S",
                  "attributeName": "Artist"
             },
             {
                  "attributeType": "S",
                  "attributeName": "SongTitle"
             }
         ],
         "itemCount": 0,
         "provisionedThroughput": {
             "writeCapacityUnits": 10,
             "numberOfDecreasesToday": 0,
             "readCapacityUnits": 10
         },
         "creationDateTime": "May 1, 2015 7:24:55 AM",
         "keySchema": [
             {
                 "attributeName": "Artist",
                 "keyType": "HASH"
             },
             {
                 "attributeName": "SongTitle",
                 "keyType": "RANGE"
             }
         ],
         "tableStatus": "CREATING",
         "tableSizeBytes": 0
     }},
     "requestID": "KAVGJR1Q0I5VHF8FS8V809EV7FVV4KQNSO5AEMVJF66Q9ASUAAJG",
     "eventID": "a8b5f864-480b-43bf-bc22-9b6d77910a29",
     "eventType": "AwsApiCall",
     "apiVersion": "2012-08-10",
     "recipientAccountId": "111122223333"
},
{
     "eventVersion": "1.03",
     "userIdentity": {
         "type": "AssumedRole",
         "principalId": "AKIAIOSFODNN7EXAMPLE:bob",
         "arn": "arn:aws:sts::111122223333:assumed-role/users/bob",
         "accountId": "444455556666",
         "accessKeyId": "AKIAIOSFODNN7EXAMPLE",
         "sessionContext": {
             "attributes": {
                 "mfaAuthenticated": "false",
                 "creationDate": "2015-05-28T18:06:01Z"
             },
             "sessionIssuer": {
                 "type": "Role",
                 "principalId": "AKIAI44QH8DHBEXAMPLE",
                 "arn": "arn:aws:iam::444455556666:role/admin-role",
                 "accountId": "444455556666",
                 "userName": "bob"
             }
         }
     },
     "eventTime": "2015-05-04T02:43:11Z",
     "eventSource": "dynamodb.amazonaws.com",
     "eventName": "DescribeTable",
     "awsRegion": "us-west-2",
     "sourceIPAddress": "192.0.2.0",
     "userAgent": "console.aws.amazon.com",
     "requestParameters": {"tableName": "Music"},
     "responseElements": null,
     "requestID": "DISTSH6DQRLCC74L48Q51LRBHFVV4KQNSO5AEMVJF66Q9ASUAAJG",
     "eventID": "c07befa7-f402-4770-8c1b-1911601ed2af",
     "eventType": "AwsApiCall",
     "apiVersion": "2012-08-10",
     "recipientAccountId": "111122223333"
},
{
     "eventVersion": "1.03",
     "userIdentity": {
         "type": "AssumedRole",
         "principalId": "AKIAIOSFODNN7EXAMPLE:bob",
         "arn": "arn:aws:sts::111122223333:assumed-role/users/bob",
         "accountId": "111122223333",
         "accessKeyId": "AKIAIOSFODNN7EXAMPLE",
         "sessionContext": {
             "attributes": {
                 "mfaAuthenticated": "false",
                 "creationDate": "2015-05-28T18:06:01Z"
             },
             "sessionIssuer": {
                 "type": "Role",
                 "principalId": "AKIAI44QH8DHBEXAMPLE",
                 "arn": "arn:aws:iam::444455556666:role/admin-role",
                 "accountId": "444455556666",
                 "userName": "bob"
             }
         }
     },
     "eventTime": "2015-05-04T02:14:52Z",
     "eventSource": "dynamodb.amazonaws.com",
     "eventName": "UpdateTable",
     "awsRegion": "us-west-2",
     "sourceIPAddress": "192.0.2.0",
     "userAgent": "console.aws.amazon.com",
     "requestParameters": {"provisionedThroughput": {
         "writeCapacityUnits": 25,
         "readCapacityUnits": 25
     }},
     "responseElements": {"tableDescription": {
         "tableName": "Music",
         "attributeDefinitions": [
             {
                 "attributeType": "S",
                 "attributeName": "Artist"
             },
             {
                 "attributeType": "S",
                 "attributeName": "SongTitle"
             }
         ],
         "itemCount": 0,
         "provisionedThroughput": {
             "writeCapacityUnits": 10,
             "numberOfDecreasesToday": 0,
             "readCapacityUnits": 10,
             "lastIncreaseDateTime": "May 3, 2015 11:34:14 PM"
         },
         "creationDateTime": "May 3, 2015 11:34:14 PM",
         "keySchema": [
             {
                 "attributeName": "Artist",
                 "keyType": "HASH"
             },
             {
                 "attributeName": "SongTitle",
                 "keyType": "RANGE"
             }
         ],
         "tableStatus": "UPDATING",
         "tableSizeBytes": 0
     }},
     "requestID": "AALNP0J2L244N5O15PKISJ1KUFVV4KQNSO5AEMVJF66Q9ASUAAJG",
     "eventID": "eb834e01-f168-435f-92c0-c36278378b6e",
     "eventType": "AwsApiCall",
     "apiVersion": "2012-08-10",
     "recipientAccountId": "111122223333"
},
{
     "eventVersion": "1.03",
     "userIdentity": {
         "type": "AssumedRole",
         "principalId": "AKIAIOSFODNN7EXAMPLE:bob",
         "arn": "arn:aws:sts::111122223333:assumed-role/users/bob",
         "accountId": "111122223333",
         "accessKeyId": "AKIAIOSFODNN7EXAMPLE",
         "sessionContext": {
             "attributes": {
                 "mfaAuthenticated": "false",
                 "creationDate": "2015-05-28T18:06:01Z"
             },
             "sessionIssuer": {
                 "type": "Role",
                 "principalId": "AKIAI44QH8DHBEXAMPLE",
                 "arn": "arn:aws:iam::444455556666:role/admin-role",
                 "accountId": "444455556666",
                 "userName": "bob"
             }
         }
     },
     "eventTime": "2015-05-04T02:42:20Z",
     "eventSource": "dynamodb.amazonaws.com",
     "eventName": "ListTables",
     "awsRegion": "us-west-2",
     "sourceIPAddress": "192.0.2.0",
     "userAgent": "console.aws.amazon.com",
     "requestParameters": null,
     "responseElements": null,
     "requestID": "3BGHST5OVHLMTPUMAUTA1RF4M3VV4KQNSO5AEMVJF66Q9ASUAAJG",
     "eventID": "bd5bf4b0-b8a5-4bec-9edf-83605bd5e54e",
     "eventType": "AwsApiCall",
     "apiVersion": "2012-08-10",
     "recipientAccountId": "111122223333"
},
{
     "eventVersion": "1.03",
     "userIdentity": {
         "type": "AssumedRole",
         "principalId": "AKIAIOSFODNN7EXAMPLE:bob",
         "arn": "arn:aws:sts::111122223333:assumed-role/users/bob",
         "accountId": "111122223333",
         "accessKeyId": "AKIAIOSFODNN7EXAMPLE",
         "sessionContext": {
             "attributes": {
                 "mfaAuthenticated": "false",
                 "creationDate": "2015-05-28T18:06:01Z"
             },
             "sessionIssuer": {
                 "type": "Role",
                 "principalId": "AKIAI44QH8DHBEXAMPLE",
                 "arn": "arn:aws:iam::444455556666:role/admin-role",
                     "accountId": "444455556666",
                     "userName": "bob"
                 }
             }
         },
         "eventTime": "2015-05-04T13:38:20Z",
         "eventSource": "dynamodb.amazonaws.com",
         "eventName": "DeleteTable",
         "awsRegion": "us-west-2",
         "sourceIPAddress": "192.0.2.0",
         "userAgent": "console.aws.amazon.com",
         "requestParameters": {"tableName": "Music"},
         "responseElements": {"tableDescription": {
             "tableName": "Music",
             "itemCount": 0,
             "provisionedThroughput": {
                 "writeCapacityUnits": 25,
                 "numberOfDecreasesToday": 0,
                 "readCapacityUnits": 25
             },
             "tableStatus": "DELETING",
             "tableSizeBytes": 0
         }},
         "requestID": "4KBNVRGD25RG1KEO9UT4V3FQDJVV4KQNSO5AEMVJF66Q9ASUAAJG",
         "eventID": "a954451c-c2fc-4561-8aea-7a30ba1fdf52",
         "eventType": "AwsApiCall",
         "apiVersion": "2012-08-10",
         "recipientAccountId": "111122223333"
     }
]}
     • Design For Uniform Data Access Across Items In Your Tables (p. 704)
     • Understand Partition Behavior (p. 706)
     • Use Burst Capacity Sparingly (p. 710)
     • Distribute Write Activity During Data Upload (p. 711)
     • Understand Access Patterns for Time Series Data (p. 712)
     • Cache Popular Items (p. 712)
     • Consider Workload Uniformity When Adjusting Provisioned Throughput (p. 713)
     • Test Your Application At Scale (p. 713)
This section covers some best practices for working with tables.
     The primary key uniquely identifies each item in a table. The primary key can be simple (partition key) or
     composite (partition key and sort key).
     When it stores data, DynamoDB divides a table's items into multiple partitions, and distributes the
     data primarily based upon the partition key value. Consequently, to achieve the full amount of request
     throughput you have provisioned for a table, keep your workload spread evenly across the partition key
     values. Distributing requests across partition key values distributes the requests across partitions.
For example, if a table has a very small number of heavily accessed partition key values, possibly
even a single very heavily used partition key value, request traffic is concentrated on a small number
of partitions – potentially only one partition. If the workload is heavily unbalanced, meaning that
it is disproportionately focused on one or a few partitions, the requests will not achieve the overall
provisioned throughput level. To get the most out of DynamoDB throughput, create tables where
the partition key has a large number of distinct values, and values are requested fairly uniformly, as
randomly as possible.
This does not mean that you must access all of the partition key values to achieve your throughput level;
nor does it mean that the percentage of accessed partition key values needs to be high. However, be
aware that when your workload accesses more distinct partition key values, those requests will be spread
out across the partitioned space in a manner that better utilizes your allocated throughput level. In
general, you will utilize your throughput more efficiently as the ratio of partition key values accessed to
the total number of partition key values in a table grows.
If a single table has only a very small number of partition key values, consider distributing your write
operations across more distinct partition key values. In other words, structure the primary key elements
to avoid one "hot" (heavily requested) partition key value that slows overall performance.
For example, consider a table with a composite primary key. The partition key represents the item's
creation date, rounded to the nearest day. The sort key is an item identifier. On a given day, say
2014-07-09, all of the new items will be written to that same partition key value.
If the table will fit entirely into a single partition (taking into consideration growth of your data over
time), and if your application's read and write throughput requirements do not exceed the read and write
capabilities of a single partition, then your application should not encounter any unexpected throttling
as a result of partitioning.
However, if you anticipate scaling beyond a single partition, then you should architect your application
so that it can use more of the table's full provisioned throughput.
concatenate it as a suffix to the date. This will yield partition key values such as 2014-07-09.1,
2014-07-09.2 and so on through 2014-07-09.200. Because you are randomizing the partition key,
the writes to the table on each day are spread evenly across all of the partition key values; this will yield
better parallelism and higher overall throughput.
To read all of the items for a given day, you would need to obtain all of the items for each suffix. For
example, you would first issue a Query request for the partition key value 2014-07-09.1, then another
Query for 2014-07-09.2, and so on through 2014-07-09.200. Finally, your application would need to
merge the results from all of the Query requests.
Continuing with our example, suppose that each item has an OrderId. Before your application writes
the item to the table, it can calculate a partition key suffix based upon the order ID. The calculation
should result in a number between 1 and 200 that is fairly evenly distributed given any set of names (or
user IDs.)
A simple calculation would suffice, such as the product of the UTF-8 code point values for the characters
in the order ID, modulo 200 + 1. The partition key value would then be the date concatenated with the
calculation result as a suffix. With this strategy, the writes are spread evenly across the partition key
values, and thus across the partitions. You can easily perform a GetItem operation on a particular item,
because you can calculate the partition key value you need when you want to retrieve a specific OrderId
value.
To read all of the items for a given day, you would still need to Query each of the 2014-07-09.N keys
(where N is 1 to 200), and your application would need to merge all of the results. However, you will
avoid having a single "hot" partition key value taking all of the workload.
You can estimate the number of partitions that DynamoDB will initially allocate for your table, and
compare that estimate against your scale and access patterns. You can also estimate the number of
additional partitions that DynamoDB will allocate in response to increased storage or provisioned
throughput requirements. These estimates can help you determine the best table design for your
application needs.
    Note
    The following details about partition sizes and throughput are subject to change.
A single partition can support a maximum of 3,000 read capacity units or 1,000 write capacity units.
When you create a new table, the initial number of partitions can be expressed as follows:
For example, suppose that you created a table with 1,000 read capacity units and 500 write capacity
units. In this case, the initial number of partitions would be:
Therefore, a single partition could accommodate all of the table's provisioned throughput requirements.
However, if you had created the table with 1,000 read capacity units and 1,000 write capacity units, then
a single partition would not be able to support the specified throughput capacity:
In this case, the table would require two partitions, each with 500 read capacity units and 500 write
capacity units.
If necessary, DynamoDB can allocate additional partitions to your table by splitting an existing partition.
Suppose that one of a table's partitions (P) exceeded its limit for storage (10 GB). In this case, DynamoDB
would split the partition as follows:
The following diagram shows how DynamoDB performs a partition split. The large squares represent
partitions, and the small squares represent data items in the table.
During a partition split, DynamoDB evenly distributes the data from the old partition to the two new
partitions (the data in other partitions are not affected). The old partition's provisioned throughput
capacity is then evenly distributed across the two new partitions (see Throughput Capacity Per
Partition (p. 709)).
Note that DynamoDB performs partition splits automatically, in the background. The table remains fully
available for read and write activity at your specified throughput levels.
For example, suppose that you created a new table with 5,000 read capacity units and 2,000 write
capacity units. Using the information from Initial Allocation of Partitions (p. 706), you can determine
that this new table will require four partitions:
Each of the four partitions can accommodate 1,250 reads per second (5,000 read capacity units / 4
partitions) and 500 writes per second (2,000 write capacity units / 4 partitions).
Now suppose that you increased the table's read capacity units from 5,000 to 8,000. The existing four
partitions would not be able to support this requirement. In response (see Subsequent Allocation of
Partitions (p. 707)), DynamoDB would double the number of partitions to eight (4 * 2 = 8). Each of the
resulting partitions would be able to accommodate 1,000 reads per second (8,000 read capacity units / 8
partitions) and 250 writes per second (2,000 write capacity units / 8 partitions).
The following diagram shows the original four partitions in the table, and the resulting partition scheme
after DynamoDB doubles the number of partitions. The large squares represent partitions, and the small
squares represent data items in the table.
The table described in Increased Provisioned Throughput Settings (p. 708) has eight partitions, so its
maximum capacity would be approximately 80 GB, as shown following:
8 partitions * 10 GB = 80 GB
If one of these partitions were to fill to capacity, DynamoDB would respond by splitting that partition,
for a total of nine partitions and an overall capacity of 90 GB, as shown following:
9 partitions * 10 GB = 90 GB
The following diagram shows one of the original partitions filling to capacity, and the resulting partition
scheme after DynamoDB splits that partition. The large squares represent partitions, and the small
squares represent data items in the table.
You could determine the amount of read and write capacity per partition as follows:
5,000 read capacity units / 4 partitions = 1,250 read capacity units per partition
2,000 write capacity units / 4 partitions = 500 write capacity units per partition
Now suppose that one of these four partitions were to fill to capacity. DynamoDB would split that
partition, resulting in five partitions allocated to the table. The read and write capacity per partition
would then be distributed as follows:
1,250 read capacity units / 2 partitions = 625 read capacity units per child partition
500 write capacity units / 2 partitions = 250 write capacity units per child partition
As a result:
• Three of the five partitions would each have 1,250 read capacity units and 500 write capacity units.
• The other two partitions would each have 625 read capacity units and 250 write capacity units.
Note that as the number of partitions in a table increases, each partition has fewer read and write
capacity units available to it. (However, the total provisioned throughput for the table remains the same.)
DynamoDB adaptive capacity can help your application continue to perform read and write operations,
without being throttled in the event of a hot partition, as long as your application consumes less than
your table's overall provisioned capacity. With adaptive capacity, DynamoDB becomes more tolerant of
imbalanced workloads.
Adapative capacity is enabled automatically for every DynamoDB table. You don't need to explicitly
enable or disable adaptive capacity.
    Note
    Even though adaptive capacity is the default behavior, you should avoid designing your
    application in such a way that it depends on adaptive capacity.
    DynamoDB provides adaptive capacity only on a best-effort basis. In addition, there is typically
    a 5- to 30-minute interval between the time a table experiences throttling and the time that
    adaptive capacity becomes active for that table.
The following diagram illustrates how adaptive capacity works. The example table is provisioned with
400 write capacity units (WCUs) evenly shared across 4 partitions, allowing each partition to sustain up
to 100 WCUs per second. Partitions 1, 2, 3 each receive write traffic of 50 WCU/sec., while partition 4
receives 150 WCU/sec. This hot partition can accept write traffic while it still has unused burst capacity,
but it will eventually throttle traffic that exceeds 100 WCU/sec. DynamoDB adaptive capacity responds
by increasing the partition's capacity, so that the partition can sustain the higher workload of 150 WCUs/
sec., without being throttled.
UserID MessageID
U1 1
U1 2
U1 ...
U1 ... up to 100
U2 1
U2 2
U2 ...
U2 ... up to 200
The problem in this case is that you are not distributing your write requests to DynamoDB across your
partition key values. You are taking one partition key value at a time and uploading all its items, before
going to the next partition key value and doing the same. Behind the scenes, DynamoDB is partitioning
the data in your tables across multiple servers. To fully utilize all of the throughput capacity that has
been provisioned for your tables, you need to distribute your workload across your partition key values.
In this case, by directing an uneven amount of upload work toward items all with the same partition key
value, you may not be able to fully utilize all of the resources DynamoDB has provisioned for your table.
You can distribute your upload work by uploading one item from each partition key value first. Then you
repeat the pattern for the next set of sort key values for all the items until you upload all the data as
shown in the example upload sequence in the following table:
UserID MessageID
U1 1
UserID MessageID
U2 1
U3 1
... ....
U1 2
U2 2
U3 2
... ...
Every upload in this sequence uses a different partition key value, keeping more DynamoDB servers busy
simultaneously and improving your throughput performance.
Suppose you design a table to track customer behavior on your site, such as URLs that they click. You
might design the table with a composite primary key consisting of Customer ID as the partition key
and date/time as the sort key. In this application, customer data grows indefinitely over time; however,
the applications might show uneven access pattern across all the items in the table where the latest
customer data is more relevant and your application might access the latest items more frequently and
as time passes these items are less accessed, eventually the older items are rarely accessed. If this is a
known access pattern, you could take it into consideration when designing your table schema. Instead
of storing all items in a single table, you could use multiple tables to store these items. For example, you
could create tables to store monthly or weekly data. For the table storing data from the latest month or
week, where data access rate is high, request higher throughput and for tables storing older data, you
could dial down the throughput and save on resources.
You can save on resources by storing "hot" items in one table with higher throughput settings, and "cold"
items in another table with lower throughput settings. You can remove old items by simply deleting the
tables. You can optionally backup these tables to other storage options such as Amazon Simple Storage
Service (Amazon S3). Deleting an entire table is significantly more efficient than removing items one-
by-one, which essentially doubles the write throughput as you do as many delete operations as put
operations.
One solution would be to cache these reads at the application layer. Caching is a technique that is used
in many high-throughput applications, offloading read activity on hot items to the cache rather than to
the database. Your application can cache the most popular items in memory, or use a product such as
ElastiCache to do the same.
Continuing with the ProductCatalog example, when a customer requests an item from that table, the
application would first consult the cache to see if there is a copy of the item there. If so, it is a cache hit;
otherwise, it is a cache miss. When there is a cache miss, the application would need to read the item
from DynamoDB and store a copy of the item in the cache. Over time, the cache misses would decrease
as the cache fills with the most popular items; applications would not need to access DynamoDB at all
for these items.
A caching solution can mitigate the skewed read activity for popular items. In addition, since it reduces
the amount of read activity against the table, caching can help reduce your overall costs for using
DynamoDB.
For applications that are designed for use with uniform workloads, DynamoDB's partition allocation
activity is not noticeable. A temporary non-uniformity in a workload can generally be absorbed by the
bursting allowance, as described in Use Burst Capacity Sparingly (p. 710). However, if your application
must accommodate non-uniform workloads on a regular basis, you should design your table with
DynamoDB's partitioning behavior in mind (see Understand Partition Behavior (p. 706)), and be mindful
when increasing and decreasing provisioned throughput on that table.
If you reduce the amount of provisioned throughput for your table, DynamoDB will not decrease the
number of partitions. Suppose that you created a table with a much larger amount of provisioned
throughput than your application actually needed, and then decreased the provisioned throughput later.
In this scenario, the provisioned throughput per partition would be less than it would have been if you
had initially created the table with less throughput.
For example, consider a situation where you need to bulk-load 20 million items into a DynamoDB table.
Assume that each item is 1 KB in size, resulting in 20 GB of data. This bulk-loading task will require a
total of 20 million write capacity units. To perform this data load within 30 minutes, you would need to
set the provisioned write throughput of the table to 11,000 write capacity units.
The maximum write throughput of a partition is 1000 write capacity units (see Understand Partition
Behavior (p. 706)); therefore, DynamoDB will create 11 partitions, each with 1000 provisioned write
capacity units.
After the bulk data load, your steady-state write throughput requirements might be much lower — for
example, suppose that your applications only require 200 writes per second. If you decrease the table's
provisioned throughput to this level, each of the 11 partitions will have around 20 write capacity units
per second provisioned. This level of per-partition provisioned throughput, combined with DynamoDB's
bursting behavior, might be adequate for the application.
However, if an application will require sustained write throughput above 20 writes per second per
partition, you should either: (a) design a schema that requires fewer writes per second per partition key
value, or (b) design the bulk data load so that it runs at a slower pace and reduces the initial throughput
requirement. For example, suppose that it was acceptable to run the bulk import for over 3 hours,
instead of just 30 minutes. In this scenario, only 1900 write capacity units per second needs to be
provisioned, rather than 11,000. As a result, DynamoDB would create only two partitions for the table.
     have defined for the table. As your table grows larger, DynamoDB automatically scales your table out
     by distributing the data across more partitions. When this occurs, the provisioned throughput that is
     allocated to each resulting partition is less than that which is allocated for the original partition(s).
     Suppose that your application accesses the table's data across all of the partition key values, but in a
     non-uniform fashion (accessing a small number of partition key values more frequently than others).
     Your application might perform acceptably when there is not very much data in the table. However, as
     the table becomes larger, there will be more partitions and less throughput per partition. You might
     discover that your application is throttled when it attempts to use the same non-uniform access pattern
     that worked in the past.
     To avoid problems with "hot" keys when your table becomes larger, make sure that you test your
     application design at scale. Consider the ratio of storage to throughput when running at scale, and
     how DynamoDB will allocate partitions to the table. (For more information, see Understand Partition
     Behavior (p. 706).)
     If it isn't possible for you to generate a large amount of test data, you can create a table that has
     very high provisioned throughput settings. This will create a table with many partitions; you can then
     use UpdateTable to reduce the settings, but keep the same ratio of storage to throughput that you
     determined for running the application at scale. You now have a table that has the throughput-per-
     partition ratio that you expect after it grows to scale. Test your application against this table using a
     realistic workload.
     Tables that store time series data can grow in an unbounded manner, and can cause slower application
     performance over time. With time series data, applications typically read and write the most recent items
     in the table more frequently than older items. If you can remove older time series data from your real-
     time table, and archive that data elsewhere, you can maintain a high ratio of throughput per partition.
For best practices with time series data, Understand Access Patterns for Time Series Data (p. 712).
     When you are working with items in DynamoDB, you need to consider how to get the best performance,
     how to reduce provisioned throughput costs, and how to avoid throttling by staying within your read and
     write capacity units. If the items that you are handling exceed the maximum item size, as described in
     Limits in DynamoDB (p. 769), you need to consider how you will deal with the situation. This section
     offers best practices for addressing these considerations.
     The Forum, Thread, and Reply tables in the Creating Tables and Loading Sample Data (p. 281) section are
     good examples of these one-to-many relationships. For example, the Thread table has one item for each
     forum thread, and the Reply table stores one or more replies for each thread.
Instead of storing replies as items in a separate table, you could store both threads and replies in the
same table. For each thread, you could store all replies in an attribute of string set type; however,
keeping thread and reply data in separate tables is beneficial in several ways:
• If you store replies as items in a table, you can store any number of replies, because a DynamoDB table
  can store any number of items.
  If you store replies as an attribute value in the Thread table, you would be constrained by the
  maximum item size, which would limit the number of replies that you could store. (See Limits in
  DynamoDB (p. 769))
• When you retrieve a Thread item, you pay less for provisioned throughput, because you are retrieving
  only the thread data and not all the replies for that thread.
• Queries allow you to retrieve only a subset of items from a table. By storing replies in a separate Reply
  table, you can retrieve only a subset of replies, for example, those within a specific date range, by
  querying the Reply table.
  If you store replies as a set type attribute value, you would always have to retrieve all the replies,
  which would consume more provisioned throughput for data that you might not need.
• When you add a new reply to a thread, you add only an item to the Reply table, which incurs the
  provisioned throughput cost of only that single Reply item. If replies are stored in the Thread table,
  you incur the full cost of writing the entire Thread item including all replies whenever you add a single
  user reply to a thread.
For example, the Reply table in the Creating Tables and Loading Sample Data (p. 281) section stores
messages written by forum users. These user replies might consist of very long strings of text, which
makes them excellent candidates for compression.
For sample code that demonstrates how to compress long messages in DynamoDB, see:
• Example: Handling Binary Type Attributes Using the AWS SDK for Java Document API (p. 383)
• Example: Handling Binary Type Attributes Using the AWS SDK for .NET Low-Level API (p. 405)
For example, consider the ProductCatalog table in the Creating Tables and Loading Sample Data (p. 281)
section. Items in the ProductCatalog table store information about item price, description, authors
for books, and dimensions for other products. If you wanted to store an image of each product, these
images could be large. It might make sense to store the images in Amazon S3 instead of DynamoDB.
• Since DynamoDB does not support transactions that cross Amazon S3 and DynamoDB, your
  application will have to deal with failures and with cleaning up orphaned Amazon S3 objects.
• Amazon S3 limits length of object identifiers, so you must organize your data in a way that
  accommodates this and other Amazon S3 constraints. For more information, see the Amazon Simple
  Storage Service Developer Guide.
For example, consider the Forum, Thread and Reply tables described in the Creating Tables and Loading
Sample Data (p. 281) section. Items in the Reply table contain forum messages that were written by
forum users. Due to the 400 KB item size limit in DynamoDB, the length of each reply is also limited. For
large replies, instead of storing one item in the Reply table, break the reply message into chunks, and
then write each chunk into its own separate item in a new ReplyChunks table with a simple primary key
(partition key).
The primary key of each chunk would be a concatenation of the primary key of its "parent" reply item, a
version number, and a sequence number. The sequence number determines the order of the chunks. The
version number ensures that if a large reply is updated later, it will be updated atomically. In addition,
chunks that were created before the update will not be mixed with chunks that were created after the
update.
You would also need to update the "parent" reply item with the number of chunks, so that when you
need to retrieve all the chunks for a reply, you will know how many chunks to look for.
As an illustration, here is how these items might appear in the Reply and ReplyChunks tables:
Reply
 "DynamoDB#Thread1"
                  "2012-03-15T20:42:54.023Z"
                                                                  3                     1
 "DynamoDB#Thread2"
                  "2012-03-21T20:41:23.192Z"
                                    "short message"                                      
ReplyChunks
Id Message
 "DynamoDB#Thread1#2012-03-15T20:42:54.023Z#1#1"
                                            "first part of long message text..."
 "DynamoDB#Thread1#2012-03-15T20:42:54.023Z#1#3"
                                            "...third part of long message text"
 "DynamoDB#Thread1#2012-03-15T20:42:54.023Z#1#2"
                                            "...second part of long message text..."
• Because DynamoDB does not support cross-item transactions, your application will need to deal with
  failure scenarios when writing multiple items and with inconsistencies between items when reading
  multiple items.
    • If your application retrieves a large amount of data all at once, it can generate nonuniform workloads,
      which can cause unexpected throttling. This is especially true when retrieving items that share a
      partition key value.
    Chunking large data items avoids this problem by using a separate table with a simple primary key
    (partition key), so that each large chunk is spread across the table.
    A workable, but less optimal, solution would be to store each chunk in a table with a composite key,
    with the partition key being the primary key of the "parent" item. With this design choice, an application
    that retrieves all of the chunks of the same "parent" item would generate a nonuniform workload, with
    uneven I/O distribution across partitions.
    A Scan operation always scans the entire table or secondary index, then filters out values to provide the
    desired result, essentially adding the extra step of removing data from the result set. Avoid using a Scan
    operation on a large table or index with a filter that removes many results, if possible. Also, as a table
    or index grows, the Scan operation slows. The Scan operation examines every item for the requested
    values, and can use up the provisioned throughput for a large table or index in a single operation. For
    faster response times, design your tables and indexes so that your applications can use Query instead of
    Scan. (For tables, you can also consider using the GetItem and BatchGetItem APIs.).
    Alternatively, design your application to use Scan operations in a way that minimizes the impact on your
    request rate.
    This represents a sudden spike in usage, compared to the configured read capacity for the table. This
    usage of capacity units by a scan prevents other potentially more important requests for the same table
    from using the available capacity units. As a result, you likely get a ProvisionedThroughputExceeded
    exception for those requests.
    Note that it is not just the sudden increase in capacity units the Scan uses that is a problem. It is also
    because the scan is likely to consume all of its capacity units from the same partition because the scan
    requests read items that are next to each other on the partition. This means that the request is hitting
    the same partition, causing all of its capacity units to be consumed, and throttling other requests to
    that partition. If the request to read data had been spread across multiple partitions, then the operation
    would not have throttled a specific partition.
    The following diagram illustrates the impact of a sudden spike of capacity unit usage by Query and
    Scan operations, and its impact on your other requests against the same table.
Instead of using a large Scan operation, you can use the following techniques to minimize the impact of
a scan on a table's provisioned throughput.
    Because a Scan operation reads an entire page (by default, 1 MB), you can reduce the impact of the
    scan operation by setting a smaller page size. The Scan operation provides a Limit parameter that you
    can use to set the page size for your request. Each Query or Scan request that has a smaller page size
    uses fewer read operations and creates a "pause" between each request. For example, if each item is
    4 KB and you set the page size to 40 items, then a Query request would consume only 20 eventually
    consistent read operations or 40 strongly consistent read operations. A larger number of smaller
    Query or Scan operations would allow your other critical requests to succeed without throttling.
• Isolate Scan Operations
    DynamoDB is designed for easy scalability. As a result, an application can create tables for distinct
    purposes, possibly even duplicating content across several tables. You want to perform scans on a
    table that is not taking "mission-critical" traffic. Some applications handle this load by rotating traffic
    hourly between two tables – one for critical traffic, and one for bookkeeping. Other applications can
    do this by performing every write on two tables: a "mission-critical" table, and a "shadow" table.
     You should configure your application to retry any request that receives a response code that indicates
     you have exceeded your provisioned throughput, or increase the provisioned throughput for your table
     using the UpdateTable operation. If you have temporary spikes in your workload that cause your
     throughput to exceed, occasionally, beyond the provisioned level, retry the request with exponential
     backoff. For more information about implementing exponential backoff, see Error Retries and
     Exponential Backoff (p. 193).
     Although parallel scans can be beneficial, they can place a heavy demand on provisioned throughput.
     With a parallel scan, your application will have multiple workers that are all running Scan operations
     concurrently, which can very quickly consume all of your table's provisioned read capacity. In that case,
     other applications that need to access the table might be throttled.
A parallel scan can be the right choice if the following conditions are met:
     Choosing TotalSegments
     The best setting for TotalSegments depends on your specific data, the table's provisioned throughput
     settings, and your performance requirements. You will probably need to experiment to get it right. We
     recommend that you begin with a simple ratio, such as one segment per 2 GB of data. For example, for
     a 30 GB table, you could set TotalSegments to 15 (30 GB / 2 GB). Your application would then use
     fifteen workers, with each worker scanning a different segment.
     You can also choose a value for TotalSegments that is based on client resources. You can set
     TotalSegments to any number from 1 to 1000000, and DynamoDB will allow you to scan that number
     of segments. If, for example, your client limits the number of threads that can run concurrently, you can
     gradually increase TotalSegments until you get the best Scan performance with your application.
     You will need to monitor your parallel scans to optimize your provisioned throughput utilization,
     while also making sure that your other applications aren't starved of resources. Increase the value for
     TotalSegments if you do not consume all of your provisioned throughput but still experience throttling
     in your Scan requests. Reduce the value for TotalSegments if the Scan requests consume more
     provisioned throughput than you want to use.
This section covers some best practices for local secondary indexes.
Avoid indexing tables, such as those used in data capture applications, that experience heavy write
activity. The cost of I/O operations required to maintain the indexes can be significant. If you need to
index the data in such a table, copy the data to another table with any necessary indexes, and query it
there.
• Consider projecting fewer attributes, which will minimize the size of items written to the index.
  However, if these items are smaller than a single write capacity unit (1 KB), then there won't be any
  savings in terms of write capacity units. For example, if the size of an index entry is only 200 bytes,
  DynamoDB rounds this up to 1 KB. In other words, as long as the index items are small, you can project
  more attributes at no extra cost.
• If you know that some attributes of that table will rarely be used in queries, then there is no reason
  to project those attributes. If you subsequently update an attribute that is not in the index, you won't
  incur the extra cost of updating the index. You can still retrieve non-projected attributes in a Query,
  but at a higher provisioned throughput cost.
Specify ALL only if you want your queries to return the entire table item but you want to sort the table
by a different sort key. Indexing all attributes will eliminate the need for table fetches, but in most cases
it will double your costs for storage and write activity.
If you only issue certain queries only occasionally, and you don't see the need to project all the requested
attributes, keep in mind that these "occasional" queries can often turn into "essential" queries! You might
regret not projecting those attributes after all.
For more information about table fetches, see Provisioned Throughput Considerations for Local
Secondary Indexes (p. 488).
    Sparse indexes can be beneficial for queries on attributes that don't appear in most table items. For
    example, suppose that you have a CustomerOrders table that stores all of your orders. The key attributes
    for the table would be as follows:
    If you want to track only orders that are open, you can have an attribute named IsOpen. If you are
    waiting to receive an order, your application can define IsOpen by writing an "X" (or any other value) for
    that particular item in the table. When the order arrives, your application can delete the IsOpen attribute
    to signify that the order has been fulfilled.
    To track open orders, you can create an index on CustomerId (partition key) and IsOpen (sort key). Only
    those orders in the table with IsOpen defined will appear in the index. Your application can then quickly
    and efficiently find the orders that are still open by querying the index. If you had thousands of orders,
    for example, but only a small number that are open, the application can query the index and return the
    OrderId of each open order. Your application will perform significantly fewer reads than it would take to
    scan the entire CustomerOrders table.
    Instead of writing an arbitrary value into the IsOpen attribute, you can use a different attribute that will
    result in a useful sort order in the index. To do this, you can create an OrderOpenDate attribute and set
    it to the date on which the order was placed (and still delete the attribute once the order is fulfilled), and
    create the OpenOrders index with the schema CustomerId (partition key) and OrderOpenDate (sort key).
    This way when you query your index, the items will be returned in a more useful sort order.
    When you add or update a table item, DynamoDB will update any local secondary indexes that are
    affected. If the indexed attributes are defined in the table, the indexes will grow with the table.
    When you create an index, think about how much data will be written to the index, and how much of
    that data will have the same partition key value. If you expect that the sum of table and index items for
    a particular partition key value will exceed 10 GB, you should consider whether you could avoid creating
    the index.
    If you cannot avoid creating the index, then you will need to anticipate the item collection size limit and
    take action before you exceed it. For strategies on working within the limit and taking corrective action,
    see Item Collection Size Limit (p. 492).
This section covers some best practices for global secondary indexes.
This same guidance is true for global secondary indexes. Choose partition keys and sort keys that have
a high number of values relative to the number of items in the index. In addition, remember that global
secondary indexes do not enforce uniqueness, so you need to understand the cardinality of your key
attributes. Cardinality refers to the distinct number of values in a particular attribute, relative to the
number of items that you have.
For example, suppose you have an Employee table with attributes such as Name, Title, Address,
PhoneNumber, Salary, and PayLevel. Now suppose that you had a global secondary index named
PayLevelIndex, with PayLevel as the partition key. Many companies only have a very small number of
pay codes, often fewer than ten, even for companies with hundreds of thousands of employees. Such an
index would not provide much benefit, if any, for an application.
Another problem with PayLevelIndex is the uneven distribution of distinct values. For example, there
may be only a few top executives in the company, but a very large number of hourly workers. Queries
on PayLevelIndex will not be very efficient because the read activity will not be evenly distributed across
partitions.
You can use a sparse global secondary index to efficiently locate table items that have an uncommon
attribute. To do this, you take advantage of the fact that table items that do not contain global
secondary index attribute(s) are not indexed at all. For example, in the GameScores table, certain players
might have earned a particular achievement for a game - such as "Champ" - but most players have not.
Rather than scanning the entire GameScores table for Champs, you could create a global secondary index
with a partition key of Champ and a sort key of UserId. This would make it easy to find all the Champs by
querying the index instead of scanning the table.
Such a query can be very efficient, because the number of items in the index will be significantly fewer
than the number of items in the table. In addition, the fewer table attributes you project into the index,
the fewer read capacity units you will consume from the index.
One use case for a global secondary index with a duplicate key schema is for quick lookups of table data,
with minimal provisioned throughput. If the table has a large number of attributes, and those attributes
themselves are large, then every query on that table might consume a large amount of read capacity. If
most of your queries do not require that much data to be returned, you can create a global secondary
index with a bare minimum of projected attributes - including no projected attributes at all, other than
the table's key. This lets you query a much smaller global secondary index, and if you really require the
additional attributes, you can then query the table using the same key values.
You can create multiple global secondary indexes to support your application's characteristics. For
example, suppose that you have two applications with very different read characteristics — a high-
priority app that requires the highest levels of read performance, and a low-priority app that can tolerate
occasional throttling of read activity. If both of these apps read from the same table, there is a chance
that they could interfere with each other: A heavy read load from the low-priority app could consume
all of the available read capacity for the table, which would in turn cause the high-priority app's read
activity to be throttled. If you create two global secondary indexes — one with a high provisioned
read throughput setting, and the other with a lower setting — you can effectively disentangle these
two different workloads, with read activity from each application being redirected to its own index.
This approach lets you tailor the amount of provisioned read throughput to each application's read
characteristics.
In some situations, you might want to restrict the applications that can read from the table. For example,
you might have an application that captures clickstream activity from a website, with frequent writes to
a DynamoDB table. You might decide to isolate this table by preventing read access by most applications.
(For more information, see Using IAM Policy Conditions for Fine-Grained Access Control (p. 661).)
However, if you have other apps that need to perform ad hoc queries on the data, you can create one or
more global secondary indexes for that purpose. When you create the index(es), be sure to project only
the attributes that your applications actually require. The apps can then read more data while consuming
less provisioned read capacity, instead of having to read large items from the table. This can result in a
significant cost savings over time.
    Topics
     • Amazon VPC Endpoints for DynamoDB (p. 724)
     • Configure AWS Credentials in Your Files Using Amazon Cognito (p. 731)
     • Loading Data From DynamoDB Into Amazon Redshift (p. 732)
     • Processing DynamoDB Data With Apache Hive on Amazon EMR (p. 733)
     • Exporting and Importing DynamoDB Data Using AWS Data Pipeline (p. 760)
    In order to access the public Internet, your VPC must have an Internet gateway—a virtual router that
    connects your VPC to the Internet. This allows applications running on Amazon EC2 in your VPC to access
    Internet resources, such as Amazon DynamoDB.
    By default, communications to and from DynamoDB use the HTTPS protocol, which protects network
    traffic by using SSL/TLS encryption. The following diagram shows how an EC2 instance in a VPC accesses
    DynamoDB:
Many customers have legitimate privacy and security concerns about sending and receiving data across
the public Internet. Customers can address these concerns by using a virtual private network (VPN)
to route all DynamoDB network traffic through the customer's own corporate network infrastructure.
However, this approach can introduce bandwidth and availability challenges.
VPC endpoints for DynamoDB can alleviate these challenges. A VPC endpoint for DynamoDB enables
Amazon EC2 instances in your VPC to use their private IP addresses to access DynamoDB with no
exposure to the public Internet. Your EC2 instances do not require public IP addresses, and you do not
need an Internet gateway, a NAT device, or a virtual private gateway in your VPC. You use endpoint
policies to control access to DynamoDB. Traffic between your VPC and the AWS service does not leave
the Amazon network.
When you create a VPC endpoint for DynamoDB, any requests to a DynamoDB endpoint within the
region (for example, dynamodb.us-west-2.amazonaws.com) are routed to a private DynamoDB endpoint
within the Amazon network. You do not need to modify your applications running on EC2 instances in
your VPC—the endpoint name remains the same, but the route to DynamoDB stays entirely within the
Amazon network, and does not access the public Internet.
The following diagram shows how an EC2 instance in a VPC can use a VPC endpoint to access
DynamoDB.
This section walks you through setting and using a VPC endpoint for DynamoDB.
• At the top of the list of AMIs, go to Amazon Linux AMI and choose Select.
     • Choose Launch.
3.   In the Select an existing key pair or create a new key pair window, do one of the following:
     • If you do not have an Amazon EC2 key pair, choose Create a new key pair and follow the
       instructions. You will be asked to download a private key file (.pem file); you will need this file later
       when you log in to your Amazon EC2 instance.
     • If you already have an existing Amazon EC2 key pair, go to Select a key pair and choose your key
       pair from the list. Note that you must already have the private key file ( .pem file) available in
       order to log in to your Amazon EC2 instance.
4.   When you have configured your key pair, choose Launch Instances.
5.   Return to the Amazon EC2 console home page and choose the instance that you launched. In
     the lower pane, on the Description tab, find the Public DNS for your instance. For example:
     ec2-00-00-00-00.us-east-1.compute.amazonaws.com.
     Make a note of this public DNS name, because you will need it in the next step in this tutorial (Step
     2: Configure Your Amazon EC2 Instance (p. 728)).
     Note
     It will take a few minutes for your Amazon EC2 instance to become available. Before you go on
     to the next step, ensure that the Instance State is running and that all of its Status Checks
     have passed.
1.   You will need to authorize inbound SSH traffic to your Amazon EC2 instance. To do this, you will
     create a new EC2 security group, and then assign the security group to your EC2 instance.
          • Security group name—type a name for your security group. For example: my-ssh-access
          • Description—type a short description for the security group.
          • VPC—choose your default VPC.
          • In the Security group rules section, choose Add Rule and do the following:
            • Type—choose SSH.
            • Source—choose My IP.
     You will need to specify your private key file (.pem file) and the public DNS name of your instance.
     (See Step 1: Launch an Amazon EC2 Instance (p. 726)).
aws configure
1. Before you begin, verify that you can communicate with DynamoDB using its public endpoint:
     The output will show a list of DynamoDB tables that you currently own. (If you don't have any tables,
     the list will be empty.).
2.   Verify that DynamoDB is an available service for creating VPC endpoints in the current AWS region.
     (The command is shown in bold text, followed by example output.)
     {
         "ServiceNames": [
             "com.amazonaws.us-east-1.s3",
             "com.amazonaws.us-east-1.dynamodb"
         ]
     }
     In the example output, DynamoDB is one of the services available, so you can proceed with creating
     a VPC endpoint for it.
3.   Determine your VPC identifier.
     {
         "Vpcs": [
             {
                 "VpcId": "vpc-0bbc736e",
                 "InstanceTenancy": "default",
                 "State": "available",
                 "DhcpOptionsId": "dopt-8454b7e1",
                 "CidrBlock": "172.31.0.0/16",
                 "IsDefault": true
             }
         ]
     }
     {
         "VpcEndpoint": {
             "PolicyDocument": "{\"Version\":\"2008-10-17\",\"Statement\":[{\"Effect\":
     \"Allow\",\"Principal\":\"*\",\"Action\":\"*\",\"Resource\":\"*\"}]}",
             "VpcId": "vpc-0bbc736e",
             "State": "available",
             "ServiceName": "com.amazonaws.us-east-1.dynamodb",
             "RouteTableIds": [],
             "VpcEndpointId": "vpce-9b15e2f2",
             "CreationTimestamp": "2017-07-26T22:00:14Z"
         }
     }
5. Verify that you can access DynamoDB through the VPC endpoint:
     If you want, you can try some other AWS CLI commands for DynamoDB. For more information, see
     the AWS CLI Command Reference.
     {
         "VpcEndpoint": {
             "PolicyDocument": "{\"Version\":\"2008-10-17\",\"Statement\":[{\"Effect\":
     \"Allow\",\"Principal\":\"*\",\"Action\":\"*\",\"Resource\":\"*\"}]}",
             "VpcId": "vpc-0bbc736e",
             "State": "available",
             "ServiceName": "com.amazonaws.us-east-1.dynamodb",
             "RouteTableIds": [],
             "VpcEndpointId": "vpce-9b15e2f2",
             "CreationTimestamp": "2017-07-26T22:00:14Z"
         }
     }
     {
         "Unsuccessful": []
     }
    For example, to configure your JavaScript files to use an Amazon Cognito unauthenticated role to access
    the DynamoDB web service:
    2.   Copy the following policy into a file named myCognitoPolicy.json. Modify the identity pool ID
         (us-west-2:12345678-1ab2-123a-1234-a12345ab12) with your own IdentityPoolId obtained in
         the previous step:
         {
           "Version": "2012-10-17",
           "Statement": [
             {
               "Effect": "Allow",
               "Principal": {
                  "Federated": "cognito-identity.amazonaws.com"
               },
               "Action": "sts:AssumeRoleWithWebIdentity",
               "Condition": {
                  "StringEquals": {
                     "cognito-identity.amazonaws.com:aud": "us-west-2:12345678-1ab2-123a-1234-
         a12345ab12"
                  },
                  "ForAnyValue:StringLike": {
                     "cognito-identity.amazonaws.com:amr": "unauthenticated"
                  }
               }
             }
           ]
         }
    3.   Create an IAM role that assumes the previous policy. In this way, Amazon Cognito becomes a trusted
         entity that can assume the Cognito_DynamoPoolUnauth role.
    4.   Grant the Cognito_DynamoPoolUnauth role full access to the DynamoDB service by attaching a
         managed policy (AmazonDynamoDBFullAccess).
             Note
             Alternatively, you can grant fine-grained access to DynamoDB. For more information, see
             Using IAM Policy Conditions for Fine-Grained Access Control.
    5.   Obtain and copy the IAM role ARN:
    6.   Add the Cognito_DynamoPoolUnauth role to the DynamoPool identity pool. The format to
         specify is KeyName=string, where KeyName is unauthenticated and the string is the role ARN
         obtained in the previous step.
    7.   Specify the Amazon Cognito credentials in your files. Modify the IdentityPoolId and RoleArn
         accordingly.
    You can now run your JavaScript programs against the DynamoDB web service using Amazon Cognito
    credentials. For more information, see Setting Credentials in a Web Browser in the AWS SDK for
    JavaScript Getting Started Guide.
    In terms of provisioned throughput, a copy operation from a DynamoDB table counts against that table's
    read capacity. After the data is copied, your SQL queries in Amazon Redshift do not affect DynamoDB
    in any way. This is because your queries act upon a copy of the data from DynamoDB, rather than upon
    DynamoDB itself.
    Before you can load data from a DynamoDB table, you must first create an Amazon Redshift table to
    serve as the destination for the data. Keep in mind that you are copying data from a NoSQL environment
    into a SQL environment, and that there are certain rules in one environment that do not apply in the
    other. Here are some of the differences to consider:
    • DynamoDB table names can contain up to 255 characters, including '.' (dot) and '-' (dash) characters,
      and are case-sensitive. Amazon Redshift table names are limited to 127 characters, cannot contain
      dots or dashes and are not case-sensitive. In addition, table names cannot conflict with any Amazon
      Redshift reserved words.
    • DynamoDB does not support the SQL concept of NULL. You need to specify how Amazon Redshift
      interprets empty or blank attribute values in DynamoDB, treating them either as NULLs or as empty
      fields.
    • DynamoDB data types do not correspond directly with those of Amazon Redshift. You need to ensure
      that each column in the Amazon Redshift table is of the correct data type and size to accommodate
      the data from DynamoDB.
    In this example, the source table in DynamoDB is my-favorite-movies-table. The target table
    in Amazon Redshift is favoritemovies. The readratio 50 clause regulates the percentage of
    provisioned throughput that is consumed; in this case, the COPY command will use no more than 50
    percent of the read capacity units provisioned for my-favorite-movies-table. We highly recommend
    setting this ratio to a value less than the average unused provisioned throughput.
    For detailed instructions on loading data from DynamoDB into Amazon Redshift, refer to the following
    sections in the Amazon Redshift Database Developer Guide:
    Topics
     • Overview (p. 734)
     • Tutorial: Working with Amazon DynamoDB and Apache Hive (p. 734)
     • Creating an External Table in Hive (p. 741)
     • Processing HiveQL Statements (p. 743)
     • Querying Data in DynamoDB (p. 744)
     • Copying Data to and from Amazon DynamoDB (p. 745)
     • Performance Tuning (p. 756)
Overview
Amazon EMR is a service that makes it easy to quickly and cost-effectively process vast amounts of data.
To use Amazon EMR, you launch a managed cluster of Amazon EC2 instances running the Hadoop open
source framework. Hadoop is a distributed application that implements the MapReduce algorithm, where
a task is mapped to multiple nodes in the cluster. Each node processes its designated work, in parallel
with the other nodes. Finally, the outputs are reduced on a single node, yielding the final result.
You can choose to launch your Amazon EMR cluster so that it is persistent or transient:
• A persistent cluster runs until you shut it down. Persistent clusters are ideal for data analysis, data
  warehousing, or any other interactive use.
• A transient cluster runs long enough to process a job flow, and then shuts down automatically.
  Transient clusters are ideal for periodic processing tasks, such as running scripts.
For information about Amazon EMR architecture and administration, see the Amazon EMR Management
Guide.
When you launch an Amazon EMR cluster, you specify the initial number and type of Amazon EC2
instances. You also specify other distributed applications (in addition to Hadoop itself) that you want to
run on the cluster. These applications include Hue, Mahout, Pig, Spark, and more.
For information about applications for Amazon EMR, see the Amazon EMR Release Guide.
Depending on the cluster configuration, you might have one or more of the following node types:
• Master node — Manages the cluster, coordinating the distribution of the MapReduce executable and
  subsets of the raw data, to the core and task instance groups. It also tracks the status of each task
  performed and monitors the health of the instance groups. There is only one master node in a cluster.
• Core nodes — Runs MapReduce tasks and stores data using the Hadoop Distributed File System
  (HDFS).
• Task nodes (optional) — Runs MapReduce tasks.
Hive is a data warehouse application for Hadoop that allows you to process and analyze data from
multiple sources. Hive provides a SQL-like language, HiveQL, that lets you work with data stored locally
in the Amazon EMR cluster or in an external data source (such as Amazon DynamoDB).
Topics
 • Before You Begin (p. 735)
 • Step 1: Create an Amazon EC2 Key Pair (p. 735)
 • Step 2: Launch an Amazon EMR Cluster (p. 735)
 • Step 3: Connect to the Master Node (p. 736)
 • Step 4: Load Data into HDFS (p. 737)
 • Step 5: Copy Data to DynamoDB (p. 738)
 • Step 6: Query the Data in the DynamoDB Table (p. 739)
• An AWS account. If you do not have one, see Signing Up for AWS (p. 46).
• An SSH client (Secure Shell). You use the SSH client to connect to the master node of the Amazon
  EMR cluster and run interactive commands. SSH clients are available by default on most Linux, Unix,
  and Mac OS X installations. Windows users can download and install the PuTTY client, which has SSH
  support.
Next Step
1.   Sign in to the AWS Management Console and open the Amazon EC2 console at https://
     console.aws.amazon.com/ec2/.
2.   Choose a region (for example, US West (Oregon)). This should be the same region in which your
     DynamoDB table is located.
3.   In the navigation pane, choose Key Pairs.
4.   Choose Create Key Pair.
5.   In Key pair name, type a name for your key pair (for example, mykeypair), and then choose Create.
6.   Download the private key file. The file name will end with .pem (such as mykeypair.pem). Keep
     this private key file in a safe place. You will need it to access any Amazon EMR cluster that you
     launch with this key pair.
          Important
          If you lose the key pair, you cannot connect to the master node of your Amazon EMR
          cluster.
     For more information about key pairs, see Amazon EC2 Key Pairs in the Amazon EC2 User Guide for
     Linux Instances.
Next Step
     a.   In Cluster name, type a name for your cluster (for example: My EMR cluster).
     b.   In EC2 key pair, choose the key pair you created earlier.
It will take several minutes to launch your cluster. You can use the Cluster Details page in the Amazon
EMR console to monitor its progress.
When the status changes to Waiting, the cluster is ready for use.
If one does not already exist, the AWS Management Console creates an Amazon S3 bucket. The bucket
name is aws-logs-account-id-region, where account-id is your AWS account number and
region is the region in which you launched the cluster (for example, aws-logs-123456789012-us-
west-2).
     Note
     You can use the Amazon S3 console to view the log files. For more information, see View Log
     Files in the Amazon EMR Management Guide.
You can use this bucket for purposes in addition to logging. For example, you can use the bucket as a
location for storing a Hive script or as a destination when exporting data from Amazon DynamoDB to
Amazon S3.
Next Step
1.   In the Amazon EMR console, choose your cluster's name to view its status.
2.   On the Cluster Details page, find the Master public DNS field. This is the public DNS name for the
     master node of your Amazon EMR cluster.
3.   To the right of the DNS name, choose the SSH link.
4.   Follow the instructions in Connect to the Master Node Using SSH .
     Depending on your operating system, choose the Windows tab or the Mac/Linux tab, and follow
     the instructions for connecting to the master node.
After you connect to the master node using either SSH or PuTTY, you should see a command prompt
similar to the following:
[hadoop@ip-192-0-2-0 ~]$
Next Step
     wget http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/samples/
     features.zip
unzip features.zip
head features.txt
     1535908|Big Run|Stream|WV|38.6370428|-80.8595469|794
     875609|Constable Hook|Cape|NJ|40.657881|-74.0990309|7
     1217998|Gooseberry Island|Island|RI|41.4534361|-71.3253284|10
     26603|Boone Moore Spring|Spring|AZ|34.0895692|-111.410065|3681
     1506738|Missouri Flat|Flat|WA|46.7634987|-117.0346113|2605
     1181348|Minnow Run|Stream|PA|40.0820178|-79.3800349|1558
     1288759|Hunting Creek|Stream|TN|36.343969|-83.8029682|1024
     533060|Big Charles Bayou|Bay|LA|29.6046517|-91.9828654|0
     829689|Greenwood Creek|Stream|NE|41.596086|-103.0499296|3671
     541692|Button Willow Island|Island|LA|31.9579389|-93.0648847|98
     The data file contains a subset of data provided by the United States Board on Geographic Names
     (http://geonames.usgs.gov/domestic/download_data.htm).
     The features.txt file contains a subset of data from the United States Board on Geographic
     Names (http://geonames.usgs.gov/domestic/download_data.htm). The fields in each line represent
     the following:
hive
6. Enter the following HiveQL statement to load the table with data:
     LOAD DATA
     LOCAL
     INPATH './features.txt'
     OVERWRITE
     INTO TABLE hive_features;
7.   You now have a native Hive table populated with data from the features.txt file. To verify, enter
     the following HiveQL statement:
The output should show a list of states and the number of geographic features in each.
Next Step
Clear Use Default Settings. For Provisioned Capacity, type the following:
     Choose Create.
4.   At the Hive prompt, enter the following HiveQL statement:
      "dynamodb.column.mapping"="feature_id:Id,feature_name:Name,feature_class:Class,state_alpha:State,p
     );
     You have now established a mapping between Hive and the Features table in DynamoDB.
5.   Enter the following HiveQL statement to import data to DynamoDB:
     Hive will submit a MapReduce job, which will be processed by your Amazon EMR cluster. It will take
     several minutes to complete the job.
6.   Verify that the data has been loaded into DynamoDB:
Next Step
3. States with at least three features higher than a mile (5,280 feet):
Next Step
If you don't need the cluster anymore, you should terminate it and remove any associated resources. This
will help you avoid being charged for resources you don't need.
You can think of an external table as a pointer to a data source that is managed and stored elsewhere.
In this case, the underlying data source is a DynamoDB table. (The table must already exist. You cannot
create, update, or delete a DynamoDB table from within Hive.) You use the CREATE EXTERNAL TABLE
statement to create the external table. After that, you can use HiveQL to work with data in DynamoDB,
as if that data were stored locally within Hive.
    Note
    You can use INSERT statements to insert data into an external table and SELECT statements to
    select data from it. However, you cannot use UPDATE or DELETE statements to manipulate data
    in the table.
If you no longer need the external table, you can remove it using the DROP TABLE statement. In this
case, DROP TABLE only removes the external table in Hive. It does not affect the underlying DynamoDB
table or any of its data.
Topics
 • CREATE EXTERNAL TABLE Syntax (p. 741)
 • Data Type Mappings (p. 742)
Line 1 is the start of the CREATE EXTERNAL TABLE statement, where you provide the name of the Hive
table (hive_table) you want to create.
Line 2 specifies the columns and data types for hive_table. You need to define columns and data types
that correspond to the attributes in the DynamoDB table.
Line 3 is the STORED BY clause, where you specify a class that handles data management
between the Hive and the DynamoDB table. For DynamoDB, STORED BY should be set to
'org.apache.hadoop.hive.dynamodb.DynamoDBStorageHandler'.
Line 4 is the start of the TBLPROPERTIES clause, where you define the following parameters for
DynamoDBStorageHandler:
• The name of the Hive table name does not have to be the same as the DynamoDB table name.
• The Hive table column names do not have to be the same as those in the DynamoDB table.
• The table specified by dynamodb.table.name must exist in DynamoDB.
• For dynamodb.column.mapping:
  • You must map the key schema attributes for the DynamoDB table. This includes the partition key
    and the sort key (if present).
  • You do not have to map the non-key attributes of the DynamoDB table. However, you will not see
    any data from those attributes when you query the Hive table.
  • If the data types of a Hive table column and a DynamoDB attribute are incompatible, you will see
    NULL in these columns when you query the Hive table.
    Note
    The CREATE EXTERNAL TABLE statement does not perform any validation on the
    TBLPROPERTIES clause. The values you provide for dynamodb.table.name and
    dynamodb.column.mapping are only evaluated by the DynamoDBStorageHandler class
    when you attempt to access the table.
String STRING
Binary BINARY
    Note
    The following DynamoDB data types are not supported by the DynamoDBStorageHandler
    class, so they cannot be used with dynamodb.column.mapping:
    • Map
    • List
    • Boolean
    • Null
If you want to map a DynamoDB attribute of type Number, you must choose an appropriate Hive type:
• The Hive BIGINT type is for 8-byte signed integers. It is the same as the long data type in Java.
• The Hive DOUBLE type is for 8-bit double precision floating point numbers. It is the same as the
  double type in Java.
If you have numeric data stored in DynamoDB that has a higher precision than the Hive data type you
choose, then accessing the DynamoDB data could cause a loss of precision.
If you export data of type Binary from DynamoDB to (Amazon S3) or HDFS, the data is stored as a
Base64-encoded string. If you import data from Amazon S3 or HDFS into the DynamoDB Binary type,
you must ensure the data is encoded as a Base64 string.
For example, consider the ddb_features table (from Tutorial: Working with Amazon DynamoDB and
Apache Hive (p. 734)). The following Hive query prints state abbreviations and the number of summits
in each:
Hive does not return the results immediately. Instead, it submits a MapReduce job, which is processed
by the Hadoop framework. Hive will wait until the job is complete before it shows the results from the
query:
AK 2
AL 2
AR 2
AZ 3
CA 7
CO 2
CT 2
ID 1
KS 1
ME 2
MI 1
MT 3
NC 1
NE 1
NM 1
NY 2
OR 5
PA 1
TN 1
TX 1
UT 4
VA 1
VT 2
WA 2
WY 3
Time taken: 8.753 seconds, Fetched: 25 row(s)
If you need to cancel the job before it is complete, you can type Ctrl+C at any time.
These examples refer to the ddb_features table in the tutorial (Step 5: Copy Data to DynamoDB (p. 738)
).
Topics
 • Using Aggregate Functions (p. 744)
 • Using the GROUP BY and HAVING Clauses (p. 744)
 • Joining Two DynamoDB tables (p. 744)
 • Joining Tables from Different Sources (p. 745)
SELECT MAX(elev_in_ft)
FROM ddb_features
WHERE state_alpha = 'CO';
The following example returns a list of the highest elevations from states that have more than five
features in the ddb_features table.
Consider a DynamoDB table named EastCoastStates that contains the following data:
StateName StateAbbrev
Maine ME
New Hampshire     NH
Massachusetts     MA
Rhode Island      RI
Connecticut       CT
New York          NY
New Jersey        NJ
Delaware          DE
Maryland          MD
Virginia          VA
North Carolina    NC
South Carolina    SC
Georgia           GA
Florida           FL
Let's assume the table is available as a Hive external table named east_coast_states:
The following join returns the states on the East Coast of the United States that have at least three
features:
table is external because it exists outside of Hive. Even if you drop the Hive table that maps to it, the
table in DynamoDB is not affected.
Hive is an excellent solution for copying data among DynamoDB tables, Amazon S3 buckets, native Hive
tables, and Hadoop Distributed File System (HDFS). This section provides examples of these operations.
Topics
 • Copying Data Between DynamoDB and a Native Hive Table (p. 746)
 • Copying Data Between DynamoDB and Amazon S3 (p. 747)
 • Copying Data Between DynamoDB and HDFS (p. 751)
 • Using Data Compression (p. 755)
 • Reading Non-Printable UTF-8 Character Data (p. 756)
You might decide to do this if you need to perform many HiveQL queries, but do not want to consume
provisioned throughput capacity from DynamoDB. Because the data in the native Hive table is a copy of
the data from DynamoDB, and not "live" data, your queries should not expect that the data is up-to-date.
The examples in this section are written with the assumption you followed the steps in Tutorial: Working
with Amazon DynamoDB and Apache Hive (p. 734) and have an external table that is mastered in
DynamoDB (ddb_features).
You can create a native Hive table and populate it with data from ddb_features, like this:
In these examples, the subquery SELECT * FROM ddb_features will retrieve all of the data from
ddb_features. If you only want to copy a subset of the data, you can use a WHERE clause in the subquery.
The following example creates a native Hive table, containing only some of the attributes for lakes and
summits:
Use the following HiveQL statement to copy the data from the native Hive table to ddb_features:
You might do this if you want to create an archive of data in your DynamoDB table. For example,
suppose you have a test environment where you need to work with a baseline set of test data in
DynamoDB. You can copy the baseline data to an Amazon S3 bucket, and then run your tests. Afterward,
you can reset the test environment by restoring the baseline data from the Amazon S3 bucket to
DynamoDB.
If you worked through Tutorial: Working with Amazon DynamoDB and Apache Hive (p. 734), then you
already have an Amazon S3 bucket that contains your Amazon EMR logs. You can use this bucket for the
examples in this section, if you know the root path for the bucket:
s3://aws-logs-accountID-region
where accountID is your AWS account ID and region is the AWS region for the bucket.
     Note
     For these examples, we will use a subpath within the bucket, as in this example:
     s3://aws-logs-123456789012-us-west-2/hive-test
The following procedures are written with the assumption you followed the steps in the tutorial and
have an external table that is mastered in DynamoDB (ddb_features).
Topics
 • Copying Data Using the Hive Default Format (p. 747)
 • Copying Data with a User-Specified Format (p. 748)
 • Copying Data Without a Column Mapping (p. 749)
 • Viewing the Data in Amazon S3 (p. 750)
Each field is separated by an SOH character (start of heading, 0x01). In the file, SOH appears as ^A.
1.   Create a Hive external table that maps to Amazon S3. When you do this, ensure that the data types
     are consistent with those of the DynamoDB external table.
With a single HiveQL statement, you can populate the DynamoDB table using the data from Amazon S3:
Name^C{"s":"Soldiers Farewell
 Hill"}^BState^C{"s":"NM"}^BClass^C{"s":"Summit"}^BElevation^C{"n":"6135"}^BLatitude^C{"n":"32.3564729"
Name^C{"s":"Jones
 Run"}^BState^C{"s":"PA"}^BClass^C{"s":"Stream"}^BElevation^C{"n":"1260"}^BLatitude^C{"n":"41.2120086"}
Name^C{"s":"Sentinel
 Dome"}^BState^C{"s":"CA"}^BClass^C{"s":"Summit"}^BElevation^C{"n":"8133"}^BLatitude^C{"n":"37.7229821"
Name^C{"s":"Neversweet
 Gulch"}^BState^C{"s":"CA"}^BClass^C{"s":"Valley"}^BElevation^C{"n":"2900"}^BLatitude^C{"n":"41.6565269
Name^C{"s":"Chacaloochee
 Bay"}^BState^C{"s":"AL"}^BClass^C{"s":"Bay"}^BElevation^C{"n":"0"}^BLatitude^C{"n":"30.6979676"}^BId^C
Each field begins with an STX character (start of text, 0x02) and ends with an ETX character (end of text,
0x03). In the file, STX appears as ^B and ETX appears as ^C.
With a single HiveQL statement, you can populate the DynamoDB table using the data from Amazon S3:
The following steps are written with the assumption you have copied data from DynamoDB to Amazon
S3 using one of the procedures in this section.
1. If you are currently at the Hive command prompt, exit to the Linux command prompt.
hive> exit;
2.   List the contents of the hive-test directory in your Amazon S3 bucket. (This is where Hive copied the
     data from DynamoDB.)
aws s3 ls s3://aws-logs-123456789012-us-west-2/hive-test/
aws s3 cp s3://aws-logs-123456789012-us-west-2/hive-test/000000_0 .
     download: s3://aws-logs-123456789012-us-west-2/hive-test/000000_0
     to ./000000_0
         Note
         The local file system on the master node has limited capacity. Do not use this command
         with files that are larger than the available space in the local file system.
You might do this if you are running a MapReduce job that requires data from DynamoDB. If you copy
the data from DynamoDB into HDFS, Hadoop can process it, using all of the available nodes in the
Amazon EMR cluster in parallel. When the MapReduce job is complete, you can then write the results
from HDFS to DDB.
In the following examples, Hive will read from and write to the following HDFS directory: /user/
hadoop/hive-test
The examples in this section are written with the assumption you followed the steps in Tutorial: Working
with Amazon DynamoDB and Apache Hive (p. 734) and you have an external table that is mastered in
DynamoDB (ddb_features).
Topics
 • Copying Data Using the Hive Default Format (p. 751)
 • Copying Data with a User-Specified Format (p. 752)
 • Copying Data Without a Column Mapping (p. 753)
 • Accessing the Data in HDFS (p. 754)
Each field is separated by an SOH character (start of heading, 0x01). In the file, SOH appears as ^A.
1.   Create a Hive external table that maps to HDFS. When you do this, ensure that the data types are
     consistent with those of the DynamoDB external table.
With a single HiveQL statement, you can populate the DynamoDB table using the data from HDFS:
Name^C{"s":"Soldiers Farewell
 Hill"}^BState^C{"s":"NM"}^BClass^C{"s":"Summit"}^BElevation^C{"n":"6135"}^BLatitude^C{"n":"32.3564729"
Name^C{"s":"Jones
 Run"}^BState^C{"s":"PA"}^BClass^C{"s":"Stream"}^BElevation^C{"n":"1260"}^BLatitude^C{"n":"41.2120086"}
Name^C{"s":"Sentinel
 Dome"}^BState^C{"s":"CA"}^BClass^C{"s":"Summit"}^BElevation^C{"n":"8133"}^BLatitude^C{"n":"37.7229821"
Name^C{"s":"Neversweet
 Gulch"}^BState^C{"s":"CA"}^BClass^C{"s":"Valley"}^BElevation^C{"n":"2900"}^BLatitude^C{"n":"41.6565269
Name^C{"s":"Chacaloochee
 Bay"}^BState^C{"s":"AL"}^BClass^C{"s":"Bay"}^BElevation^C{"n":"0"}^BLatitude^C{"n":"30.6979676"}^BId^C
Each field begins with an STX character (start of text, 0x02) and ends with an ETX character (end of text,
0x03). In the file, STX appears as ^B and ETX appears as ^C.
With a single HiveQL statement, you can populate the DynamoDB table using the data from HDFS:
HDFS is not the same thing as the local file system on the master node. You cannot work with files and
directories in HDFS using standard Linux commands (such as cat, cp, mv, or rm). Instead, you perform
these tasks using the hadoop fs command.
The following steps are written with the assumption you have copied data from DynamoDB to HDFS
using one of the procedures in this section.
1. If you are currently at the Hive command prompt, exit to the Linux command prompt.
hive> exit;
2.   List the contents of the /user/hadoop/hive-test directory in HDFS. (This is where Hive copied the
     data from DynamoDB.)
     Found 1 items
     -rw-r--r-- 1 hadoop hadoop 29504 2016-06-08 23:40 /user/hadoop/hive-test/000000_0
         Note
         In this example, the file is relatively small (approximately 29 KB). Be careful when you use
         this command with files that are very large or contain non-printable characters.
4.   (Optional) You can copy the data file from HDFS to the local file system on the master node. After
     you do this, you can use standard Linux command line utilities to work with the data in the file.
The following example compresses data using the Lempel-Ziv-Oberhumer (LZO) algorithm.
SET hive.exec.compress.output=true;
SET io.seqfile.compression.type=BLOCK;
SET mapred.output.compression.codec = com.hadoop.compression.lzo.LzopCodec;
The resulting file in Amazon S3 will have a system-generated name with .lzo at the end (for example,
8d436957-57ba-4af7-840c-96c2fc7bb6f5-000000.lzo).
• org.apache.hadoop.io.compress.GzipCodec
• org.apache.hadoop.io.compress.DefaultCodec
• com.hadoop.compression.lzo.LzoCodec
• com.hadoop.compression.lzo.LzopCodec
• org.apache.hadoop.io.compress.BZip2Codec
• org.apache.hadoop.io.compress.SnappyCodec
Performance Tuning
When you create a Hive external table that maps to a DynamoDB table, you do not consume any read or
write capacity from DynamoDB. However, read and write activity on the Hive table (such as INSERT or
SELECT) translates directly into read and write operations on the underlying DynamoDB table.
Apache Hive on Amazon EMR implements its own logic for balancing the I/O load on the DynamoDB
table and seeks to minimize the possibility of exceeding the table's provisioned throughput. At the
end of each Hive query, Amazon EMR returns runtime metrics, including the number of times your
provisioned throughput was exceeded. You can use this information, together with CloudWatch metrics
on your DynamoDB table, to improve performance in subsequent requests.
The Amazon EMR console provides basic monitoring tools for your cluster. For more information, see
View and Monitor a Cluster in the Amazon EMR Management Guide.
You can also monitor your cluster and Hadoop jobs using web-based tools, such as Hue, Ganglia, and the
Hadoop web interface. For more information, see View Web Interfaces Hosted on Amazon EMR Clusters
in the Amazon EMR Management Guide.
This section describes steps you can take to performance-tune Hive operations on external DynamoDB
tables.
Topics
 • DynamoDB Provisioned Throughput (p. 757)
 • Adjusting the Mappers (p. 758)
For example, suppose that you have provisioned 100 read capacity units for your DynamoDB table. This
will let you read 409,600 bytes per second (100 × 4 KB read capacity unit size). Now suppose that the
table contains 20 GB of data (21,474,836,480 bytes) and you want to use the SELECT statement to
select all of the data using HiveQL. You can estimate how long the query will take to run like this:
In this scenario, the DynamoDB table is a bottleneck. It won't help to add more Amazon EMR nodes,
because the Hive throughput is constrained to only 409,600 bytes per second. The only way to
decrease the time required for the SELECT statement is to increase the provisioned read capacity of the
DynamoDB table.
You can perform a similar calculation to estimate how long it would take to bulk-load data into a Hive
external table mapped to a DynamoDB table. Determine the total number of bytes in the data you want
to load, and then divide it by the size of one DynamoDB write capacity unit (1 KB). This will yield the
number of seconds it will take to load the table.
You should regularly monitor the CloudWatch metrics for your table. For a quick overview in the
DynamoDB console, choose your table and then choose the Metrics tab. From here, you can view read
and write capacity units consumed and read and write requests that have been throttled.
Read Capacity
Amazon EMR manages the request load against your DynamoDB table, according to
the table's provisioned throughput settings. However, if you notice a large number of
ProvisionedThroughputExceeded messages in the job output, you can adjust the default read rate.
To do this, you can modify the dynamodb.throughput.read.percent configuration variable. You can
use the SET command to set this variable at the Hive command prompt:
SET dynamodb.throughput.read.percent=1.0;
This variable persists for the current Hive session only. If you exit Hive and return to it later,
dynamodb.throughput.read.percent will return to its default value.
The value of dynamodb.throughput.read.percent can be between 0.1 and 1.5, inclusively. 0.5
represents the default read rate, meaning that Hive will attempt to consume half of the read capacity of
the table. If you increase the value above 0.5, Hive will increase the request rate; decreasing the value
below 0.5 decreases the read request rate. (The actual read rate will vary, depending on factors such as
whether there is a uniform key distribution in the DynamoDB table.)
If you notice that Hive is frequently depleting the provisioned read capacity of the table, or if your read
requests are being throttled too much, try reducing dynamodb.throughput.read.percent below
0.5. If you have sufficient read capacity in the table and want more responsive HiveQL operations, you
can set the value above 0.5.
Write Capacity
Amazon EMR manages the request load against your DynamoDB table, according to
the table's provisioned throughput settings. However, if you notice a large number of
ProvisionedThroughputExceeded messages in the job output, you can adjust the default write rate.
To do this, you can modify the dynamodb.throughput.write.percent configuration variable. You
can use the SET command to set this variable at the Hive command prompt:
SET dynamodb.throughput.write.percent=1.0;
This variable persists for the current Hive session only. If you exit Hive and return to it later,
dynamodb.throughput.write.percent will return to its default value.
The value of dynamodb.throughput.write.percent can be between 0.1 and 1.5, inclusively. 0.5
represents the default write rate, meaning that Hive will attempt to consume half of the write capacity
of the table. If you increase the value above 0.5, Hive will increase the request rate; decreasing the value
below 0.5 decreases the write request rate. (The actual write rate will vary, depending on factors such as
whether there is a uniform key distribution in the DynamoDB table.)
If you notice that Hive is frequently depleting the provisioned write capacity of the table, or if your write
requests are being throttled too much, try reducing dynamodb.throughput.write.percent below
0.5. If you have sufficient capacity in the table and want more responsive HiveQL operations, you can set
the value above 0.5.
When you write data to DynamoDB using Hive, ensure that the number of write capacity units is greater
than the number of mappers in the cluster. For example, consider an Amazon EMR cluster consisting of
10 m1.xlarge nodes. The m1.xlarge node type provides 8 mapper tasks, so the cluster would have a total
of 80 mappers (10 × 8). If your DynamoDB table has fewer than 80 write capacity units, then a Hive write
operation could consume all of the write throughput for that table.
To determine the number of mappers for Amazon EMR node types, see Task Configuration in the
Amazon EMR Developer Guide.
For more information on mappers, see Adjusting the Mappers (p. 758).
If your DynamoDB table has ample throughput capacity for reads, you can try increasing the number of
mappers by doing one of the following:
• Increase the size of the nodes in your cluster. For example, if your cluster is using m1.large nodes (three
  mappers per node), you can try upgrading to m1.xlarge nodes (eight mappers per node).
• Increase the number of nodes in your cluster. For example, if you have three-node cluster of m1.xlarge
  nodes, you have a total of 24 mappers available. If you were to double the size of the cluster, with the
  same type of node, you would have 48 mappers.
You can use the AWS Management Console to manage the size or the number of nodes in your cluster.
(You might need to restart the cluster for these changes to take effect.)
You set the value for mapred.tasktracker.map.tasks.maximum as a bootstrap action when you
first launch your Amazon EMR cluster. For more information, see (Optional) Create Bootstrap Actions to
Install Additional Software in the Amazon EMR Management Guide.
You can use the dynamodb.max.map.tasks parameter to set an upper limit for map tasks:
SET dynamodb.max.map.tasks=1
This value must be equal to or greater than 1. When Hive processes your query, the resulting Hadoop job
will use no more than dynamodb.max.map.tasks when reading from the DynamoDB table.
Additional Topics
The following are some more ways to tune applications that use Hive to access DynamoDB.
Retry Duration
By default, Hive will rerun a Hadoop job if it has not returned any results from DynamoDB within two
minutes. You can adjust this interval by modifying the dynamodb.retry.duration parameter:
SET dynamodb.retry.duration=2;
The value must be a nonzero integer, representing the number of minutes in the retry interval. The
default for dynamodb.retry.duration is 2 (minutes).
    Process Duration
    Data consistency in DynamoDB depends on the order of read and write operations on each node. While
    a Hive query is in progress, another application might load new data into the DynamoDB table or modify
    or delete existing data. In this case, the results of the Hive query might not reflect changes made to the
    data while the query was running.
    Request Time
    Scheduling Hive queries that access a DynamoDB table when there is lower demand on the DynamoDB
    table improves performance. For example, if most of your application's users live in San Francisco,
    you might choose to export daily data at 4:00 A.M. PST when the majority of users are asleep and not
    updating records in your DynamoDB database.
    The ability to export and import data is useful in many scenarios. For example, suppose you want to
    maintain a baseline set of data, for testing purposes. You could put the baseline data into a DynamoDB
    table and export it to Amazon S3. Then, after you run an application that modifies the test data, you
    could "reset" the data set by importing the baseline from Amazon S3 back into the DynamoDB table.
    Another example involves accidental deletion of data, or even an accidental DeleteTable operation. In
    these cases, you could restore the data from a previous export file in Amazon S3. You can even copy data
    from a DynamoDB table in one AWS region, store the data in Amazon S3, and then import the data from
    Amazon S3 to an identical DynamoDB table in a second region. Applications in the second region could
    then access their nearest DynamoDB endpoint and work with their own copy of the data, with reduced
    network latency.
    The following diagram shows an overview of exporting and importing DynamoDB data using AWS Data
    Pipeline.
To export a DynamoDB table, you use the AWS Data Pipeline console to create a new pipeline. The
pipeline launches an Amazon EMR cluster to perform the actual export. Amazon EMR reads the data
from DynamoDB, and writes the data to an export file in an Amazon S3 bucket.
The process is similar for an import, except that the data is read from the Amazon S3 bucket and written
to the DynamoDB table.
    Important
    When you export or import DynamoDB data, you will incur additional costs for the underlying
    AWS services that are used:
     • Amazon EMR— runs a managed Hadoop cluster to performs reads and writes between
       DynamoDB to Amazon S3. The cluster configuration is one m3.xlarge instance master node
       and one m3.xlarge instance core node.
     For more information see AWS Data Pipeline Pricing, Amazon EMR Pricing, and Amazon S3
     Pricing.
You can also control access by creating IAM policies and attaching them to IAM users or groups . These
policies let you specify which users are allowed to import and export your DynamoDB data.
     Important
     The IAM user that performs the exports and imports must have an active AWS Access Key Id and
     Secret Key. For more information, see Administering Access Keys for IAM Users in the IAM User
     Guide.
• DataPipelineDefaultRole — the actions that your pipeline can take on your behalf.
• DataPipelineDefaultResourceRole — the AWS resources that the pipeline will provision on your behalf.
  For exporting and importing DynamoDB data, these resources include an Amazon EMR cluster and the
  Amazon EC2 instances associated with that cluster.
If you have never used AWS Data Pipeline before, you will need to create DataPipelineDefaultRole and
DataPipelineDefaultResourceRole yourself. Once you have created these roles, you can use them any time
you want to export or import DynamoDB data.
     Note
     If you have previously used the AWS Data Pipeline console to create a pipeline, then
     DataPipelineDefaultRole and DataPipelineDefaultResourceRole were created for you at that time.
     No further action is required; you can skip this section and begin creating pipelines using the
     DynamoDB console. For more information, see Exporting Data From DynamoDB to Amazon
     S3 (p. 765) and Importing Data From Amazon S3 to DynamoDB (p. 766).
1.   Sign in to the AWS Management Console and open the IAM console at https://
     console.aws.amazon.com/iam/.
2.   From the IAM Console Dashboard, click Roles.
3.   Click Create Role and do the following:
Now that you have created these roles, you can begin creating pipelines using the DynamoDB console.
For more information, see Exporting Data From DynamoDB to Amazon S3 (p. 765) and Importing Data
From Amazon S3 to DynamoDB (p. 766).
1.   Sign in to the AWS Management Console and open the IAM console at https://
     console.aws.amazon.com/iam/.
2.   From the IAM Console Dashboard, click Users and select the user you want to modify.
3.   In the Permissions tab, click Attach Policy.
4.   In the Attach Policy panel, select AmazonDynamoDBFullAccesswithDataPipeline and click
     Attach Policy.
     Note
     You can use a similar procedure to attach this managed policy to a group, rather than to a user.
For example, suppose that you want to allow an IAM user to export and import only the Forum, Thread,
and Reply tables. This procedure describes how to create a custom policy so that a user can work with
those tables, but no others.
1.   Sign in to the AWS Management Console and open the IAM console at https://
     console.aws.amazon.com/iam/.
2.   From the IAM Console Dashboard, click Policies and then click Create Policy.
3.   In the Create Policy panel, go to Copy an AWS Managed Policy and click Select.
4.   In the Copy an AWS Managed Policy panel, go to
     AmazonDynamoDBFullAccesswithDataPipeline and click Select.
5.   In the Review Policy panel, do the following:
     a.   Review the autogenerated Policy Name and Description. If you want, you can modify these
          values.
b.   In the Policy Document text box, edit the policy to restrict access to specific tables. By default,
     the policy permits all DynamoDB actions on all of your tables:
     {
         "Version": "2012-10-17",
         "Statement": [
           {
             "Action": [
               "cloudwatch:DeleteAlarms",
               "cloudwatch:DescribeAlarmHistory",
               "cloudwatch:DescribeAlarms",
               "cloudwatch:DescribeAlarmsForMetric",
               "cloudwatch:GetMetricStatistics",
               "cloudwatch:ListMetrics",
               "cloudwatch:PutMetricAlarm",
               "dynamodb:*",
               "sns:CreateTopic",
               "sns:DeleteTopic",
               "sns:ListSubscriptions",
               "sns:ListSubscriptionsByTopic",
               "sns:ListTopics",
               "sns:Subscribe",
               "sns:Unsubscribe"
             ],
             "Effect": "Allow",
             "Resource": "*",
             "Sid": "DDBConsole"
           },
"dynamodb:*",
Next, construct a new Action that allows access to only the Forum, Thread and Reply tables:
           {
             "Action": [
               "dynamodb:*"
             ],
             "Effect": "Allow",
             "Resource": [
               "arn:aws:dynamodb:us-west-2:123456789012:table/Forum",
               "arn:aws:dynamodb:us-west-2:123456789012:table/Thread",
               "arn:aws:dynamodb:us-west-2:123456789012:table/Reply"
             ]
           },
          Note
          Replace us-west-2 with the region in which your DynamoDB tables reside. Replace
          123456789012 with your AWS account number.
            "Version": "2012-10-17",
            "Statement": [
              {
                "Action": [
                  "dynamodb:*"
                ],
                "Effect": "Allow",
                "Resource": [
                  "arn:aws:dynamodb:us-west-2:123456789012:table/Forum",
                  "arn:aws:dynamodb:us-west-2:123456789012:table/Thread",
                  "arn:aws:dynamodb:us-west-2:123456789012:table/Reply"
                ]
              },
              {
                "Action": [
                  "cloudwatch:DeleteAlarms",
                  "cloudwatch:DescribeAlarmHistory",
                  "cloudwatch:DescribeAlarms",
                  "cloudwatch:DescribeAlarmsForMetric",
                  "cloudwatch:GetMetricStatistics",
                  "cloudwatch:ListMetrics",
                  "cloudwatch:PutMetricAlarm",
                  "sns:CreateTopic",
                  "sns:DeleteTopic",
                  "sns:ListSubscriptions",
                  "sns:ListSubscriptionsByTopic",
                  "sns:ListTopics",
                  "sns:Subscribe",
                  "sns:Unsubscribe"
                ],
                "Effect": "Allow",
                "Resource": "*",
                "Sid": "DDBConsole"
              },
6. When the policy settings are as you want them, click Create Policy.
After you have created the policy, you can attach it to an IAM user.
1.   From the IAM Console Dashboard, click Users and select the user you want to modify.
2.   In the Permissions tab, click Attach Policy.
3.   In the Attach Policy panel, select the name of your policy and click Attach Policy.
     Note
     You can use a similar procedure to attach your policy to a group, rather than to a user.
1.   Sign in to the AWS Management Console and open the AWS Data Pipeline console at https://
     console.aws.amazon.com/datapipeline/.
2. If you do not already have any pipelines in the current AWS region, choose Get started now.
     Otherwise, if you already have at least one pipeline, choose Create new pipeline.
3.   On the Create Pipeline page, do the following:
     a.   In the Name field, type a name for your pipeline. For example: MyDynamoDBExportPipeline.
     b.   For the Source parameter, select Build using a template. From the drop-down template list,
          choose Export DynamoDB table to S3.
     c.   In the Source DynamoDB table name field, type the name of the DynamoDB table that you
          want to export.
     d.   In the Output S3 Folder text box, enter an Amazon S3 URI where the export file will be written.
          For example: s3://mybucket/exports
          The URI format for S3 Log Folder is the same as for Output S3 Folder. The URI must resolve to
          a folder; log files cannot be written to the top level of the S3 bucket.
4.   When the settings are as you want them, click Activate.
Your pipeline will now be created; this process can take several minutes to complete. You can monitor
the progress in the AWS Data Pipeline console.
When the export has finished, you can go to the Amazon S3 console to view your export file. The file will
be in a folder with the same name as your table, and the file will be named using the following format:
YYYY-MM-DD_HH.MM. The internal format of this file is described at Verify Data Export File in the AWS
Data Pipeline Developer Guide.
We will use the term source table for the original table from which the data was exported, and
destination table for the table that will receive the imported data. You can import data from an export
file in Amazon S3, provided that all of the following are true:
• The destination table already exists. (The import process will not create the table for you.)
• The destination table has the same key schema as the source table.
The destination table does not have to be empty. However, the import process will replace any data
items in the table that have the same keys as the items in the export file. For example, suppose you have
a Customer table with a key of CustomerId, and that there are only three items in the table (CustomerId
1, 2, and 3). If your export file also contains data items for CustomerID 1, 2, and 3, the items in the
destination table will be replaced with those from the export file. If the export file also contains a data
item for CustomerId 4, then that item will be added to the table.
                                    API Version 2012-08-10
                                              766
                             Amazon DynamoDB Developer Guide
                                     Troubleshooting
The destination table can be in a different AWS region. For example, suppose you have a Customer table
in the US West (Oregon) region and export its data to Amazon S3. You could then import that data into
an identical Customer table in the EU (Ireland) region. This is referred to as a cross-region export and
import. For a list of AWS regions, go to Regions and Endpoints in the AWS General Reference.
Note that the AWS Management Console lets you export multiple source tables at once. However, you
can only import one table at a time.
1.   Sign in to the AWS Management Console and open the AWS Data Pipeline console at https://
     console.aws.amazon.com/datapipeline/.
2.   (Optional) If you want to perform a cross region import, go to the upper right corner of the window
     and choose the destination region.
3.   Choose Create new pipeline.
4.   On the Create Pipeline page, do the following:
     a.   In the Name field, type a name for your pipeline. For example: MyDynamoDBImportPipeline.
     b.   For the Source parameter, select Build using a template. From the drop-down template list,
          choose Import DynamoDB backup data from S3.
     c.   In the Input S3 Folder text box, enter an Amazon S3 URI where the export file can be found. For
          example: s3://mybucket/exports
          The import job will expect to find a file at the specified Amazon S3 location. The internal format
          of the file is described at Verify Data Export File in the AWS Data Pipeline Developer Guide.
     d.   In the Target DynamoDB table name field, type the name of the DynamoDB table into which
          you want to import the data.
     e.   In the S3 location for logs text box, enter an Amazon S3 URI where the log file for the import
          will be written. For example: s3://mybucket/logs/
          The URI format for S3 Log Folder is the same as for Output S3 Folder. The URI must resolve to
          a folder; log files cannot be written to the top level of the S3 bucket.
5.   When the settings are as you want them, click Activate.
Your pipeline will now be created; this process can take several minutes to complete. The import job will
begin immediately after the pipeline has been created.
Troubleshooting
This section covers some basic failure modes and troubleshooting for DynamoDB exports.
If an error occurs during an export or import, the pipeline status in the AWS Data Pipeline console will
display as ERROR. If this happens, click the name of the failed pipeline to go to its detail page. This will
show details about all of the steps in the pipeline, and the status of each one. In particular, examine any
execution stack traces that you see.
Finally, go to your Amazon S3 bucket and look for any export or import log files that were written there.
The following are some common issues that may cause a pipeline to fail, along with corrective actions. To
diagnose your pipeline, compare the errors you have seen with the issues noted below.
                                  API Version 2012-08-10
                                            767
                             Amazon DynamoDB Developer Guide
                               Predefined Templates for AWS
                               Data Pipeline and DynamoDB
• For an import, ensure that the destination table already exists, and the destination table has the same
  key schema as the source table. These conditions must be met, or the import will fail.
• Ensure that the Amazon S3 bucket you specified has been created, and that you have read and write
  permissions on it.
• The pipeline might have exceeded its execution timeout. (You set this parameter when you created the
  pipeline.) For example, you might have set the execution timeout for 1 hour, but the export job might
  have required more time than this. Try deleting and then re-creating the pipeline, but with a longer
  execution timeout interval this time.
• Update the manifest file if you restore from a Amazon S3 bucket that is not the original bucket that
  the export was performed with (contains a copy of the export).
• You might not have the correct permissions for performing an export or import. For more information,
  see Prerequisites to Export and Import Data (p. 762).
• You might have reached a resource limit in your AWS account, such as the maximum number of
  Amazon EC2 instances or the maximum number of AWS Data Pipeline pipelines. For more information,
  including how to request increases in these limits, see AWS Service Limits in the AWS General
  Reference.
    Note
    For more details on troubleshooting a pipeline, go to Troubleshooting in the AWS Data Pipeline
    Developer Guide.
AWS Data Pipeline offers several templates for creating pipelines; the following templates are relevant
to DynamoDB.
Limits in DynamoDB
    This section describes current limits within Amazon DynamoDB (or no limit, in some cases). Each limit
    listed below applies on a per-region basis unless otherwise specified.
    Topics
     • Capacity Units and Provisioned Throughput (p. 769)
     • Tables (p. 770)
     • Secondary Indexes (p. 771)
     • Partition Keys and Sort Keys (p. 771)
     • Naming Rules (p. 771)
     • Data Types (p. 772)
     • Items (p. 773)
     • Attributes (p. 773)
     • Expression Parameters (p. 773)
     • DynamoDB Streams (p. 774)
     • DynamoDB Accelerator (DAX) (p. 775)
     • API-Specific Limits (p. 775)
One write capacity unit = one write per second, for items up to 1 KB in size.
    AWS places some default limits on the throughput you can provision. These are the limits unless you
    request a higher amount. To request a service limit increase see https://aws.amazon.com/support.
    The provisioned throughput limit includes the sum of the capacity of the table together with the
    capacity of all of its global secondary indexes.
    In the AWS Management Console, you can see what your current provisioned capacity is in a given region
    and make sure you are not too close to the limits. If you increased your default limits, you can use the
    DescribeLimits operation to see the current limit values.
    You cannot exceed your per-account limits when you add provisioned capacity, and DynamoDB will not
    permit you to increase provisioned capacity extremely rapidly. Aside from these restrictions, you can
    increase the provisioned capacity for your tables as high as you need. For more information about per-
    account limits, see the preceding section Provisioned Throughput Default Limits (p. 769).
    Example
    A table with a GSI, in the first 4 hours of a day, can be modified as follows:
    At the end of that same day the table and the GSI's throughput can potentially be decreased a total of 9
    times each.
Tables
    Table Size
    There is no practical limit on a table's size. Tables are unconstrained in terms of the number of items or
    the number of bytes.
Secondary Indexes
     Secondary Indexes Per Table
     You can define a maximum of 5 local secondary indexes and 5 global secondary indexes per table.
This limit does not apply for secondary indexes with a ProjectionType of KEYS_ONLY or ALL.
     The exception is for tables with local secondary indexes. With a local secondary index, there is a limit on
     item collection sizes: For every distinct partition key value, the total sizes of all table and index items
     cannot exceed 10 GB. This might constrain the number of sort keys per partition key value. For more
     information, see Item Collection Size Limit (p. 492).
Naming Rules
     Table Names and Secondary Index Names
     Names for tables and secondary indexes must be at least 3 characters long, but no greater than 255
     characters long. Allowed characters are:
    • A-Z
    • a-z
    • 0-9
    • _ (underscore)
    • - (hyphen)
    • . (dot)
    Attribute Names
    In general, an attribute name must be at least 1 character long, but no greater than 64 KB long.
The exceptions are listed below. These attribute names must be no greater than 255 characters long:
    These attribute names must be encoded using UTF-8, and the total size of each name (after encoding)
    cannot exceed 255 bytes.
Data Types
    String
    The length of a String is constrained by the maximum item size of 400 KB.
    Strings are Unicode with UTF-8 binary encoding. Because UTF-8 is a variable width encoding, DynamoDB
    determines the length of a String using its UTF-8 bytes.
    Number
    A Number can have up to 38 digits of precision, and can be positive, negative, or zero.
    DynamoDB uses JSON strings to represent Number data in requests and replies. For more information,
    see DynamoDB Low-Level API (p. 185).
    If number precision is important, you should pass numbers to DynamoDB using strings that you convert
    from a number type.
    Binary
    The length of a Binary is constrained by the maximum item size of 400 KB.
     Applications that work with Binary attributes must encode the data in Base64 format before sending it
     to DynamoDB. Upon receipt of the data, DynamoDB decodes it into an unsigned byte array and uses that
     as the length of the attribute.
Items
     Item Size
     The maximum item size in DynamoDB is 400 KB, which includes both attribute name binary length
     (UTF-8 length) and attribute value lengths (again binary length). The attribute name counts towards the
     size limit.
     For example, consider an item with two attributes: one attribute named "shirt-color" with value "R" and
     another attribute named "shirt-size" with value "M". The total size of that item is 23 bytes.
Attributes
     Attribute Name-Value Pairs Per Item
     The cumulative size of attributes per item must fit within the maximum DynamoDB item size (400 KB).
     Attribute Values
     An attribute value cannot be an empty String or empty Set (String Set, Number Set, or Binary Set).
     However, empty Lists and Maps are allowed.
Expression Parameters
     Expression parameters include ProjectionExpression, ConditionExpression,
     UpdateExpression, and FilterExpression.
    Lengths
    The maximum length of any expression string is 4 KB. For example, the size of the
    ConditionExpression a=b is three bytes.
    The maximum length of any single expression attribute name or expression attribute value is 255 bytes.
    For example, #name is five bytes; :val is four bytes.
    The maximum length of all substitution variables in an expression is 2 MB. This is the sum of the lengths
    of all ExpressionAttributeNames and ExpressionAttributeValues.
    Reserved Words
    DynamoDB does not prevent you from using names that conflict with reserved words. (For a complete
    list, see Reserved Words in DynamoDB (p. 820).)
    However, if you use a reserved word in an expression parameter, you must also specify
    ExpressionAttributeNames. For more information, see Expression Attribute Names (p. 341).
DynamoDB Streams
    Simultaneous Readers of a Shard in DynamoDB
    Streams
    Do not allow more than two processes to read from the same DynamoDB Streams shard at the same
    time. Exceeding this limit can result in request throttling.
        Note
        The provisioned throughput limits also apply for DynamoDB tables with Streams enabled. For
        more information, see Provisioned Throughput Default Limits (p. 769).
    Nodes
    A DAX cluster consists of exactly 1 primary node, and between 0 and 9 read replica nodes.
The total number of nodes (per AWS account) cannot exceed 50 in a single AWS region.
    Parameter Groups
    You can create up to 20 DAX parameter groups per region.
    Subnet Groups
    You can create up to 50 DAX subnet groups per region.
API-Specific Limits
    CreateTable/UpdateTable/DeleteTable
        The only exception is when you are creating a table with one or more secondary indexes. You
        can have up to 5 such requests running at a time; however, if the table or index specifications are
        complex, DynamoDB might temporarily reduce the number of concurrent requests below 5.
BatchGetItem
        A single BatchGetItem operation can retrieve a maximum of 100 items. The total size of all the
        items retrieved cannot exceed 16 MB.
BatchWriteItem
DescribeLimits
   DescribeLimits should only be called periodically. You can expect throttling errors if you call it
   more than once in a minute.
Query
   The result set from a Query is limited to 1 MB per call. You can use the LastEvaluatedKey from
   the query response to retrieve more results.
Scan
   The result set from a Scan is limited to 1 MB per call. You can use the LastEvaluatedKey from the
   scan response to retrieve more results.
DynamoDB Appendix
    Topics
     • Troubleshooting SSL/TLS connection establishment issues (p. 777)
     • Example Tables and Data (p. 779)
     • Creating Example Tables and Uploading Data (p. 788)
     • DynamoDB Example Application Using AWS SDK for Python (Boto): Tic-Tac-Toe (p. 803)
     • Amazon DynamoDB Storage Backend for Titan (p. 820)
     • Reserved Words in DynamoDB (p. 820)
     • Legacy Conditional Parameters (p. 829)
     • Current Low-Level API Version (2012-08-10) (p. 846)
     • Previous Low-Level API Version (2011-12-05) (p. 846)
    To validate access to DynamoDB endpoints, you will need to develop a test that accesses DynamoDB API
    or DynamoDB Streams API in the EU-WEST-3 region and validate that the TLS handshake succeeds. The
    specific endpoints you will need to access in such test are:
    • DynamoDB: https://dynamodb.eu-west-3.amazonaws.com
    • DynamoDB Streams: https://streams.dynamodb.eu-west-3.amazonaws.com
    If your application does not support Amazon Trust Services Certificate Authority you will see one of the
    following failures:
healthy: dynamodb.eu-west-3.amazonaws.com
• Amazon Root CA 1
• Starfield Services Root Certificate Authority - G2
• Starfield Class 2 Certification Authority
If the clients already trust ANY of the above three CAs then these will trust certificates used by
DynamoDB and no action is required. However, if your clients do not already trust any of the above CAs,
the HTTPS connections to the DynamoDB or DynamoDB Streams APIs will fail. For more information,
please visit this blog post: https://aws.amazon.com/blogs/security/how-to-prepare-for-aws-move-to-
its-own-certificate-authority/.
• Chrome: https://support.google.com/chrome/answer/95414?hl=en
• Firefox: https://support.mozilla.org/en-US/kb/update-firefox-latest-version
• Safari: https://support.apple.com/en-us/HT204416
• Internet Explorer: https://support.microsoft.com/en-us/help/17295/windows-internet-explorer-
  which-version#ie=other
The following operating systems and programming languages support Amazon Trust Services
certificates:
• Microsoft Windows versions that have January 2005 or later updates installed, Windows Vista,
  Windows 7, Windows Server 2008, and newer versions.
• Mac OS X 10.4 with Java for Mac OS X 10.4 Release 5, Mac OS X 10.5 and newer versions.
• Red Hat Enterprise Linux 5 (March 2007), Linux 6, and Linux 7 and CentOS 5, CentOS 6, and CentOS 7
• Ubuntu 8.10
• Debian 5.0
    If you are still unable to connect please consult your software documentation, OS Vendor or contact AWS
    Support https://aws.amazon.com/support for further assistance.
• Id (Number)
• Name (String)
                                                             • ForumName (String)
                                                             • Subject (String)
                                                             • Id (String)
                                                             • ReplyDateTime (String)
    The Reply table has a global secondary index named PostedBy-Message-Index. This index will facilitate
    queries on two non-key attributes of the Reply table.
                                                             • PostedBy (String)
                                                             • Message (String)
    For more information about these tables, see Use Case 1: Product Catalog (p. 281) and Use Case 2:
    Forum Application (p. 281).
The following sections show the sample data files that are used for loading the ProductCatalog, Forum,
Thread and Reply tables.
Each data file contains multiple PutRequest elements, each of which contain a single item. These
PutRequest elements are used as input to the BatchWriteItem operation, using the AWS Command
Line Interface (AWS CLI).
For more information, see Step 2: Load Data into Tables (p. 283) in Creating Tables and Loading Sample
Data (p. 281).
             "Authors": {
                 "L": [
                     {
                            "S": "Author1"
                     },
                     {
                            "S": "Author2"
                     }
                 ]
             },
             "Price": {
                 "N": "20"
             },
             "Dimensions": {
                 "S": "8.5 x 11.0 x 0.8"
             },
             "PageCount": {
                 "N": "600"
             },
             "InPublication": {
                 "BOOL": true
             },
             "ProductCategory": {
                 "S": "Book"
             }
         }
     }
},
{
     "PutRequest": {
         "Item": {
             "Id": {
                 "N": "103"
             },
             "Title": {
                 "S": "Book 103 Title"
             },
             "ISBN": {
                 "S": "333-3333333333"
             },
             "Authors": {
                 "L": [
                     {
                          "S": "Author1"
                     },
                     {
                          "S": "Author2"
                     }
                 ]
             },
             "Price": {
                 "N": "2000"
             },
             "Dimensions": {
                 "S": "8.5 x 11.0 x 1.5"
             },
             "PageCount": {
                 "N": "600"
             },
             "InPublication": {
                 "BOOL": false
             },
             "ProductCategory": {
                 "S": "Book"
             }
         }
     }
},
{
     "PutRequest": {
         "Item": {
             "Id": {
                 "N": "201"
             },
             "Title": {
                 "S": "18-Bike-201"
             },
             "Description": {
                 "S": "201 Description"
             },
             "BicycleType": {
                 "S": "Road"
             },
             "Brand": {
                 "S": "Mountain A"
             },
             "Price": {
                 "N": "100"
             },
             "Color": {
                 "L": [
                     {
                         "S": "Red"
                     },
                     {
                         "S": "Black"
                     }
                 ]
             },
             "ProductCategory": {
                 "S": "Bicycle"
             }
         }
     }
},
{
     "PutRequest": {
         "Item": {
             "Id": {
                 "N": "202"
             },
             "Title": {
                 "S": "21-Bike-202"
             },
             "Description": {
                 "S": "202 Description"
             },
             "BicycleType": {
                 "S": "Road"
             },
             "Brand": {
                 "S": "Brand-Company A"
             },
             "Price": {
                 "N": "200"
             },
             "Color": {
                 "L": [
                     {
                         "S": "Green"
                     },
                     {
                          "S": "Black"
                     }
                 ]
             },
             "ProductCategory": {
                 "S": "Bicycle"
             }
         }
     }
},
{
     "PutRequest": {
         "Item": {
             "Id": {
                 "N": "203"
             },
             "Title": {
                 "S": "19-Bike-203"
             },
             "Description": {
                 "S": "203 Description"
             },
             "BicycleType": {
                 "S": "Road"
             },
             "Brand": {
                 "S": "Brand-Company B"
             },
             "Price": {
                 "N": "300"
             },
             "Color": {
                 "L": [
                     {
                         "S": "Red"
                     },
                     {
                         "S": "Green"
                     },
                     {
                         "S": "Black"
                     }
                 ]
             },
             "ProductCategory": {
                 "S": "Bicycle"
             }
         }
     }
},
{
     "PutRequest": {
         "Item": {
             "Id": {
                 "N": "204"
             },
             "Title": {
                 "S": "18-Bike-204"
             },
             "Description": {
                 "S": "204 Description"
             },
             "BicycleType": {
                 "S": "Mountain"
             },
             "Brand": {
                 "Item": {
                     "Name": {"S":"Amazon DynamoDB"},
                     "Category": {"S":"Amazon Web Services"},
                     "Threads": {"N":"2"},
                     "Messages": {"N":"4"},
                     "Views": {"N":"1000"}
                 }
             }
        },
        {
             "PutRequest": {
                 "Item": {
                     "Name": {"S":"Amazon S3"},
                     "Category": {"S":"Amazon Web Services"}
                 }
             }
        }
    ]
}
         }
     }
},
{
     "PutRequest": {
         "Item": {
             "ForumName": {
                 "S": "Amazon DynamoDB"
             },
             "Subject": {
                 "S": "DynamoDB Thread 2"
             },
             "Message": {
                 "S": "DynamoDB thread 2 message"
             },
             "LastPostedBy": {
                 "S": "User A"
             },
             "LastPostedDateTime": {
                 "S": "2015-09-15T19:58:22.514Z"
             },
             "Views": {
                 "N": "3"
             },
             "Replies": {
                 "N": "0"
             },
             "Answered": {
                 "N": "0"
             },
             "Tags": {
                 "L": [
                     {
                          "S": "items"
                     },
                     {
                          "S": "attributes"
                     },
                     {
                          "S": "throughput"
                     }
                 ]
             }
         }
     }
},
{
     "PutRequest": {
         "Item": {
             "ForumName": {
                 "S": "Amazon S3"
             },
             "Subject": {
                 "S": "S3 Thread 1"
             },
             "Message": {
                 "S": "S3 thread 1 message"
             },
             "LastPostedBy": {
                 "S": "User A"
             },
             "LastPostedDateTime": {
                 "S": "2015-09-29T19:58:22.514Z"
             },
             "Views": {
                 "N": "0"
                    },
                    "Replies": {
                        "N": "0"
                    },
                    "Answered": {
                        "N": "0"
                    },
                    "Tags": {
                        "L": [
                            {
                                 "S": "largeobjects"
                            },
                            {
                                 "S": "multipart upload"
                            }
                        ]
                    }
                }
            }
        }
    ]
}
                    "PutRequest": {
                        "Item": {
                            "Id": {
                                "S": "Amazon DynamoDB#DynamoDB Thread 2"
                            },
                            "ReplyDateTime": {
                                "S": "2015-09-29T19:58:22.947Z"
                            },
                            "Message": {
                                "S": "DynamoDB Thread 2 Reply 1 text"
                            },
                            "PostedBy": {
                                "S": "User A"
                            }
                        }
                    }
               },
               {
                    "PutRequest": {
                        "Item": {
                            "Id": {
                                "S": "Amazon DynamoDB#DynamoDB Thread 2"
                            },
                            "ReplyDateTime": {
                                "S": "2015-10-05T19:58:22.947Z"
                            },
                            "Message": {
                                "S": "DynamoDB Thread 2 Reply 2 text"
                            },
                            "PostedBy": {
                                "S": "User A"
                            }
                        }
                    }
               }
          ]
    }
    In Creating Tables and Loading Sample Data (p. 281), you first create tables using the DynamoDB console
    and then use the AWS CLI to add data to the tables. This appendix provides code to both create the
    tables and add data programmatically.
import   java.text.SimpleDateFormat;
import   java.util.ArrayList;
import   java.util.Arrays;
import   java.util.Date;
import   java.util.HashSet;
import   java.util.TimeZone;
import   com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
import   com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder;
import   com.amazonaws.services.dynamodbv2.document.DynamoDB;
import   com.amazonaws.services.dynamodbv2.document.Item;
import   com.amazonaws.services.dynamodbv2.document.Table;
import   com.amazonaws.services.dynamodbv2.model.AttributeDefinition;
import   com.amazonaws.services.dynamodbv2.model.CreateTableRequest;
import   com.amazonaws.services.dynamodbv2.model.KeySchemaElement;
import   com.amazonaws.services.dynamodbv2.model.KeyType;
import   com.amazonaws.services.dynamodbv2.model.LocalSecondaryIndex;
import   com.amazonaws.services.dynamodbv2.model.Projection;
import   com.amazonaws.services.dynamodbv2.model.ProjectionType;
import   com.amazonaws.services.dynamodbv2.model.ProvisionedThroughput;
try {
              deleteTable(productCatalogTableName);
              deleteTable(forumTableName);
              deleteTable(threadTableName);
              deleteTable(replyTableName);
              loadSampleProducts(productCatalogTableName);
              loadSampleForums(forumTableName);
              loadSampleThreads(threadTableName);
              loadSampleReplies(replyTableName);
          }
          catch (Exception e) {
              System.err.println("Program failed:");
              System.err.println(e.getMessage());
       }
       System.out.println("Success.");
   }
       }
       catch (Exception e) {
           System.err.println("DeleteTable request failed for " + tableName);
           System.err.println(e.getMessage());
       }
   }
try {
// key
           if (sortKeyName != null) {
               keySchema.add(new
KeySchemaElement().withAttributeName(sortKeyName).withKeyType(KeyType.RANGE)); // Sort
                     // key
               attributeDefinitions
                   .add(new
AttributeDefinition().withAttributeName(sortKeyName).withAttributeType(sortKeyType));
           }
               attributeDefinitions
                   .add(new
AttributeDefinition().withAttributeName("PostedBy").withAttributeType("S"));
                             // key
                       new
KeySchemaElement().withAttributeName("PostedBy").withKeyType(KeyType.RANGE)) // Sort
               // key
                   .withProjection(new
Projection().withProjectionType(ProjectionType.KEYS_ONLY)));
                 request.setLocalSecondaryIndexes(localSecondaryIndexes);
           }
request.setAttributeDefinitions(attributeDefinitions);
       }
       catch (Exception e) {
           System.err.println("CreateTable request failed for " + tableName);
           System.err.println(e.getMessage());
       }
   }
try {
// Add bikes.
       }
       catch (Exception e) {
           System.err.println("Failed to create item in " + tableName);
           System.err.println(e.getMessage());
       }
try {
       }
       catch (Exception e) {
           System.err.println("Failed to create item in " + tableName);
           System.err.println(e.getMessage());
       }
   }
dateFormatter.setTimeZone(TimeZone.getTimeZone("UTC"));
       }
       catch (Exception e) {
           System.err.println("Failed to create item in " + tableName);
           System.err.println(e.getMessage());
       }
dateFormatter.setTimeZone(TimeZone.getTimeZone("UTC"));
// Add threads.
          }
          catch (Exception e) {
              System.err.println("Failed to create item in " + tableName);
              System.err.println(e.getMessage());
          }
    }
using   System;
using   System.Collections.Generic;
using   Amazon.DynamoDBv2;
using   Amazon.DynamoDBv2.DocumentModel;
using   Amazon.DynamoDBv2.Model;
using   Amazon.Runtime;
using   Amazon.SecurityToken;
namespace com.amazonaws.codesamples
{
    class CreateTablesLoadData
    {
        private static AmazonDynamoDBClient client = new AmazonDynamoDBClient();
                  // Create tables (using the AWS SDK for .NET low-level API).
                  CreateTableProductCatalog();
                  CreateTableForum();
{
    string tableName = "Forum";
                 {
                      ReadCapacityUnits = 10,
                      WriteCapacityUnits = 5
                 }
           });
                               },
                 ProvisionedThroughput = new ProvisionedThroughput
                 {
                     ReadCapacityUnits = 10,
                     WriteCapacityUnits = 5
                 }
           });
    }
    catch (ResourceNotFoundException)
    {
        // Table deleted.
    }
}
    book2["Id"] = 102;
    book2["Title"] = "Book 102 Title";
    book2["ISBN"] = "222-2222222222";
    book2["Authors"] = new List<string> { "Author 1", "Author 2" }; ;
    book2["Price"] = 20;
    book2["Dimensions"] = "8.5 x 11.0 x 0.8";
    book2["PageCount"] = 600;
    book2["InPublication"] = true;
    book2["ProductCategory"] = "Book";
    productCatalogTable.PutItem(book2);
    bicycle2["Price"] = 200;
    bicycle2["Color"] = new List<string> { "Green", "Black" };
    bicycle2["ProductCategory"] = "Bicycle";
    productCatalogTable.PutItem(bicycle2);
forumTable.PutItem(forum1);
    forumTable.PutItem(forum2);
}
    // Thread 1.
    var thread1 = new Document();
threadTable.PutItem(thread1);
           // Thread 2.
           var thread2 = new Document();
           thread2["ForumName"] = "Amazon DynamoDB"; // Hash attribute.
           thread2["Subject"] = "DynamoDB Thread 2"; // Range attribute.
           thread2["Message"] = "DynamoDB thread 2 message text";
           thread2["LastPostedBy"] = "User A";
           thread2["LastPostedDateTime"] = DateTime.UtcNow.Subtract(new TimeSpan(21, 0, 0,
0));
           thread2["Views"] = 0;
           thread2["Replies"] = 0;
           thread2["Answered"] = false;
           thread2["Tags"] = new List<string> { "index", "primarykey", "rangekey" };
threadTable.PutItem(thread2);
           // Thread 3.
           var thread3 = new Document();
           thread3["ForumName"] = "Amazon S3"; // Hash attribute.
           thread3["Subject"] = "S3 Thread 1"; // Range attribute.
           thread3["Message"] = "S3 thread 3 message text";
           thread3["LastPostedBy"] = "User A";
           thread3["LastPostedDateTime"] = DateTime.UtcNow.Subtract(new TimeSpan(7, 0, 0,
0));
           thread3["Views"] = 0;
           thread3["Replies"] = 0;
           thread3["Answered"] = false;
           thread3["Tags"] = new List<string> { "largeobjects", "multipart upload" };
           threadTable.PutItem(thread3);
       }
           // Reply 1 - thread 1.
           var thread1Reply1 = new Document();
           thread1Reply1["Id"] = "Amazon DynamoDB#DynamoDB Thread 1"; // Hash attribute.
           thread1Reply1["ReplyDateTime"] = DateTime.UtcNow.Subtract(new TimeSpan(21, 0,
0, 0)); // Range attribute.
           thread1Reply1["Message"] = "DynamoDB Thread 1 Reply 1 text";
           thread1Reply1["PostedBy"] = "User A";
replyTable.PutItem(thread1Reply1);
           // Reply 2 - thread 1.
           var thread1reply2 = new Document();
           thread1reply2["Id"] = "Amazon DynamoDB#DynamoDB Thread 1"; // Hash attribute.
           thread1reply2["ReplyDateTime"] = DateTime.UtcNow.Subtract(new TimeSpan(14, 0,
0, 0)); // Range attribute.
           thread1reply2["Message"] = "DynamoDB Thread 1 Reply 2 text";
           thread1reply2["PostedBy"] = "User B";
replyTable.PutItem(thread1reply2);
                // Reply 3 - thread 1.
                var thread1Reply3 = new Document();
                thread1Reply3["Id"] = "Amazon DynamoDB#DynamoDB Thread 1"; // Hash attribute.
                thread1Reply3["ReplyDateTime"] = DateTime.UtcNow.Subtract(new TimeSpan(7, 0, 0,
     0)); // Range attribute.
                thread1Reply3["Message"] = "DynamoDB Thread 1 Reply 3 text";
                thread1Reply3["PostedBy"] = "User B";
replyTable.PutItem(thread1Reply3);
                // Reply 1 - thread 2.
                var thread2Reply1 = new Document();
                thread2Reply1["Id"] = "Amazon DynamoDB#DynamoDB Thread 2"; // Hash attribute.
                thread2Reply1["ReplyDateTime"] = DateTime.UtcNow.Subtract(new TimeSpan(7, 0, 0,
     0)); // Range attribute.
                thread2Reply1["Message"] = "DynamoDB Thread 2 Reply 1 text";
                thread2Reply1["PostedBy"] = "User A";
replyTable.PutItem(thread2Reply1);
                // Reply 2 - thread 2.
                var thread2Reply2 = new Document();
                thread2Reply2["Id"] = "Amazon DynamoDB#DynamoDB Thread 2"; // Hash attribute.
                thread2Reply2["ReplyDateTime"] = DateTime.UtcNow.Subtract(new TimeSpan(1, 0, 0,
     0)); // Range attribute.
                thread2Reply2["Message"] = "DynamoDB Thread 2 Reply 2 text";
                thread2Reply2["PostedBy"] = "User A";
                    replyTable.PutItem(thread2Reply2);
               }
          }
    }
    The Tic-Tac-Toe game is an example web application built on Amazon DynamoDB. The application
    uses the AWS SDK for Python (Boto) to make the necessary DynamoDB calls to store game data in a
    DynamoDB table, and the Python web framework Flask to illustrate end-to-end application development
    in DynamoDB, including how to model data. It also demonstrates best practices when it comes to
    modeling data in DynamoDB, including the table you create for the game application, the primary key
    you define, additional indexes you need based on your query requirements, and the use of concatenated
    value attributes.
   Until another user accepts your invitation, the game status remains as PENDING. After an opponent
   accepts the invite, the game status changes to IN_PROGRESS.
3. The game begins after the opponent logs in and accepts the invite.
4. The application stores all game moves and status information in a DynamoDB table.
5. The game ends with a win or a draw, which sets the game status to FINISHED.
• Step 1: Deploy and Test Locally (p. 804) – In this section, you download, deploy, and test the
  application on your local computer. You will create the required tables in the downloadable version of
  DynamoDB.
• Step 2: Examine the Data Model and Implementation Details (p. 807) – This section first describes
  in detail the data model, including the indexes and the use of the concatenated value attribute. Then
  the section explains how the application works.
• Step 3: Deploy in Production Using the DynamoDB Service (p. 814) – This section focuses on
  deployment considerations in production. In this step, you create a table using the Amazon DynamoDB
  service and deploy the application using AWS Elastic Beanstalk. When you have the application in
  production, you also grant appropriate permissions so the application can access the DynamoDB table.
  The instructions in this section walk you through the end-to-end production deployment.
• Step 4: Clean Up Resources (p. 820) – This section highlights areas that are not covered by this
  example. The section also provides steps for you to remove the AWS resources you created in the
  preceding steps so that you avoid incurring any charges.
In this step you download, deploy, and test the Tic-Tac-Toe game application on your local computer.
Instead of using the Amazon DynamoDB web service, you will download DynamoDB to your computer,
and create the required table there.
• Python
• Flask (a microframework for Python)
• AWS SDK for Python (Boto)
• DynamoDB running on your computer
• Git
  The Tic-Tac-Toe application has been tested using Python version 2.7.
2. Install Flask and AWS SDK for Python (Boto) using the Python Package Installer (PIP):
   • Install PIP.
       For instructions, go to Install PIP. On the installation page, choose the get-pip.py link, and then
       save the file. Then open a command terminal as an administrator, and type the following at the
       command prompt:
python.exe get-pip.py
       On Linux, you don't specify the .exe extension. You only specify python get-pip.py.
     • Using PIP, install the Flask and Boto packages using the following code:
3. Download DynamoDB to your computer. For instructions on how to run it, see Setting Up DynamoDB
   Local (Downloadable Version) (p. 41).
4. Download the Tic-Tac-Toe application:
     a. Install Git. For instructions, go to git Downloads.
     b. Execute the following code to download the application:
1.    Start DynamoDB.
2.    Start the web server for the Tic-Tac-Toe application.
      To do so, open a command terminal, navigate to the folder where you downloaded the Tic-Tac-Toe
      application, and run the application locally using the following code:
http://localhost:5000/
         Note
         This example application does not perform any user authentication. The user ID is only used
         to identify players. If two players log in with the same alias, the application works as if you
         are playing in two different browsers.
5.   If this is your first time playing the game, a page appears requesting you to create the required table
     (Games) in DynamoDB. Choose CREATE TABLE.
     Doing this creates the game by adding an item in the Games table. It sets the game status to
     PENDING.
8.   Open another browser window, and type the following.
http://localhost:5000/
     The browser passes information through cookies, so you should use incognito mode or private
     browsing so that your cookies don't carry over.
9.   Log in as user2.
    The game page appears with an empty tic-tac-toe grid. The page also shows relevant game
    information such as the game ID, whose turn it is, and game status.
11. Play the game.
For each user move, the web service sends a request to DynamoDB to conditionally update the game
item in the Games table. For example, the conditions ensure the move is valid, the square the user chose
is available, and it was the turn of the user who made the move. For a valid move, the update operation
adds a new attribute corresponding to the selection on the board. The update operation also sets the
value of the existing attribute to the user who can make the next move.
On the game page, the application makes asynchronous JavaScript calls every second, for up to five
minutes, to check if the game state in DynamoDB has changed. If it has, the application updates the
page with new information. After five minutes, the application stops making the requests and you need
to refresh the page to get updated information.
• Table – In DynamoDB, a table is a collection of items (that is, records), and each item is a collection of
  name-value pairs called attributes.
  In this Tic-Tac-Toe example, the application stores all game data in a table, Games. The application
  creates one item in the table per game and stores all game data as attributes. A tic-tac-toe game
  can have up to nine moves. Because DynamoDB tables do not have a schema in cases where only the
  primary key is the required attribute, the application can store varying number of attributes per game
  item.
  The Games table has a simple primary key made of one attribute, GameId, of string type. The
  application assigns a unique ID to each game. For more information on DynamoDB primary keys, see
  Primary Key (p. 5).
  When a user initiates a tic-tac-toe game by inviting another user to play, the application creates a new
  item in the Games table with attributes storing game metadata, such as the following:
  • HostId, the user who initiated the game.
  • Opponent, the user who was invited to play.
  • The user whose turn it is to play. The user who initiated the game plays first.
  • The user who uses the O symbol on the board. The user who initiates the games uses the O symbol.
  In addition, the application creates a StatusDate concatenated attribute, marking the initial game
  state as PENDING. The following screenshot shows an example item as it appears in the DynamoDB
  console:
  As the game progresses, the application adds one attribute to the table for each game move. The
  attribute name is the board position, for example TopLeft or BottomRight. For example, a move
  might have a TopLeft attribute with the value O, a TopRight attribute with the value O, and a
  BottomRight attribute with the value X. The attribute value is either O or X, depending on which user
  made the move. For example, consider the following board:
  The application then uses the StatusDate attribute in creating secondary indexes by specifying
  StatusDate as a sort key for the index. The benefit of using the StatusDate concatenated value
  attribute is further illustrated in the indexes discussed next.
• Global secondary indexes – You can use the table's primary key, GameId, to efficiently query the
  table to find a game item. To query the table on attributes other than the primary key attributes,
  DynamoDB supports the creation of secondary indexes. In this example application, you build the
  following two secondary indexes:
  • HostId-StatusDate-index. This index has HostId as a partition key and StatusDate as a sort key.
    You can use this index to query on HostId, for example to find games hosted by a particular user.
  • OpponentId-StatusDate-index. This index has OpponentId as a partition key and StatusDate
    as a sort key. You can use this index to query on Opponent, for example to find games where a
    particular user is the opponent.
  These indexes are called global secondary indexes because the partition key in these indexes is not the
  same the partition key (GameId), used in the primary key of the table.
  Note that both the indexes specify StatusDate as a sort key. Doing this enables the following:
  • You can query using the BEGINS_WITH comparison operator. For example, you can find all games
    with the IN_PROGRESS attribute hosted by a particular user. In this case, the BEGINS_WITH
    operator checks for the StatusDate value that begins with IN_PROGRESS.
  • DynamoDB stores the items in the index in sorted order, by sort key value. So if all status prefixes
    are the same (for example, IN_PROGRESS), the ISO format used for the date part will have items
    sorted from oldest to the newest. This approach enables certain queries to be performed efficiently,
    for example the following:
    • Retrieve up to 10 of the most recent IN_PROGRESS games hosted by the user who is logged in.
      For this query, you specify the HostId-StatusDate-index index.
    • Retrieve up to 10 of the most recent IN_PROGRESS games where the user logged in is the
      opponent. For this query, you specify the OpponentId-StatusDate-index index.
For more information about secondary indexes, see Improving Data Access with Secondary
Indexes (p. 444).
• Home page – This page provides the user a simple login, a CREATE button to create a new tic-tac-toe
  game, a list of games in progress, game history, and any active pending game invitations.
  The home page is not refreshed automatically; you must refresh the page to refresh the lists.
• Game page – This page shows the tic-tac-toe grid where users play.
  The application updates the game page automatically every second. The JavaScript in your browser
  calls the Python web server every second to query the Games table whether the game items in the
  table have changed. If they have, JavaScript triggers a page refresh so that the user sees the updated
  board.
Home Page
After the user logs in, the application displays the following three lists of information:
• Invitations – This list shows up to the 10 most recent invitations from others that are pending
  acceptance by the user who is logged in. In the preceding screenshot, user1 has invitations from user5
  and user2 pending.
• Games In-Progress – This list shows up to the 10 most recent games that are in progress. These are
  games that the user is actively playing, which have the status IN_PROGRESS. In the screenshot, user1
  is actively playing a tic-tac-toe game with user3 and user4.
• Recent History – This list shows up to the 10 most recent games that the user finished, which have the
  status FINISHED. In game shown in the screenshot, user1 has previously played with user2. For each
  completed game, the list shows the game result.
In the code, the index function (in application.py) makes the following three calls to retrieve game
status information:
inviteGames     = controller.getGameInvites(session["username"])
inProgressGames = controller.getGamesWithStatus(session["username"], "IN_PROGRESS")
finishedGames   = controller.getGamesWithStatus(session["username"], "FINISHED")
Each of these calls return a list of items from DynamoDB that are wrapped by the Game objects. It is easy
to extract data from these objects in the view. The index function passes these object lists to the view to
render the HTML.
return render_template("index.html",
                       user=session["username"],
                       invites=inviteGames,
                       inprogress=inProgressGames,
                       finished=finishedGames)
The Tic-Tac-Toe application defines the Game class primarily to store game data retrieved from
DynamoDB. These functions return lists of Game objects that enable you to isolate the rest of the
application from code related to Amazon DynamoDB items. Thus, these functions help you decouple
your application code from the details of the data store layer.
The architectural pattern described here is also referred as the model-view-controller (MVC) UI pattern.
In this case, the Game object instances (representing data) are the model, and the HTML page is the view.
The controller is divided into two files. The application.py file has the controller logic for the Flask
framework, and the business logic is isolated in the gameController.py file. That is, the application
stores everything that has to do with DynamoDB SDK in its own separate file in the dynamodb folder.
Let us review the three functions and how they query the Games table using global secondary indexes to
retrieve relevant data.
• It specifies the OpponentId-StatusDate-index index to use with the following index key values
  and comparison operators:
  • The partition key is OpponentId and takes the index key user ID.
  • The sort key is StatusDate and takes the comparison operator and index key value
    beginswith="PENDING_".
  You use the OpponentId-StatusDate-index index to retrieve games to which the logged-in user is
  invited—that is, where the logged-in user is the opponent.
• The query limits the result to 10 items.
gameInvitesIndex = self.cm.getGamesTable().query(
                                            Opponent__eq=user,
                                            StatusDate__beginswith="PENDING_",
                                            index="OpponentId-StatusDate-index",
                                            limit=10)
In the index, for each OpponentId (the partition key) DynamoDB keeps items sorted by StatusDate
(the sort key). Therefore, the games that the query returns will be the 10 most recent games.
Queries to find games that are either in progress or finished are the same except for the different status
value. Therefore, the application defines the getGamesWithStatus function, which takes the status
value as a parameter.
The following section discusses in-progress games, but the same description also applies to finished
games.
A list of in-progress games for a given user includes both the following:
The getGamesWithStatus function runs the following two queries, each time using the appropriate
secondary index.
• The function queries the Games table using the HostId-StatusDate-index index. For the index,
  the query specifies primary key values—both the partition key (HostId) and sort key (StatusDate)
  values, along with comparison operators.
                                                           StatusDate__beginswith=status,
                                                           index="HostId-StatusDate-index",
                                                           limit=10)
  oppGamesInProgress = self.cm.getGamesTable().query(Opponent__eq=user,
                                               StatusDate__beginswith=status,
                                               index="OpponentId-StatusDate-index",
                                               limit=10)
• The function then combines the two lists, sorts, and for the first 0 to 10 items creates a list of the
  Game objects and returns the list to the calling function (that is, the index).
  games = self.mergeQueries(hostGamesInProgress,
                          oppGamesInProgress)
  return games
Game Page
The game page is where the user plays tic-tac-toe games. It shows the game grid along with game-
relevant information. The following screenshot shows an example game in progress:
  In this case, the page shows the user as host and the game status as PENDING, waiting for the
  opponent to accept.
• The user accepts one of the pending invitations on the home page.
In this case, the page show the user as the opponent and game status as IN_PROGRESS.
A user selection on the board generates a form POST request to the application. That is, Flask calls the
selectSquare function (in application.py) with the HTML form data. This function, in turn, calls the
updateBoardAndTurn function (in gameController.py) to update the game item as follows:
The function returns true if the item update was successful; otherwise, it returns false. Note the
following about the updateBoardAndTurn function:
• The function calls the update_item function of the AWS SDK for Python to make a finite set of
  updates to an existing item. The function maps to the UpdateItem operation in DynamoDB. For more
  information, see UpdateItem.
      Note
      The difference between the UpdateItem and PutItem operations is that PutItem replaces
      the entire item. For more information, see PutItem.
• The new attribute to add, specific to the current user move, and its value (for example, TopLeft="X").
   attributeUpdates = {
                    position : {
                        "Action" : "PUT",
                        "Value" : { "S" : representation }
                        }
                    }
 self.cm.db.update_item("Games", key=key,
                        attribute_updates=attributeUpdates,
                        expected=expectations)
After the function returns, the selectSquare function calls redirect as shown in the following example:
redirect("/game="+gameId)
This call causes the browser to refresh. As part of this refresh, the application checks to see if the game
has ended in a win or draw. If it has, the application will update the game item accordingly.
In the preceding sections, you deployed and tested the Tic-Tac-Toe application locally on your computer
using DynamoDB Local. Now, you deploy the application in production as follows:
• Deploy the application using Elastic Beanstalk, an easy-to-use service for deploying and scaling web
  applications and services. For more information, go to Deploying a Flask Application to AWS Elastic
  Beanstalk.
  Elastic Beanstalk will launch one or more Amazon Elastic Compute Cloud (Amazon EC2) instances,
  which you configure through Elastic Beanstalk, on which your Tic-Tac-Toe application will run.
• Using the Amazon DynamoDB service, create a Games table that exists on AWS rather than locally on
  your computer.
In addition, you also have to configure permissions. Any AWS resources you create, such as the Games
table in DynamoDB, are private by default. Only the resource owner, that is the AWS account that created
the Games table, can access this table. Thus, by default your Tic-Tac-Toe application cannot update the
Games table.
To grant necessary permissions, you will create an AWS Identity and Access Management (IAM) role and
grant this role permissions to access the Games table. Your Amazon EC2 instance first assumes this role.
In response, AWS returns temporary security credentials that the Amazon EC2 instance can use to update
the Games table on behalf of the Tic-Tac-Toe application. When you configure your Elastic Beanstalk
application, you specify the IAM role that the Amazon EC2 instance or instances can assume. For more
information about IAM roles, go to IAM Roles for Amazon EC2 in the Amazon EC2 User Guide for Linux
Instances.
    Note
    Before you create Amazon EC2 instances for the Tic-Tac-Toe application, you must first decide
    the AWS region where you want Elastic Beanstalk to create the instances. After you create the
    Elastic Beanstalk application, you provide the same region name and endpoint in a configuration
    file. The Tic-Tac-Toe application uses information in this file to create the Games table and
    send subsequent requests in a specific AWS region. Both the DynamoDB Games table and the
    Amazon EC2 instances that Elastic Beanstalk launches must be in the same AWS region. For a list
    of available regions, go to Amazon DynamoDB in the Amazon Web Services General Reference.
1. Create an IAM role using the AWS IAM service. You will attach a policy to this role granting
   permissions for DynamoDB actions to access the Games table.
2. Bundle the Tic-Tac-Toe application code and a configuration file, and create a .zip file. You use this
   .zip file to give the Tic-Tac-Toe application code to Elastic Beanstalk to put on your servers. For more
   information on creating a bundle, go to Creating an Application Source Bundle in the AWS Elastic
   Beanstalk Developer Guide.
   In the configuration file (beanstalk.config), you provide AWS region and endpoint information.
   The Tic-Tac-Toe application uses this information to determine which DynamoDB region to talk to.
3. Set up the Elastic Beanstalk environment. Elastic Beanstalk will launch an Amazon EC2 instance
   or instances and deploy your Tic-Tac-Toe application bundle on them. After the Elastic Beanstalk
   environment is ready, you provide the configuration file name by adding the CONFIG_FILE
   environment variable.
4. Create the DynamoDB table. Using the Amazon DynamoDB service, you create the Games table on
   AWS, rather than locally on your computer. Remember, this table has a simple primary key made of
   the GameId partition key of string type.
5. Test the game in production.
{
    "Version":"2012-10-17",
    "Statement":[
       {
          "Action":[
             "dynamodb:ListTables"
          ],
          "Effect":"Allow",
          "Resource":"*"
       },
       {
          "Action":[
             "dynamodb:*"
          ],
          "Effect":"Allow",
          "Resource":[
             "arn:aws:dynamodb:us-west-2:922852403271:table/Games",
             "arn:aws:dynamodb:us-west-2:922852403271:table/Games/index/*"
          ]
       }
    ]
}
For further instructions, go to Creating a Role for an AWS Service (AWS Management Console) in the IAM
User Guide.
After you extract all files, note that you will have a code folder. To hand off this folder to Electric
Beanstalk, you will bundle the contents of this folder as a .zip file. First, you need to add a
configuration file to that folder. Your application will use the region and endpoint information to create
a DynamoDB table in the specified region and make subsequent table operation requests using the
specified endpoint.
     [dynamodb]
     region=<AWS region>
     endpoint=<DynamoDB endpoint>
     [dynamodb]
     region=us-west-2
     endpoint=dynamodb.us-west-2.amazonaws.com
     For a list of available regions, go to Amazon DynamoDB in the Amazon Web Services General
     Reference.
         Important
         The region specified in the configuration file is the location where the Tic-Tac-Toe
         application creates the Games table in DynamoDB. You must create the Elastic Beanstalk
         application discussed in the next section in the same region.
         Note
         When you create your Elastic Beanstalk application, you will request to launch an
         environment where you can choose the environment type. To test the Tic-Tac-Toe example
         application, you can choose the Single Instance environment type, skip the following, and
         go to the next step.
         However, note that the Load balancing, autoscaling environment type provides a highly
         available and scalable environment, something you should consider when you create
         and deploy other applications. If you choose this environment type, you will also need to
         generate a UUID and add it to the configuration file as shown following:
         [dynamodb]
         region=us-west-2
         endpoint=dynamodb.us-west-2.amazonaws.com
         [flask]
         secret_key= 284e784d-1a25-4a19-92bf-8eeb7a9example
         In client-server communication when the server sends response, for security's sake the
         server sends a signed cookie that the client sends back to the server in the next request.
         When there is only one server, the server can locally generate an encryption key when
         it starts. When there are many servers, they all need to know the same encryption
         key; otherwise, they won't be able to read cookies set by the peer servers. By adding
         secret_key to the configuration file, we tell all servers to use this encryption key.
3.   Zip the content of the root folder of the application (which includes the beanstalk.config file)—
     for example, TicTacToe.zip.
4.   Upload the .zip file to an Amazon Simple Storage Service (Amazon S3) bucket. In the next section,
     you provide this .zip file to Elastic Beanstalk to upload on the server or servers.
     For instructions on how to upload to an Amazon S3 bucket, go to the Create a Bucket and Add an
     Object to a Bucket topics in the Amazon Simple Storage Service Getting Started Guide.
1. Type the following custom URL to set up an Elastic Beanstalk console to set up the environment:
     https://console.aws.amazon.com/elasticbeanstalk/?region=<AWS-Region>#/newApplication
     ?applicationName=TicTacToeyour-name
     &solutionStackName=Python
     &sourceBundleUrl=https://s3.amazonaws.com/<bucket-name>/TicTacToe.zip
     &environmentType=SingleInstance
     &instanceType=t1.micro
     For more information about custom URLs, go to Constructing a Launch Now URL in the AWS Elastic
     Beanstalk Developer Guide. For the URL, note the following:
     • You will need to provide an AWS region name (the same as the one you provided in the
       configuration file), an Amazon S3 bucket name, and the object name.
     • For testing, the URL requests the SingleInstance environment type, and t1.micro as the instance
       type.
     • The application name must be unique. Thus, in the preceding URL, we suggest you prepend your
       name to the applicationName.
     Doing this opens the Elastic Beanstalk console. In some cases, you might need to sign in.
2.   In the Elastic Beanstalk console, choose Review and Launch, and then choose Launch.
3.   Note the URL for future reference. This URL opens your Tic-Tac-Toe application home page.
4. Configure the Tic-Tac-Toe application so it knows the location of the configuration file.
a. Choose the gear box next to Software Configuration, as shown in the following screenshot.
     b.   At the end of the Environment Properties section, type CONFIG_FILE and its value
          beanstalk.config, and then choose Save.
http://<pen-name>.elasticbeanstalk.com
     Make sure that you clear all cookies in your browser window so you won't be logged in as same user.
9.   Type the same URL to open the application home page, as shown in the following example:
http://<env-name>.elasticbeanstalk.com
     Both testuser1 and testuser2 can play the game. For each move, the application will save the move
     in the corresponding item in the Games table.
    If you are done testing, you can remove the resources you created to test the Tic-Tac-Toe application to
    avoid incurring any charges.
For up to date instructions on the DynamoDB Storage Backend for JanusGraph, see the README.md file.
    If you need to write an expression containing an attribute name that conflicts with a DynamoDB reserved
    word, you can define an expression attribute name to use in the place of the reserved word. For more
    information, see Expression Attribute Names (p. 341).
    ABORT
    ABSOLUTE
    ACTION
    ADD
    AFTER
    AGENT
    AGGREGATE
    ALL
    ALLOCATE
    ALTER
    ANALYZE
    AND
    ANY
    ARCHIVE
    ARE
    ARRAY
    AS
    ASC
    ASCII
    ASENSITIVE
    ASSERTION
ASYMMETRIC
AT
ATOMIC
ATTACH
ATTRIBUTE
AUTH
AUTHORIZATION
AUTHORIZE
AUTO
AVG
BACK
BACKUP
BASE
BATCH
BEFORE
BEGIN
BETWEEN
BIGINT
BINARY
BIT
BLOB
BLOCK
BOOLEAN
BOTH
BREADTH
BUCKET
BULK
BY
BYTE
CALL
CALLED
CALLING
CAPACITY
CASCADE
CASCADED
CASE
CAST
CATALOG
CHAR
CHARACTER
CHECK
CLASS
CLOB
CLOSE
CLUSTER
CLUSTERED
CLUSTERING
CLUSTERS
COALESCE
COLLATE
COLLATION
COLLECTION
COLUMN
COLUMNS
COMBINE
COMMENT
COMMIT
COMPACT
COMPILE
COMPRESS
CONDITION
CONFLICT
CONNECT
CONNECTION
CONSISTENCY
CONSISTENT
CONSTRAINT
CONSTRAINTS
CONSTRUCTOR
CONSUMED
CONTINUE
CONVERT
COPY
CORRESPONDING
COUNT
COUNTER
CREATE
CROSS
CUBE
CURRENT
CURSOR
CYCLE
DATA
DATABASE
DATE
DATETIME
DAY
DEALLOCATE
DEC
DECIMAL
DECLARE
DEFAULT
DEFERRABLE
DEFERRED
DEFINE
DEFINED
DEFINITION
DELETE
DELIMITED
DEPTH
DEREF
DESC
DESCRIBE
DESCRIPTOR
DETACH
DETERMINISTIC
DIAGNOSTICS
DIRECTORIES
DISABLE
DISCONNECT
DISTINCT
DISTRIBUTE
DO
DOMAIN
DOUBLE
DROP
DUMP
DURATION
DYNAMIC
EACH
ELEMENT
ELSE
ELSEIF
EMPTY
ENABLE
END
EQUAL
EQUALS
ERROR
ESCAPE
ESCAPED
EVAL
EVALUATE
EXCEEDED
EXCEPT
EXCEPTION
EXCEPTIONS
EXCLUSIVE
EXEC
EXECUTE
EXISTS
EXIT
EXPLAIN
EXPLODE
EXPORT
EXPRESSION
EXTENDED
EXTERNAL
EXTRACT
FAIL
FALSE
FAMILY
FETCH
FIELDS
FILE
FILTER
FILTERING
FINAL
FINISH
FIRST
FIXED
FLATTERN
FLOAT
FOR
FORCE
FOREIGN
FORMAT
FORWARD
FOUND
FREE
FROM
FULL
FUNCTION
FUNCTIONS
GENERAL
GENERATE
GET
GLOB
GLOBAL
GO
GOTO
GRANT
GREATER
GROUP
GROUPING
HANDLER
HASH
HAVE
HAVING
HEAP
HIDDEN
HOLD
HOUR
IDENTIFIED
IDENTITY
IF
IGNORE
IMMEDIATE
IMPORT
IN
INCLUDING
INCLUSIVE
INCREMENT
INCREMENTAL
INDEX
INDEXED
INDEXES
INDICATOR
INFINITE
INITIALLY
INLINE
INNER
INNTER
INOUT
INPUT
INSENSITIVE
INSERT
INSTEAD
INT
INTEGER
INTERSECT
INTERVAL
INTO
INVALIDATE
IS
ISOLATION
ITEM
ITEMS
ITERATE
JOIN
KEY
KEYS
LAG
LANGUAGE
LARGE
LAST
LATERAL
LEAD
LEADING
LEAVE
LEFT
LENGTH
LESS
LEVEL
LIKE
LIMIT
LIMITED
LINES
LIST
LOAD
LOCAL
LOCALTIME
LOCALTIMESTAMP
LOCATION
LOCATOR
LOCK
LOCKS
LOG
LOGED
LONG
LOOP
LOWER
MAP
MATCH
MATERIALIZED
MAX
MAXLEN
MEMBER
MERGE
METHOD
METRICS
MIN
MINUS
MINUTE
MISSING
MOD
MODE
MODIFIES
MODIFY
MODULE
MONTH
MULTI
MULTISET
NAME
NAMES
NATIONAL
NATURAL
NCHAR
NCLOB
NEW
NEXT
NO
NONE
NOT
NULL
NULLIF
NUMBER
NUMERIC
OBJECT
OF
OFFLINE
OFFSET
OLD
ON
ONLINE
ONLY
OPAQUE
OPEN
OPERATOR
OPTION
OR
ORDER
ORDINALITY
OTHER
OTHERS
OUT
OUTER
OUTPUT
OVER
OVERLAPS
OVERRIDE
OWNER
PAD
PARALLEL
PARAMETER
PARAMETERS
PARTIAL
PARTITION
PARTITIONED
PARTITIONS
PATH
PERCENT
PERCENTILE
PERMISSION
PERMISSIONS
PIPE
PIPELINED
PLAN
POOL
POSITION
PRECISION
PREPARE
PRESERVE
PRIMARY
PRIOR
PRIVATE
PRIVILEGES
PROCEDURE
PROCESSED
PROJECT
PROJECTION
PROPERTY
PROVISIONING
PUBLIC
PUT
QUERY
QUIT
QUORUM
RAISE
RANDOM
RANGE
RANK
RAW
READ
READS
REAL
REBUILD
RECORD
RECURSIVE
REDUCE
REF
REFERENCE
REFERENCES
REFERENCING
REGEXP
REGION
REINDEX
RELATIVE
RELEASE
REMAINDER
RENAME
REPEAT
REPLACE
REQUEST
RESET
RESIGNAL
RESOURCE
RESPONSE
RESTORE
RESTRICT
RESULT
RETURN
RETURNING
RETURNS
REVERSE
REVOKE
RIGHT
ROLE
ROLES
ROLLBACK
ROLLUP
ROUTINE
ROW
ROWS
RULE
RULES
SAMPLE
SATISFIES
SAVE
SAVEPOINT
SCAN
SCHEMA
SCOPE
SCROLL
SEARCH
SECOND
SECTION
SEGMENT
SEGMENTS
SELECT
SELF
SEMI
SENSITIVE
SEPARATE
SEQUENCE
SERIALIZABLE
SESSION
SET
SETS
SHARD
SHARE
SHARED
SHORT
SHOW
SIGNAL
SIMILAR
SIZE
SKEWED
SMALLINT
SNAPSHOT
SOME
SOURCE
SPACE
SPACES
SPARSE
SPECIFIC
SPECIFICTYPE
SPLIT
SQL
SQLCODE
SQLERROR
SQLEXCEPTION
SQLSTATE
SQLWARNING
START
STATE
STATIC
STATUS
STORAGE
STORE
STORED
STREAM
STRING
STRUCT
STYLE
SUB
SUBMULTISET
SUBPARTITION
SUBSTRING
SUBTYPE
SUM
SUPER
SYMMETRIC
SYNONYM
SYSTEM
TABLE
TABLESAMPLE
TEMP
TEMPORARY
TERMINATED
TEXT
THAN
THEN
THROUGHPUT
TIME
TIMESTAMP
TIMEZONE
TINYINT
TO
TOKEN
TOTAL
TOUCH
TRAILING
TRANSACTION
TRANSFORM
TRANSLATE
TRANSLATION
TREAT
TRIGGER
TRIM
TRUE
TRUNCATE
TTL
TUPLE
TYPE
UNDER
UNDO
UNION
UNIQUE
UNIT
UNKNOWN
UNLOGGED
UNNEST
UNPROCESSED
UNSIGNED
UNTIL
UPDATE
UPPER
URL
USAGE
USE
USER
USERS
USING
UUID
VACUUM
VALUE
VALUED
    VALUES
    VARCHAR
    VARIABLE
    VARIANCE
    VARINT
    VARYING
    VIEW
    VIEWS
    VIRTUAL
    VOID
    WAIT
    WHEN
    WHENEVER
    WHERE
    WHILE
    WINDOW
    WITH
    WITHIN
    WITHOUT
    WORK
    WRAPPED
    WRITE
    YEAR
    ZONE
    With the introduction of expression parameters (see Using Expressions in DynamoDB (p. 337)), several
    older parameters have been deprecated. New applications should not use these legacy parameters,
    but should use expression parameters instead. (For more information, see Using Expressions in
    DynamoDB (p. 337).)
        Note
        DynamoDB does not allow mixing legacy conditional parameters and expression parameters
        in a single call. For example, calling the Query operation with AttributesToGet and
        ConditionExpression will result in an error.
    The following table shows the DynamoDB APIs that still support these legacy parameters, and which
    expression parameter to use instead. This table can be helpful if you are considering updating your
    applications so that they use expression parameters instead.
     If You Use This API...            With These Legacy                  Use This Expression Parameter
                                       Parameters...                      Instead
KeyConditions KeyConditionExpression
QueryFilter FilterExpression
 If You Use This API...              With These Legacy                  Use This Expression Parameter
                                     Parameters...                      Instead
ScanFilter FilterExpression
Expected ConditionExpression
The following sections provide more information about legacy conditional parameters.
Topics
 • AttributesToGet (p. 830)
 • AttributeUpdates (p. 831)
 • ConditionalOperator (p. 832)
 • Expected (p. 833)
 • KeyConditions (p. 836)
 • QueryFilter (p. 838)
 • ScanFilter (p. 839)
 • Writing Conditions With Legacy Parameters (p. 840)
AttributesToGet
AttributesToGet is an array of one or more attributes to retrieve from DynamoDB. If no attribute
names are provided, then all attributes will be returned. If any of the requested attributes are not found,
they will not appear in the result.
AttributesToGet allows you to retrieve attributes of type List or Map; however, it cannot retrieve
individual elements within a List or a Map.
    --table-name Music \
    --projection-expression "Artist, Genre" \
    --key '{
        "Artist": {"S":"No One You Know"},
        "SongTitle": {"S":"Call Me Today"}
    }'
AttributeUpdates
In an UpdateItem operation, AttributeUpdates are the names of attributes to be modified, the
action to perform on each, and the new value for each. If you are updating an attribute that is an index
key attribute for any indexes on that table, the attribute type must match the index key type defined in
the AttributesDefinition of the table description. You can use UpdateItem to update any non-key
attributes.
Attribute values cannot be null. String and Binary type attributes must have lengths greater than
zero. Set type attributes must not be empty. Requests with empty values will be rejected with a
ValidationException exception.
Each AttributeUpdates element consists of an attribute name to modify, along with the following:
  If an item with the specified primary key is found in the table, the following values perform the
  following actions:
  • PUT - Adds the specified attribute to the item. If the attribute already exists, it is replaced by the new
     value.
  • DELETE - Removes the attribute and its value, if no value is specified for DELETE. The data type of
     the specified value must match the existing value's data type.
    If a set of values is specified, then those values are subtracted from the old set. For example, if the
    attribute value was the set [a,b,c] and the DELETE action specifies [a,c], then the final attribute
    value is [b]. Specifying an empty set is an error.
  • ADD - Adds the specified value to the item, if the attribute does not already exist. If the attribute
    does exist, then the behavior of ADD depends on the data type of the attribute:
    • If the existing attribute is a number, and if Value is also a number, then Value is mathematically
      added to the existing attribute. If Value is a negative number, then it is subtracted from the
      existing attribute.
           Note
           If you use ADD to increment or decrement a number value for an item that doesn't exist
           before the update, DynamoDB uses 0 as the initial value.
           Similarly, if you use ADD for an existing item to increment or decrement an attribute value
           that doesn't exist before the update, DynamoDB uses 0 as the initial value. For example,
           suppose that the item you want to update doesn't have an attribute named itemcount,
           but you decide to ADD the number 3 to this attribute anyway. DynamoDB will create the
           itemcount attribute, set its initial value to 0, and finally add 3 to it. The result will be a
           new itemcount attribute, with a value of 3.
    • If the existing data type is a set, and if Value is also a set, then Value is appended to the existing
      set. For example, if the attribute value is the set [1,2], and the ADD action specified [3], then
      the final attribute value is [1,2,3]. An error occurs if an ADD action is specified for a set attribute
      and the attribute type specified does not match the existing set type.
      Both sets must have the same primitive data type. For example, if the existing data type is a set of
      strings, Value must also be a set of strings.
  If no item with the specified key is found in the table, the following values perform the following
  actions:
  • PUT - Causes DynamoDB to create a new item with the specified primary key, and then adds the
     attribute.
  • DELETE - Nothing happens, because attributes cannot be deleted from a nonexistent item. The
     operation succeeds, but DynamoDB does not create a new item.
  • ADD - Causes DynamoDB to create an item with the supplied primary key and number (or set of
     numbers) for the attribute value. The only data types allowed are Number and Number Set.
If you provide any attributes that are part of an index key, then the data types for those attributes must
match those of the schema in the table's attribute definition.
ConditionalOperator
A logical operator to apply to the conditions in a Expected, QueryFilter or ScanFilter map:
• AND - If all of the conditions evaluate to true, then the entire map evaluates to true.
• OR - If at least one of the conditions evaluate to true, then the entire map evaluates to true.
The operation will succeed only if the entire map evaluates to true.
    Note
    This parameter does not support attributes of type List or Map.
Expected
Expected is a conditional block for an UpdateItem operation. Expected is a map of attribute/
condition pairs. Each element of the map consists of an attribute name, a comparison operator, and one
or more values. DynamoDB compares the attribute with the value(s) you supplied, using the comparison
operator. For each Expected element, the result of the evaluation is either true or false.
If you specify more than one element in the Expected map, then by default all of the conditions
must evaluate to true. In other words, the conditions are ANDed together. (You can use the
ConditionalOperator parameter to OR the conditions instead. If you do this, then at least one of the
conditions must evaluate to true, rather than all of them.)
If the Expected map evaluates to true, then the conditional operation succeeds; otherwise, it fails.
• AttributeValueList - One or more values to evaluate against the supplied attribute. The number
  of values in the list depends on the ComparisonOperator being used.
  String value comparisons for greater than, equals, or less than are based on Unicode with UTF-8 binary
  encoding. For example, a is greater than A, and a is greater than B.
  For type Binary, DynamoDB treats each byte of the binary data as unsigned when it compares binary
  values.
• ComparisonOperator - A comparator for evaluating attributes in the AttributeValueList. When
  performing the comparison, DynamoDB uses strongly consistent reads.
    AttributeValueList can contain only one AttributeValue element of type String, Number,
    Binary, String Set, Number Set, or Binary Set. If an item contains an AttributeValue element
    of a different type than the one provided in the request, the value does not match. For example,
    {"S":"6"} does not equal {"N":"6"}. Also, {"N":"6"} does not equal {"NS":["6", "2",
    "1"]}.
  • NE : Not equal. NE is supported for all datatypes, including lists and maps.
    AttributeValueList can contain only one AttributeValue of type String, Number, Binary,
    String Set, Number Set, or Binary Set. If an item contains an AttributeValue of a different type
    than the one provided in the request, the value does not match. For example, {"S":"6"} does not
    equal {"N":"6"}. Also, {"N":"6"} does not equal {"NS":["6", "2", "1"]}.
  • LE : Less than or equal.
    AttributeValueList can contain only one AttributeValue element of type String, Number,
    or Binary (not a set type). If an item contains an AttributeValue element of a different type than
    the one provided in the request, the value does not match. For example, {"S":"6"} does not equal
    {"N":"6"}. Also, {"N":"6"} does not compare to {"NS":["6", "2", "1"]}.
  • LT : Less than.
    AttributeValueList can contain only one AttributeValue of type String, Number, or Binary
    (not a set type). If an item contains an AttributeValue element of a different type than the
  one provided in the request, the value does not match. For example, {"S":"6"} does not equal
  {"N":"6"}. Also, {"N":"6"} does not compare to {"NS":["6", "2", "1"]}.
• GE : Greater than or equal.
  AttributeValueList can contain only one AttributeValue element of type String, Number,
  or Binary (not a set type). If an item contains an AttributeValue element of a different type than
  the one provided in the request, the value does not match. For example, {"S":"6"} does not equal
  {"N":"6"}. Also, {"N":"6"} does not compare to {"NS":["6", "2", "1"]}.
• GT : Greater than.
  AttributeValueList can contain only one AttributeValue element of type String, Number,
  or Binary (not a set type). If an item contains an AttributeValue element of a different type than
  the one provided in the request, the value does not match. For example, {"S":"6"} does not equal
  {"N":"6"}. Also, {"N":"6"} does not compare to {"NS":["6", "2", "1"]}.
• NOT_NULL : The attribute exists. NOT_NULL is supported for all datatypes, including lists and maps.
      Note
      This operator tests for the existence of an attribute, not its data type. If the data type of
      attribute "a" is null, and you evaluate it using NOT_NULL, the result is a Boolean true.
      This result is because the attribute "a" exists; its data type is not relevant to the NOT_NULL
      comparison operator.
• NULL : The attribute does not exist. NULL is supported for all datatypes, including lists and maps.
      Note
      This operator tests for the nonexistence of an attribute, not its data type. If the data type
      of attribute "a" is null, and you evaluate it using NULL, the result is a Boolean false. This
      is because the attribute "a" exists; its data type is not relevant to the NULL comparison
      operator.
• CONTAINS : Checks for a subsequence, or value in a set.
  AttributeValueList can contain only one AttributeValue element of type String, Number, or
  Binary (not a set type). If the target attribute of the comparison is of type String, then the operator
  checks for a substring match. If the target attribute of the comparison is of type Binary, then the
  operator looks for a subsequence of the target that matches the input. If the target attribute of the
  comparison is a set ("SS", "NS", or "BS"), then the operator evaluates to true if it finds an exact match
  with any member of the set.
  CONTAINS is supported for lists: When evaluating "a CONTAINS b", " a" can be a list; however, "b"
  cannot be a set, a map, or a list.
• NOT_CONTAINS : Checks for absence of a subsequence, or absence of a value in a set.
  AttributeValueList can contain only one AttributeValue element of type String, Number,
  or Binary (not a set type). If the target attribute of the comparison is a String, then the operator
  checks for the absence of a substring match. If the target attribute of the comparison is Binary, then
  the operator checks for the absence of a subsequence of the target that matches the input. If the
  target attribute of the comparison is a set ("SS", "NS", or "BS"), then the operator evaluates to true if
  it does not find an exact match with any member of the set.
  NOT_CONTAINS is supported for lists: When evaluating "a NOT CONTAINS b", " a" can be a list;
  however, "b" cannot be a set, a map, or a list.
• BEGINS_WITH : Checks for a prefix.
  AttributeValueList can contain only one AttributeValue of type String or Binary (not a
  Number or a set type). The target attribute of the comparison must be of type String or Binary (not
  a Number or a set type).
• IN : Checks for matching elements within two sets.
                               API Version 2012-08-10
                                         834
                             Amazon DynamoDB Developer Guide
                                        Expected
    AttributeValueList must contain two AttributeValue elements of the same type, either
    String, Number, or Binary (not a set type). A target attribute matches if the target value is greater
    than, or equal to, the first element and less than, or equal to, the second element. If an item
    contains an AttributeValue element of a different type than the one provided in the request, the
    value does not match. For example, {"S":"6"} does not compare to {"N":"6"}. Also, {"N":"6"}
    does not compare to {"NS":["6", "2", "1"]}
The Value and Exists parameters are incompatible with AttributeValueList and
ComparisonOperator. Note that if you use both sets of parameters at once, DynamoDB will return a
ValidationException exception.
    Note
    This parameter does not support attributes of type List or Map.
KeyConditions
KeyConditions are the selection criteria for a Query operation. For a query on a table, you can have
conditions only on the table primary key attributes. You must provide the partition key name and value
as an EQ condition. You can optionally provide a second condition, referring to the sort key.
    Note
    If you don't provide a sort key condition, all of the items that match the partition key will be
    retrieved. If a FilterExpression or QueryFilter is present, it will be applied after the items
    are retrieved.
For a query on an index, you can have conditions only on the index key attributes. You must provide the
index partition key name and value as an EQ condition. You can optionally provide a second condition,
referring to the index sort key.
Each KeyConditions element consists of an attribute name to compare, along with the following:
• AttributeValueList - One or more values to evaluate against the supplied attribute. The number
  of values in the list depends on the ComparisonOperator being used.
  String value comparisons for greater than, equals, or less than are based on Unicode with UTF-8 binary
  encoding. For example, a is greater than A, and a is greater than B.
  For Binary, DynamoDB treats each byte of the binary data as unsigned when it compares binary values.
• ComparisonOperator - A comparator for evaluating attributes, for example, equals, greater than,
  less than, and so on.
EQ | LE | LT | GE | GT | BEGINS_WITH | BETWEEN
    AttributeValueList can contain only one AttributeValue of type String, Number, or Binary
    (not a set type). If an item contains an AttributeValue element of a different type than the
    one specified in the request, the value does not match. For example, {"S":"6"} does not equal
    {"N":"6"}. Also, {"N":"6"} does not equal {"NS":["6", "2", "1"]}.
  • LE : Less than or equal.
    AttributeValueList can contain only one AttributeValue element of type String, Number,
    or Binary (not a set type). If an item contains an AttributeValue element of a different type than
    the one provided in the request, the value does not match. For example, {"S":"6"} does not equal
    {"N":"6"}. Also, {"N":"6"} does not compare to {"NS":["6", "2", "1"]}.
  • LT : Less than.
    AttributeValueList can contain only one AttributeValue of type String, Number, or Binary
    (not a set type). If an item contains an AttributeValue element of a different type than the
    one provided in the request, the value does not match. For example, {"S":"6"} does not equal
    {"N":"6"}. Also, {"N":"6"} does not compare to {"NS":["6", "2", "1"]}.
  • GE : Greater than or equal.
    AttributeValueList can contain only one AttributeValue element of type String, Number,
    or Binary (not a set type). If an item contains an AttributeValue element of a different type than
    the one provided in the request, the value does not match. For example, {"S":"6"} does not equal
    {"N":"6"}. Also, {"N":"6"} does not compare to {"NS":["6", "2", "1"]}.
  • GT : Greater than.
    AttributeValueList can contain only one AttributeValue element of type String, Number,
    or Binary (not a set type). If an item contains an AttributeValue element of a different type than
    the one provided in the request, the value does not match. For example, {"S":"6"} does not equal
    {"N":"6"}. Also, {"N":"6"} does not compare to {"NS":["6", "2", "1"]}.
  • BEGINS_WITH : Checks for a prefix.
    AttributeValueList can contain only one AttributeValue of type String or Binary (not a
    Number or a set type). The target attribute of the comparison must be of type String or Binary (not
    a Number or a set type).
  • BETWEEN : Greater than or equal to the first value, and less than or equal to the second value.
    AttributeValueList must contain two AttributeValue elements of the same type, either
    String, Number, or Binary (not a set type). A target attribute matches if the target value is greater
    than, or equal to, the first element and less than, or equal to, the second element. If an item
    contains an AttributeValue element of a different type than the one provided in the request, the
    value does not match. For example, {"S":"6"} does not compare to {"N":"6"}. Also, {"N":"6"}
    does not compare to {"NS":["6", "2", "1"]}
    --expression-attribute-values '{
        ":a": {"S": "No One You Know"},
        ":t1": {"S": "A"},
        ":t2": {"S": "M"}
    }'
QueryFilter
In a Query operation, QueryFilter is a condition that evaluates the query results after the items are
read and returns only the desired values.
If you provide more than one condition in the QueryFilter map, then by default all of the
conditions must evaluate to true. In other words, the conditions are ANDed together. (You can use the
ConditionalOperator (p. 832) parameter to OR the conditions instead. If you do this, then at least one
of the conditions must evaluate to true, rather than all of them.)
Note that QueryFilter does not allow key attributes. You cannot define a filter condition on a partition
key or a sort key.
Each QueryFilter element consists of an attribute name to compare, along with the following:
• AttributeValueList - One or more values to evaluate against the supplied attribute. The number
  of values in the list depends on the operator specified in ComparisonOperator.
  String value comparisons for greater than, equals, or less than are based on UTF-8 binary encoding.
  For example, a is greater than A, and a is greater than B.
  For type Binary, DynamoDB treats each byte of the binary data as unsigned when it compares binary
  values.
  For information on specifying data types in JSON, see DynamoDB Low-Level API (p. 185).
• ComparisonOperator - A comparator for evaluating attributes. For example, equals, greater than,
  less than, etc.
         }
    }' \
    --query-filter '{
         "Price": {
             "ComparisonOperator": "GT",
             "AttributeValueList": [ {"N": "1.00"} ]
         }
    }'
ScanFilter
In a Scan operation, ScanFilter is a condition that evaluates the scan results and returns only the
desired values.
    Note
    This parameter does not support attributes of type List or Map.
If you specify more than one condition in the ScanFilter map, then by default all of the conditions
must evaluate to true. In other words, the conditions are ANDed together. (You can use the
ConditionalOperator (p. 832) parameter to OR the conditions instead. If you do this, then at least one
of the conditions must evaluate to true, rather than all of them.)
Each ScanFilter element consists of an attribute name to compare, along with the following:
• AttributeValueList - One or more values to evaluate against the supplied attribute. The number
  of values in the list depends on the operator specified in ComparisonOperator .
  String value comparisons for greater than, equals, or less than are based on UTF-8 binary encoding.
  For example, a is greater than A, and a is greater than B.
For Binary, DynamoDB treats each byte of the binary data as unsigned when it compares binary values.
  For information on specifying data types in JSON, see DynamoDB Low-Level API (p. 185).
• ComparisonOperator - A comparator for evaluating attributes. For example, equals, greater than,
  less than, etc.
Simple Conditions
With attribute values, you can write conditions for comparisons against table attributes. A condition
always evaluates to true or false, and consists of:
The following sections describe the various comparison operators, along with examples of how to use
them in conditions.
Use these operators to check whether an attribute exists, or doesn't exist. Because there is no value to
compare against, do not specify AttributeValueList.
Example
...
      "Dimensions": {
           ComparisonOperator: "NOT_NULL"
      }
...
  AttributeValueList can contain only one value of type String, Number, Binary, String Set, Number
  Set, or Binary Set. If an item contains a value of a different type than the one specified in the request,
  the value does not match. For example, the string "3" is not equal to the number 3. Also, the number
  3 is not equal to the number set [3, 2, 1].
• NE - true if an attribute is not equal to a value.
  AttributeValueList can contain only one value of type String, Number, Binary, String Set, Number
  Set, or Binary Set. If an item contains a value of a different type than the one specified in the request,
  the value does not match.
• LE - true if an attribute is less than or equal to a value.
  AttributeValueList can contain only one value of type String, Number, or Binary (not a set). If an
  item contains an AttributeValue of a different type than the one specified in the request, the value
  does not match.
• LT - true if an attribute is less than a value.
  AttributeValueList can contain only one value of type String, Number, or Binary (not a set). If
  an item contains a value of a different type than the one specified in the request, the value does not
  match.
• GE - true if an attribute is greater than or equal to a value.
  AttributeValueList can contain only one value of type String, Number, or Binary (not a set). If
  an item contains a value of a different type than the one specified in the request, the value does not
  match.
• GT - true if an attribute is greater than a value.
  AttributeValueList can contain only one value of type String, Number, or Binary (not a set). If
  an item contains a value of a different type than the one specified in the request, the value does not
  match.
• CONTAINS - true if a value is present within a set, or if one value contains another.
  AttributeValueList can contain only one value of type String, Number, or Binary (not a set). If the
  target attribute of the comparison is a String, then the operator checks for a substring match. If the
  target attribute of the comparison is Binary, then the operator looks for a subsequence of the target
  that matches the input. If the target attribute of the comparison is a set, then the operator evaluates
  to true if it finds an exact match with any member of the set.
• NOT_CONTAINS - true if a value is not present within a set, or if one value does not contain another
  value.
  AttributeValueList can contain only one value of type String, Number, or Binary (not a set). If the
  target attribute of the comparison is a String, then the operator checks for the absence of a substring
  match. If the target attribute of the comparison is Binary, then the operator checks for the absence of
  a subsequence of the target that matches the input. If the target attribute of the comparison is a set,
  then the operator evaluates to true if it does not find an exact match with any member of the set.
• BEGINS_WITH - true if the first few characters of an attribute match the provided value. Do not use
  this operator for comparing numbers.
  AttributeValueList can contain only one value of type String or Binary (not a Number or a set).
  The target attribute of the comparison must be a String or Binary (not a Number or a set).
Use these operators to compare an attribute with a value. You must specify an AttributeValueList
consisting of a single value. For most of the operators, this value must be a scalar; however, the EQ and
NE operators also support sets.
Examples
  ...
        "Price": {
            ComparisonOperator: "GT",
            AttributeValueList: [ {"N":100"} ]
        }
  ...
  ...
        "ProductCategory": {
            ComparisonOperator: "BEGINS_WITH",
            AttributeValueList: [ {"S":"Bo"} ]
        }
  ...
  ...
        "Color": {
            ComparisonOperator: "EQ",
            AttributeValueList: [
                [ {"S":"Black"}, {"S":"Red"}, {"S":"Green"} ]
            ]
        }
  ...
        Note
        When comparing set data types, the order of the elements does not matter. DynamoDB will
        return only the items with the same set of values, regardless of the order in which you specify
        them in your request.
  AttributeValueList must contain two elements of the same type, either String, Number, or Binary
  (not a set). A target attribute matches if the target value is greater than, or equal to, the first element
  and less than, or equal to, the second element. If an item contains a value of a different type than the
  one specified in the request, the value does not match.
Use this operator to determine if an attribute value is within a range. The AttributeValueList must
contain two scalar elements of the same type - String, Number, or Binary.
Example
The following expression evaluates to true if a product's price is between 100 and 200.
...
      "Price": {
          ComparisonOperator: "BETWEEN",
          AttributeValueList: [ {"N":"100"}, {"N":"200"} ]
      }
...
  AttributeValueList can contain one or more elements of type String, Number, or Binary (not
  a set). These attributes are compared against an existing non-set type attribute of an item. If any
  elements of the input set are present in the item attribute, the expression evaluates to true.
  AttributeValueList can contain one or more values of type String, Number, or Binary (not a set).
  The target attribute of the comparison must be of the same type and exact value to match. A String
  never matches a String set.
Use this operator to determine whether the supplied value is within an enumerated list. You can specify
any number of scalar values in AttributeValueList, but they all must be of the same data type.
Example
The following expression evaluates to true if the value for Id is 201, 203, or 205.
...
      "Id": {
          ComparisonOperator: "IN",
          AttributeValueList: [ {"N":"201"}, {"N":"203"}, {"N":"205"} ]
      }
...
By default, when you specify more than one condition, all of the conditions must evaluate to true in
order for the entire expression to evaluate to true. In other words, an implicit AND operation takes place.
Example
The following expression evaluates to true if a product is a book which has at least 600 pages. Both of
the conditions must evaluate to true, since they are implicitly ANDed together.
...
      "ProductCategory": {
          ComparisonOperator:    "EQ",
          AttributeValueList:    [ {"S":"Book"} ]
      },
      "PageCount": {
          ComparisonOperator:    "GE",
          AttributeValueList:    [ {"N":600"} ]
      }
...
You can use ConditionalOperator (p. 832) to clarify that an AND operation will take place. The
following example behaves in the same manner as the previous one.
...
      "ConditionalOperator" : "AND",
      "ProductCategory": {
          "ComparisonOperator": "EQ",
          "AttributeValueList": [ {"N":"Book"} ]
      },
      "PageCount": {
          "ComparisonOperator": "GE",
          "AttributeValueList": [ {"N":600"} ]
      }
...
You can also set ConditionalOperator to OR, which means that at least one of the conditions must
evaluate to true.
Example
The following expression evaluates to true if a product is a mountain bike, if it is a particular brand name,
or if its price is greater than 100.
...
      ConditionalOperator : "OR",
      "BicycleType": {
          "ComparisonOperator": "EQ",
          "AttributeValueList": [ {"S":"Mountain" ]
      },
      "Brand": {
          "ComparisonOperator": "EQ",
          "AttributeValueList": [ {"S":"Brand-Company A" ]
      },
      "Price": {
          "ComparisonOperator": "GT",
          "AttributeValueList": [ {"N":"100"} ]
      }
...
      Note
      In a complex expression, the conditions are processed in order, from the first condition to the
      last.
      You cannot use both AND and OR in a single expression.
The Value and Exists options continue to be supported in DynamoDB; however, they only let
you test for an equality condition, or whether an attribute exists. We recommend that you use
ComparisonOperator and AttributeValueList instead, because these options let you construct a
much wider range of conditions.
Example
A DeleteItem can check to see whether a book is no longer in publication, and only delete it if this
condition is true. Here is an AWS CLI example using a legacy condition:
The following example does the same thing, but does not use a legacy condition:
Example
A PutItem operation can protect against overwriting an existing item with the same primary key
attributes. Here is an example using a legacy condition:
The following example does the same thing, but does not use a legacy condition:
    Note
    For conditions in the Expected map, do not use the legacy Value and Exists options
    together with ComparisonOperator and AttributeValueList. If you do this, your
    conditional write will fail.
    • BatchGetItem
    • BatchWriteItem
    • CreateTable
    • DeleteItem
    • DeleteTable
    • DescribeLimits
    • DescribeTable
    • DescribeTimeToLive
    • GetItem
    • ListTables
    • ListTagsOfResource
    • PutItem
    • Query
    • Scan
    • TagResource
    • UpdateItem
    • UpdateTable
    • UpdateTimeToLive
    • UntagResource
    New applications should use the current API version (2012-08-10). For more information, see Current
    Low-Level API Version (2012-08-10) (p. 846).
        Note
        We recommend that you migrate your applications to the latest API version (2012-08-10), since
        new DynamoDB features will not be backported to the previous API version.
    Topics
     • BatchGetItem (p. 847)
     • BatchWriteItem (p. 851)
     • CreateTable (p. 856)
     • DeleteItem (p. 861)
     • DeleteTable (p. 865)
     • DescribeTables (p. 868)
BatchGetItem
    Important
    This section refers to API version 2011-12-05, which is deprecated and
    should not be used for new applications.
    For documentation on the current low-level API, see the Amazon DynamoDB API Reference.
Description
The BatchGetItem operation returns the attributes for multiple items from multiple tables using their
primary keys. The maximum number of items that can be retrieved for a single operation is 100. Also,
the number of items retrieved is constrained by a 1 MB size limit. If the response size limit is exceeded
or a partial result is returned because the table’s provisioned throughput is exceeded, or because of
an internal processing failure, DynamoDB returns an UnprocessedKeys value so you can retry the
operation starting with the next item to get. DynamoDB automatically adjusts the number of items
returned per page to enforce this limit. For example, even if you ask to retrieve 100 items, but each
individual item is 50 KB in size, the system returns 20 items and an appropriate UnprocessedKeys
value so you can get the next page of results. If desired, your application can include its own logic to
assemble the pages of results into one set.
If no items could be processed because of insufficient provisioned throughput on each of the tables
involved in the request, DynamoDB returns a ProvisionedThroughputExceededException error.
    Note
    By default, BatchGetItem performs eventually consistent reads on every table in the request.
    You can set the ConsistentRead parameter to true, on a per-table basis, if you want
    consistent reads instead.
    BatchGetItem fetches items in parallel to minimize response latencies.
    When designing your application, keep in mind that DynamoDB does not guarantee how
    attributes are ordered in the returned response. Include the primary key values in the
    AttributesToGet for the items in your request to help parse the response by item.
    If the requested items do not exist, nothing is returned in the response for those items. Requests
    for non-existent items consume the minimum read capacity units according to the type of read.
    For more information, see Item Sizes and Capacity Unit Consumption (p. 297).
Requests
Syntax
// This header is abbreviated. For a sample of a complete header, see DynamoDB Low-Level
 API.
POST / HTTP/1.1
x-amz-target: DynamoDB_20111205.BatchGetItem
content-type: application/x-amz-json-1.0
{"RequestItems":
    {"Table1":
        {"Keys":
            [{"HashKeyElement": {"S":"KeyValue1"}, "RangeKeyElement":{"N":"KeyValue2"}},
            {"HashKeyElement": {"S":"KeyValue3"}, "RangeKeyElement":{"N":"KeyValue4"}},
            {"HashKeyElement": {"S":"KeyValue5"}, "RangeKeyElement":{"N":"KeyValue6"}}],
        "AttributesToGet":["AttributeName1", "AttributeName2", "AttributeName3"]},
    "Table2":
        {"Keys":
            [{"HashKeyElement": {"S":"KeyValue4"}},
            {"HashKeyElement": {"S":"KeyValue5"}}],
        "AttributesToGet": ["AttributeName4", "AttributeName5", "AttributeName6"]
        }
    }
}
Type: String
Default: None
Type: String
Default: None
Type: Keys
Type: Array
Type: Boolean
Responses
Syntax
HTTP/1.1 200
x-amzn-RequestId: 8966d095-71e9-11e0-a498-71d736f27375
content-type: application/x-amz-json-1.0
content-length: 855
{"Responses":
    {"Table1":
        {"Items":
        [{"AttributeName1": {"S":"AttributeValue"},
        "AttributeName2": {"N":"AttributeValue"},
        "AttributeName3": {"SS":["AttributeValue", "AttributeValue", "AttributeValue"]}
        },
        {"AttributeName1": {"S": "AttributeValue"},
        "AttributeName2": {"S": "AttributeValue"},
        "AttributeName3": {"NS": ["AttributeValue", "AttributeValue", "AttributeValue"]}
        }],
    "ConsumedCapacityUnits":1},
    "Table2":
        {"Items":
        [{"AttributeName1": {"S":"AttributeValue"},
        "AttributeName2": {"N":"AttributeValue"},
        "AttributeName3": {"SS":["AttributeValue", "AttributeValue", "AttributeValue"]}
        },
        {"AttributeName1": {"S": "AttributeValue"},
        "AttributeName2": {"S": "AttributeValue"},
        "AttributeName3": {"NS": ["AttributeValue", "AttributeValue","AttributeValue"]}
        }],
    "ConsumedCapacityUnits":1}
    },
    "UnprocessedKeys":
        {"Table3":
        {"Keys":
            [{"HashKeyElement": {"S":"KeyValue1"}, "RangeKeyElement":{"N":"KeyValue2"}},
            {"HashKeyElement": {"S":"KeyValue3"}, "RangeKeyElement":{"N":"KeyValue4"}},
            {"HashKeyElement": {"S":"KeyValue5"}, "RangeKeyElement":{"N":"KeyValue6"}}],
        "AttributesToGet":["AttributeName1", "AttributeName2", "AttributeName3"]}
        }
}
Name Description
Type: Map
Type: String
Name Description
Type: Number
Type: Array
UnprocessedKeys: Table: Keys                     The primary key attribute values that define
                                                 the items and the attributes associated with the
                                                 items. For more information about primary keys,
                                                 see Primary Key (p. 5) .
Type: Boolean.
Special Errors
Error Description
Examples
The following examples show an HTTP POST request and response using the BatchGetItem operation.
For examples using the AWS SDK, see Working with Items in DynamoDB (p. 326).
Sample Request
The following sample requests attributes from two different tables.
{"RequestItems":
    {"comp2":
        {"Keys":
             [{"HashKeyElement":{"S":"Julie"}},{"HashKeyElement":{"S":"Mingus"}}],
        "AttributesToGet":["user","friends"]},
    "comp1":
        {"Keys":
             [{"HashKeyElement":{"S":"Casey"},"RangeKeyElement":{"N":"1319509152"}},
             {"HashKeyElement":{"S":"Dave"},"RangeKeyElement":{"N":"1319509155"}},
             {"HashKeyElement":{"S":"Riley"},"RangeKeyElement":{"N":"1319509158"}}],
        "AttributesToGet":["user","status"]}
    }
}
Sample Response
The following sample is the response.
HTTP/1.1 200 OK
x-amzn-RequestId: GTPQVRM4VJS792J1UFJTKUBVV4KQNSO5AEMVJF66Q9ASUAAJG
content-type: application/x-amz-json-1.0
content-length: 373
Date: Fri, 02 Sep 2011 23:07:39 GMT
{"Responses":
    {"comp2":
        {"Items":
             [{"status":{"S":"online"},"user":{"S":"Casey"}},
             {"status":{"S":"working"},"user":{"S":"Riley"}},
             {"status":{"S":"running"},"user":{"S":"Dave"}}],
        "ConsumedCapacityUnits":1.5},
    "comp2":
        {"Items":
             [{"friends":{"SS":["Elisabeth", "Peter"]},"user":{"S":"Mingus"}},
             {"friends":{"SS":["Dave", "Peter"]},"user":{"S":"Julie"}}],
        "ConsumedCapacityUnits":1}
    },
    "UnprocessedKeys":{}
}
BatchWriteItem
    Important
    This section refers to API version 2011-12-05, which is deprecated and
    should not be used for new applications.
    For documentation on the current low-level API, see the Amazon DynamoDB API Reference.
Description
This operation enables you to put or delete several items across multiple tables in a single call.
To upload one item, you can use PutItem, and to delete one item, you can use DeleteItem. However,
when you want to upload or delete large amounts of data, such as uploading large amounts of
data from Amazon EMR (Amazon EMR) or migrating data from another database in to DynamoDB,
BatchWriteItem offers an efficient alternative.
If you use languages such as Java, you can use threads to upload items in parallel. This adds complexity
in your application to handle the threads. Other languages don't support threading. For example, if you
are using PHP, you must upload or delete items one at a time. In both situations, BatchWriteItem
provides an alternative where the specified put and delete operations are processed in parallel, giving
you the power of the thread pool approach without having to introduce complexity in your application.
Note that each individual put and delete specified in a BatchWriteItem operation costs the same
in terms of consumed capacity units. However, because BatchWriteItem performs the specified
operations in parallel, you get lower latency. Delete operations on non-existent items consume 1
write capacity unit. For more information about consumed capacity units, see Working with Tables in
DynamoDB (p. 291).
DynamoDB rejects the entire batch write operation if any one of the following is true:
• If one or more tables specified in the BatchWriteItem request does not exist.
• If primary key attributes specified on an item in the request does not match the corresponding table's
  primary key schema.
• If you try to perform multiple operations on the same item in the same BatchWriteItem request. For
  example, you cannot put and delete the same item in the same BatchWriteItem request.
• If the total request size exceeds the 1 MB request size (the HTTP payload) limit.
• If any individual item in a batch exceeds the 64 KB item size limit.
Requests
Syntax
// This header is abbreviated. For a sample of a complete header, see DynamoDB Low-Level
 API.
POST / HTTP/1.1
x-amz-target: DynamoDB_20111205.BatchGetItem
content-type: application/x-amz-json-1.0
{
    "RequestItems" :   RequestItems
}
RequestItems
{
     "TableName1" :     [ Request, Request, ... ],
     "TableName2" :     [ Request, Request, ... ],
     ...
}
Request ::=
  PutRequest | DeleteRequest
PutRequest ::=
{
  "PutRequest" : {
     "Item" : {
        "Attribute-Name1" : Attribute-Value,
        "Attribute-Name2" : Attribute-Value,
         ...
     }
  }
}
DeleteRequest ::=
{
   "DeleteRequest" : {
      "Key" : PrimaryKey-Value
   }
}
HashTypePK ::=
{
   "HashKeyElement" : Attribute-Value
}
HashAndRangeTypePK
{
   "HashKeyElement" : Attribute-Value,
   "RangeKeyElement" : Attribute-Value,
}
Numeric ::=
{
   "N": "Number"
}
String ::=
{
     "S": "String"
}
Binary ::=
{
    "B": "Base64 encoded binary data"
}
StringSet ::=
{
   "SS": [ "String1", "String2", ... ]
}
NumberSet ::=
{
   "NS": [ "Number1", "Number2", ... ]
}
BinarySet ::=
{
   "BS": [ "Binary1", "Binary2", ... ]
}
In the request body, the RequestItems JSON object describes the operations that you want to perform.
The operations are grouped by tables. You can use BatchWriteItem to update or delete several items
across multiple tables. For each specific write request, you must identify the type of request (PutItem,
DeleteItem) followed by detail information about the operation.
• For a PutRequest, you provide the item, that is, a list of attributes and their values.
• For a DeleteRequest, you provide the primary key name and value.
Responses
Syntax
The following is the syntax of the JSON body returned in the response.
{
    "Responses" :           ConsumedCapacityUnitsByTable
    "UnprocessedItems" :    RequestItems
}
ConsumedCapacityUnitsByTable
{
    "TableName1" : { "ConsumedCapacityUnits", : NumericValue },
    "TableName2" : { "ConsumedCapacityUnits", : NumericValue },
     ...
}
RequestItems
This syntax is identical to the one described in the JSON syntax in the request.
Special Errors
No errors specific to this operation.
Examples
The following example shows an HTTP POST request and the response of a BatchWriteItem operation.
The request specifies the following operations on the Reply and the Thread tables:
For examples using the AWS SDK, see Working with Items in DynamoDB (p. 326).
Sample Request
// This header is abbreviated. For a sample of a complete header, see DynamoDB Low-Level
 API.
POST / HTTP/1.1
x-amz-target: DynamoDB_20111205.BatchGetItem
content-type: application/x-amz-json-1.0
{
    "RequestItems":{
      "Reply":[
         {
            "PutRequest":{
              "Item":{
                "ReplyDateTime":{
                   "S":"2012-04-03T11:04:47.034Z"
                },
                "Id":{
                   "S":"DynamoDB#DynamoDB Thread 5"
                }
              }
            }
         },
         {
            "DeleteRequest":{
              "Key":{
                "HashKeyElement":{
                   "S":"DynamoDB#DynamoDB Thread 4"
                },
                "RangeKeyElement":{
                   "S":"oops - accidental row"
                }
              }
            }
         }
      ],
      "Thread":[
         {
            "PutRequest":{
              "Item":{
                "ForumName":{
                   "S":"DynamoDB"
                },
                "Subject":{
                   "S":"DynamoDB Thread 5"
                }
              }
            }
         }
      ]
    }
}
Sample Response
The following example response shows a put operation on both the Thread and Reply tables succeeded
and a delete operation on the Reply table failed (for reasons such as throttling that is caused when you
exceed the provisioned throughput on the table). Note the following in the JSON response:
• The Responses object shows one capacity unit was consumed on both the Thread and Reply tables
  as a result of the successful put operation on each of these tables.
• The UnprocessedItems object shows the unsuccessful delete operation on the Reply table. You can
  then issue a new BatchWriteItem call to address these unprocessed requests.
HTTP/1.1 200 OK
x-amzn-RequestId: G8M9ANLOE5QA26AEUHJKJE0ASBVV4KQNSO5AEMVJF66Q9ASUAAJG
Content-Type: application/x-amz-json-1.0
Content-Length: 536
Date: Thu, 05 Apr 2012 18:22:09 GMT
{
    "Responses":{
       "Thread":{
          "ConsumedCapacityUnits":1.0
       },
       "Reply":{
          "ConsumedCapacityUnits":1.0
       }
    },
    "UnprocessedItems":{
       "Reply":[
          {
             "DeleteRequest":{
                 "Key":{
                    "HashKeyElement":{
                       "S":"DynamoDB#DynamoDB Thread 4"
                    },
                    "RangeKeyElement":{
                       "S":"oops - accidental row"
                    }
                 }
             }
          }
       ]
    }
}
CreateTable
    Important
    This section refers to API version 2011-12-05, which is deprecated and
    should not be used for new applications.
    For documentation on the current low-level API, see the Amazon DynamoDB API Reference.
Description
The CreateTable operation adds a new table to your account.
The table name must be unique among those associated with the AWS Account issuing the request,
and the AWS region that receives the request (such as dynamodb.us-west-2.amazonaws.com). Each
DynamoDB endpoint is entirely independent. For example, if you have two tables called "MyTable," one
The CreateTable operation triggers an asynchronous workflow to begin creating the table. DynamoDB
immediately returns the state of the table (CREATING) until the table is in the ACTIVE state. Once the
table is in the ACTIVE state, you can perform data plane operations.
Use the DescribeTables (p. 868) operation to check the status of the table.
Requests
Syntax
{"TableName":"Table1",
    "KeySchema":
        {"HashKeyElement":{"AttributeName":"AttributeName1","AttributeType":"S"},
        "RangeKeyElement":{"AttributeName":"AttributeName2","AttributeType":"N"}},
    "ProvisionedThroughput":{"ReadCapacityUnits":5,"WriteCapacityUnits":10}
}
Type: String
Type: Array
Type: Number
Type: Number
Responses
Syntax
HTTP/1.1 200 OK
x-amzn-RequestId: CSOC7TJPLR0OOKIRLGOHVAICUFVV4KQNSO5AEMVJF66Q9ASUAAJG
content-type: application/x-amz-json-1.0
content-length: 311
Date: Tue, 12 Jul 2011 21:31:03 GMT
{"TableDescription":
    {"CreationDateTime":1.310506263362E9,
    "KeySchema":
        {"HashKeyElement":{"AttributeName":"AttributeName1","AttributeType":"S"},
        "RangeKeyElement":{"AttributeName":"AttributeName2","AttributeType":"N"}},
    "ProvisionedThroughput":{"ReadCapacityUnits":5,"WriteCapacityUnits":10},
    "TableName":"Table1",
    "TableStatus":"CREATING"
    }
}
Name Description
Type: Number
Type: Array
Type: Number
Type: Number
Type: String
Name Description
Type: String
Special Errors
Error Description
Examples
The following example creates a table with a composite primary key containing a string and a number.
For examples using the AWS SDK, see Working with Tables in DynamoDB (p. 291).
Sample Request
{"TableName":"comp-table",
    "KeySchema":
        {"HashKeyElement":{"AttributeName":"user","AttributeType":"S"},
        "RangeKeyElement":{"AttributeName":"time","AttributeType":"N"}},
    "ProvisionedThroughput":{"ReadCapacityUnits":5,"WriteCapacityUnits":10}
}
Sample Response
HTTP/1.1 200 OK
x-amzn-RequestId: CSOC7TJPLR0OOKIRLGOHVAICUFVV4KQNSO5AEMVJF66Q9ASUAAJG
content-type: application/x-amz-json-1.0
content-length: 311
Date: Tue, 12 Jul 2011 21:31:03 GMT
{"TableDescription":
    {"CreationDateTime":1.310506263362E9,
    "KeySchema":
        {"HashKeyElement":{"AttributeName":"user","AttributeType":"S"},
        "RangeKeyElement":{"AttributeName":"time","AttributeType":"N"}},
    "ProvisionedThroughput":{"ReadCapacityUnits":5,"WriteCapacityUnits":10},
    "TableName":"comp-table",
    "TableStatus":"CREATING"
    }
}
Related Actions
• DescribeTables (p. 868)
• DeleteTable (p. 865)
DeleteItem
    Important
    This section refers to API version 2011-12-05, which is deprecated and
    should not be used for new applications.
    For documentation on the current low-level API, see the Amazon DynamoDB API Reference.
Description
Deletes a single item in a table by primary key. You can perform a conditional delete operation that
deletes the item if it exists, or if it has an expected attribute value.
    Note
    If you specify DeleteItem without attributes or values, all the attributes for the item are
    deleted.
    Unless you specify conditions, the DeleteItem is an idempotent operation; running it multiple
    times on the same item or attribute does not result in an error response.
    Conditional deletes are useful for only deleting items and attributes if specific conditions are
    met. If the conditions are met, DynamoDB performs the delete. Otherwise, the item is not
    deleted.
    You can perform the expected conditional check on one attribute per operation.
Requests
Syntax
{"TableName":"Table1",
    "Key":
        {"HashKeyElement":{"S":"AttributeValue1"},"RangeKeyElement":
{"N":"AttributeValue2"}},
    "Expected":{"AttributeName3":{"Value":{"S":"AttributeValue3"}}},
    "ReturnValues":"ALL_OLD"}
}
Type: String
Type: String
                          "Expected" :
                           {"Color":{"Exists":false}}
                          "Expected" :
                           {"Color":{"Exists":true},
                          {"Value":{"S":"Yellow"}}}
                               "Expected" :
                                {"Color":{"Value":
                               {"S":"Yellow"}}}
                                   Note
                                   If you specify
                                   {"Exists":true}
                                   without an attribute
                                   value to check,
                                   DynamoDB returns an
                                   error.
Type: String
Responses
Syntax
HTTP/1.1 200 OK
x-amzn-RequestId: CSOC7TJPLR0OOKIRLGOHVAICUFVV4KQNSO5AEMVJF66Q9ASUAAJG
content-type: application/x-amz-json-1.0
content-length: 353
Date: Tue, 12 Jul 2011 21:31:03 GMT
{"Attributes":
    {"AttributeName3":{"SS":["AttributeValue3","AttributeValue4","AttributeValue5"]},
    "AttributeName2":{"S":"AttributeValue2"},
    "AttributeName1":{"N":"AttributeValue1"}
    },
"ConsumedCapacityUnits":1
}
Name Description
Type: Number
Special Errors
Error Description
Examples
Sample Request
{"TableName":"comp-table",
    "Key":
        {"HashKeyElement":{"S":"Mingus"},"RangeKeyElement":{"N":"200"}},
    "Expected":
        {"status":{"Value":{"S":"shopping"}}},
    "ReturnValues":"ALL_OLD"
}
Sample Response
HTTP/1.1 200 OK
x-amzn-RequestId: U9809LI6BBFJA5N2R0TB0P017JVV4KQNSO5AEMVJF66Q9ASUAAJG
content-type: application/x-amz-json-1.0
content-length: 353
Date: Tue, 12 Jul 2011 22:31:23 GMT
{"Attributes":
    {"friends":{"SS":["Dooley","Ben","Daisy"]},
    "status":{"S":"shopping"},
    "time":{"N":"200"},
    "user":{"S":"Mingus"}
    },
"ConsumedCapacityUnits":1
}
Related Actions
• PutItem (p. 875)
DeleteTable
    Important
    This section refers to API version 2011-12-05, which is deprecated and
    should not be used for new applications.
    For documentation on the current low-level API, see the Amazon DynamoDB API Reference.
Description
The DeleteTable operation deletes a table and all of its items. After a DeleteTable request, the
specified table is in the DELETING state until DynamoDB completes the deletion. If the table is in
the ACTIVE state, you can delete it. If a table is in CREATING or UPDATING states, then DynamoDB
returns a ResourceInUseException error. If the specified table does not exist, DynamoDB returns a
ResourceNotFoundException. If table is already in the DELETING state, no error is returned.
    Note
    DynamoDB might continue to accept data plane operation requests, such as GetItem and
    PutItem, on a table in the DELETING state until the table deletion is complete.
Tables are unique among those associated with the AWS Account issuing the request, and the AWS
region that receives the request (such as dynamodb.us-west-1.amazonaws.com). Each DynamoDB
endpoint is entirely independent. For example, if you have two tables called "MyTable," one in
dynamodb.us-west-2.amazonaws.com and one in dynamodb.us-west-1.amazonaws.com, they are
completely independent and do not share any data; deleting one does not delete the other.
Use the DescribeTables (p. 868) operation to check the status of the table.
Requests
Syntax
{"TableName":"Table1"}
Type: String
Responses
Syntax
HTTP/1.1 200 OK
x-amzn-RequestId: 4HONCKIVH1BFUDQ1U68CTG3N27VV4KQNSO5AEMVJF66Q9ASUAAJG
content-type: application/x-amz-json-1.0
content-length: 311
Date: Sun, 14 Aug 2011 22:56:22 GMT
{"TableDescription":
    {"CreationDateTime":1.313362508446E9,
    "KeySchema":
        {"HashKeyElement":{"AttributeName":"user","AttributeType":"S"},
        "RangeKeyElement":{"AttributeName":"time","AttributeType":"N"}},
    "ProvisionedThroughput":{"ReadCapacityUnits":10,"WriteCapacityUnits":10},
    "TableName":"Table1",
    "TableStatus":"DELETING"
    }
}
Name Description
Type: Number
Type: Number
Type: Number
Name Description
Type: String
Type: String
Special Errors
Error Description
Examples
Sample Request
// This header is abbreviated. For a sample of a complete header, see DynamoDB Low-Level
 API.
POST / HTTP/1.1
x-amz-target: DynamoDB_20111205.DeleteTable
content-type: application/x-amz-json-1.0
content-length: 40
{"TableName":"favorite-movies-table"}
Sample Response
HTTP/1.1 200 OK
x-amzn-RequestId: 4HONCKIVH1BFUDQ1U68CTG3N27VV4KQNSO5AEMVJF66Q9ASUAAJG
content-type: application/x-amz-json-1.0
content-length: 160
Date: Sun, 14 Aug 2011 17:20:03 GMT
{"TableDescription":
    {"CreationDateTime":1.313362508446E9,
    "KeySchema":
        {"HashKeyElement":{"AttributeName":"name","AttributeType":"S"}},
    "TableName":"favorite-movies-table",
    "TableStatus":"DELETING"
}
Related Actions
• CreateTable (p. 856)
DescribeTables
    Important
    This section refers to API version 2011-12-05, which is deprecated and
    should not be used for new applications.
    For documentation on the current low-level API, see the Amazon DynamoDB API Reference.
Description
Returns information about the table, including the current status of the table, the primary key schema
and when the table was created. DescribeTable results are eventually consistent. If you use DescribeTable
too early in the process of creating a table, DynamoDB returns a ResourceNotFoundException. If you
use DescribeTable too early in the process of updating a table, the new values might not be immediately
available.
Requests
Syntax
{"TableName":"Table1"}
Type: String
Responses
Syntax
HTTP/1.1 200
x-amzn-RequestId: 8966d095-71e9-11e0-a498-71d736f27375
content-type: application/x-amz-json-1.0
Content-Length: 543
{"Table":
    {"CreationDateTime":1.309988345372E9,
    ItemCount:1,
    "KeySchema":
        {"HashKeyElement":{"AttributeName":"AttributeName1","AttributeType":"S"},
        "RangeKeyElement":{"AttributeName":"AttributeName2","AttributeType":"N"}},
    "ProvisionedThroughput":{"LastIncreaseDateTime": Date, "LastDecreaseDateTime": Date,
 "ReadCapacityUnits":10,"WriteCapacityUnits":10},
    "TableName":"Table1",
    "TableSizeBytes":1,
    "TableStatus":"ACTIVE"
    }
}
Name Description
Type: String
Type: Number
Type: Array
Type: String
Type: Number
Special Errors
No errors are specific to this operation.
Examples
The following examples show an HTTP POST request and response using the DescribeTable operation for
a table named "comp-table". The table has a composite primary key.
Sample Request
// This header is abbreviated.
// For a sample of a complete header, see DynamoDB Low-Level API.
POST / HTTP/1.1
x-amz-target: DynamoDB_20111205.DescribeTable
content-type: application/x-amz-json-1.0
{"TableName":"users"}
Sample Response
HTTP/1.1 200
x-amzn-RequestId: 8966d095-71e9-11e0-a498-71d736f27375
content-type: application/x-amz-json-1.0
content-length: 543
{"Table":
     {"CreationDateTime":1.309988345372E9,
     "ItemCount":23,
     "KeySchema":
         {"HashKeyElement":{"AttributeName":"user","AttributeType":"S"},
         "RangeKeyElement":{"AttributeName":"time","AttributeType":"N"}},
     "ProvisionedThroughput":{"LastIncreaseDateTime": 1.309988345384E9,
  "ReadCapacityUnits":10,"WriteCapacityUnits":10},
     "TableName":"users",
     "TableSizeBytes":949,
     "TableStatus":"ACTIVE"
     }
}
Related Actions
• CreateTable (p. 856)
• DeleteTable (p. 865)
• ListTables (p. 873)
GetItem
    Important
    This section refers to API version 2011-12-05, which is deprecated and
    should not be used for new applications.
    For documentation on the current low-level API, see the Amazon DynamoDB API Reference.
Description
The GetItem operation returns a set of Attributes for an item that matches the primary key. If there
is no matching item, GetItem does not return any data.
The GetItem operation provides an eventually consistent read by default. If eventually consistent reads
are not acceptable for your application, use ConsistentRead. Although this operation might take
longer than a standard read, it always returns the last updated value. For more information, see Read
Consistency (p. 15).
Requests
Syntax
{"TableName":"Table1",
   "Key":
   {"HashKeyElement": {"S":"AttributeValue1"},
   "RangeKeyElement": {"N":"AttributeValue2"}
  },
  "AttributesToGet":["AttributeName3","AttributeName4"],
  "ConsistentRead":Boolean
}
Type: String
Type: Array
Type: Boolean
Responses
Syntax
HTTP/1.1 200
x-amzn-RequestId: 8966d095-71e9-11e0-a498-71d736f27375
content-type: application/x-amz-json-1.0
content-length: 144
{"Item":{
  "AttributeName3":{"S":"AttributeValue3"},
  "AttributeName4":{"N":"AttributeValue4"},
  "AttributeName5":{"B":"dmFsdWU="}
  },
"ConsumedCapacityUnits": 0.5
}
Name Description
Type: Number
Special Errors
No errors specific to this operation.
Examples
For examples using the AWS SDK, see Working with Items in DynamoDB (p. 326).
Sample Request
{"TableName":"comptable",
  "Key":
   {"HashKeyElement":{"S":"Julie"},
   "RangeKeyElement":{"N":"1307654345"}},
  "AttributesToGet":["status","friends"],
  "ConsistentRead":true
}
Sample Response
Notice the ConsumedCapacityUnits value is 1, because the optional parameter ConsistentRead is set
to true. If ConsistentRead is set to false (or not specified) for the same request, the response is
eventually consistent and the ConsumedCapacityUnits value would be 0.5.
HTTP/1.1 200
x-amzn-RequestId: 8966d095-71e9-11e0-a498-71d736f27375
content-type: application/x-amz-json-1.0
content-length: 72
{"Item":
  {"friends":{"SS":["Lynda, Aaron"]},
  "status":{"S":"online"}
  },
"ConsumedCapacityUnits": 1
}
ListTables
    Important
    This section refers to API version 2011-12-05, which is deprecated and
    should not be used for new applications.
    For documentation on the current low-level API, see the Amazon DynamoDB API Reference.
Description
Returns an array of all the tables associated with the current account and endpoint.
Each DynamoDB endpoint is entirely independent. For example, if you have two tables called "MyTable,"
one in dynamodb.us-west-2.amazonaws.com and one in dynamodb.us-east-1.amazonaws.com, they
are completely independent and do not share any data. The ListTables operation returns all of the table
names associated with the account making the request, for the endpoint that receives the request.
Requests
Syntax
{"ExclusiveStartTableName":"Table1","Limit":3}
The ListTables operation, by default, requests all of the table names associated with the account making
the request, for the endpoint that receives the request.
Type: Integer
Type: String
Responses
Syntax
HTTP/1.1 200 OK
x-amzn-RequestId: S1LEK2DPQP8OJNHVHL8OU2M7KRVV4KQNSO5AEMVJF66Q9ASUAAJG
content-type: application/x-amz-json-1.0
content-length: 81
Date: Fri, 21 Oct 2011 20:35:38 GMT
{"TableNames":["Table1","Table2","Table3"], "LastEvaluatedTableName":"Table3"}
Name Description
Type: Array
Type: String
Special Errors
No errors are specific to this operation.
Examples
The following examples show an HTTP POST request and response using the ListTables operation.
Sample Request
{"ExclusiveStartTableName":"comp2","Limit":3}
Sample Response
HTTP/1.1 200 OK
x-amzn-RequestId: S1LEK2DPQP8OJNHVHL8OU2M7KRVV4KQNSO5AEMVJF66Q9ASUAAJG
content-type: application/x-amz-json-1.0
content-length: 81
Date: Fri, 21 Oct 2011 20:35:38 GMT
{"LastEvaluatedTableName":"comp5","TableNames":["comp3","comp4","comp5"]}
Related Actions
• DescribeTables (p. 868)
• CreateTable (p. 856)
• DeleteTable (p. 865)
PutItem
    Important
    This section refers to API version 2011-12-05, which is deprecated and
    should not be used for new applications.
    For documentation on the current low-level API, see the Amazon DynamoDB API Reference.
Description
Creates a new item, or replaces an old item with a new item (including all the attributes). If an item
already exists in the specified table with the same primary key, the new item completely replaces the
existing item. You can perform a conditional put (insert a new item if one with the specified primary key
doesn't exist), or replace an existing item if it has certain attribute values.
Attribute values may not be null; string and binary type attributes must have lengths greater than
zero; and set type attributes must not be empty. Requests with empty values will be rejected with a
ValidationException.
    Note
    To ensure that a new item does not replace an existing item, use a conditional put operation
    with Exists set to false for the primary key attribute, or attributes.
For more information about using PutItem, see Working with Items in DynamoDB (p. 326).
Requests
Syntax
{"TableName":"Table1",
    "Item":{
        "AttributeName1":{"S":"AttributeValue1"},
        "AttributeName2":{"N":"AttributeValue2"},
      "AttributeName5":{"B":"dmFsdWU="}
  },
  "Expected":{"AttributeName3":{"Value": {"S":"AttributeValue"}, "Exists":Boolean}},
  "ReturnValues":"ReturnValuesConstant"}
Type: String
Type: String
                             "Expected" :
                              {"Color":{"Exists":false}}
                                 "Expected" :
                                  {"Color":{"Exists":true,
                                 {"Value":{"S":"Yellow"}}}
                                 "Expected" :
                                  {"Color":{"Value":
                                 {"S":"Yellow"}}}
                                     Note
                                     If you specify
                                     {"Exists":true}
                                     without an attribute
                                     value to check,
                                     DynamoDB returns an
                                     error.
Type: String
Responses
Syntax
The following syntax example assumes the request specified a ReturnValues parameter of ALL_OLD;
otherwise, the response has only the ConsumedCapacityUnits element.
HTTP/1.1 200
x-amzn-RequestId: 8966d095-71e9-11e0-a498-71d736f27375
content-type: application/x-amz-json-1.0
content-length: 85
{"Attributes":
  {"AttributeName3":{"S":"AttributeValue3"},
  "AttributeName2":{"SS":"AttributeValue2"},
  "AttributeName1":{"SS":"AttributeValue1"},
  },
"ConsumedCapacityUnits":1
}
Name Description
Type: Number
Special Errors
Error                                             Description
Examples
For examples using the AWS SDK, see Working with Items in DynamoDB (p. 326).
Sample Request
// This header is abbreviated. For a sample of a complete header, see DynamoDB Low-Level
 API.
POST / HTTP/1.1
x-amz-target: DynamoDB_20111205.PutItem
content-type: application/x-amz-json-1.0
{"TableName":"comp5",
 "Item":
  {"time":{"N":"300"},
  "feeling":{"S":"not surprised"},
  "user":{"S":"Riley"}
  },
 "Expected":
  {"feeling":{"Value":{"S":"surprised"},"Exists":true}}
 "ReturnValues":"ALL_OLD"
Sample Response
HTTP/1.1 200
x-amzn-RequestId: 8952fa74-71e9-11e0-a498-71d736f27375
content-type: application/x-amz-json-1.0
content-length: 84
{"Attributes":
  {"feeling":{"S":"surprised"},
  "time":{"N":"300"},
  "user":{"S":"Riley"}},
"ConsumedCapacityUnits":1
}
Related Actions
• UpdateItem (p. 898)
• DeleteItem (p. 861)
• GetItem (p. 870)
• BatchGetItem (p. 847)
Query
    Important
    This section refers to API version 2011-12-05, which is deprecated and
    should not be used for new applications.
    For documentation on the current low-level API, see the Amazon DynamoDB API Reference.
Description
A Query operation gets the values of one or more items and their attributes by primary key (Query is
only available for hash-and-range primary key tables). You must provide a specific HashKeyValue, and
can narrow the scope of the query using comparison operators on the RangeKeyValue of the primary
key. Use the ScanIndexForward parameter to get results in forward or reverse order by range key.
Queries that do not return results consume the minimum read capacity units according to the type of
read.
    Note
    If the total number of items meeting the query parameters exceeds the 1MB limit, the query
    stops and results are returned to the user with a LastEvaluatedKey to continue the query in a
    subsequent operation. Unlike a Scan operation, a Query operation never returns an empty result
    set and a LastEvaluatedKey. The LastEvaluatedKey is only provided if the results exceed
    1MB, or if you have used the Limit parameter.
    The result can be set for a consistent read using the ConsistentRead parameter.
Requests
Syntax
content-type: application/x-amz-json-1.0
{"TableName":"Table1",
  "Limit":2,
  "ConsistentRead":true,
  "HashKeyValue":{"S":"AttributeValue1":},
  "RangeKeyCondition": {"AttributeValueList":
[{"N":"AttributeValue2"}],"ComparisonOperator":"GT"}
  "ScanIndexForward":true,
  "ExclusiveStartKey":{
   "HashKeyElement":{"S":"AttributeName1"},
   "RangeKeyElement":{"N":"AttributeName2"}
  },
     "AttributesToGet":["AttributeName1", "AttributeName2", "AttributeName3"]},
}
Type: String
Type: Array
Type: Number
Type: Boolean
Type: Boolean
Type: Map
                           Type: A map of
                           AttributeValue to a
                           ComparisonOperator.
EQ : Equal.
LT : Less than.
GT : Greater than.
             For BEGINS_WITH,
             AttributeValueList
             can contain only one
             AttributeValue of type String
             or Binary (not a Number or a
             set). The target attribute of the
             comparison must be a String or
             Binary (not a Number or a set).
             For BETWEEN,
             AttributeValueList must
             contain two AttributeValue
             elements of the same type,
             either String, Number, or Binary
             (not a set). A target attribute
             matches if the target value
             is greater than, or equal to,
             the first element and less
             than, or equal to, the second
             element. If an item contains
             an AttributeValue of a
             different type than the one
             specified in the request, the
             value does not match. For
             example, {"S":"6"} does not
             compare to {"N":"6"}. Also,
             {"N":"6"} does not compare
             to {"NS":["6", "2", "1"]}.
Type: Boolean
                               Type: HashKeyElement,
                               or HashKeyElement and
                               RangeKeyElement for a
                               composite primary key.
Responses
Syntax
HTTP/1.1 200
x-amzn-RequestId: 8966d095-71e9-11e0-a498-71d736f27375
content-type: application/x-amz-json-1.0
content-length: 308
{"Count":2,"Items":[{
    "AttributeName1":{"S":"AttributeValue1"},
    "AttributeName2":{"N":"AttributeValue2"},
    "AttributeName3":{"S":"AttributeValue3"}
    },{
    "AttributeName1":{"S":"AttributeValue3"},
    "AttributeName2":{"N":"AttributeValue4"},
    "AttributeName3":{"S":"AttributeValue3"},
    "AttributeName5":{"B":"dmFsdWU="}
}],
    "LastEvaluatedKey":{"HashKeyElement":{"AttributeValue3":"S"},
                        "RangeKeyElement":{"AttributeValue4":"N"}
     },
     "ConsumedCapacityUnits":1
}
Name Description
Type: Number
Type: Number
Special Errors
Error Description
Examples
For examples using the AWS SDK, see Working with Queries (p. 408).
Sample Request
// This header is abbreviated. For a sample of a complete header, see DynamoDB Low-Level
 API.
POST / HTTP/1.1
x-amz-target: DynamoDB_20111205.Query
content-type: application/x-amz-json-1.0
{"TableName":"1-hash-rangetable",
  "Limit":2,
  "HashKeyValue":{"S":"John"},
  "ScanIndexForward":false,
  "ExclusiveStartKey":{
    "HashKeyElement":{"S":"John"},
    "RangeKeyElement":{"S":"The Matrix"}
  }
}
Sample Response
HTTP/1.1 200
x-amzn-RequestId: 3647e778-71eb-11e0-a498-71d736f27375
content-type: application/x-amz-json-1.0
content-length: 308
{"Count":2,"Items":[{
  "fans":{"SS":["Jody","Jake"]},
  "name":{"S":"John"},
  "rating":{"S":"***"},
  "title":{"S":"The End"}
  },{
  "fans":{"SS":["Jody","Jake"]},
  "name":{"S":"John"},
  "rating":{"S":"***"},
  "title":{"S":"The Beatles"}
  }],
  "LastEvaluatedKey":{"HashKeyElement":{"S":"John"},"RangeKeyElement":{"S":"The Beatles"}},
"ConsumedCapacityUnits":1
}
Sample Request
// This header is abbreviated. For a sample of a complete header, see DynamoDB Low-Level
 API.
POST / HTTP/1.1
x-amz-target: DynamoDB_20111205.Query
content-type: application/x-amz-json-1.0
{"TableName":"1-hash-rangetable",
 "Limit":2,
 "HashKeyValue":{"S":"Airplane"},
 "RangeKeyCondition":{"AttributeValueList":[{"N":"1980"}],"ComparisonOperator":"EQ"},
 "ScanIndexForward":false}
Sample Response
HTTP/1.1 200
x-amzn-RequestId: 8b9ee1ad-774c-11e0-9172-d954e38f553a
content-type: application/x-amz-json-1.0
content-length: 119
{"Count":1,"Items":[{
 "fans":{"SS":["Dave","Aaron"]},
 "name":{"S":"Airplane"},
 "rating":{"S":"***"},
  "year":{"N":"1980"}
  }],
"ConsumedCapacityUnits":1
}
Related Actions
• Scan (p. 888)
Scan
    Important
    This section refers to API version 2011-12-05, which is deprecated and
    should not be used for new applications.
    For documentation on the current low-level API, see the Amazon DynamoDB API Reference.
Description
The Scan operation returns one or more items and its attributes by performing a full scan of a table.
Provide a ScanFilter to get more specific results.
    Note
    If the total number of scanned items exceeds the 1MB limit, the scan stops and results
    are returned to the user with a LastEvaluatedKey to continue the scan in a subsequent
    operation. The results also include the number of items exceeding the limit. A scan can result in
    no table data meeting the filter criteria.
    The result set is eventually consistent.
Requests
Syntax
{"TableName":"Table1",
    "Limit": 2,
    "ScanFilter":{
        "AttributeName1":{"AttributeValueList":
[{"S":"AttributeValue"}],"ComparisonOperator":"EQ"}
    },
    "ExclusiveStartKey":{
        "HashKeyElement":{"S":"AttributeName1"},
        "RangeKeyElement":{"N":"AttributeName2"}
    },
    "AttributesToGet":["AttributeName1", "AttributeName2", "AttributeName3"]},
}
Type: String
Type: Array
Type: Number
Type: Boolean
                               Type: A map of
                               AttributeValue to a
                               Condition.
EQ : Equal.
NE : Not Equal.
LT : Less than.
GT : Greater than.
             For CONTAINS,
             AttributeValueList
             can contain only one
             AttributeValue of type
             String, Number, or Binary (not a
             set). If the target attribute of the
             comparison is a String, then the
             operation checks for a substring
             match. If the target attribute
             of the comparison is Binary,
             then the operation looks for a
             subsequence of the target that
             matches the input. If the target
             attribute of the comparison is a
             set ("SS", "NS", or "BS"), then the
             operation checks for a member
             of the set (not as a substring).
             For NOT_CONTAINS,
             AttributeValueList
             can contain only one
             AttributeValue of type
             String, Number, or Binary (not
             a set). If the target attribute of
             the comparison is a String, then
             the operation checks for the
             absence of a substring match.
             If the target attribute of the
             comparison is Binary, then
             the operation checks for the
             absence of a subsequence of the
             target that matches the input.
             If the target attribute of the
             comparison is a set ("SS", "NS",
             or "BS"), then the operation
             checks for the absence of a
             member of the set (not as a
             substring).
             For BEGINS_WITH,
             AttributeValueList
             can contain only one
             AttributeValue of type String
             or Binary (not a Number or a
             set). The target attribute of the
             comparison must be a String or
             Binary (not a Number or a set).
             For BETWEEN,
             AttributeValueList must
             contain two AttributeValue
             elements of the same type,
             either String, Number, or Binary
             (not a set). A target attribute
             matches if the target value
             is greater than, or equal to,
             the first element and less
             than, or equal to, the second
             element. If an item contains
             an AttributeValue of a
             different type than the one
             specified in the request, the
             value does not match. For
             example, {"S":"6"} does not
             compare to {"N":"6"}. Also,
             {"N":"6"} does not compare
             to {"NS":["6", "2", "1"]}.
                               Type: HashKeyElement,
                               or HashKeyElement and
                               RangeKeyElement for a
                               composite primary key.
Responses
Syntax
HTTP/1.1 200
x-amzn-RequestId: 8966d095-71e9-11e0-a498-71d736f27375
content-type: application/x-amz-json-1.0
content-length: 229
{"Count":2,"Items":[{
    "AttributeName1":{"S":"AttributeValue1"},
    "AttributeName2":{"S":"AttributeValue2"},
    "AttributeName3":{"S":"AttributeValue3"}
    },{
    "AttributeName1":{"S":"AttributeValue4"},
    "AttributeName2":{"S":"AttributeValue5"},
    "AttributeName3":{"S":"AttributeValue6"},
    "AttributeName5":{"B":"dmFsdWU="}
    }],
    "LastEvaluatedKey":
        {"HashKeyElement":{"S":"AttributeName1"},
        "RangeKeyElement":{"N":"AttributeName2"},
    "ConsumedCapacityUnits":1,
    "ScannedCount":2}
}
Name Description
Name                                               Description
                                                   Type: Number
Type: Number
Type: Number
Special Errors
Error Description
Examples
For examples using the AWS SDK, see Working with Scans (p. 425).
Sample Request
// This header is abbreviated. For a sample of a complete header, see DynamoDB Low-Level
 API.
POST / HTTP/1.1
x-amz-target: DynamoDB_20111205.Scan
content-type: application/x-amz-json-1.0
{"TableName":"1-hash-rangetable","ScanFilter":{}}
Sample Response
HTTP/1.1 200
x-amzn-RequestId: 4e8a5fa9-71e7-11e0-a498-71d736f27375
content-type: application/x-amz-json-1.0
content-length: 465
{"Count":4,"Items":[{
 "date":{"S":"1980"},
 "fans":{"SS":["Dave","Aaron"]},
 "name":{"S":"Airplane"},
 "rating":{"S":"***"}
 },{
 "date":{"S":"1999"},
 "fans":{"SS":["Ziggy","Laura","Dean"]},
 "name":{"S":"Matrix"},
 "rating":{"S":"*****"}
 },{
 "date":{"S":"1976"},
 "fans":{"SS":["Riley"]},"
 name":{"S":"The Shaggy D.A."},
 "rating":{"S":"**"}
 },{
 "date":{"S":"1985"},
 "fans":{"SS":["Fox","Lloyd"]},
 "name":{"S":"Back To The Future"},
 "rating":{"S":"****"}
 }],
     "ConsumedCapacityUnits":0.5
 "ScannedCount":4}
Sample Request
// This header is abbreviated. For a sample of a complete header, see DynamoDB Low-Level
 API.
POST / HTTP/1.1
x-amz-target: DynamoDB_20111205.Scan
content-type: application/x-amz-json-1.0
content-length: 125
{"TableName":"comp5",
  "ScanFilter":
    {"time":
     {"AttributeValueList":[{"N":"400"}],
     "ComparisonOperator":"GT"}
  }
}
Sample Response
HTTP/1.1 200 OK
x-amzn-RequestId: PD1CQK9QCTERLTJP20VALJ60TRVV4KQNSO5AEMVJF66Q9ASUAAJG
content-type: application/x-amz-json-1.0
content-length: 262
Date: Mon, 15 Aug 2011 16:52:02 GMT
{"Count":2,
 "Items":[
  {"friends":{"SS":["Dave","Ziggy","Barrie"]},
  "status":{"S":"chatting"},
  "time":{"N":"2000"},
  "user":{"S":"Casey"}},
  {"friends":{"SS":["Dave","Ziggy","Barrie"]},
  "status":{"S":"chatting"},
  "time":{"N":"2000"},
  "user":{"S":"Fredy"}
  }],
"ConsumedCapacityUnits":0.5
"ScannedCount":4
Sample Request
// This header is abbreviated. For a sample of a complete header, see DynamoDB Low-Level
 API.
POST / HTTP/1.1
x-amz-target: DynamoDB_20111205.Scan
content-type: application/x-amz-json-1.0
{"TableName":"comp5",
  "Limit":2,
  "ScanFilter":
   {"time":
     {"AttributeValueList":[{"N":"400"}],
     "ComparisonOperator":"GT"}
  },
  "ExclusiveStartKey":
   {"HashKeyElement":{"S":"Fredy"},"RangeKeyElement":{"N":"2000"}}
}
Sample Response
HTTP/1.1 200 OK
x-amzn-RequestId: PD1CQK9QCTERLTJP20VALJ60TRVV4KQNSO5AEMVJF66Q9ASUAAJG
content-type: application/x-amz-json-1.0
content-length: 232
Date: Mon, 15 Aug 2011 16:52:02 GMT
{"Count":1,
  "Items":[
   {"friends":{"SS":["Jane","James","John"]},
   "status":{"S":"exercising"},
   "time":{"N":"2200"},
   "user":{"S":"Roger"}}
  ],
  "LastEvaluatedKey":{"HashKeyElement":{"S":"Riley"},"RangeKeyElement":{"N":"250"}},
"ConsumedCapacityUnits":0.5
"ScannedCount":2
}
Related Actions
• Query (p. 879)
• BatchGetItem (p. 847)
UpdateItem
    Important
    This section refers to API version 2011-12-05, which is deprecated and
    should not be used for new applications.
    For documentation on the current low-level API, see the Amazon DynamoDB API Reference.
Description
Edits an existing item's attributes. You can perform a conditional update (insert a new attribute name-
value pair if it doesn't exist, or replace an existing name-value pair if it has certain expected attribute
values).
    Note
    You cannot update the primary key attributes using UpdateItem. Instead, delete the item and
    use PutItem to create a new item with new attributes.
The UpdateItem operation includes an Action parameter, which defines how to perform the update.
You can put, delete, or add attribute values.
Attribute values may not be null; string and binary type attributes must have lengths greater than
zero; and set type attributes must not be empty. Requests with empty values will be rejected with a
ValidationException.
• PUT— Adds the specified attribute. If the attribute exists, it is replaced by the new value.
• DELETE— If no value is specified, this removes the attribute and its value. If a set of values is specified,
  then the values in the specified set are removed from the old set. So if the attribute value contains
  [a,b,c] and the delete action contains [a,c], then the final attribute value is [b]. The type of the
  specified value must match the existing value type. Specifying an empty set is not valid.
• ADD— Only use the add action for numbers or if the target attribute is a set (including string sets).
  ADD does not work if the target attribute is a single string value or a scalar binary value. The specified
  value is added to a numeric value (incrementing or decrementing the existing numeric value) or added
  as an additional value in a string set. If a set of values is specified, the values are added to the existing
  set. For example if the original set is [1,2] and supplied value is [3], then after the add operation the
  set is [1,2,3], not [4,5]. An error occurs if an Add action is specified for a set attribute and the attribute
  type specified does not match the existing set type.
If you use ADD for an attribute that does not exist, the attribute and its values are added to the item.
• PUT— Creates a new item with specified primary key. Then adds the specified attribute.
• DELETE— Nothing happens.
• ADD— Creates an item with supplied primary key and number (or set of numbers) for the attribute
  value. Not valid for a string or a binary type.
    Note
    If you use ADD to increment or decrement a number value for an item that doesn't exist before
    the update, DynamoDB uses 0 as the initial value. Also, if you update an item using ADD to
    increment or decrement a number value for an attribute that doesn't exist before the update
    (but the item does) DynamoDB uses 0 as the initial value. For example, you use ADD to add +3 to
    an attribute that did not exist before the update. DynamoDB uses 0 for the initial value, and the
    value after the update is 3.
For more information about using this operation, see Working with Items in DynamoDB (p. 326).
Requests
Syntax
{"TableName":"Table1",
    "Key":
        {"HashKeyElement":{"S":"AttributeValue1"},
        "RangeKeyElement":{"N":"AttributeValue2"}},
    "AttributeUpdates":{"AttributeName3":{"Value":
{"S":"AttributeValue3_New"},"Action":"PUT"}},
    "Expected":{"AttributeName3":{"Value":{"S":"AttributeValue3_Current"}}},
    "ReturnValues":"ReturnValuesConstant"
}
Type: String
Type: String
Default: PUT
Type: String
                          "Expected" :
                           {"Color":{"Exists":false}}
                          "Expected" :
                           {"Color":{"Exists":true},
                          {"Value":{"S":"Yellow"}}}
                          "Expected" :
                           {"Color":{"Value":
                          {"S":"Yellow"}}}
                              Note
                              If you specify
                              {"Exists":true}
                              without an attribute
                              value to check,
                              DynamoDB returns an
                              error.
Type: String
Responses
Syntax
The following syntax example assumes the request specified a ReturnValues parameter of ALL_OLD;
otherwise, the response has only the ConsumedCapacityUnits element.
HTTP/1.1 200
x-amzn-RequestId: 8966d095-71e9-11e0-a498-71d736f27375
content-type: application/x-amz-json-1.0
content-length: 140
{"Attributes":{
  "AttributeName1":{"S":"AttributeValue1"},
  "AttributeName2":{"S":"AttributeValue2"},
  "AttributeName3":{"S":"AttributeValue3"},
  "AttributeName5":{"B":"dmFsdWU="}
  },
"ConsumedCapacityUnits":1
}
Name Description
Name                                              Description
                                                  applied toward your provisioned throughput. For
                                                  more information see Throughput Settings for
                                                  Reads and Writes (p. 294).
Type: Number
Special Errors
Error Description
Examples
For examples using the AWS SDK, see Working with Items in DynamoDB (p. 326).
Sample Request
// This header is abbreviated. For a sample of a complete header, see DynamoDB Low-Level
 API.
POST / HTTP/1.1
x-amz-target: DynamoDB_20111205.UpdateItem
content-type: application/x-amz-json-1.0
{"TableName":"comp5",
    "Key":
        {"HashKeyElement":{"S":"Julie"},"RangeKeyElement":{"N":"1307654350"}},
    "AttributeUpdates":
        {"status":{"Value":{"S":"online"},
        "Action":"PUT"}},
    "Expected":{"status":{"Value":{"S":"offline"}}},
    "ReturnValues":"ALL_NEW"
}
Sample Response
HTTP/1.1 200 OK
x-amzn-RequestId: 5IMHO7F01Q9P7Q6QMKMMI3R3QRVV4KQNSO5AEMVJF66Q9ASUAAJG
content-type: application/x-amz-json-1.0
content-length: 121
Date: Fri, 26 Aug 2011 21:05:00 GMT
{"Attributes":
    {"friends":{"SS":["Lynda, Aaron"]},
    "status":{"S":"online"},
    "time":{"N":"1307654350"},
    "user":{"S":"Julie"}},
"ConsumedCapacityUnits":1
}
Related Actions
• PutItem (p. 875)
• DeleteItem (p. 861)
UpdateTable
    Important
    This section refers to API version 2011-12-05, which is deprecated and
    should not be used for new applications.
    For documentation on the current low-level API, see the Amazon DynamoDB API Reference.
Description
Updates the provisioned throughput for the given table. Setting the throughput for a table helps
you manage performance and is part of the provisioned throughput feature of DynamoDB. For more
information, see Throughput Settings for Reads and Writes (p. 294).
The provisioned throughput values can be upgraded or downgraded based on the maximums and
minimums listed in Limits in DynamoDB (p. 769).
The table must be in the ACTIVE state for this operation to succeed. UpdateTable is an asynchronous
operation; while executing the operation, the table is in the UPDATING state. While the table is in the
UPDATING state, the table still has the provisioned throughput from before the call. The new provisioned
throughput setting is in effect only when the table returns to the ACTIVE state after the UpdateTable
operation.
Requests
Syntax
{"TableName":"Table1",
    "ProvisionedThroughput":{"ReadCapacityUnits":5,"WriteCapacityUnits":15}
}
Type: String
Type: Array
Type: Number
Type: Number
Responses
Syntax
HTTP/1.1 200 OK
x-amzn-RequestId: CSOC7TJPLR0OOKIRLGOHVAICUFVV4KQNSO5AEMVJF66Q9ASUAAJG
Content-Type: application/json
Content-Length: 311
Date: Tue, 12 Jul 2011 21:31:03 GMT
{"TableDescription":
    {"CreationDateTime":1.321657838135E9,
    "KeySchema":
        {"HashKeyElement":{"AttributeName":"AttributeValue1","AttributeType":"S"},
        "RangeKeyElement":{"AttributeName":"AttributeValue2","AttributeType":"N"}},
    "ProvisionedThroughput":
        {"LastDecreaseDateTime":1.321661704489E9,
        "LastIncreaseDateTime":1.321663607695E9,
        "ReadCapacityUnits":5,
        "WriteCapacityUnits":10},
    "TableName":"Table1",
    "TableStatus":"UPDATING"}}
Name Description
Name                                           Description
                                               Type: Number
Type: Array
Type: String
Type: String
Special Errors
Error Description
Examples
Sample Request
content-type: application/x-amz-json-1.0
{"TableName":"comp1",
    "ProvisionedThroughput":{"ReadCapacityUnits":5,"WriteCapacityUnits":15}
}
Sample Response
HTTP/1.1 200 OK
content-type: application/x-amz-json-1.0
content-length: 390
Date: Sat, 19 Nov 2011 00:46:47 GMT
{"TableDescription":
    {"CreationDateTime":1.321657838135E9,
    "KeySchema":
        {"HashKeyElement":{"AttributeName":"user","AttributeType":"S"},
        "RangeKeyElement":{"AttributeName":"time","AttributeType":"N"}},
    "ProvisionedThroughput":
        {"LastDecreaseDateTime":1.321661704489E9,
        "LastIncreaseDateTime":1.321663607695E9,
        "ReadCapacityUnits":5,
        "WriteCapacityUnits":10},
    "TableName":"comp1",
    "TableStatus":"UPDATING"}
}
Related Actions
• CreateTable (p. 856)
• DescribeTables (p. 868)
• DeleteTable (p. 865)
   Node.js support for DAX           Node.js developers can leverage    October 5, 2017
                                     Amazon DynamoDB Accelerator
                                     (DAX), using the DAX client for
                                     Node.js. For more information,
                                     see In-Memory Acceleration with
                                     DAX (p. 571).
   VPC Endpoints for DynamoDB        DynamoDB endpoints allow           August 16, 2017
                                     Amazon EC2 instances in
                                     your Amazon VPC to access
                                     DynamoDB, without exposure
                                     to the public Internet. Network
                                     traffic between your VPC
                                     and DynamoDB does not
                                     leave the Amazon network.
                                     For more information, see
                                     Amazon VPC Endpoints for
                                     DynamoDB (p. 724).
   Auto Scaling for DynamoDB         DynamoDB auto scaling              June 14, 2017
                                     eliminates the need for
                                     manually defining or adjust
                                     provisioned throughput settings.
                                     Instead, DynamoDB auto
                                     scaling dynamically adjusts
                                     read and write capacity in
                                     response to actual traffic
                                     patterns. This allows a table
                                     or a global secondary index to
                                     increase its provisioned read
                                     and write capacity to handle
                                     sudden increases in traffic,
                                     without throttling. When the
                                     workload decreases, DynamoDB
                                     auto scaling decreases
                                     the provisioned capacity.
                                     For more information, see
                                     Managing Throughput Capacity
                                     Automatically with DynamoDB
                                     Auto Scaling (p. 299).
DynamoDB now supports Cost       You can now add tags to            Jan 19, 2017
Allocation Tags                  your Amazon DynamoDB
                                 tables for improved usage
                                 categorization and more
                                 granular cost reporting. For
                                 more information, see Tagging
                                 for DynamoDB (p. 313).
DynamoDB Console Update and   The DynamoDB management             November 12, 2015
New Terminology for Primary   console has been redesigned to
Key Attributes                be more intuitive and easy to
                              use. As part of this update, we
                              are introducing new terminology
                              for primary key attributes:
Amazon DynamoDB Storage       The DynamoDB Storage Backend August 20, 2015
Backend for Titan             for Titan is a storage backend
                              for the Titan graph database
                              implemented on top of Amazon
                              DynamoDB. When using the
                              DynamoDB Storage Backend for
                              Titan, your data benefits from
                              the protection of DynamoDB,
                              which runs across Amazon’s
                              high-availability data centers.
                              The plugin is available for Titan
                              version 0.4.4 (primarily for
                              compatibility with existing
                              applications) and Titan version
                              0.5.4 (recommended for
                              new applications). Like other
                              storage backends for Titan, this
                              plugin supports the Tinkerpop
                              stack (versions 2.4 and 2.5),
                              including the Blueprints API
                              and the Gremlin shell. For
                              more information, see Amazon
                              DynamoDB Storage Backend for
                              Titan (p. 820).
                                   DynamoDB cross-region
                                   replication is a client-side
                                   solution for maintaining
                                   identical copies of DynamoDB
                                   tables across different AWS
                                   regions, in near real time. You
                                   can use cross region replication
                                   to back up DynamoDB tables,
                                   or to provide low-latency
                                   access to data where users are
                                   geographically distributed. For
                                   more information, see Cross-
                                   Region Replication (p. 537).
AWS CloudTrail support for         DynamoDB is now integrated         May 28, 2015
Amazon DynamoDB                    with CloudTrail. CloudTrail
                                   captures API calls made from
                                   the DynamoDB console or
                                   from the DynamoDB API and
                                   tracks them in log files. For
                                   more information, see Logging
                                   DynamoDB Operations by Using
                                   AWS CloudTrail (p. 696) and the
                                   AWS CloudTrail User Guide.
Improved support for Query     This release adds a new           April 27, 2015
expressions                    KeyConditionExpression
                               parameter to the Query API.
                               A Query reads items from
                               a table or an index using
                               primary key values. The
                               KeyConditionExpression
                               parameter is a string that
                               identifies primary key
                               names, and conditions to be
                               applied to the key values;
                               the Query retrieves only
                               those items that satisfy the
                               expression. The syntax of
                               KeyConditionExpression
                               is similar to that of other
                               expression parameters in
                               DynamoDB, and allows you to
                               define substitution variables
                               for names and values within
                               the expression. For more
                               information, see Working with
                               Queries (p. 408).
Scan API for secondary indexes   In DynamoDB, a Scan operation        February 10, 2015
                                 reads all of the items in a table,
                                 applies user-defined filtering
                                 criteria, and returns the selected
                                 data items to the application.
                                 This same capability is now
                                 available for secondary indexes
                                 too. To scan a local secondary
                                 index or a global secondary
                                 index, you specify the index
                                 name and the name of its
                                 parent table. By default, an
                                 index Scan returns all of the
                                 data in the index; you can use
                                 a filter expression to narrow
                                 the results that are returned
                                 to the application. For more
                                 information, see Working with
                                 Scans (p. 425).
Online operations for global     Online indexing lets you add         January 27, 2015
secondary indexes                or remove global secondary
                                 indexes on existing tables. With
                                 online indexing, you do not
                                 need to define all of a table's
                                 indexes when you create a table;
                                 instead, you can add a new
                                 index at any time. Similarly, if
                                 you decide you no longer need
                                 an index, you can remove it
                                 at any time. Online indexing
                                 operations are non-blocking, so
                                 that the table remains available
                                 for read and write activity while
                                 indexes are being added or
                                 removed. For more information,
                                 see Managing Global Secondary
                                 Indexes (p. 453).
Document model support with   DynamoDB allows you to store       October 7, 2014
JSON                          and retrieve documents with
                              full support for document
                              models. New data types are
                              fully compatible with the
                              JSON standard and allow you
                              to nest document elements
                              within one another. You can
                              use document path dereference
                              operators to read and write
                              individual elements, without
                              having to retrieve the entire
                              document. This release also
                              introduces new expression
                              parameters for specifying
                              projections, conditions and
                              update actions when reading
                              or writing data items. To
                              learn more about document
                              model support with JSON,
                              see Data Types (p. 12) and Using
                              Expressions in
                              DynamoDB (p. 337).
Data export and import using    The DynamoDB console has          March 6, 2014
the AWS Management Console      been enhanced to simplify
                                exports and imports of data
                                in DynamoDB tables. With
                                just a few clicks, you can set
                                up an AWS Data Pipeline to
                                orchestrate the workflow, and
                                an Amazon Elastic MapReduce
                                cluster to copy data from
                                DynamoDB tables to an Amazon
                                S3 bucket, or vice-versa. You can
                                perform an export or import
                                one time only, or set up a
                                daily export job. You can even
                                perform cross-region exports
                                and imports, copying DynamoDB
                                data from a table in one AWS
                                region to a table in another AWS
                                region. For more information,
                                see Exporting and Importing
                                DynamoDB Data Using AWS
                                Data Pipeline (p. 760).
Reorganized higher-level API    Information about the following   January 20, 2014
documentation                   APIs is now easier to find:
                                • Java: DynamoDBMappper
                                • .NET: Document model and
                                  object-persistence model
Global secondary indexes            DynamoDB adds support for           December 12, 2013
                                    global secondary indexes. As
                                    with a local secondary index,
                                    you define a global secondary
                                    index by using an alternate
                                    key from a table and then
                                    issuing Query requests on the
                                    index. Unlike a local secondary
                                    index, the partition key for the
                                    global secondary index does not
                                    have to be the same as that of
                                    the table; it can be any scalar
                                    attribute from the table. The
                                    sort key is optional and can also
                                    be any scalar table attribute.
                                    A global secondary index
                                    also has its own provisioned
                                    throughput settings, which
                                    are separate from those of
                                    the parent table. For more
                                    information, see Improving
                                    Data Access with Secondary
                                    Indexes (p. 444) and Global
                                    Secondary Indexes (p. 446).
Fine-grained access control         DynamoDB adds support for           October 29, 2013
                                    fine-grained access control.
                                    This feature allows customers
                                    to specify which principals
                                    (users, groups, or roles) can
                                    access individual items and
                                    attributes in a DynamoDB table
                                    or secondary index. Applications
                                    can also leverage web identity
                                    federation to offload the task
                                    of user authentication to a
                                    third-party identity provider,
                                    such as Facebook, Google, or
                                    Login with Amazon. In this
                                    way, applications (including
                                    mobile apps) can handle very
                                    large numbers of users, while
                                    ensuring that no one can access
                                    DynamoDB data items unless
                                    they are authorized to do so.
                                    For more information, see Using
                                    IAM Policy Conditions for Fine-
                                    Grained Access Control (p. 661).
4 KB read capacity unit size         The capacity unit size for reads   May 14, 2013
                                     has increased from 1 KB to 4 KB.
                                     This enhancement can reduce
                                     the number of provisioned read
                                     capacity units required for many
                                     applications. For example, prior
                                     to this release, reading a 10 KB
                                     item would consume 10 read
                                     capacity units; now that same
                                     10 KB read would consume only
                                     3 units (10 KB / 4 KB, rounded
                                     up to the next 4 KB boundary).
                                     For more information, see
                                     Throughput Capacity for Reads
                                     and Writes (p. 15).
Local secondary indexes              DynamoDB adds support for          April 18, 2013
                                     local secondary indexes. You
                                     can define sort key indexes on
                                     non-key attributes, and then
                                     use these indexes in Query
                                     requests. With local secondary
                                     indexes, applications can
                                     efficiently retrieve data items
                                     across multiple dimensions. For
                                     more information, see Local
                                     Secondary Indexes (p. 482).
New API version                     With this release, DynamoDB          April 18, 2013
                                    introduces a new API version
                                    (2012-08-10). The previous
                                    API version (2011-12-05) is
                                    still supported for backward
                                    compatibility with existing
                                    applications. New applications
                                    should use the new API version
                                    2012-08-10. We recommend
                                    that you migrate your existing
                                    applications to API version
                                    2012-08-10, since new
                                    DynamoDB features (such
                                    as local secondary indexes)
                                    will not be backported to the
                                    previous API version. For more
                                    information on API version
                                    2012-08-10, see the Amazon
                                    DynamoDB API Reference.
IAM policy variable support         The IAM access policy language       April 4, 2013
                                    now supports variables. When
                                    a policy is evaluated, any policy
                                    variables are replaced with
                                    values that are supplied by
                                    context-based information from
                                    the authenticated user's session.
                                    You can use policy variables to
                                    define general purpose policies
                                    without explicitly listing all the
                                    components of the policy. For
                                    more information about policy
                                    variables, go to Policy Variables
                                    in the AWS Identity and Access
                                    Management Using IAM guide.
PHP code samples updated for        Version 2 of the AWS SDK for         January 23, 2013
AWS SDK for PHP version 2           PHP is now available. The PHP
                                    code samples in the Amazon
                                    DynamoDB Developer Guide
                                    have been updated to use this
                                    new SDK. For more information
                                    on Version 2 of the SDK, see
                                    AWS SDK for PHP.
Support for binary data type    In addition to the Number and       August 21, 2012
                                String types, DynamoDB now
                                supports Binary data type.
DynamoDB table items can be       DynamoDB users can now              August 14, 2012
updated and copied using the      update and copy table items
DynamoDB console                  using the DynamoDB Console,
                                  in addition to being able to
                                  add and delete items. This new
                                  functionality simplifies making
                                  changes to individual items
                                  through the Console.
Table explorer support in         The DynamoDB Console now            May 22, 2012
DynamoDB Console                  supports a table explorer that
                                  enables you to browse and
                                  query the data in your tables.
                                  You can also insert new items
                                  or delete existing items. The
                                  Creating Tables and Loading
                                  Sample Data (p. 281) and Using
                                  the Console (p. 48) sections have
                                  been updated for these features.
Documented more error codes     For more information, see Error    April 5, 2012
                                Handling (p. 189).
                                Working with
                                Items: .NET (p. 385).