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 FreeAmazon 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.
Scenario
We have an order table, and below is the table structure:
xxxxxxxxxx
{
"orderId": "10c9b171-37e9-456e-aeaf-d88a0d38eea5",
"userName": "Mule",
"orderDate": "2020-09-26",
"expectedDeliveryDate": "2020-10-01",
"shipingAddress": "Toronto, Canada",
"billingAddress": "Toronto, Canada"
}
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.
Configuration
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.
Operations
Put Item
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.
xxxxxxxxxx
%dw 2.0
output application/json
---
{
orderId:{
S: vars.orderId
},
userName: {
S: payload.userName
},
orderDate: {
S: payload.orderDate
},
expectedDeliveryDate: {
S: payload.expectedDeliveryDate
},
shipingAddress: {
S: payload.shipingAddress
},
billingAddress: {
S: payload.billingAddress
}
}
Query
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.
xxxxxxxxxx
%dw 2.0
output application/json
---
{
":orderDate": {"S": attributes.queryParams.date}
}
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:
xxxxxxxxxx
{
"scannedCount": 1,
"lastEvaluatedKey": null,
"count": 1,
"consumedCapacity": null,
"items": [
{
"orderId": {
"ss": null,
"nullvalue": null,
"b": null,
"bool": null,
"ns": null,
"l": null,
"m": null,
"n": null,
"bs": null,
"s": "618d6793-aaaa-42d6-8d4d-53e0da18261a"
},
"expectedDeliveryDate": {
"ss": null,
"nullvalue": null,
"b": null,
"bool": null,
"ns": null,
"l": null,
"m": null,
"n": null,
"bs": null,
"s": "2020-10-09"
},
"shipingAddress": {
"ss": null,
"nullvalue": null,
"b": null,
"bool": null,
"ns": null,
"l": null,
"m": null,
"n": null,
"bs": null,
"s": "Vancouver, Canada"
},
"billingAddress": {
"ss": null,
"nullvalue": null,
"b": null,
"bool": null,
"ns": null,
"l": null,
"m": null,
"n": null,
"bs": null,
"s": "Vancouver, Canada"
},
"userName": {
"ss": null,
"nullvalue": null,
"b": null,
"bool": null,
"ns": null,
"l": null,
"m": null,
"n": null,
"bs": null,
"s": "Mule"
},
"orderDate": {
"ss": null,
"nullvalue": null,
"b": null,
"bool": null,
"ns": null,
"l": null,
"m": null,
"n": null,
"bs": null,
"s": "2020-09-26"
}
}
]
}
Below dataweave will transform the output data into the required format:
xxxxxxxxxx
%dw 2.0
output application/json
---
payload.items default [] map {
orderId: $.orderId.s,
expectedDeliveryDate: $.expectedDeliveryDate.s,
shipingAddress: $.shipingAddress.s,
billingAddress: $.billingAddress.s,
userName: $.userName.s,
orderDate: $.orderDate.s
}
Conclusion
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.
Github Repository
Opinions expressed by DZone contributors are their own.
Comments