image
Querying and Scanning DynamoDB From Code
Start course
Difficulty
Intermediate
Duration
3h 3m
Students
1697
Ratings
4.7/5
Description

This course provides detail on the AWS Database services relevant to the AWS Certified Developer - Associate exam. This includes Amazon RDS, Aurora, DynamoDB, MemoryDB for Redis, and ElastiCache.

Want more? Try a lab playground or do a Lab Challenge!

Learning Objectives

  • Obtain a solid understanding of the following Amazon database services: Amazon RDS, Aurora, DynamoDB, MemoryDB for Redis, and ElastiCache
  • Create an Amazon RDS database
  • Create a DynamoDB database
  • Create an ElastiCache cluster
Transcript

Now, when looking for information concerning your DynamoDB table, should you query or scan?

If you have access to the partition key and the sort key, then of course it's a no-brainer, you're going to be using a query operation. I'll show you how to do both right now. And later on, we're going to talk briefly about performance. But for now, just remember that scanning is a slow inefficient process that you should avoid if possible. Let me show you my lambda here. It's called test-dynamo-read. Let's look up the code. And just like the right operation, we're going to start with the border three resource and we're going to connect to our table using the resource we just created and the table method here, or attribute. Now, once we're connected, we can just call the get item operation for a query. In this case, I'm providing the partition key of the table. And of course, in this case, the sort key as well, which is, last_flight. And I'm providing a couple of values here that I know exist in this table, so that we get a response. Now, moving on to line 13, I have a full table scan. This is obviously something that you really want to avoid, especially if you have hundreds of thousands, if not millions of records in your table. You should never do a full table scan like this.

But I wanted to show you how it's done. It's simply calling the scan method on the table resource that we created. Now, the interesting line here is line 20, which is the same table scan. But in this case, we're passing a filter. So, if we know at least some of the information inside the table. In this case, I'm just interested in subscribers. So, the record needs to have a subscriber attribute, and in this case, it needs to be equal to true for it to be returned. Again, these scan operations will literally go to every record, every row in the table. So, like I said, if you have lots and lots of information, this will take a lot of time and it would be very inefficient, especially if you're trying to do operations involving tons of users accessing your table at the same time and handling lots and lots of data. Let's do an execution here. As you can see, the very first one, we have the query, we get our record. And the next one is the entire table scan. In this case, we get every single row in the table. Of course, in this case, it doesn't matter all that much because we have a very few rows. I believe we have four total records in our table. And finally, we have our same scan operation, but this time with a filter. So, as you can see, this is how this works, it's very, very straightforward and simple.

Just remember that, as long as you have a couple dozen users, it shouldn't matter. You should not need to write super efficient code. This will work just fine for anything that you're doing with your DynamoDB table. But as soon as you start to scale, you need to consider queries over scans because things can get very slow very quickly depending on how much data you are handling.

 

About the Author
Students
237782
Labs
1
Courses
232
Learning Paths
187

Stuart has been working within the IT industry for two decades covering a huge range of topic areas and technologies, from data center and network infrastructure design, to cloud architecture and implementation.

To date, Stuart has created 150+ courses relating to Cloud reaching over 180,000 students, mostly within the AWS category and with a heavy focus on security and compliance.

Stuart is a member of the AWS Community Builders Program for his contributions towards AWS.

He is AWS certified and accredited in addition to being a published author covering topics across the AWS landscape.

In January 2016 Stuart was awarded ‘Expert of the Year Award 2015’ from Experts Exchange for his knowledge share within cloud services to the community.

Stuart enjoys writing about cloud technologies and you will find many of his articles within our blog pages.