1 00:00:00,580 --> 00:00:04,020 ‫So now let's have a look at the DynamoDB service. 2 00:00:04,020 --> 00:00:08,300 ‫And DynamoDB service is going to allow us to create tables. 3 00:00:08,300 --> 00:00:11,160 ‫As you can see, we can create tables, not databases 4 00:00:11,160 --> 00:00:13,920 ‫because while the database is already created for us 5 00:00:13,920 --> 00:00:15,160 ‫and this is a serverless offering. 6 00:00:15,160 --> 00:00:17,920 ‫So we just go ahead and create tables. 7 00:00:17,920 --> 00:00:19,660 ‫So the table name has to have a name, 8 00:00:19,660 --> 00:00:21,250 ‫so we'll enter "Users." 9 00:00:21,250 --> 00:00:23,240 ‫And then we have to define a partition key 10 00:00:23,240 --> 00:00:25,380 ‫and optionally a sort key. 11 00:00:25,380 --> 00:00:27,603 ‫So for partition key, I'll use "user_ID" 12 00:00:28,990 --> 00:00:30,440 ‫and then we can change and type, 13 00:00:30,440 --> 00:00:32,250 ‫so it could be binary, number or string, 14 00:00:32,250 --> 00:00:33,740 ‫but I will leave it as string. 15 00:00:33,740 --> 00:00:35,070 ‫And then we can use a sort key 16 00:00:35,070 --> 00:00:37,010 ‫but I will show you this later on. 17 00:00:37,010 --> 00:00:39,450 ‫So currently we'll leave it empty. 18 00:00:39,450 --> 00:00:40,970 ‫Now the quick start allows you to start 19 00:00:40,970 --> 00:00:43,880 ‫with a default settings and it sets some settings for you. 20 00:00:43,880 --> 00:00:45,690 ‫Or you can go ahead and customize the settings 21 00:00:45,690 --> 00:00:48,240 ‫but because we are learning about DynamoDB, 22 00:00:48,240 --> 00:00:50,620 ‫let's go ahead and learn about custom settings. 23 00:00:50,620 --> 00:00:52,150 ‫So the first thing is around table class. 24 00:00:52,150 --> 00:00:54,090 ‫So it's either the DynamoDB standard 25 00:00:54,090 --> 00:00:55,510 ‫which is a general purpose table class 26 00:00:55,510 --> 00:00:58,170 ‫which is recommended for most use cases. 27 00:00:58,170 --> 00:00:59,790 ‫But if you have data that's going to be sitting 28 00:00:59,790 --> 00:01:01,750 ‫around for a long time and you're 29 00:01:01,750 --> 00:01:04,110 ‫not going to have a lot of reads or writes, 30 00:01:04,110 --> 00:01:06,730 ‫then DynamoDB standard IA is going to say 31 00:01:06,730 --> 00:01:08,760 ‫that the data is infrequently accessed 32 00:01:08,760 --> 00:01:11,220 ‫and give you some cost optimizations. 33 00:01:11,220 --> 00:01:12,810 ‫But right now we'll use a default 34 00:01:12,810 --> 00:01:14,600 ‫which is DynamoDB standard. 35 00:01:14,600 --> 00:01:16,210 ‫Capacity calculator, we'll see this later on 36 00:01:16,210 --> 00:01:18,260 ‫because we have to understand how to compute capacity 37 00:01:18,260 --> 00:01:21,560 ‫in DynamoDB, but for now we don't do anything in here. 38 00:01:21,560 --> 00:01:25,140 ‫And then two modes for read and write capacity on demand 39 00:01:25,140 --> 00:01:28,560 ‫or provisioned, and provisioned is within the free tier. 40 00:01:28,560 --> 00:01:30,850 ‫Again, we'll have a long discussion around these modes 41 00:01:30,850 --> 00:01:32,840 ‫later on, but we'll use provision because 42 00:01:32,840 --> 00:01:34,520 ‫it is within the free tier. 43 00:01:34,520 --> 00:01:37,460 ‫Next, we have to set up the read and the write capacity 44 00:01:37,460 --> 00:01:40,070 ‫and I'm going to disable auto scaling for both. 45 00:01:40,070 --> 00:01:43,030 ‫So we just provision a fixed read and write capacity. 46 00:01:43,030 --> 00:01:45,170 ‫And we have two capacity units within the free tier, 47 00:01:45,170 --> 00:01:46,670 ‫so I'm going to set up, we have 10, sorry 48 00:01:46,670 --> 00:01:47,780 ‫within the free tier. 49 00:01:47,780 --> 00:01:48,640 ‫So I'm going to set up 50 00:01:48,640 --> 00:01:50,950 ‫two provision capacity units for reads 51 00:01:50,950 --> 00:01:52,670 ‫and two for writes. 52 00:01:52,670 --> 00:01:55,660 ‫And we'll see how we make them evolve over time. 53 00:01:55,660 --> 00:01:58,350 ‫We're now going to create a secondary index for now 54 00:01:58,350 --> 00:02:00,720 ‫because this is something that's going to be advanced later 55 00:02:00,720 --> 00:02:01,880 ‫on this course. 56 00:02:01,880 --> 00:02:04,410 ‫And here we get the estimated cost for this table 57 00:02:04,410 --> 00:02:06,970 ‫which is $1.32 per month. 58 00:02:06,970 --> 00:02:08,390 ‫Now because we are within the free tier, 59 00:02:08,390 --> 00:02:10,660 ‫you can disregard this number, okay? 60 00:02:10,660 --> 00:02:12,460 ‫But if you are not within the free tier 61 00:02:12,460 --> 00:02:15,760 ‫this would be how much this table would cost you per month. 62 00:02:15,760 --> 00:02:17,390 ‫Then we will encrypt the data at rest 63 00:02:17,390 --> 00:02:19,230 ‫using the DynamoDB key. 64 00:02:19,230 --> 00:02:21,340 ‫We'll leave it like this, but we have other options. 65 00:02:21,340 --> 00:02:24,053 ‫And when you're ready, you click on create table. 66 00:02:25,160 --> 00:02:27,470 ‫Okay, so my table has now been created. 67 00:02:27,470 --> 00:02:31,270 ‫I can click on it, and I will remove the left panel, 68 00:02:31,270 --> 00:02:33,300 ‫and as we can see we get a lot of the information 69 00:02:33,300 --> 00:02:34,133 ‫around our table. 70 00:02:34,133 --> 00:02:35,970 ‫So around the settings for our table. 71 00:02:35,970 --> 00:02:38,500 ‫So which one is the partition key, the sort key, 72 00:02:38,500 --> 00:02:41,370 ‫the capacity mode, and there's some replication, 73 00:02:41,370 --> 00:02:42,270 ‫date created and so on. 74 00:02:42,270 --> 00:02:44,710 ‫So a lot of information, and for us, 75 00:02:44,710 --> 00:02:47,200 ‫the important things is going to be around the items. 76 00:02:47,200 --> 00:02:49,570 ‫So if I scroll down, there is View Items. 77 00:02:49,570 --> 00:02:52,780 ‫Or if I go on the right hand side, there's also View Items. 78 00:02:52,780 --> 00:02:55,300 ‫Now, as we can see, I can scan or query my table, 79 00:02:55,300 --> 00:02:57,830 ‫but what I'm going to do is just create an item. 80 00:02:57,830 --> 00:03:01,480 ‫So in this create item, I can choose a user ID 81 00:03:01,480 --> 00:03:03,310 ‫and we need to define the partition keys. 82 00:03:03,310 --> 00:03:06,070 ‫So for example, John123. 83 00:03:06,070 --> 00:03:07,660 ‫And this is just a user ID, random one, 84 00:03:07,660 --> 00:03:09,270 ‫we can enter whatever you want. 85 00:03:09,270 --> 00:03:12,010 ‫And then we can add attributes for John123. 86 00:03:12,010 --> 00:03:13,390 ‫So we can add the strength for example 87 00:03:13,390 --> 00:03:16,720 ‫and the strength is going to be "first_name", 88 00:03:16,720 --> 00:03:18,950 ‫and the value is going to be well, John. 89 00:03:18,950 --> 00:03:23,122 ‫And then if we have a string and it's "last_name", 90 00:03:23,122 --> 00:03:25,303 ‫then we can have Doe, John Doe. 91 00:03:26,230 --> 00:03:27,090 ‫And as we can see, 92 00:03:27,090 --> 00:03:30,190 ‫we could add many attributes as we wanted of these types. 93 00:03:30,190 --> 00:03:32,860 ‫So we're good to go, we're going to just create an item. 94 00:03:32,860 --> 00:03:34,360 ‫And as we can see, this could be either 95 00:03:34,360 --> 00:03:37,670 ‫through a form or through adjacent document. 96 00:03:37,670 --> 00:03:40,433 ‫So we'll just keep it as a form and create this item. 97 00:03:42,460 --> 00:03:44,840 ‫So now this item, John Doe, is appearing in my table 98 00:03:44,840 --> 00:03:49,110 ‫as we can see, we have user ID, first name and last name. 99 00:03:49,110 --> 00:03:52,390 ‫So what we can do is to start adding a second item. 100 00:03:52,390 --> 00:03:54,360 ‫So let's create a second item. 101 00:03:54,360 --> 00:03:57,160 ‫And if I keep John123 as my value, 102 00:03:57,160 --> 00:04:02,110 ‫and then I will just say Johnny as my first name, 103 00:04:02,110 --> 00:04:04,060 ‫for example just to show you something, 104 00:04:05,400 --> 00:04:07,251 ‫if I do so and click on create item, 105 00:04:07,251 --> 00:04:08,510 ‫I'm going to have a problem 106 00:04:08,510 --> 00:04:10,760 ‫because well, I'm using the same user ID as before 107 00:04:10,760 --> 00:04:13,720 ‫and it has to be unique when I have just a partition key. 108 00:04:13,720 --> 00:04:15,310 ‫So this does not work. 109 00:04:15,310 --> 00:04:19,810 ‫Okay, so what I can do instead is I can create Alice456 110 00:04:19,810 --> 00:04:21,890 ‫and the first name is going to be Alice. 111 00:04:21,890 --> 00:04:23,380 ‫We don't know her last name 112 00:04:23,380 --> 00:04:24,610 ‫so we're not going to specify her last name 113 00:04:24,610 --> 00:04:27,750 ‫but we know her age and her age is 41. 114 00:04:27,750 --> 00:04:28,900 ‫Now, if I try to create this item 115 00:04:28,900 --> 00:04:31,070 ‫something very interesting will happen. 116 00:04:31,070 --> 00:04:33,710 ‫So as you can see, the request was successful. 117 00:04:33,710 --> 00:04:37,610 ‫So in a RDS SQL database, you would've had a problem 118 00:04:37,610 --> 00:04:40,200 ‫saying the column was not defined, some values are null, 119 00:04:40,200 --> 00:04:42,927 ‫whatever, but here we're able to get John 120 00:04:42,927 --> 00:04:45,350 ‫to have a first name and a last name 121 00:04:45,350 --> 00:04:48,060 ‫and Alice to just have an age and a first name. 122 00:04:48,060 --> 00:04:48,893 ‫So in DynamoDB, 123 00:04:48,893 --> 00:04:51,740 ‫it is completely fine for you to add attributes over time, 124 00:04:51,740 --> 00:04:55,215 ‫okay? The only thing that has to be non-null is the user ID, 125 00:04:55,215 --> 00:04:58,490 ‫but as we can see, things gets null by default. 126 00:04:58,490 --> 00:05:00,130 ‫So John has a null age 127 00:05:00,130 --> 00:05:02,910 ‫and Alice has a null last name, but this is fine. 128 00:05:02,910 --> 00:05:04,350 ‫So this is the power of DynamoDB, 129 00:05:04,350 --> 00:05:06,030 ‫also a risk but also the power. 130 00:05:06,030 --> 00:05:07,665 ‫You can add attributes over time 131 00:05:07,665 --> 00:05:09,960 ‫without impacting your previous data. 132 00:05:09,960 --> 00:05:12,090 ‫And this is completely fine, okay? 133 00:05:12,090 --> 00:05:14,280 ‫You cannot define in DynamoDB a constraint 134 00:05:14,280 --> 00:05:16,430 ‫of this column that's not been null, 135 00:05:16,430 --> 00:05:18,380 ‫it just doesn't exist, okay? 136 00:05:18,380 --> 00:05:20,110 ‫So we have our first table being created 137 00:05:20,110 --> 00:05:22,760 ‫but it's super easy to keep on creating tables. 138 00:05:22,760 --> 00:05:25,500 ‫So I go back to DynamoDB and click on "create table" 139 00:05:25,500 --> 00:05:30,340 ‫we can create a second table called "UsersPosts," okay? 140 00:05:30,340 --> 00:05:32,050 ‫Or "UserPosts." 141 00:05:32,050 --> 00:05:34,516 ‫Okay. Now the partition key is going to be yet again 142 00:05:34,516 --> 00:05:37,767 ‫user ID, but now we're going to have a sort key 143 00:05:37,767 --> 00:05:41,070 ‫which is post TS for timestamp, 144 00:05:41,070 --> 00:05:43,997 ‫again, of type string, binary, or number. 145 00:05:43,997 --> 00:05:46,335 ‫And what we want to create here is we create 146 00:05:46,335 --> 00:05:51,220 ‫a table of data, which will have the post of the users. 147 00:05:51,220 --> 00:05:53,990 ‫And so again we're going to customize the settings 148 00:05:53,990 --> 00:05:55,920 ‫and we're going to use provisions, 149 00:05:55,920 --> 00:05:59,293 ‫we'll just use "off," and we're gonna have it two and two, 150 00:06:00,320 --> 00:06:04,320 ‫and then scroll down and create this second table. 151 00:06:04,320 --> 00:06:06,270 ‫So as we can see now I have two tables in my database. 152 00:06:06,270 --> 00:06:09,560 ‫And again, no need to say what's happening to the table, 153 00:06:09,560 --> 00:06:10,670 ‫what's the underlying database, 154 00:06:10,670 --> 00:06:12,535 ‫it's really, you can create as many tables as you want 155 00:06:12,535 --> 00:06:15,890 ‫within your quote unquote DynamoDB database 156 00:06:15,890 --> 00:06:18,193 ‫which is done at the region level. 157 00:06:19,220 --> 00:06:21,390 ‫So now let's go into UserPosts, 158 00:06:21,390 --> 00:06:23,220 ‫and now we can see that there's a partition key 159 00:06:23,220 --> 00:06:24,677 ‫and a sort key being defined. 160 00:06:24,677 --> 00:06:27,040 ‫So if I go to view items 161 00:06:27,040 --> 00:06:30,120 ‫and then I'm going to create an item, 162 00:06:30,120 --> 00:06:33,360 ‫now we need to specify a user ID, for example John123 163 00:06:33,360 --> 00:06:35,160 ‫and a post timestamp. 164 00:06:35,160 --> 00:06:39,090 ‫So for example, 2021,10, 09. 165 00:06:39,090 --> 00:06:40,950 ‫And then you ensure that that's the correct timestamp 166 00:06:40,950 --> 00:06:42,380 ‫but let's hope it is. 167 00:06:42,380 --> 00:06:45,890 ‫TS, and then a time, so 123409Z. 168 00:06:48,850 --> 00:06:52,180 ‫Okay. So next we need to add some content for our post. 169 00:06:52,180 --> 00:06:53,557 ‫So I will say content and say, 170 00:06:53,557 --> 00:06:57,840 ‫"Hello world, this is my first blog." 171 00:06:57,840 --> 00:06:59,673 ‫Okay, excellent. 172 00:06:59,673 --> 00:07:01,823 ‫Now let's create this item. 173 00:07:03,830 --> 00:07:06,210 ‫And as you can see, now we have user ID John 174 00:07:06,210 --> 00:07:09,260 ‫and post timestamped this and some contents. 175 00:07:09,260 --> 00:07:12,410 ‫So now what happens if we create a second item 176 00:07:12,410 --> 00:07:15,700 ‫where we can keep John123 as my user ID 177 00:07:15,700 --> 00:07:19,090 ‫and for the post time stamp I'm going to just have 2021, 178 00:07:19,090 --> 00:07:20,070 ‫maybe something a bit later. 179 00:07:20,070 --> 00:07:25,070 ‫So 11, 04T and then again, just a random timestamp. 180 00:07:26,870 --> 00:07:31,637 ‫Okay. And we're going to add a new content and say, 181 00:07:31,637 --> 00:07:34,210 ‫"Second post yay!" 182 00:07:34,210 --> 00:07:36,790 ‫Okay, and create this item. 183 00:07:36,790 --> 00:07:39,210 ‫So as we can see, this has worked. 184 00:07:39,210 --> 00:07:41,490 ‫So even though we have the same user ID, 185 00:07:41,490 --> 00:07:44,100 ‫because the post timestamp was different, 186 00:07:44,100 --> 00:07:47,280 ‫we were able to enter this data into my table. 187 00:07:47,280 --> 00:07:48,910 ‫So the uniqueness has to be 188 00:07:48,910 --> 00:07:52,690 ‫on user ID and post timestamp as a combination. 189 00:07:52,690 --> 00:07:56,500 ‫And so that means that the data is partitioned by user ID. 190 00:07:56,500 --> 00:07:58,090 ‫So this was, this is why John is clickable 191 00:07:58,090 --> 00:08:02,660 ‫because we can query and search for John123 as my user ID. 192 00:08:02,660 --> 00:08:06,450 ‫And then we can sort the data by post timestamp, okay? 193 00:08:06,450 --> 00:08:08,420 ‫So it's called a sort key. 194 00:08:08,420 --> 00:08:10,720 ‫So this is very, very, very handy. 195 00:08:10,720 --> 00:08:14,440 ‫And as we can see, if we wanted to create one last item 196 00:08:14,440 --> 00:08:17,190 ‫and this was for Alice456, 197 00:08:17,190 --> 00:08:19,010 ‫again you will need to enter a post timestamp 198 00:08:19,010 --> 00:08:21,180 ‫and some content and you'll be good to go. 199 00:08:21,180 --> 00:08:22,550 ‫And so this is why it's super important to 200 00:08:22,550 --> 00:08:25,670 ‫choose a really good partition key in these kind of moments, 201 00:08:25,670 --> 00:08:26,870 ‫because if you choose a partition key 202 00:08:26,870 --> 00:08:29,260 ‫that keeps on coming back, so if John123 203 00:08:29,260 --> 00:08:32,100 ‫is the only user that posts and you have 10,000 posts 204 00:08:32,100 --> 00:08:33,974 ‫then the data is going to be heavily skewed 205 00:08:33,974 --> 00:08:35,665 ‫toward John123. 206 00:08:35,665 --> 00:08:38,010 ‫So there's something to be aware of, okay? 207 00:08:38,010 --> 00:08:39,380 ‫So that's it for this lecture. 208 00:08:39,380 --> 00:08:40,213 ‫I hope you liked it. 209 00:08:40,213 --> 00:08:42,090 ‫And I will see you in the next lecture.