This blog post provides quick examples of how to use the Amazon DynamoDB Connector in Mule 4 using Anypoint Studio. There is a lot of documentation available on this connector’s usage, but none could be found that specifically shows how to structure the JSON requests, which are required for all DynamoDB connector operations.
This article explains all the connector operations along with their JSON requests, with simple use case scenarios.
We have considered all available DynamoDB data types for this use case, so you will have a very good understanding and usage of different DynamoDB data types.
Operations
Below is the list of connector operations considered for the first part of this series.
Use Case
To better understand the DynamoDB operations, we have considered a use case of the continents & their countries. All this data is segregated based on types of information & stored into two DynamoDB tables.
Each continent consists of multiple countries, & each country will have its own specific information like unique id, available city name, specialties, available zip codes, year-wise populations, etc. All this information is stored in the table “CONTINENT-COUNTRY-INFO-TABLE”.
Along with the above information, countries also have some classified information, like available agents in-country and intelligence agency information. All this classified information is stored in the table “COUNTRY-CLASIFIED-INFO-TABLE”.
This use case has taken into consideration (almost) all available DynamoDB connector operations and their data types for clear understanding.
CONTINENT-COUNTRY-INFO-TABLE
Column Name | Data Type | Attributes |
continent_name | String | primary-partition-key |
country_unique_id | Number | primary-sort-key |
country_name | String | |
isActive | Boolean | |
country_specialities | StringSet | |
country_available_pincodes | NumberSet | |
country_yearwise_population | Map | |
country_city_name | List | |
country_geo_value | null | |
COUNTRY-CLASIFIED-INFO-TABLE
Column Name | Data Type | Attributes |
country_unique_id | Number | primary-partition-key |
country_agents | Number | |
country_intelligence_agency | String | |
1. Create Table
Let's start with our first operation. This operation will create a new table with the provided configurations.
Note: Execute this connector operation separately for both tables, with their own configurations.
Below are the provided parameters:
Table name: The name for the table.
AttributeDefinitions: This value is an array of attributes that describe the key schema for the table and indexes.
We have configured a composite primary key, which is a combination of primary key and sort key.
continent_name: Primary Partition Key
country_unique_id: Primary Sort key
A composite primary key gives you additional flexibility when querying data. For example, if you provide only the value for continent_name, DynamoDB retrieves all of the country_unique_id associated with it.
KeySchema: This value specifies the attributes that make up the primary key for a table or an index.
Read Capacity Units: 100 – The maximum number of strongly consistent reads per second.
Write Capacity Units: 100 – The maximum number of writes consumed per second.
Response
Once you execute this connector, the DynamoDB tables will be created and you will receive a SUCCESS Response.
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.
The only parameter we configure for this operation is the Table name.
Response
Once executed, the connector will return a response in the below format.
3. List Tables
This operation will return all the table names associated with your current AWS account and endpoint.
Below parameters are configured for this operation:
Exclusive start table name: The first table name that this operation will evaluate. If no table names are provided, it will list all tables.
Limit: A maximum number of table names to return. If this parameter is not specified, the limit is 100.
Response
Once executed, the connector will return a response in the below format.
4. Put Item
Use this operation when the need is to create a new item or replace an existing item in the DB. 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.
Request
Let's now create our first country “India” with other required information.
Response
Once you execute the above flow, you will see 1 record is created in the table “CONTINENT-COUNTRY-INFO-TABLE”.
5. Batch Put Item - Single Table
This operation inserts/puts multiple records or items in one table.
Request
Let's now create 2 country records “Singapore” and “Japan” in the same table “CONTINENT-COUNTRY-INFO-TABLE”.
Response
Once you execute the above flow, you will see both these records are created in table “CONTINENT-COUNTRY-INFO-TABLE”.
6. Batch Put Item - Multiple Tables
This operation Inserts/Puts multiple records or items in one or more tables.
Request
Let's now create one more country “Switzerland” in table “CONTINENT-COUNTRY-INFO-TABLE” and all 4 countries classified information (India, Singapore, Japan, Switzerland) in table “COUNTRY-CLASIFIED-INFO-TABLE”.
Response
Once you execute the above flow, you will see all these records are created in the respective table.
7. Get Item
The GetItem operation returns a set of attributes for the item with the given primary key. If there is no matching item GetItem does not return any data.
Request
Let's now fetch the record from table “CONTINENT-COUNTRY-INFO-TABLE”, having continent = ‘ASIA’ & country unique id = ‘2’.
Response
Once you execute the above flow, you will see the respective records in response.
8. Batch Get Item - Single Table
The Batch Get Item operation returns the attributes of one or more items from one table. You identify the requested item by primary key and sort key combination.
Request
Let’s now fetch two records from table “CONTINENT-COUNTRY-INFO-TABLE” , having continent = ‘ASIA’ & ‘Europe’ & country unique id = ‘1’ & ‘4’.
Response
Once you execute the above flow, you will see both these respective records in response.
9. Batch Get Item - Multiple Tables
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.
Request
Let's now fetch two records each from table “CONTINENT-COUNTRY-INFO-TABLE” & “COUNTRY-CLASIFIED-INFO-TABLE”.
Response
Once you execute the above flow, you will see both these respective records from each table, in the response.
10. Update Item
This operation edits an existing item's attributes or adds a new item to the table if it does not already exist.
Request
Let's now update the record with country name "India" with:
isActive = 'false'
country_name = 'India/Bharat'
Response
Once the above request is served, both these columns (isActive & country_name) will be updated respectively with the new values.
11. Query
The Query operation finds items based on primary key values. You can query any table that has or secondary index that has a composite primary key (a partition key and a sort key).
Request
Now, Let’s query the table “CONTINENT-COUNTRY-INFO-TABLE” and fetch all the records that satisfy the following criteria:
continent_name = 'ASIA'
isActive = 'true'
Response
Once you execute the above flow, you will find 3 records in the response.
12. Scan
The scan operation returns one or more items or item attributes By accessing every item in a table or secondary index. To have DynamoDB return fewer items, you can provide a FilterExpression operation.
Request
Let's scan the table “CONTINENT-COUNTRY-INFO-TABLE” and fetch all the records that satisfy the following criteria:
continent_name = 'Europe'
isActive = 'true'
Response
Once you execute the above flow, you will find only 1 record in response.
13. Delete Item
This operation deletes a single item in a table. Items are identified using primary key and sort key combination.
Request
Let's now delete the record with country = ‘Japan’ from table "CONTINENT-COUNTRY-INFO-TABLE"
Response
Once executed, the record will be deleted from the table.
14. Batch Delete Item - Single Table
This operation deletes multiple items in one table.
Request
We’ll now delete 2 records in a single request. Let’s delete both countries “Singapore” & “Switzerland” from the table “CONTINENT-COUNTRY-INFO-TABLE”.
Response
Once executed, both these records will have been deleted from the table.
15. Batch Delete Item - Multiple Tables
This operation deletes multiple items from multiple tables.
Request
We’ll delete country ‘India’ from table “CONTINENT-COUNTRY-INFO-TABLE” and its classified information from the table “COUNTRY-CLASIFIED-INFO-TABLE”.
Response
Once executed, all 3 records will have been deleted from both tables.
Conclusion
We have seen the working examples of 15 operations of DynamoDB connectors.
Stay tuned for the next part.
Thank you and happy learning!!
Comments