Getting started Community Training Tutorials Documentation APIs, AI & Tools
The following are common use cases for Amazon DynamoDB Connector:
Creating an Amazon DynamoDB table
Deleting an Amazon DynamoDB table
Performing an Amazon DynamoDB table scan
This example shows how to use Amazon DynamoDB connector to create an Amazon DynamoDB table.
The following screenshot shows an Anypoint Studio flow for creating an Amazon DynamoDB table:
Follow these steps to start creating an Amazon DynamoDB table:
Create a new Mule project in Studio.
Add the following properties to the mule-artifact.properties file to store your Amazon DynamoDB credentials:
config.accesskey=<Access Key>
config.secretkey=<Secret Key>
Place the mule-artifact.properties file in the project’s src/main/resources directory.
Configure HTTP Listener to listen for HTTP requests on the /createtable path:
In the Mule Palette view, search for http and select the Listener operation.
Drag the Listener operation onto the Studio canvas.
On the Listener tab, click the plus sign (+) next to the Connector configuration field to access the global element configuration fields for HTTP Listener.
In the Host field, select localhost:
localhost in the Host field to listen for HTTP requests on your local computer.Click OK.
In the Path field on the Listener tab, enter /createtable:
/createtable in the Path field to listen for HTTP requests on the /createtable path.Configure the Create Table operation to create a table named Issues:
In the Mule Palette view, click Search in Exchange and search for amazon dynamodb.
Select Amazon DynamoDB Connector, click Add>, and then click Finish.
Drag the Create Table operation to the right of Listener on the Studio canvas.
Click the Global Elements link below the flow.
Add the properties file you created earlier to the Configuration properties field, as described in Use Property Placeholders for Property Values.
Click Create.
Expand Connector Configuration.
Select Amazon DynamoDB Configuration and click OK.
Complete the following fields:
| Parameter | Description | Value |
|---|---|---|
Name |
Configuration name |
|
Access Key |
Alphanumeric text string that uniquely identifies the user who owns the account |
|
Secret Key |
Key that acts as a password |
|
Region Endpoint |
Region for the Amazon DynamoDB client |
|
The following screenshot shows an example of configuring a global element for Amazon DynamoDB Connector:
Click Test Connection to confirm that Mule can connect with the Amazon DynamoDB instance:
If the connection is successful, click OK to save the configuration.
If the connection is unsuccessful, review or correct any incorrect parameters, and then test again.
Click OK.
On the Create table tab, configure the following fields:
| Field | Description | User Action |
|---|---|---|
Table name |
The name of the table |
Enter |
Attribute definitions |
An array of attributes that describe the key schema for the table and its indexes |
Select
|
Key schemas |
The attributes that make up the primary key for the table or one of its indexes |
Select
|
Read capacity units |
The maximum number of strongly consistent reads consumed per second before Amazon DynamoDB returns a throttling exception |
Enter |
Write capacity units |
The maximum number of writes consumed per second before Amazon DynamoDB returns a throttling exception |
Enter |
Global secondary indexes |
An optional, additional key structure for the table |
Enter |
The following screenshot shows an example of configuring the Create table operation:
Create table operation fields.In this screenshot:
The first item specifies the name of the component that represents the Create table operation in the flow.
The second item specifies the name of the table to create.
The third item defines the attributes of the new table.
The fourth item defines the key schemas of the new table.
The fifth item specifies the maximum number of strongly consistent reads consumed per second and strongly consistent writes consumed per second before Amazon DynamoDB returns a throttling exception.
The sixth item specifies a variable for the secondary index.
The XML flow for the create table example looks like this after you configure the HTTP Listener and Create table operations:
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:http="http://www.mulesoft.org/schema/mule/http"
xmlns:dynamodb="http://www.mulesoft.org/schema/mule/dynamodb"
xmlns="http://www.mulesoft.org/schema/mule/core"
xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/core
http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/dynamodb
http://www.mulesoft.org/schema/mule/dynamodb/current/mule-dynamodb.xsd
http://www.mulesoft.org/schema/mule/http
http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd">
<dynamodb:config
name="Amazon_DynamoDB_Configuration"
doc:name="Amazon DynamoDB Configuration">
<dynamodb:basic-connection
accessKey="${config.accesskey}"
secretKey="${config.secretkey}" />
</dynamodb:config>
<http:listener-config
name="HTTP_Listener_config"
doc:name="HTTP Listener config">
<http:listener-connection host="localhost"
port="8081" />
</http:listener-config>
<flow name="create-table-flow" >
<http:listener doc:name="Listener" config-ref="HTTP_Listener_config" path="/createtable"/>
<dynamodb:create-table doc:name="Create table" config-ref="Amazon_DynamoDB_Configuration" tableName="Issues" readCapacityUnits="5" writeCapacityUnits="5" globalSecondaryIndexes="#[vars.secondary]">
<dynamodb:attribute-definitions>
<dynamodb:attribute-definition attributeName="IssueId" attributeType="STRING" />
<dynamodb:attribute-definition attributeName="Title" attributeType="STRING" />
<dynamodb:attribute-definition attributeName="CreateDate" attributeType="STRING" />
<dynamodb:attribute-definition attributeName="DueDate" attributeType="STRING" />
</dynamodb:attribute-definitions>
<dynamodb:key-schemas>
<dynamodb:key-schema-element attributeName="IssueId" keyType="HASH" />
<dynamodb:key-schema-element attributeName="Title" keyType="RANGE" />
</dynamodb:key-schemas>
</dynamodb:create-table>
</flow>
</mule>
To add a global secondary index to the Issues table, add a variable before the <dynamodb:create-table> element in the XML.
The following example adds three secondary indexes to the Issues table:
CreateDateIndex, with the CreateDate partition key and the IssueId sort key
TitleIndex with the Title partition key and the IssueId sort key
DueDateIndex with the DueDate partition key
You must include these elements in the XML:
<Projection>, which specifies the attributes to copy from the table into the index.
In this example, the value ALL means that all attributes are copied.
The primary key attributes and index key attributes are automatically copied, no matter what value you set for the <Projection> element.
ProvisionedThroughput provisions throughput for read and write activity.
KeySchema specifies the key schema for the index.
<set-variable value='#[[
{
"IndexName": "CreateDateIndex",
"Projection": {
"ProjectionType": "ALL"
},
"ProvisionedThroughput": {
"WriteCapacityUnits": 5,
"ReadCapacityUnits": 5
},
"KeySchema": [
{
"KeyType": "HASH",
"AttributeName": "CreateDate"
},
{
"KeyType": "RANGE",
"AttributeName": "IssueId"
}
]
} as Object {
class: "org.mule.extension.dynamodb.api.model.GlobalSecondaryIndex"
},
{
"IndexName": "TitleIndex",
"Projection": {
"ProjectionType": "ALL"
},
"ProvisionedThroughput": {
"WriteCapacityUnits": 5,
"ReadCapacityUnits": 5
},
"KeySchema": [
{
"KeyType": "HASH",
"AttributeName": "Title"
},
{
"KeyType": "RANGE",
"AttributeName": "IssueId"
}
]
} as Object {
class: "org.mule.extension.dynamodb.api.model.GlobalSecondaryIndex"
},
{
"IndexName": "DueDateIndex",
"Projection": {
"ProjectionType": "ALL"
},
"ProvisionedThroughput": {
"WriteCapacityUnits": 5,
"ReadCapacityUnits": 5
},
"KeySchema": [
{
"KeyType": "HASH",
"AttributeName": "DueDate"
}
]
} as Object {
class: "org.mule.extension.dynamodb.api.model.GlobalSecondaryIndex"
}
]]' doc:name="Secondary" variableName="secondary"/>
Configure a Logger component to print to the Mule console the response generated by the Create Table operation:
In the Mule Palette view, search for logger.
Drag the Logger component to the right of Create Table on the Studio canvas.
Enter #[payload] in the Message field on the Logger tab.
#[payload] in the Message field to print the operation’s response to the Mule console.The XML for the complete create table example looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:http="http://www.mulesoft.org/schema/mule/http"
xmlns:dynamodb="http://www.mulesoft.org/schema/mule/dynamodb"
xmlns="http://www.mulesoft.org/schema/mule/core"
xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/core
http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/dynamodb
http://www.mulesoft.org/schema/mule/dynamodb/current/mule-dynamodb.xsd
http://www.mulesoft.org/schema/mule/http
http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd">
<dynamodb:config
name="Amazon_DynamoDB_Configuration"
doc:name="Amazon DynamoDB Configuration">
<dynamodb:basic-connection
accessKey="${config.accesskey}"
secretKey="${config.secretkey}" />
</dynamodb:config>
<http:listener-config
name="HTTP_Listener_config"
doc:name="HTTP Listener config">
<http:listener-connection host="localhost"
port="8081" />
</http:listener-config>
<flow name="create-table-flow" >
<http:listener doc:name="Listener" config-ref="HTTP_Listener_config" path="/createtable"/>
<set-variable value='#[[
{
"IndexName": "CreateDateIndex",
"Projection": {
"ProjectionType": "ALL"
},
"ProvisionedThroughput": {
"WriteCapacityUnits": 5,
"ReadCapacityUnits": 5
},
"KeySchema": [
{
"KeyType": "HASH",
"AttributeName": "CreateDate"
},
{
"KeyType": "RANGE",
"AttributeName": "IssueId"
}
]
} as Object {
class: "org.mule.extension.dynamodb.api.model.GlobalSecondaryIndex"
},
{
"IndexName": "TitleIndex",
"Projection": {
"ProjectionType": "ALL"
},
"ProvisionedThroughput": {
"WriteCapacityUnits": 5,
"ReadCapacityUnits": 5
},
"KeySchema": [
{
"KeyType": "HASH",
"AttributeName": "Title"
},
{
"KeyType": "RANGE",
"AttributeName": "IssueId"
}
]
} as Object {
class: "org.mule.extension.dynamodb.api.model.GlobalSecondaryIndex"
},
{
"IndexName": "DueDateIndex",
"Projection": {
"ProjectionType": "ALL"
},
"ProvisionedThroughput": {
"WriteCapacityUnits": 5,
"ReadCapacityUnits": 5
},
"KeySchema": [
{
"KeyType": "HASH",
"AttributeName": "DueDate"
}
]
} as Object {
class: "org.mule.extension.dynamodb.api.model.GlobalSecondaryIndex"
}
]]' doc:name="Secondary" variableName="secondary"/>
<dynamodb:create-table doc:name="Create table" config-ref="Amazon_DynamoDB_Configuration" tableName="Issues" readCapacityUnits="5" writeCapacityUnits="5" globalSecondaryIndexes="#[vars.secondary]">
<dynamodb:attribute-definitions>
<dynamodb:attribute-definition attributeName="IssueId" attributeType="STRING" />
<dynamodb:attribute-definition attributeName="Title" attributeType="STRING" />
<dynamodb:attribute-definition attributeName="CreateDate" attributeType="STRING" />
<dynamodb:attribute-definition attributeName="DueDate" attributeType="STRING" />
</dynamodb:attribute-definitions>
<dynamodb:key-schemas>
<dynamodb:key-schema-element attributeName="IssueId" keyType="HASH" />
<dynamodb:key-schema-element attributeName="Title" keyType="RANGE" />
</dynamodb:key-schemas>
</dynamodb:create-table>
<logger level="INFO" doc:name="Logger"
message="#[payload]"/>
</flow>
</mule>
Save and run the project as a Mule app:
In Package Explorer, right-click the project name and click Run As > Mule Application.
Open a browser and check the response after you enter the http://localhost:8081/createtable URL.
You see the generated response from the Create table operation in the Mule console.
This example shows how to use Amazon DynamoDB connector to delete an Amazon DynamoDB table.
The following screenshot shows an Anypoint Studio flow for deleting an Amazon DynamoDB table:
Follow these steps to start deleting an Amazon DynamoDB table:
Create a new Mule project in Studio.
Add the following properties to the mule-artifact.properties file to hold your Amazon DynamoDB credentials:
config.accesskey=<Access Key>
config.secretkey=<Secret Key>
Place the mule-artifact.properties file in the project’s src/main/resources directory.
Configure HTTP Listener to listen for HTTP requests on the /deletetable path:
In the Mule Palette view, search for http and select the Listener operation.
Drag the Listener operation onto the Studio canvas.
On the Listener tab, click the plus sign (+) next to the Connector configuration field to access the global element configuration fields for HTTP Listener.
In the Host field, select localhost and click OK.
In the Path field on the Listener tab, enter /deletetable.
Configure the Delete table operation to delete the Issues table.
In the Mule Palette view, click Search in Exchange and search for amazon dynamodb.
Select Amazon DynamoDB Connector, click Add>, and then click Finish.
Drag the Delete Table operation to the right of Listener on the Studio canvas.
Click the Global Elements link below the flow.
Add the properties file you created earlier to the Configuration properties field, as described in Use Property Placeholders for Property Values.
Click Create.
Expand Connector Configuration.
Select Amazon DynamoDB Configuration and click OK.
Complete the following fields:
| Parameter | Description | Value |
|---|---|---|
Name |
Configuration name |
|
Access Key |
Alphanumeric text string that uniquely identifies the user who owns the account |
|
Secret Key |
Key that acts as a password |
|
Region Endpoint |
Region for the Amazon DynamoDB client |
|
The corresponding XML configuration looks like this:
<dynamodb:config name="Amazon_DynamoDB_Configuration1" doc:name="Amazon DynamoDB Configuration" >
<dynamodb:basic-connection
accessKey="${config.accesskey}"
secretKey="${config.secretkey}"
/>
</dynamodb:config>
Click Test Connection to confirm that Mule can connect with the Amazon DynamoDB instance:
If the connection is successful, click OK to save the configuration.
If the connection is unsuccessful, review or correct any incorrect parameters, and then test again.
Click OK.
On the Delete table tab, set the value of Table name to Issues.
Configure a Logger component to print to the Mule console the response generated by the Delete table operation:
In the Mule Palette view, search for logger.
Drag the Logger component to the right of Delete Table on the Studio canvas.
On the Logger tab, enter #[payload] in the Message field.
The XML for the delete table example looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:dynamodb="http://www.mulesoft.org/schema/mule/dynamodb" xmlns:http="http://www.mulesoft.org/schema/mule/http"
xmlns="http://www.mulesoft.org/schema/mule/core"
xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/dynamodb http://www.mulesoft.org/schema/mule/dynamodb/current/mule-dynamodb.xsd">
<http:listener-config name="HTTP_Listener_config" doc:name="HTTP Listener config" >
<http:listener-connection host="localhost" port="8081" />
</http:listener-config>
<dynamodb:config name="Amazon_DynamoDB_Configuration" doc:name="Amazon DynamoDB Configuration" >
<dynamodb:basic-connection accessKey="${config.accesskey}" secretKey="${config.secretkey}" />
</dynamodb:config>
<flow name="delete-table-flow" >
<http:listener doc:name="Listener" config-ref="HTTP_Listener_config" path="/deletetable"/>
<dynamodb:delete-table doc:name="Delete table" config-ref="Amazon_DynamoDB_Configuration" tableName="Issues"/>
<logger level="INFO" doc:name="Logger" message="#[payload]"/>
</flow>
</mule>
Save and run the project as a Mule app:
In Package Explorer, right-click the project name and click Run As > Mule Application.
Open a browser and check the response after you enter the http://localhost:8081/deletetable URL.
You see the generated response from the Delete table operation in the Mule console.
This example shows how to use Amazon DynamoDB Connector to scan an Amazon DynamoDB table.
The following considerations apply to performing a scan using Amazon DynamoDB Connector:
The DynamoDB AWS SDK paginates results by dividing them into specific pages.
A 1-MB limit applies to returned results. When this limit is exceeded, you must perform another scan to receive the rest of the data.
To perform subsequent scans, apply the value of the LastEvaluatedKey field in the JSON response to the Exclusive start keys field in the Studio configuration of the Scan operation.
When the operation has returned all pages of data, the LastEvaluatedKey value in the JSON response is null.
The following screenshot shows an Anypoint Studio flow for scanning an Amazon DynamoDB table:
Follow these steps to start scanning an Amazon DynamoDB table:
Create a new Mule project in Studio.
Add the following properties to the mule-artifact.properties file to hold your Amazon DynamoDB credentials:
config.accesskey=<Access Key>
config.secretkey=<Secret Key>
Place the mule-artifact.properties file in the project’s src/main/resources directory.
Configure HTTP Listener to listen for HTTP requests on the /scantable path:
In the Mule Palette view, search for http and select the Listener operation.
Drag the Listener operation onto the Studio canvas.
On the Listener tab, click the plus sign (+) next to the Connector configuration field to access the global element configuration fields for HTTP Listener.
In the Host field, select localhost and click OK.
In the Path field on the Listener tab, enter /scantable.
In the Mule Palette view, click Search in Exchange and search for amazon dynamodb.
Select Amazon DynamoDB Connector, click Add>, and then click Finish.
Drag the Scan operation to the right of Listener on the Studio canvas.
Click the Global Elements link below the flow.
Add the properties file you created earlier to the Configuration properties field, as described in Use Property Placeholders for Property Values.
Click Create.
Expand Connector Configuration.
Select Amazon DynamoDB Configuration and click OK.
Complete the following fields:
| Parameter | Description | Value |
|---|---|---|
Name |
Configuration name |
|
Access Key |
Alphanumeric text string that uniquely identifies the user who owns the account |
|
Secret Key |
Key that acts as a password |
|
Region Endpoint |
Region for the Amazon DynamoDB client |
|
Click Test Connection to confirm that Mule can connect with the Amazon DynamoDB instance:
If the connection is successful, click OK to save the configuration.
If the connection is unsuccessful, review or correct any incorrect parameters, and then test again.
Click OK.
On the Scan tab, set the value of Table name to Issues.
Configure a Transform Message component to change the Scan operation output from Java to JSON:
In the Mule Palette view, search for transform.
Drag the Transform Message component to the right of Scan on the Studio canvas.
In the Output section of the Scan operation, change output/application/java to output/application/json.
Configure a Logger component to print to the Mule console the response generated by the Scan operation:
In the Mule Palette view, search for Logger.
Drag the Logger component to the right of Transform on the Studio canvas.
Enter #[payload] in the Message field on the Logger tab.
The XML for the scan table example looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core" xmlns:dynamodb="http://www.mulesoft.org/schema/mule/dynamodb"
xmlns:http="http://www.mulesoft.org/schema/mule/http"
xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/dynamodb http://www.mulesoft.org/schema/mule/dynamodb/current/mule-dynamodb.xsd
http://www.mulesoft.org/schema/mule/ee/core http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd">
<http:listener-config name="HTTP_Listener_config" doc:name="HTTP Listener config" >
<http:listener-connection host="localhost" port="8081" />
</http:listener-config>
<dynamodb:config name="Amazon_DynamoDB_Configuration" doc:name="Amazon DynamoDB Configuration" >
<dynamodb:basic-connection accessKey="${config.accesskey}" secretKey="${config.secretkey}" />
</dynamodb:config>
<dynamodb:config name="Amazon_DynamoDB_Configuration1" doc:name="Amazon DynamoDB Configuration" >
<dynamodb:basic-connection accessKey="${config.accesskey}" secretKey="${config.secretkey}" />
</dynamodb:config>
<flow name="delete-table-flow" >
<http:listener doc:name="Listener" path="/scantable" config-ref="HTTP_Listener_config"/>
<dynamodb:scan doc:name="Scan" config-ref="Amazon_DynamoDB_Configuration1" tableName="Issues"/>
<ee:transform doc:name="Transform Message" >
<ee:message >
<ee:set-payload ><![CDATA[%dw 2.0
output application/json
---
{
}]]></ee:set-payload>
</ee:message>
</ee:transform>
<logger level="INFO" doc:name="Logger" message="#[payload]"/>
</flow>
</mule>
The response to a Scan operation might look like this (with other sections of the response omitted for brevity):
{
"scannedCount": 2,
"lastEvaluatedKey": null,
"count": 2,
"consumedCapacity": null,
"items": [
{
"studentID": {
"ss": null,
"nullvalue": null,
"b": null,
"bool": null,
"ns": null,
"l": null,
"m": null,
"n": null,
"bs": null,
"s": "102"
},
...
]
}
Save and run the project as a Mule app:
In Package Explorer, right-click the project name and click Run As > Mule Application.
Open a browser and check the response after you enter the http://localhost:8081/scantable URL.
You see the generated response from the Scan operation in the Mule console.