DynamoDB API Calls

Learn via video courses
Topics Covered

Overview

DynamoDB is a NoSQL-based database server managed by AWS. It stores data in key-value pairs instead of relational tables. We can fetch the data stored in the DynamoDB-based database servers using DynamoDB API Calls in AWS. The API calls not only help in fetching the data, but we can also perform other operations like read, write, create, update, delete, etc.

What are API Operations?

API stands short for Application Program Interface. As the name suggests, it acts as an interface/ communication medium between an application and the users.

The working of the API is condensed in the following points:

  • Users send requests for receiving information from database servers.
  • These servers are received by API, which is forwarded to database servers.
  • The response sent by the database servers is taken back to the users via these API calls.

api operations

The API operations work to carry out the CRUD operations. CRUD stands short for Create, Read, Update, and Delete. We have an HTTP function defined for each activity to be performed using API calls. For example, we have the GET method for the Read operation. We have the PUT method for the Update operation, etc.

What is DynamoDB?

AWS provides various kinds of database services, which AWS fully manages. Dynamo DB is one such NoSQL database service provided by AWS.

aws dynamodb logo

Let's discuss some of the features related to DynamoDB better to understand the working of DynamoDB API calls in AWS.

  • In SQL databases, the data is stored in rows and columns, forming a tabular structure.
  • DynamoDB follows the NoSQL format. Hence the data here is stored in key-value pairs instead of tables. We do not need to define the schema for storing the data; only the primary key needs to be determined. This makes the process of scaling DynamoDB databases a lot easier.
  • You can easily get started with DynamoDB in AWS, as it is beginner-friendly.
  • To fetch the data stored in our DyanoDB servers, we can use APIs. In addition to fetching data, we can also perform other operations on our data using API calls.

DynamoDB Components

DynamoDB comprises three major components:

Attribute - Attribute refers to a particular field that is part of an item.

Item - An item is a set of attributes. It is used to identify an entity in the visual table.

Table - Although data in the DynamoDB is not stored in tables, the data can be visualized in tables considering a certain group of items.

Control Plane

The API operations are categorized into different types based on their operations. Let's have a look at the Control plane API operation.

  • Control Plane API operations are used to manage and create the visual tables of DynamoDB.
  • Under this, you can also work with some dependencies of tables like indexes, streams, etc.

Let's have a look at the API operations which come under the Control plane category:

  • CreateTable – This operation is used for creating a new table in our DynamoDB servers. Moreover, it can also be used for creating secondary indexes and DynamoDB Streams.
  • DescribeTable – This operation is used for fetching information associated with a table in the DynamoDB servers. The information returned due to the DynamoDB API calls here comprises details like schema, primary key, indexing-related information, etc.
  • ListTables – As the name suggests, this operation is used to list all the tables in our DynamoDB servers.
  • UpdateTable – This operation is used for modifying table components, indexes, etc.
  • DeleteTable – As the name suggests, this operation is used for deleting the table and its dependencies from our DynamoDB servers.

Data Plane

Data Plane is another category of API operations used to carry out CRUD (Create, Read, Update, and Delete) operations.

There are two distinct ways to carry out the CRUD operation.

  • Using PartiQL
  • Using Classic APIs

Let's discuss in brief how to execute the tasks using each of the ways.

PartiQL - A SQL-Compatible Query Language

PartiQL is a query language that is compatible with SQL queries. Under this, we make use of two important operations:

  • ExecuteStatement – This allows you to read one or more items from a table. In addition to reading, we can also perform writing operations on the items on a table. The primary key attribute must be specified to carry out the writing operation on the tables.
  • BatchExecuteStatement – As the name suggests, this can be used to operate on tables in batch/group. We can carry out any operation on multiple items on the table, like reading, writing, updating, etc. It increases efficiency and reduces the time involved, as only one round trip needs to be taken to execute the operation on multiple items.

Classic APIs

Under the Classic APIs, we have a list of methods for each activity of the CRUD operation, each designed for a specific purpose.

