# Ingest Data into DynamoDB
Setup a DynamoDB database, taking Mac OSX for instance:
$ brew install dynamodb-local $ dynamodb-local -sharedDb
Copied!
2
3
For details about how to deply DynamoDB and AWS CLI locally, please see Deploying DynamoDB Locally (opens new window)
Initiate the DynamoDB table:
1). Create table definition file "mqtt_msg.json" for DynamoDB:
{ "TableName": "mqtt_msg", "KeySchema": [ { "AttributeName": "msgid", "KeyType": "HASH" } ], "AttributeDefinitions": [ { "AttributeName": "msgid", "AttributeType": "S" } ], "ProvisionedThroughput": { "ReadCapacityUnits": 5, "WriteCapacityUnits": 5 } }
Copied!
2
3
4
5
6
7
8
9
10
11
12
13
2). Create the DynamoDB table:
$aws dynamodb create-table --cli-input-json file://mqtt_msg.json --endpoint-url http://localhost:8000
Copied!
Create a rule:
Go to EMQX Dashboard (opens new window), select the "rule" tab on the menu to the left.
Select "message.publish", then type in the following SQL:
SELECT msgid as id, topic, payload FROM "message.publish"
Copied!
2
3
4
Bind an action:
Click on the "+ Add" button under "Action Handler", and then select "Data to DynamoDB" in the pop-up dialog window.
Fill in the parameters required by the action:
Four parameters is required by action "Data to DynamoDB":
1). DynamoDB Table. Here set it to "mqtt_msg".
2). Hash Key. Here set it to "msgid".
3). DynamoDB Range Key. Leave it empty as we didn't define a range key in the dynamodb definition file.
4). Bind a resource to the action. Since the dropdown list "Resource" is empty for now, we create a new resource by clicking on the "New Resource" to the top right, and then select "DynamoDB":
Configure the resource:
Fill in the configs as following:
- DynamoDB Region: us-west-2
- DynamoDB Server: http://localhost:8000 (opens new window)
- AWS Access Key Id: "AKIAU5IM2XOC7AQWG7HK"
- AWS Secret Access Key: "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"
And then click on the "Create" button.
Back to the "Actions" dialog, and then click on the "Confirm" button.
Back to the creating rule page, then click on "Create" button. The rule we created will be show in the rule list:
We have finished, testing the rule by sending an MQTT message to emqx:
> Topic: "t/a" > > QoS: 1 > > Payload: "hello"
Copied!
2
3
4
5
Then inspect the DynamoDB table, verify a new record has been inserted:
And from the rule list, verify that the "Matched" column has increased to 1: