2.6 DynamoDB: A NoSQL Data Store
To store non-relational data, Amazon created DynamoDB—its NoSQL database service. One of the features of DynamoDB is its ability to scale to any level of application usage. We are going to extend our order example so that it subscribes to an SNS topic, calculates the order totals, and then stores the results in DynamoDB.
1.Introduction3 lessons, 13:15
2.Amazon Web Services10 lessons, 1:17:39
3.Conclusion1 lesson, 01:25
2.6 DynamoDB: A NoSQL Data Store
Hi, welcome back to explore Amazon Web Services. In this lesson, we are going to calculate the totals on the fly using lambda and store our received orders and DynamoDBb. Before we start looking at DynamoDB, let's first, prepare our example. I want to again, execute a lambda function when we receive data from our SMS topic. Calculate the totals and store the order in the database. I'm going to start off with the SMS message blueprint where I can configure my SMS topic subscription, then we can start writing our function in life. I'm going to start by passing out SMS message and extract the order data out of it, then I can use map reduce to calculate the subtotal of the order items array Next are the taxes. Here in Austria, sales tax is at 20%. So, we multiply our subtotal by 0.2 to get the tax amount. The total is then just adding both of the previous values together. I also want to generate an ID, which I extract out of the message ID for this example. Now onto DynamoDB, we can use it like any service instantiating it from the AWS-SDK, then we need to use putItem to store some data. We have to specify the TableName and the Item dictionary. Each item has multiple key value paths where one of them is the primary key. In our case, it's ID. You're not limited to any scheme, but you always have to specify the primary key. To make DynamoDB aware of the data type, you have to use the S or N key to store a string or a number. Why don't we extend our pipeline a bit and then send the message to our queue with the calculated totals and with the ID? We can send the order object as a JSON string. Of course, we have to instantiate SQS var queue URL first. Then we have to set the role for the function. I'm going to create the Basic with DynamoDB one, but also extend it to have access to SQS. Since sometimes the list doesn't update correctly, I'm temporarily choosing a different role and edit it again on the review page. We can also enable the stream source here. Now we have to create our DynamoDB table. As you can see, you have two different options for your primary key. Hash and range or just the hash. The first option would be used if you, for instance, have custom actions for conversation. The interaction ID would be the hash and on each reply. For example, the reply time would be the range. The range and the hash combination has to be in the you can have Multiple arranges for a hash. In our case, using just the hash is fine. We can also choose to add the Global Secondary Index. The reference to instance the customer name, you can also specify which throughput the table can handle. The higher the throughput, the higher the cost. Now that we have our table set up, we can test our lambda function. I'm going to use the sample data we used to produce the HTML file before and just clean it up to put it into the topic message. When testing the function, we get an error right away. This is due to the fact that although DynamoDB also has number attributes, it only accepts them in string form. This is a bit strange, but we can comply to this constrain pretty easily. Note that it also supplies you with strings when you receive data, so you have to convert it yourself. After the change it works, I also have to consumer from the SQS lesson running in the background. It shows our process message. We now have some data in our table and I can show you a bit more of the console. You can view a list of items and also query by hash or index. In our case, this is the ID or customer name. You can also edit items here. The attributes will be shown as a tree. If we wanted, we could have stored the Items array in deeper levels. We can also view the data as JSON or as DynamoDB JSON, the format that uses the type as an additional dictionary key. We have now successfully start data in a new SQL data store in AWS. In the next lesson, we will look at delivering emails using ses. See you there.