Creating Data

  • PutItem – This is utilized for writing an item to a table. For this operation, only the primary key attribute needs to be specified.
  • BatchWriteItem – As the name suggests, this is used to carry out operations on multiple items. It can write a maximum of 25 items simultaneously to the DynamoDB table. In addition to writing, we can also perform deletion operations for multiple items. This is a time-saving process, as multiple items can be operated in a single round trip.

Reading Data

  • GetItem – This method is used to fetch data for a particular item of the database table with the help of a specified primary key.
  • BatchGetItem – As the name suggests, it works similarly to the GetItem method, with the only difference being that it can execute the operation on multiple items simultaneously. It can fetch data for approximately 100 items at a time. This is a more efficient and time-saving process, as the operation is carried out on multiple items simultaneously in a single round trip.
  • Query – If you specify the partition key value, then this method fetches data for all items with the specified partition key.
  • Scan – This method is used to fetch data for all the items in a particular database table. In addition, we can also add conditions based on which data fetching/retrieval is done.

Updating Data

  • UpdateItem - This method helps update any item of the database table by specifying the table's primary key. Apart from updating the existing attributes, we can use this to create new attributes or delete the existing ones. Moreover, we can also define some conditions based on which the update operation can be carried out.

Deleting Data

  • DeleteItem - This is used for deleting a particular item in a database table. We need to specify the table's primary key for which we want to delete the item.
  • BatchWriteItem - This is used for deleting multiple items in one go and helps in saving time and increase efficiency. We can delete a maximum of 25 items using this operation. Moreover, we can use this to delete multiple items from multiple tables.

DynamoDB Streams

Another simple operation that can work with Amazon DynamoDB is DynamoDB Streams. These Operations help us to enable or disable a stream on a table. DynamoDB Streams Operations enable access to a stream's data modification records.

  • ListStreams - List Stream operation allows us to view all the streams. We can also specify a specific table to be the stream.
  • DescribeStream - DescribeStream provides detailed information about the stream. The details include the Amazon Resource Name (ARN) and the location where the application can start reading the stream's initial few records.
  • GetShardIterator - GetShardIterator provides back a shard iterator. It is a data structure used by the application to get records from the stream.
  • GetRecords - GetRecords operation gives us one or more stream records. It uses the specified shard iterator to obtain these stream records.

Transactions

Transactions support the ACID properties. Under the ACID properties, we have Atomicity, Consistency, Isolation, and Durability. Under transactional operations, on carrying out the operation, either all the batch items should get affected or none of them.

Transactions in DynamoDB can be carried out using the following methods:

  • PartiQL: It is used for performing the transactional operation in the AWS DynamoDB using API calls.
  • Classic APIs: Under this, we have distinct API calls designed for carrying out each CRUD operation.

Let's briefly discuss the methods which are part of the two.

PartiQL - A SQL-Compatible Query Language

PartiQL is a query language that is compatible with SQL queries.

  • ExecuteTransaction - This operation is used to carry out CRUD operation on multiple items across the DynamoDB database tables using DynamoDB API calls. In carrying out this operation, either all the batch items will be affected or none.

Classic APIs

  • TransactWriteItems - This operation works with multiple items simultaneously. Using this, we can simultaneously carry out the put, delete and update operation on multiple table items, saving on the time involved. In carrying out this operation, either all the batch items will be affected or none.

  • TransactGetItems - This operation is also used for working with multiple items at a time, the difference being here, we can fetch or carry out a get operation on multiple items simultaneously. Similar to the previous one, On carrying out this operation, either all the batch items will get affected or none of them.

Conclusion

  • AWS DynamoDB is a NoSQL-based managed database service provided by AWS that stores data in key-value pairs. The data stored in the AWS DynamoDB database servers can be fetched using DynamoDB API calls in AWS.
  • API stands for Application Program Interface, which acts as a communication medium between database servers and user requests.
  • API operations are used to carry out CRUD operations. CRUD stands short for Create, Read, Update, and Delete.
  • The API operation is categorized into sub-categories based on their operations.
    • Control Plane API operations are used to manage and create the visual tables of DynamoDB.
    • Data Plane is another category of API operations used to carry out CRUD (Create, Read, Update, and Delete) operations.
    • DynamoDB Streams operations help us to enable or disable a stream on a table.