Book Image

DynamoDB Cookbook

By : Tanmay Deshpande
Book Image

DynamoDB Cookbook

By: Tanmay Deshpande

Overview of this book

Table of Contents (18 chapters)
DynamoDB Cookbook
Credits
About the Author
Acknowledgments
About the Reviewers
www.PacktPub.com
Preface
Index

Querying data using the DynamoDB console


In the earlier recipe, we learned how to add new items to the DynamoDB table. In this recipe, we will learn how to query the data that is added using the DynamoDB console.

Getting ready

To perform this recipe, you need to have completed the earlier recipe to add items to the DynamoDB table.

How to do it…

Let's perform different types of queries on AWS DynamoDB console:

  1. To perform the various types of queries, we will first need to go to the AWS DynamoDB console, select the table against which we would like to execute our queries, and then click on the Explore Table button. This will execute a scan operation on the selected table, which will look like this:

  2. Now, to perform a query operation, select the Query radio button. In the dropdown below it, you will see a choice to perform the query either on the table indexes or secondary indexes, which were created earlier. The first query we will perform will be to get the item by its id. Here, if you want to fetch an item/s whose id is 1, put 1 in the textbox against the Hash Key, click on the Query button, and you will see the results immediately:

  3. We can also query data by providing a combination of Hash Key and Range Key to narrow down our search. In the earlier scenario, we got two items: one with the type phone, and another with the type book. Now if we want to fetch the item whose id is 1 and the type is phone, then we can query that accordingly, and we will get the results as required:

  4. We can also query data on the Global Secondary Index, which we created at the time of table creation:

  5. Similarly, we can use the scan operations in order to fetch the data from the DynamoDB table. We can either do a complete scan or a filtered scan. To perform a complete table scan, we have to select the Scan radio button, and click on the Start New Scan button. As we have not put any filters on the scan, this operation will fetch all the items present in the given DynamoDB table:

  6. We can also add filters to the scan operation by clicking on the Add Filter button, and then by providing the filtering criteria. For example, if we want to fetch all the items of the phone type, then we can specify the criteria, and execute the scan operation.

  7. Scan filters allow comparison operations, such as equal to, less than, less than or equal to, greater than, greater than or equal to, null, not null, contains, not contain, begins with, and so on. You can use it as per your need. Scan filtering can also be performed on the global secondary index. For example, if we want to fetch all the items whose manufacture name starts with A, then we can add this filter and get the results:

How it works…

Query and Scan are powerful operations that help us to retrieve data. However, both have their own working style and their own pros and cons.

A Query operation finds items in the table using the primary key attributes. You can specify the hash key and optional range key in order to get the desired items. By default, a Query operation returns all the attributes of a given item, but there is an option available to limit the attributes using ProjectionExpressions. While providing the key conditions, the Query operation expects an equality condition on the Hash Key attribute, but there are a set of conditions allowed to specify the Range Key conditions.

A query request can retrieve a maximum of 1 MB of data at a time. So, you need to plan the Query operation accordingly. You can optionally use FilterExpressions to narrow down this data. The Hash Key index is an unordered index, which means that we can only specify an exact key to fetch the items, whereas the Range Key index is an ordered index, which allows us to query that index using various ConditionalOperations, such as less than, greater than, begins with, between, and so on. If no matches are found, Query returns an empty result set.

A Scan operation examines each and every item in the given DynamoDB table, compares it with the condition, and gets the results back. By default, this also returns all the attributes of the items back to the user. Of course, we can use ProjectionExpressions to limit the set of attributes returned by the scan request.

A single scan request can fetch a maximum of 1 MB of data. To make the best use of it, we can also use FilterExpressions to narrow down the result set.

There are ways to handle this limitation of 1 MB of data per request, but we cannot do much using the DynamoDB console, so we will talk about all these details in Chapter 2, Operating with DynamoDB Tables.