1 00:00:00,130 --> 00:00:02,190 ‫Okay, so a few CLI options 2 00:00:02,190 --> 00:00:04,270 ‫that may come up in the exam for DynamoDB. 3 00:00:04,270 --> 00:00:06,160 ‫So the first one is projection-expression, 4 00:00:06,160 --> 00:00:08,750 ‫and with it, we can specify one or more attributes 5 00:00:08,750 --> 00:00:09,583 ‫to retrieve. 6 00:00:09,583 --> 00:00:10,810 ‫The idea is that we don't want to retrieve 7 00:00:10,810 --> 00:00:12,000 ‫all the columns, all the attributes. 8 00:00:12,000 --> 00:00:14,500 ‫We just want to retrieve a subset to maybe have less data 9 00:00:14,500 --> 00:00:16,510 ‫come to us, or only the data we need. 10 00:00:16,510 --> 00:00:18,950 ‫There is filter-expression, which is to filter items 11 00:00:18,950 --> 00:00:21,600 ‫returned to you, so there is going to be some filtering, 12 00:00:21,600 --> 00:00:22,910 ‫and we can specify some conditions 13 00:00:22,910 --> 00:00:24,610 ‫that we'll see very, very soon. 14 00:00:24,610 --> 00:00:26,810 ‫There is also some very important pagination options 15 00:00:26,810 --> 00:00:28,540 ‫for DynamoDB S1. 16 00:00:28,540 --> 00:00:31,100 ‫So, for DynamoDB, of course, page-size. 17 00:00:31,100 --> 00:00:32,560 ‫So page-size is saying 18 00:00:32,560 --> 00:00:35,670 ‫that we still want to retrieve the entire dataset, 19 00:00:35,670 --> 00:00:39,710 ‫but this time, each sub-API call that we'll make to AWS 20 00:00:39,710 --> 00:00:40,680 ‫will be smaller. 21 00:00:40,680 --> 00:00:41,910 ‫And the idea is your API call won't time out. 22 00:00:41,910 --> 00:00:44,740 ‫For example, if you have a table of 10,000 items 23 00:00:44,740 --> 00:00:45,850 ‫and you do one API call, 24 00:00:45,850 --> 00:00:48,350 ‫maybe you will retrieve 10,000 items at a time 25 00:00:48,350 --> 00:00:49,510 ‫and you will time out. 26 00:00:49,510 --> 00:00:52,130 ‫But if you specify a page-size of 100, 27 00:00:52,130 --> 00:00:53,520 ‫what's going to happen is that there's going to be 28 00:00:53,520 --> 00:00:55,949 ‫100 API calls of size 100, 29 00:00:55,949 --> 00:00:57,510 ‫that are going to be made behind the scenes, 30 00:00:57,510 --> 00:01:00,120 ‫to make sure that the complete API call succeeds. 31 00:01:00,120 --> 00:01:02,530 ‫So page-size is an optimization in that sense, 32 00:01:02,530 --> 00:01:04,810 ‫in that it allows you to do more API calls, 33 00:01:04,810 --> 00:01:07,210 ‫but also avoid timeouts. 34 00:01:07,210 --> 00:01:10,690 ‫Max-items, instead, is to have a limited number of items 35 00:01:10,690 --> 00:01:14,090 ‫to show when you get a result back from the CLI call. 36 00:01:14,090 --> 00:01:16,846 ‫And max-items works in combination with NextToken, 37 00:01:16,846 --> 00:01:19,508 ‫or starting-token, which is to say 38 00:01:19,508 --> 00:01:22,560 ‫that now that you've received, say, 25 items, 39 00:01:22,560 --> 00:01:23,910 ‫we want to get the next 25, 40 00:01:23,910 --> 00:01:26,510 ‫and so therefore, you need to use that NextToken 41 00:01:26,510 --> 00:01:29,360 ‫to retrieve the next 25 items. 42 00:01:29,360 --> 00:01:30,590 ‫Now, I'll make this very clear, 43 00:01:30,590 --> 00:01:32,820 ‫because we're going to practice right now. 44 00:01:32,820 --> 00:01:35,470 ‫So let's go into our UserPosts table 45 00:01:35,470 --> 00:01:37,130 ‫and let's view the items. 46 00:01:37,130 --> 00:01:38,830 ‫So here are all the items in our table, 47 00:01:38,830 --> 00:01:40,530 ‫and we're going to have a play with it. 48 00:01:40,530 --> 00:01:41,510 ‫Now, I want you to open the CLI 49 00:01:41,510 --> 00:01:43,300 ‫so you can use a terminal, if you've configured it, 50 00:01:43,300 --> 00:01:45,673 ‫or I'm going to use AWS CloudShell. 51 00:01:46,570 --> 00:01:48,210 ‫And also, please make sure that you open 52 00:01:48,210 --> 00:01:51,193 ‫the cli-examples.sh file in your DynamoDB folder. 53 00:01:52,290 --> 00:01:53,930 ‫Okay, so the first thing is that we're going to do 54 00:01:53,930 --> 00:01:54,950 ‫a DynamoDB scan. 55 00:01:54,950 --> 00:01:55,783 ‫Okay. 56 00:01:55,783 --> 00:01:57,550 ‫And we should specify the table name, 57 00:01:57,550 --> 00:02:00,260 ‫but we'll also specify a projection-expression 58 00:02:00,260 --> 00:02:02,060 ‫on user_id and content. 59 00:02:02,060 --> 00:02:04,780 ‫So what's going to happen is that in this table currently, 60 00:02:04,780 --> 00:02:05,990 ‫we have three attributes. 61 00:02:05,990 --> 00:02:08,620 ‫We have user_id, post timestamp, and content. 62 00:02:08,620 --> 00:02:10,170 ‫By using a projection-expression, 63 00:02:10,170 --> 00:02:12,770 ‫we're not going to retrieve post timestamp. 64 00:02:12,770 --> 00:02:14,460 ‫So let's have a proof of this. 65 00:02:14,460 --> 00:02:16,410 ‫So let's copy and paste this. 66 00:02:16,410 --> 00:02:17,243 ‫Press Enter. 67 00:02:18,670 --> 00:02:21,660 ‫And as you can see, I'm getting some items back, okay? 68 00:02:21,660 --> 00:02:23,730 ‫And as we can see, we only have content and user_id. 69 00:02:23,730 --> 00:02:26,830 ‫We don't have post timestamp as part of the results. 70 00:02:26,830 --> 00:02:27,663 ‫Okay. 71 00:02:27,663 --> 00:02:29,010 ‫Next, let's talk about a filter-expression. 72 00:02:29,010 --> 00:02:30,690 ‫So we want to scan this table. 73 00:02:30,690 --> 00:02:33,000 ‫But this time, we're going to filter all the results 74 00:02:33,000 --> 00:02:34,230 ‫using a filter-expression, 75 00:02:34,230 --> 00:02:36,370 ‫so this is happening client-side. 76 00:02:36,370 --> 00:02:39,210 ‫And we'll look for user_id = u. 77 00:02:39,210 --> 00:02:43,703 ‫And then u has to be a string that has the value john123. 78 00:02:44,720 --> 00:02:46,880 ‫So that means that we want to only retrieve 79 00:02:46,880 --> 00:02:48,860 ‫the rows that have john123 in it. 80 00:02:48,860 --> 00:02:50,690 ‫And as we can see, we have items. 81 00:02:50,690 --> 00:02:52,080 ‫Count is 2. 82 00:02:52,080 --> 00:02:54,640 ‫And we only retrieve the content for john123. 83 00:02:54,640 --> 00:02:55,710 ‫So this is working. 84 00:02:55,710 --> 00:02:58,450 ‫And this is all happening client-side, okay? 85 00:02:58,450 --> 00:02:59,970 ‫If you wanted to have it server-side, 86 00:02:59,970 --> 00:03:03,220 ‫because the user_id is john123, 87 00:03:03,220 --> 00:03:05,220 ‫and it's a user_id call and so it's a primary key, 88 00:03:05,220 --> 00:03:08,110 ‫we could run a query directly to be a lot more efficient. 89 00:03:08,110 --> 00:03:10,690 ‫But at least we show the power of filter-expression. 90 00:03:10,690 --> 00:03:12,330 ‫We could filter on user_id. 91 00:03:12,330 --> 00:03:13,550 ‫We could filter on post timestamp. 92 00:03:13,550 --> 00:03:16,150 ‫And we could filter on content as well. 93 00:03:16,150 --> 00:03:19,120 ‫Next, we want to demo the page-size. 94 00:03:19,120 --> 00:03:23,923 ‫So if you are doing this API call to scan this entire table, 95 00:03:24,930 --> 00:03:27,350 ‫then it's going to be one API call in the background, 96 00:03:27,350 --> 00:03:29,000 ‫and this API call is small enough, of course, 97 00:03:29,000 --> 00:03:32,050 ‫but we retrieve three items. 98 00:03:32,050 --> 00:03:33,430 ‫But if you wanted to be efficient, 99 00:03:33,430 --> 00:03:35,350 ‫say there was like 10,000 items, 100 00:03:35,350 --> 00:03:37,550 ‫you could specify a page-size 1. 101 00:03:37,550 --> 00:03:39,980 ‫Now, page-size 1 will just do three API calls 102 00:03:39,980 --> 00:03:41,940 ‫in the background, but you will still retrieve 103 00:03:41,940 --> 00:03:44,690 ‫your three items as part of one command. 104 00:03:44,690 --> 00:03:46,560 ‫So if I press Enter, as we can see, 105 00:03:46,560 --> 00:03:49,250 ‫I did receive yet again my three items, okay? 106 00:03:49,250 --> 00:03:51,230 ‫But what happened in the background is that 107 00:03:51,230 --> 00:03:53,823 ‫there were three API calls made to DynamoDB. 108 00:03:53,823 --> 00:03:57,530 ‫So this is the page-size optimization to avoid timeouts. 109 00:03:57,530 --> 00:04:00,530 ‫Instead, if you wanted to retrieve one item at a time, 110 00:04:00,530 --> 00:04:03,150 ‫you would use the max-items and then a number, 111 00:04:03,150 --> 00:04:06,020 ‫so max-items 1 should return only one item. 112 00:04:06,020 --> 00:04:09,130 ‫As you can see, we have this item being returned to us. 113 00:04:09,130 --> 00:04:11,060 ‫And then the count is 3. 114 00:04:11,060 --> 00:04:14,360 ‫But we have now a NextToken available to us. 115 00:04:14,360 --> 00:04:17,290 ‫So this NextToken, we have to use using this command. 116 00:04:17,290 --> 00:04:19,410 ‫So we're going to use the same max-items 1, 117 00:04:19,410 --> 00:04:22,587 ‫but now we have the starting-token that we have to enter. 118 00:04:22,587 --> 00:04:26,503 ‫And the value of the starting-token is what is in your CLI. 119 00:04:26,503 --> 00:04:29,280 ‫So you paste the starting-token in your command, 120 00:04:29,280 --> 00:04:30,743 ‫and then you press Enter. 121 00:04:31,740 --> 00:04:33,400 ‫And now you get the second post. 122 00:04:33,400 --> 00:04:35,970 ‫So here was the first blog. 123 00:04:35,970 --> 00:04:37,810 ‫And we had a NextToken. 124 00:04:37,810 --> 00:04:42,810 ‫And now it's our second blog, and we again have a NextToken. 125 00:04:42,840 --> 00:04:45,360 ‫And so finally, if you wanted to retrieve the last one, 126 00:04:45,360 --> 00:04:47,390 ‫we would run this command yet again, okay? 127 00:04:47,390 --> 00:04:49,280 ‫So we'd run this command, 128 00:04:49,280 --> 00:04:53,080 ‫and this time we would specify the NextToken from above, 129 00:04:53,080 --> 00:04:54,260 ‫which happens to be the exact same. 130 00:04:54,260 --> 00:04:55,440 ‫I don't want to show you again the process. 131 00:04:55,440 --> 00:05:00,440 ‫So you copy this entire token in your command, press Enter, 132 00:05:01,460 --> 00:05:03,590 ‫and now we get "Alice blog edited." 133 00:05:03,590 --> 00:05:05,160 ‫And now we don't have any more NextToken. 134 00:05:05,160 --> 00:05:07,570 ‫That's when we know that we've reached the limits 135 00:05:07,570 --> 00:05:10,280 ‫of all the things we could scan from this table, okay? 136 00:05:10,280 --> 00:05:13,090 ‫So hopefully these options of the CLI make sense. 137 00:05:13,090 --> 00:05:13,923 ‫I hope you liked it. 138 00:05:13,923 --> 00:05:15,790 ‫And I will see you in the next lecture.