Amazon Dynamo DB Connector Operations Walkthrough in Mule 4, Part 1
This article provides a step-by-step guide on how to use multiple operations of Dynamo DB connector, along with their JSON requests and configurations.
Join the DZone community and get the full member experience.
Join For FreeAmazon Dynamo DB is a fully managed NoSQL database service that provides fast and predictable performance with seamless scalability. Dynamo DB lets you offload the administrative burdens of operating and scaling a distributed database so that you don't have to worry about hardware provisioning, setup and configuration, replication, software patching, or cluster scaling.
The following provides a quick example of how to use the “Amazon Dynamo DB Connector Operations – Mule 4” using Anypoint Studio. While there is a lot of documentation available on this connector usage, none could be found that specifically shows how to structure the JSON requests, required for multiple connector operations.
Below is the list of operations considered for Part 1 (remaining operations will be continued in Part 2).
- Create Table
- Describe Table
- List Tables
- Put Item
- Batch Put Items
- Batch Put Item: Single Table
- Batch Put Item: Multiple Table
- Get Item
- Batch Get Item
- Batch Get Item: Single Table
- Batch Get Item: Multiple Table
- Update Item
- Delete Item
- Batch Delete Item
- Batch Delete Item: Single Table
- Batch Delete Item: Multiple Table
Prerequisites
To use the Amazon Dynamo DB connector, we need to have the following:
- An AWS account with accessibility to the Amazon Dynamo DB
- AWS Identity and Access Management (IAM) credentials including access and secret keys
Use Case
We have considered two tables in Dynamo DB with employee-specific information in each of them. Below are the tables configurations.
Table 1: AWS-EMPLOYEE-DETAILS-TABLE
AWS-EMPLOYEE-DETAILS-TABLE | ||
---|---|---|
Column Name | Datatype | Attributes |
employee_alphanumeric |
String | primary-partition-key |
employee_unique_id |
Number | primary-sort-key |
employee_name |
String | |
isActive |
Boolean | |
employee_previous_organizations |
StringSet | |
employee_previous_organizations_id |
NumberSet | |
employee_detail |
Map | |
employee_secondary_job_titles |
List | |
employee_fax_info |
nullvalue |
Table 2: AWS-EMPLOYEE-SALARY-TABLE
AWS-EMPLOYEE-SALARY-TABLE
|
||
---|---|---|
Column Name | Datatype | Attributes |
employee_unique_id |
Number | primary-partition-key |
employee_salary |
Number | |
employee_currency |
String | |
employee_department | String |
Getting Started
Create a sample Mule application in Anypoint Studio, as shown below.
Let's start with our first operation.
1. Create Table
This operation will create a new table, with all provided configurations.
2. Describe Table
This operation is used to get information about the table, including the current status of the table, when it was created, the primary key schema, and any indexes on the table.
Response:
{
"globalSecondaryIndexes": null,
"latestStreamLabel": null,
"latestStreamArn": null,
"keySchema": [
{
"attributeName": "employee_alphanumeric",
"keyType": "HASH"
},
{
"attributeName": "employee_unique_id",
"keyType": "RANGE"
}
],
"provisionedThroughput": {
"writeCapacityUnits": 500,
"readCapacityUnits": 500,
"numberOfDecreasesToday": 0,
"lastIncreaseDateTime": null,
"lastDecreaseDateTime": null
},
"tableStatus": "ACTIVE",
"tableName": "AWS-EMPLOYEE-DETAILS-TABLE",
"tableArn": "xxx:aws:dynamodb:xx-xxxx-xx:xxxxxxxxxxx:table/AWS-EMPLOYEE-DETAILS-TABLE",
"itemCount": 0,
"tableSizeBytes": 0,
"streamSpecification": null,
"attributeDefinitions": [
{
"attributeType": "STRING",
"attributeName": "employee_alphanumeric"
},
{
"attributeType": "NUMBER",
"attributeName": "employee_unique_id"
}
],
"localSecondaryIndexes": null,
"creationDateTime": "2022-02-17T10:54:51.339"
}
3. List Tables
This operation will return all the table names associated with your current AWS account and endpoint.
Response:
{
"tableNames": [
"sample-table-name-1",
"sample-table-name-2",
"sample-table-name-3",
"sample-table-name-4",
"sample-table-name-5"
],
"lastEvaluatedTableName": "sample-table-name-5"
}
4. Put Item
Use this operation when the requirement is to create a new item or replace an existing item in the database. If an item that has the same primary key as a new item already exists in a specified table, the new item replaces an existing item.
The below request will insert a single record into table 'AWS-EMPLOYEE-DETAILS-TABLE':
{
"employee_alphanumeric": {
"S": "ALPHA1"
},
"employee_unique_id": {
"N": 1
},
"employee_name": {
"S": "John"
},
"isActive": {
"Bool": true
},
"employee_previous_organizations": {
"ss": ["Google",
"Microsoft",
"Meta/Facebook"]
},
"employee_previous_organizations_id": {
"ns": [123456,
213456,
654321]
},
"employee_detail": {
"M": {
"unique_id": {
"N": 1
},
"residential": {
"M": {
"city": {
"S": "New York"
},
"country": {
"S": "United States"
},
"countryCode": {
"N": "212"
},
"contact_detail": {
"N": "9876543210"
},
"employee_fax_info": {
"nullvalue": true
}
}
}
}
},
"employee_secondary_job_titles": {
"L": [{
"S": "Mulesoft Architect"
},
{
"S": "Technical Lead"
},
{
"S": "Senior Associate"
},
{
"S": "Senior Mulesoft Developer"
}]
}
}
5a. Batch Put Item: Single Item
This operation inserts multiple records/items in one or more tables.
The below request has 2 items with a unique combination of primary key and sort key. Once executed, it inserts both of these items in a single table "AWS-EMPLOYEE-DETAILS-TABLE".
{
"AWS-EMPLOYEE-DETAILS-TABLE": [{ //Table1
"PutRequest": { // Item 1
"employee_alphanumeric": {
"S": "ALPHA2"
} as Object {
class:"org.mule.extension.dynamodb.api.model.AttributeValue"
},
"employee_unique_id": {
"N": 2
} as Object {
class:"org.mule.extension.dynamodb.api.model.AttributeValue"
},
"employee_name": {
"S": "Peter"
} as Object {
class:"org.mule.extension.dynamodb.api.model.AttributeValue"
},
"isActive": {
"Bool": true
} as Object {
class:"org.mule.extension.dynamodb.api.model.AttributeValue"
},
"employee_previous_organizations": {
"ss": ["IBM","Tesla","LinkedIn"]
} as Object {
class:"org.mule.extension.dynamodb.api.model.AttributeValue"
},
"employee_previous_organizations_id": {
"ns": [911123,4697896,654111]
} as Object {
class:"org.mule.extension.dynamodb.api.model.AttributeValue"
},
"employee_detail": {
"M": {
"unique_id": {
"N": 2
},
"residential": "M": {
"city": {
"S": "New York"
},
"country": {
"S": "United States"
},
"countryCode": {
"N": "212"
},
"contact_detail": {
"N": "9878987654"
},
"employee_fax_info": {
"nullvalue": true
}
}
}
} as Object {
class:"org.mule.extension.dynamodb.api.model.AttributeValue"
},
"employee_secondary_job_titles": {
"L": [{
"S": "Mulesoft Architect"
},{
"S": "Senior Mulesoft Lead"
}]
} as Object {
class:"org.mule.extension.dynamodb.api.model.AttributeValue"
}
}
} as Object {
class:"org.mule.extension.dynamodb.api.model.WriteRequest"
},
{
"PutRequest": { // Item 2
"employee_alphanumeric": {
"S": "ALPHA3"
} as Object {
class:"org.mule.extension.dynamodb.api.model.AttributeValue"
},
"employee_unique_id": {
"N": 3
} as Object {
class:"org.mule.extension.dynamodb.api.model.AttributeValue"
},
"employee_name": {
"S": "Kale"
} as Object {
class:"org.mule.extension.dynamodb.api.model.AttributeValue"
},
"isActive": {
"Bool": true
} as Object {
class:"org.mule.extension.dynamodb.api.model.AttributeValue"
},
"employee_previous_organizations": {
"ss": ["Alphabet","Skelia"]
} as Object {
class:"org.mule.extension.dynamodb.api.model.AttributeValue"
},
"employee_previous_organizations_id": {
"ns": [456843,1695334]
} as Object {
class:"org.mule.extension.dynamodb.api.model.AttributeValue"
},
"employee_detail": {
"M": {
"unique_id": {
"N": 3
},
"residential": "M": {
"city": {
"S": "New York"
},
"country": {
"S": "United States"
},
"countryCode": {
"N": "212"
},
"contact_detail": {
"N": "5633213210"
},
"employee_fax_info": {
"nullvalue": true
}
}
}
} as Object {
class:"org.mule.extension.dynamodb.api.model.AttributeValue"
},
"employee_secondary_job_titles": {
"L": [{
"S": "Mulesoft Director"
},{
"S": "Mulesoft Practice Lead"
}]
} as Object {
class:"org.mule.extension.dynamodb.api.model.AttributeValue"
}
}
} as Object {
class:"org.mule.extension.dynamodb.api.model.WriteRequest"
}]
}
5b. Batch Put Item: Multiple Table
This operation inserts multiple records or items in one or more tables. This example shows how to insert multiple records in multiple tables.
The request below has in total 5 items with a unique combination of primary key and sort key. This request will insert two items in table "AWS-EMPLOYEE-DETAILS-TABLE" and three items in table "AWS-EMPLOYEE-SALARY-TABLE".
{
"AWS-EMPLOYEE-DETAILS-TABLE": [{
"PutRequest": {
"employee_alphanumeric": {
"S": "ALPHA2"
} as Object {
class:"org.mule.extension.dynamodb.api.model.AttributeValue"
},
"employee_unique_id": {
"N": 2
} as Object {
class:"org.mule.extension.dynamodb.api.model.AttributeValue"
},
"employee_name": {
"S": "Peter"
} as Object {
class:"org.mule.extension.dynamodb.api.model.AttributeValue"
},
"isActive": {
"Bool": true
} as Object {
class:"org.mule.extension.dynamodb.api.model.AttributeValue"
},
"employee_previous_organizations": {
"ss": ["IBM","Tesla","LinkedIn"]
} as Object {
class:"org.mule.extension.dynamodb.api.model.AttributeValue"
},
"employee_previous_organizations_id": {
"ns": [911123,4697896,654111]
} as Object {
class:"org.mule.extension.dynamodb.api.model.AttributeValue"
},
"employee_detail": {
"M": {
"unique_id": {
"N": 2
},
"residential": "M": {
"city": {
"S": "New York"
},
"country": {
"S": "United States"
},
"countryCode": {
"N": "212"
},
"contact_detail": {
"N": "9878987654"
},
"employee_fax_info": {
"nullvalue": true
}
}
}
} as Object {
class:"org.mule.extension.dynamodb.api.model.AttributeValue"
},
"employee_secondary_job_titles": {
"L": [{
"S": "Mulesoft Architect"
},{
"S": "Senior Mulesoft Lead"
}]
} as Object {
class:"org.mule.extension.dynamodb.api.model.AttributeValue"
}
}
} as Object {
class:"org.mule.extension.dynamodb.api.model.WriteRequest"
},
{
"PutRequest": {
"employee_alphanumeric": {
"S": "ALPHA3"
} as Object {
class:"org.mule.extension.dynamodb.api.model.AttributeValue"
},
"employee_unique_id": {
"N": 3
} as Object {
class:"org.mule.extension.dynamodb.api.model.AttributeValue"
},
"employee_name": {
"S": "Kale"
} as Object {
class:"org.mule.extension.dynamodb.api.model.AttributeValue"
},
"isActive": {
"Bool": true
} as Object {
class:"org.mule.extension.dynamodb.api.model.AttributeValue"
},
"employee_previous_organizations": {
"ss": ["Alphabet","Skelia"]
} as Object {
class:"org.mule.extension.dynamodb.api.model.AttributeValue"
},
"employee_previous_organizations_id": {
"ns": [456843,1695334]
} as Object {
class:"org.mule.extension.dynamodb.api.model.AttributeValue"
},
"employee_detail": {
"M": {
"unique_id": {
"N": 3
},
"residential": "M": {
"city": {
"S": "New York"
},
"country": {
"S": "United States"
},
"countryCode": {
"N": "212"
},
"contact_detail": {
"N": "5633213210"
},
"employee_fax_info": {
"nullvalue": true
}
}
}
} as Object {
class:"org.mule.extension.dynamodb.api.model.AttributeValue"
},
"employee_secondary_job_titles": {
"L": [{
"S": "Mulesoft Director"
},{
"S": "Mulesoft Practice Lead"
}]
} as Object {
class:"org.mule.extension.dynamodb.api.model.AttributeValue"
}
}
} as Object {
class:"org.mule.extension.dynamodb.api.model.WriteRequest"
}],
"AWS-EMPLOYEE-SALARY-TABLE": [{
"PutRequest": {
"employee_unique_id": {
"N": 1
} as Object {
class:"org.mule.extension.dynamodb.api.model.AttributeValue"
},
"employee_salary": {
"N": 100000
} as Object {
class:"org.mule.extension.dynamodb.api.model.AttributeValue"
},
"employee_currency": {
"S": "US DOLLAR"
} as Object {
class:"org.mule.extension.dynamodb.api.model.AttributeValue"
},
"employee_department": {
"S": "Information Technology"
} as Object {
class:"org.mule.extension.dynamodb.api.model.AttributeValue"
}
}
} as Object {
class:"org.mule.extension.dynamodb.api.model.WriteRequest"
},
{
"PutRequest": {
"employee_unique_id": {
"N": 2
} as Object {
class:"org.mule.extension.dynamodb.api.model.AttributeValue"
},
"employee_salary": {
"N": 200000
} as Object {
class:"org.mule.extension.dynamodb.api.model.AttributeValue"
},
"employee_currency": {
"S": "US DOLLAR"
} as Object {
class:"org.mule.extension.dynamodb.api.model.AttributeValue"
},
"employee_department": {
"S": "Information Technology"
} as Object {
class:"org.mule.extension.dynamodb.api.model.AttributeValue"
}
}
} as Object {
class:"org.mule.extension.dynamodb.api.model.WriteRequest"
},
{
"PutRequest": {
"employee_unique_id": {
"N": 3
} as Object {
class:"org.mule.extension.dynamodb.api.model.AttributeValue"
},
"employee_salary": {
"N": 300000
} as Object {
class:"org.mule.extension.dynamodb.api.model.AttributeValue"
},
"employee_currency": {
"S": "US DOLLAR"
} as Object {
class:"org.mule.extension.dynamodb.api.model.AttributeValue"
},
"employee_department": {
"S": "Information Technology"
} as Object {
class:"org.mule.extension.dynamodb.api.model.AttributeValue"
}
}
} as Object {
class:"org.mule.extension.dynamodb.api.model.WriteRequest"
}]
}
6. Get Item
The Get Item operation returns a set of attributes for the item with the given primary key. If there is no matching item, Get Item does not return any data.
7a. Batch Get Item: Single Item
The batch Get Item operation returns the attributes of one or more items from one or more tables. You identify the requested item by primary key and sort key combination.
The below request has 2 items with their own primary key and sort key. This request will search for both records in table "AWS-EMPLOYEE-DETAILS-TABLE".
{
"AWS-EMPLOYEE-DETAILS-TABLE" : {
"Keys": [
{
"employee_alphanumeric" : {"S":"ALPHA1"} as Object {class:"org.mule.extension.dynamodb.api.model.AttributeValue"},
"employee_unique_id":{"N": 1} as Object {class:"org.mule.extension.dynamodb.api.model.AttributeValue"}
},
{
"employee_alphanumeric" : {"S":"ALPHA2"} as Object {class:"org.mule.extension.dynamodb.api.model.AttributeValue"},
"employee_unique_id":{"N": 2} as Object {class:"org.mule.extension.dynamodb.api.model.AttributeValue"}
}
]
} as Object {class:"org.mule.extension.dynamodb.api.model.KeysAndAttributes"}
}
7b. Batch Get Item: Multiple Table
The batch Get Item operation returns the attributes of one or more items from one or more tables. You identify the requested item by primary key.
The below request has a total of 4 items with their own primary key and sort key. This request will search for two records in table "AWS-EMPLOYEE-DETAILS-TABLE" and the remaining 2 records in table "AWS-EMPLOYEE-SALARY-TABLE".
{
"AWS-EMPLOYEE-DETAILS-TABLE": {
"Keys": [{
"employee_alphanumeric": {
"S": "ALPHA1"
} as Object {
class:"org.mule.extension.dynamodb.api.model.AttributeValue"
},
"employee_unique_id": {
"N": 1
} as Object {
class:"org.mule.extension.dynamodb.api.model.AttributeValue"
}
},
{
"employee_alphanumeric": {
"S": "ALPHA2"
} as Object {
class:"org.mule.extension.dynamodb.api.model.AttributeValue"
},
"employee_unique_id": {
"N": 2
} as Object {
class:"org.mule.extension.dynamodb.api.model.AttributeValue"
}
}]
} as Object {
class:"org.mule.extension.dynamodb.api.model.KeysAndAttributes"
},
"AWS-EMPLOYEE-SALARY-TABLE": {
"Keys": [{
"employee_unique_id": {
"N": 1
} as Object {
class:"org.mule.extension.dynamodb.api.model.AttributeValue"
}
},
{
"employee_unique_id": {
"N": 2
} as Object {
class:"org.mule.extension.dynamodb.api.model.AttributeValue"
}
}]
} as Object {
class:"org.mule.extension.dynamodb.api.model.KeysAndAttributes"
}
}
8. Update Item
This operation edits an existing item's attributes or adds a new item to the table if it does not already exist.
The below request will search for an item with a primary key and sort key combination, and update "isActive" = "false" and "employee_name" = "Prashant G" (as per the above connector configurations).
{
"employee_alphanumeric": {"S": "ALPHA1"} as Object {
class:"org.mule.extension.dynamodb.api.model.AttributeValue"
},
"employee_unique_id": {"N": 1} as Object {
class:"org.mule.extension.dynamodb.api.model.AttributeValue"
}
}
9. Delete Item
This operation deletes a single item in a table. Items are identified using a primary key and sort key combination.
The below request will delete an item from table "AWS-EMPLOYEE-DETAILS-TABLE", with the primary key as "Alpha1" and the sort key as "1".
{
"employee_alphanumeric": {
"S": "ALPHA1"
} as Object {
class:"org.mule.extension.dynamodb.api.model.AttributeValue"
},
"employee_unique_id": {
"N": 1
} as Object {
class:"org.mule.extension.dynamodb.api.model.AttributeValue"
}
}
10a. Batch Delete Item: Single Table
This operation deletes multiple items in one or more tables.
The below request will search for 2 items in table "AWS-EMPLOYEE-DETAILS-TABLE" and delete them.
{
"AWS-EMPLOYEE-DETAILS-TABLE": [{
"DeleteRequest": {
"employee_alphanumeric": {
"S": "ALPHA2"
} as Object {
class: "org.mule.extension.dynamodb.api.model.AttributeValue"
},
"employee_unique_id": {
"N": "2"
} as Object {
class: "org.mule.extension.dynamodb.api.model.AttributeValue"
}
}
} as Object {
class : "org.mule.extension.dynamodb.api.model.WriteRequest"
},
{
"DeleteRequest": {
"employee_alphanumeric": {
"S": "ALPHA3"
} as Object {
class: "org.mule.extension.dynamodb.api.model.AttributeValue"
},
"employee_unique_id": {
"N": "3"
} as Object {
class: "org.mule.extension.dynamodb.api.model.AttributeValue"
}
}
} as Object {
class : "org.mule.extension.dynamodb.api.model.WriteRequest"
}]
}
10b. Batch Delete Items: Multiple Table
This operation deletes multiple items in one or more tables.
The below request has in total 4 items with primary key and sort key combinations. The respective two items will be deleted from table "AWS-EMPLOYEE-DETAILS-TABLE" and the remaining two will be deleted from table "AWS-EMPLOYEE-SALARY-TABLE".
{
"AWS-EMPLOYEE-DETAILS-TABLE": [{
"DeleteRequest": {
"employee_alphanumeric": {
"S": "ALPHA2"
} as Object {
class: "org.mule.extension.dynamodb.api.model.AttributeValue"
},
"employee_unique_id": {
"N": 2
} as Object {
class: "org.mule.extension.dynamodb.api.model.AttributeValue"
}
}
} as Object {
class : "org.mule.extension.dynamodb.api.model.WriteRequest"
},
{
"DeleteRequest": {
"employee_alphanumeric": {
"S": "ALPHA3"
} as Object {
class: "org.mule.extension.dynamodb.api.model.AttributeValue"
},
"employee_unique_id": {
"N": 3
} as Object {
class: "org.mule.extension.dynamodb.api.model.AttributeValue"
}
}
} as Object {
class : "org.mule.extension.dynamodb.api.model.WriteRequest"
}],
"AWS-EMPLOYEE-SALARY-TABLE": [{
"DeleteRequest": {
"employee_unique_id": {
"N": 2
} as Object {
class: "org.mule.extension.dynamodb.api.model.AttributeValue"
}
}
} as Object {
class : "org.mule.extension.dynamodb.api.model.WriteRequest"
},
{
"DeleteRequest": {
"employee_unique_id": {
"N": 3
} as Object {
class: "org.mule.extension.dynamodb.api.model.AttributeValue"
}
}
} as Object {
class : "org.mule.extension.dynamodb.api.model.WriteRequest"
}]
}
Conclusion
We have seen the working examples of 10 operations of Dynamo DB connector, with 14 different JSON request formats. The remaining operations will be continued in our next section, Part 2.
Stay Tuned. Thank You and Happy Learning!
Opinions expressed by DZone contributors are their own.
Comments