‫So now let's have a look at the DynamoDB service.
‫And DynamoDB service is going to allow us to create tables.
‫As you can see, we can create tables, not databases
‫because while the database is already created for us
‫and this is a serverless offering.
‫So we just go ahead and create tables.
‫So the table name has to have a name,
‫so we'll enter "Users."
‫And then we have to define a partition key
‫and optionally a sort key.
‫So for partition key, I'll use "user_ID"
‫and then we can change and type,
‫so it could be binary, number or string,
‫but I will leave it as string.
‫And then we can use a sort key
‫but I will show you this later on.
‫So currently we'll leave it empty.
‫Now the quick start allows you to start
‫with a default settings and it sets some settings for you.
‫Or you can go ahead and customize the settings
‫but because we are learning about DynamoDB,
‫let's go ahead and learn about custom settings.
‫So the first thing is around table class.
‫So it's either the DynamoDB standard
‫which is a general purpose table class
‫which is recommended for most use cases.
‫But if you have data that's going to be sitting
‫around for a long time and you're
‫not going to have a lot of reads or writes,
‫then DynamoDB standard IA is going to say
‫that the data is infrequently accessed
‫and give you some cost optimizations.
‫But right now we'll use a default
‫which is DynamoDB standard.
‫Capacity calculator, we'll see this later on
‫because we have to understand how to compute capacity
‫in DynamoDB, but for now we don't do anything in here.
‫And then two modes for read and write capacity on demand
‫or provisioned, and provisioned is within the free tier.
‫Again, we'll have a long discussion around these modes
‫later on, but we'll use provision because
‫it is within the free tier.
‫Next, we have to set up the read and the write capacity
‫and I'm going to disable auto scaling for both.
‫So we just provision a fixed read and write capacity.
‫And we have two capacity units within the free tier,
‫so I'm going to set up, we have 10, sorry
‫within the free tier.
‫So I'm going to set up
‫two provision capacity units for reads
‫and two for writes.
‫And we'll see how we make them evolve over time.
‫We're now going to create a secondary index for now
‫because this is something that's going to be advanced later
‫on this course.
‫And here we get the estimated cost for this table
‫which is $1.32 per month.
‫Now because we are within the free tier,
‫you can disregard this number, okay?
‫But if you are not within the free tier
‫this would be how much this table would cost you per month.
‫Then we will encrypt the data at rest
‫using the DynamoDB key.
‫We'll leave it like this, but we have other options.
‫And when you're ready, you click on create table.
‫Okay, so my table has now been created.
‫I can click on it, and I will remove the left panel,
‫and as we can see we get a lot of the information
‫around our table.
‫So around the settings for our table.
‫So which one is the partition key, the sort key,
‫the capacity mode, and there's some replication,
‫date created and so on.
‫So a lot of information, and for us,
‫the important things is going to be around the items.
‫So if I scroll down, there is View Items.
‫Or if I go on the right hand side, there's also View Items.
‫Now, as we can see, I can scan or query my table,
‫but what I'm going to do is just create an item.
‫So in this create item, I can choose a user ID
‫and we need to define the partition keys.
‫So for example, John123.
‫And this is just a user ID, random one,
‫we can enter whatever you want.
‫And then we can add attributes for John123.
‫So we can add the strength for example
‫and the strength is going to be "first_name",
‫and the value is going to be well, John.
‫And then if we have a string and it's "last_name",
‫then we can have Doe, John Doe.
‫And as we can see,
‫we could add many attributes as we wanted of these types.
‫So we're good to go, we're going to just create an item.
‫And as we can see, this could be either
‫through a form or through adjacent document.
‫So we'll just keep it as a form and create this item.
‫So now this item, John Doe, is appearing in my table
‫as we can see, we have user ID, first name and last name.
‫So what we can do is to start adding a second item.
‫So let's create a second item.
‫And if I keep John123 as my value,
‫and then I will just say Johnny as my first name,
‫for example just to show you something,
‫if I do so and click on create item,
‫I'm going to have a problem
‫because well, I'm using the same user ID as before
‫and it has to be unique when I have just a partition key.
‫So this does not work.
‫Okay, so what I can do instead is I can create Alice456
‫and the first name is going to be Alice.
‫We don't know her last name
‫so we're not going to specify her last name
‫but we know her age and her age is 41.
‫Now, if I try to create this item
‫something very interesting will happen.
‫So as you can see, the request was successful.
‫So in a RDS SQL database, you would've had a problem
‫saying the column was not defined, some values are null,
‫whatever, but here we're able to get John
‫to have a first name and a last name
‫and Alice to just have an age and a first name.
‫So in DynamoDB,
‫it is completely fine for you to add attributes over time,
‫okay? The only thing that has to be non-null is the user ID,
‫but as we can see, things gets null by default.
‫So John has a null age
‫and Alice has a null last name, but this is fine.
‫So this is the power of DynamoDB,
‫also a risk but also the power.
‫You can add attributes over time
‫without impacting your previous data.
‫And this is completely fine, okay?
‫You cannot define in DynamoDB a constraint
‫of this column that's not been null,
‫it just doesn't exist, okay?
‫So we have our first table being created
‫but it's super easy to keep on creating tables.
‫So I go back to DynamoDB and click on "create table"
‫we can create a second table called "UsersPosts," okay?
‫Or "UserPosts."
‫Okay. Now the partition key is going to be yet again
‫user ID, but now we're going to have a sort key
‫which is post TS for timestamp,
‫again, of type string, binary, or number.
‫And what we want to create here is we create
‫a table of data, which will have the post of the users.
‫And so again we're going to customize the settings
‫and we're going to use provisions,
‫we'll just use "off," and we're gonna have it two and two,
‫and then scroll down and create this second table.
‫So as we can see now I have two tables in my database.
‫And again, no need to say what's happening to the table,
‫what's the underlying database,
‫it's really, you can create as many tables as you want
‫within your quote unquote DynamoDB database
‫which is done at the region level.
‫So now let's go into UserPosts,
‫and now we can see that there's a partition key
‫and a sort key being defined.
‫So if I go to view items
‫and then I'm going to create an item,
‫now we need to specify a user ID, for example John123
‫and a post timestamp.
‫So for example, 2021,10, 09.
‫And then you ensure that that's the correct timestamp
‫but let's hope it is.
‫TS, and then a time, so 123409Z.
‫Okay. So next we need to add some content for our post.
‫So I will say content and say,
‫"Hello world, this is my first blog."
‫Okay, excellent.
‫Now let's create this item.
‫And as you can see, now we have user ID John
‫and post timestamped this and some contents.
‫So now what happens if we create a second item
‫where we can keep John123 as my user ID
‫and for the post time stamp I'm going to just have 2021,
‫maybe something a bit later.
‫So 11, 04T and then again, just a random timestamp.
‫Okay. And we're going to add a new content and say,
‫"Second post yay!"
‫Okay, and create this item.
‫So as we can see, this has worked.
‫So even though we have the same user ID,
‫because the post timestamp was different,
‫we were able to enter this data into my table.
‫So the uniqueness has to be
‫on user ID and post timestamp as a combination.
‫And so that means that the data is partitioned by user ID.
‫So this was, this is why John is clickable
‫because we can query and search for John123 as my user ID.
‫And then we can sort the data by post timestamp, okay?
‫So it's called a sort key.
‫So this is very, very, very handy.
‫And as we can see, if we wanted to create one last item
‫and this was for Alice456,
‫again you will need to enter a post timestamp
‫and some content and you'll be good to go.
‫And so this is why it's super important to
‫choose a really good partition key in these kind of moments,
‫because if you choose a partition key
‫that keeps on coming back, so if John123
‫is the only user that posts and you have 10,000 posts
‫then the data is going to be heavily skewed
‫toward John123.
‫So there's something to be aware of, okay?
‫So that's it for this lecture.
‫I hope you liked it.
‫And I will see you in the next lecture.