Instructor: Okay, just a little bit of practice
with the DynamoDB CLI 'cause there is
some good-to-know parameters that we can use,
and that the exam may ask you questions about.
The first one is a projection-expression,
and this is to specify a list of attributes
that you may want to retrieve from the table.
Maybe you don't want all the attributes from the table.
Maybe you just want a subset,
and so projection-expression is the way to say that.
Also if you want to filter the results,
you can use a filter-expression,
and we'll see how to do both these things into the hands-on.
Now, there are some general CLI pagination option
that includes the DynamoDB and the S3 CLI
but could also include other CLI.
And so, these commands are not just applied to
these two services but to CLI overall
when it returns results,
and they're really good to know overall.
The first one is an optimization which is page-size.
This option is saying that you still wanna retrieve
the full dataset, for example if you're doin' S3 ListBucket
or DynamoDB Scan, but you want each API call made to AWS
to request less data, and maybe a reason
why you would want to do this is to help avoid timeouts.
So by specifying a smaller page size, you will have
more API calls being done, but each API call will return
less data, so it probably will help you avoid timeouts.
We'll see this in a second.
There's also pagination.
Pagination, as the name indicates, is to use pages.
With this, you specify the max number of items
you want to be returned by the CLI,
and you will get a token at the end of this command
called NextToken, and you would pass on that token
to the starting token parameters
for the next command you run
to keep on reading the next page hence the name pagination.
So the first two, projection-expression
and filter-expression, they're only applicable
to the DynamoDB CLI, but all these optimization
and pagination, so page-size, max-item and starting-token,
are applicable to way more CLI in the AWS world,
and you need to know those going into the exam.
So, let's go and have a little bit of practice.
I'm going to use the UsersPost table,
and there's three items in there,
and I'm going to run the queries against those.
So, we have three items.
Two belong to the same user ID,
and we have three different columns,
user_id, post_ts and content.
So, let's go how we can query this table.
Here is my terminal, and I've created a file called
cli-examples that you can just use to run these commands.
The first command I want to show you
is the projection-expression.
If the copy and paste works, here we go, it works.
This projection-expression is saying okay, we'll run
a DynamoDB Scan command on the table name UserPosts,
and I'm running in the region US East one,
so change this if you don't run in US East one.
The projection-expression is saying I only want
to retrieve the user ID and the content column.
No other attributes should be retrieved.
So if I press enter, I'm gonna get my results
of all my items, but all these items, the three of them,
they only returned the content and the user ID.
I did not retrieve from this, the post time stamp.
So, this is a demo into
how the projection-expression can be used.
Next command we want to look at is this filter-expression.
Now, they're really not easy to figure out,
so I'm glad I did.
Again, we run a scan on the same table.
This time we say we want the user ID
to be equal to this value u.
What is u?
Well, you need to specify an expression-attributes value
to say that this u-value is S, which means string,
equals to this value.
What does this value represent?
Well, it represents this user ID right here.
So, it's a little bit tricky to put together this command,
but what it should do is filter our table for the scan
just based on the results for this user ID.
So if I press enter, this time it scanned three rows,
but it returned to me two rows, and the response
of the two rows were the user ID has a value of
string one, two, three usersoi3 so excellent.
So, this filter-expression works as well.
Now, let's get into the page-size demo.
Here, I'm just going to run a straight forward scan.
If I do a scan, this does one API call to DynamoDB, just one
because it tries to return as much data as possible.
But if you wanna do three API calls, we are going to run
this command where we specify the page size to be one,
and by having the page size being one,
we're saying the max number of results I want
from each API call is going to be one,
but because we will still get the full data set,
in the background, it will run three API calls.
If I press enter, as we can see, we still get three
as a final result, but we were issuing three API calls
to DynamoDB because of this page-size argument.
How do we limit the number of results per call to the CLI?
Well, we use the max-items.
If I use this max-item here, I do a scan on the same table,
same region but now say number of items
that I want to be returned at the maximum is one.
I press enter, and here I only get one item back.
And I'm given a NextToken,
and this token is what I should be using
as part of the starting token argument,
and so I'm going to paste the starting token here,
and so here I got the hello world first post.
But if I press enter, now I'm getting my second post
which is awesome new post, second ever,
and I'm getting a new NextToken again,
so we're going to reuse that new NextToken and replace it.
So, let's use this NextToken and paste it here, press enter.
Now, I'm getting new user new post,
and we don't get any new NextToken.
We don't have any new NextToken, so what does that mean?
That means that we are done reading this table,
and there is no more pages to read
hence the name pagination.
That's it, just what I wanted to show you.
It's pretty simple, but hopefully you need to see it once
before you understand how these things work,
so once you remember the structure, look at the code,
try to remember these options.
I think it's really important to understand the difference
between max-items and the starting-token and page-size.
One is for optimization.
The other one is for pagination.
That's it for the CLI practice.
I hope you liked this,
and I will see you in the next lecture.