DynamoDB Connector Mule 4
In this article, take a look at a working example of the Put item and query operations in Mule 4.
Join the DZone community and get the full member experience.Join For Free
Amazon DynamoDB is a fully managed NoSQL database service that supports key-value and document data structures.
This article will document the working example in Mule 4 of the Put item and Query operations and for the Create table, Delete table, and Scan operations, check out the MuleSoft documentation.
NOTE: The Github repository with the Mule Project can be found at the end of the post.
We have an order table, and below is the table structure:
The order table's primary key is the combination of orderId and orderDate - where orderDate is the partition key, and orderId as the sort key. The partition key plays a critical role in the DynamoDB. Check out the AWS documentation on choosing the right partition key.
We will create an item in the order table and query the table to retrieve all the items for the given order date.
MuleSoft connector access the DynamoDB programmatically, and for that, you will need the AWS access key. Check the AWS documentation to generate the key.
Configure the access key and secret key, as shown in the below screenshot.
This operation creates a new item or replaces an old item with a new item if it has the same primary key as the new item.
The configuration for the Put item is simple, as shown in the below screenshot. You need to provide a table name and payload.
Note that the payload should be an item - a map of attribute name/value pairs, and each element in the Item map is an AttributeValue object.
The below dataweave creates an Item for the order table.
The Query operation in Amazon DynamoDB finds items based on primary key values. One must provide the name of the partition key attribute and a single value for that attribute. The Query returns all items with that partition key value.
The search criteria specified using a key condition expression—a string that determines the items to be read from the table. One must specify the partition key name and value as an equal condition.
For the scenario we took for this article, the key condition expression would be:
orderDate = :orderDate
The below dwl script is stored in a variable named attributeValue.
The below screenshot shows the Query operation configuration.
The query operation will return you below object have an item array matching the key-condition expression:
Below dataweave will transform the output data into the required format:
We have seen the working examples Put item and Query operations and make sure to select the right partition key as both of these operations are partition key-dependent.
Opinions expressed by DZone contributors are their own.