Reader small image

You're reading from  DynamoDB Cookbook

Product typeBook
Published inSep 2015
Publisher
ISBN-139781784393755
Edition1st Edition
Concepts
Right arrow
Author (1)
Tanmay Deshpande
Tanmay Deshpande
author image
Tanmay Deshpande

Tanmay Deshpande is a Hadoop and big data evangelist. He currently works with Schlumberger as a Big Data Architect in Pune, India. He has interest in a wide range of technologies, such as Hadoop, Hive, Pig, NoSQL databases, Mahout, Sqoop, Java, cloud computing, and so on. He has vast experience in application development in various domains, such as oil and gas, finance, telecom, manufacturing, security, and retail. He enjoys solving machine-learning problems and spends his time reading anything that he can get his hands on. He has great interest in open source technologies and has been promoting them through his talks. Before Schlumberger, he worked with Symantec, Lumiata, and Infosys. Through his innovative thinking and dynamic leadership, he has successfully completed various projects. He regularly blogs on his website http://hadooptutorials.co.in. You can connect with him on LinkedIn at https://www.linkedin.com/in/deshpandetanmay/. He has also authored Mastering DynamoDB, published in August 2014, DynamoDB Cookbook, published in September 2015, Hadoop Real World Solutions Cookbook-Second Edition, published in March 2016, Hadoop: Data Processing and Modelling, published in August, 2016, and Hadoop Blueprints, published in September 2016, all by Packt Publishing.
Read more about Tanmay Deshpande

Right arrow

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.

Previous PageNext Page
You have been reading a chapter from
DynamoDB Cookbook
Published in: Sep 2015Publisher: ISBN-13: 9781784393755
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Tanmay Deshpande

Tanmay Deshpande is a Hadoop and big data evangelist. He currently works with Schlumberger as a Big Data Architect in Pune, India. He has interest in a wide range of technologies, such as Hadoop, Hive, Pig, NoSQL databases, Mahout, Sqoop, Java, cloud computing, and so on. He has vast experience in application development in various domains, such as oil and gas, finance, telecom, manufacturing, security, and retail. He enjoys solving machine-learning problems and spends his time reading anything that he can get his hands on. He has great interest in open source technologies and has been promoting them through his talks. Before Schlumberger, he worked with Symantec, Lumiata, and Infosys. Through his innovative thinking and dynamic leadership, he has successfully completed various projects. He regularly blogs on his website http://hadooptutorials.co.in. You can connect with him on LinkedIn at https://www.linkedin.com/in/deshpandetanmay/. He has also authored Mastering DynamoDB, published in August 2014, DynamoDB Cookbook, published in September 2015, Hadoop Real World Solutions Cookbook-Second Edition, published in March 2016, Hadoop: Data Processing and Modelling, published in August, 2016, and Hadoop Blueprints, published in September 2016, all by Packt Publishing.
Read more about Tanmay Deshpande