DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
Securing Your Software Supply Chain with JFrog and Azure
Register Today

Trending

  • How to Submit a Post to DZone
  • DZone's Article Submission Guidelines
  • Is Podman a Drop-In Replacement for Docker?
  • Microservices With Apache Camel and Quarkus

Trending

  • How to Submit a Post to DZone
  • DZone's Article Submission Guidelines
  • Is Podman a Drop-In Replacement for Docker?
  • Microservices With Apache Camel and Quarkus
  1. DZone
  2. Data Engineering
  3. Databases
  4. DynamoDB Connector Mule 4

DynamoDB Connector Mule 4

In this article, take a look at a working example of the Put item and query operations in Mule 4.

Vikalp Bhalia user avatar by
Vikalp Bhalia
CORE ·
Riddhi Thacker user avatar by
Riddhi Thacker
CORE ·
Oct. 05, 20 · Tutorial
Like (2)
Save
Tweet
Share
6.24K Views

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.

Scenario

We have an order table, and below is the table structure:

JSON
 




xxxxxxxxxx
1


 
1
  {
2
    "orderId": "10c9b171-37e9-456e-aeaf-d88a0d38eea5",
3
    "userName": "Mule",
4
    "orderDate": "2020-09-26",
5
    "expectedDeliveryDate": "2020-10-01",
6
    "shipingAddress": "Toronto, Canada",
7
    "billingAddress": "Toronto, Canada"
8
  }


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.

JSON
 




xxxxxxxxxx
1
24


 
1
%dw 2.0
2
output application/json
3
---
4
{
5
        orderId:{
6
            S: vars.orderId
7
        },
8
        userName: {
9
            S: payload.userName
10
        },
11
        orderDate: {
12
            S: payload.orderDate
13
        },
14
        expectedDeliveryDate: {
15
            S: payload.expectedDeliveryDate
16
        },
17
        shipingAddress: {
18
            S: payload.shipingAddress
19
        },
20
        billingAddress: {
21
            S: payload.billingAddress
22
        }
23
 
          
24
}



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.

JSON
 




xxxxxxxxxx
1


 
1
%dw 2.0
2
output application/json
3
---
4
{
5
    ":orderDate": {"S": attributes.queryParams.date}
6
}



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:

JSON
 




xxxxxxxxxx
1
82


 
1
{
2
    "scannedCount": 1,
3
    "lastEvaluatedKey": null,
4
    "count": 1,
5
    "consumedCapacity": null,
6
    "items": [
7
        {
8
            "orderId": {
9
                "ss": null,
10
                "nullvalue": null,
11
                "b": null,
12
                "bool": null,
13
                "ns": null,
14
                "l": null,
15
                "m": null,
16
                "n": null,
17
                "bs": null,
18
                "s": "618d6793-aaaa-42d6-8d4d-53e0da18261a"
19
            },
20
            "expectedDeliveryDate": {
21
                "ss": null,
22
                "nullvalue": null,
23
                "b": null,
24
                "bool": null,
25
                "ns": null,
26
                "l": null,
27
                "m": null,
28
                "n": null,
29
                "bs": null,
30
                "s": "2020-10-09"
31
            },
32
            "shipingAddress": {
33
                "ss": null,
34
                "nullvalue": null,
35
                "b": null,
36
                "bool": null,
37
                "ns": null,
38
                "l": null,
39
                "m": null,
40
                "n": null,
41
                "bs": null,
42
                "s": "Vancouver, Canada"
43
            },
44
            "billingAddress": {
45
                "ss": null,
46
                "nullvalue": null,
47
                "b": null,
48
                "bool": null,
49
                "ns": null,
50
                "l": null,
51
                "m": null,
52
                "n": null,
53
                "bs": null,
54
                "s": "Vancouver, Canada"
55
            },
56
            "userName": {
57
                "ss": null,
58
                "nullvalue": null,
59
                "b": null,
60
                "bool": null,
61
                "ns": null,
62
                "l": null,
63
                "m": null,
64
                "n": null,
65
                "bs": null,
66
                "s": "Mule"
67
            },
68
            "orderDate": {
69
                "ss": null,
70
                "nullvalue": null,
71
                "b": null,
72
                "bool": null,
73
                "ns": null,
74
                "l": null,
75
                "m": null,
76
                "n": null,
77
                "bs": null,
78
                "s": "2020-09-26"
79
            }
80
        }
81
    ]
82
}


Below dataweave will transform the output data into the required format:

JSON
 




xxxxxxxxxx
1
11


 
1
%dw 2.0
2
output application/json
3
---
4
payload.items default [] map {
5
    orderId: $.orderId.s,
6
    expectedDeliveryDate: $.expectedDeliveryDate.s,
7
    shipingAddress: $.shipingAddress.s,
8
    billingAddress: $.billingAddress.s,
9
    userName: $.userName.s,
10
    orderDate: $.orderDate.s
11
}



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

dynamo-db-demo

Database Connector (mathematics)

Opinions expressed by DZone contributors are their own.

Trending

  • How to Submit a Post to DZone
  • DZone's Article Submission Guidelines
  • Is Podman a Drop-In Replacement for Docker?
  • Microservices With Apache Camel and Quarkus

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: