Save data to DynamoDB

Setup a DynamoDB database, taking Mac OSX for instance:

  1. $ brew install dynamodb-local
  2. $ dynamodb-local -sharedDb

For details about how to deply DynamoDB and AWS CLI locally, please see Deploying DynamoDB LocallySave data to DynamoDB - 图1 (opens new window)

Initiate the DynamoDB table:

1). Create table definition file “mqtt_msg.json” for DynamoDB:

  1. {
  2. "TableName": "mqtt_msg",
  3. "KeySchema": [
  4. { "AttributeName": "msgid", "KeyType": "HASH" }
  5. ],
  6. "AttributeDefinitions": [
  7. { "AttributeName": "msgid", "AttributeType": "S" }
  8. ],
  9. "ProvisionedThroughput": {
  10. "ReadCapacityUnits": 5,
  11. "WriteCapacityUnits": 5
  12. }
  13. }

2). Create the DynamoDB table:

  1. $aws dynamodb create-table --cli-input-json file://mqtt_msg.json --endpoint-url http://localhost:8000

Create a rule:

Go to EMQX DashboardSave data to DynamoDB - 图2 (opens new window), select the “rule” tab on the menu to the left.

Select “message.publish”, then type in the following SQL:

  1. SELECT
  2. msgid as id, topic, payload
  3. FROM
  4. "message.publish"

Bind an action:

Click on the “+ Add” button under “Action Handler”, and then select “Data to DynamoDB” in the pop-up dialog window.

image

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.

image

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”:

image

Configure the resource:

Fill in the configs as following:

And then click on the “Create” button.

image

Back to the “Actions” dialog, and then click on the “Confirm” button.

image

Back to the creating rule page, then click on “Create” button. The rule we created will be show in the rule list:

image

We have finished, testing the rule by sending an MQTT message to emqx:

  1. > Topic: "t/a"
  2. >
  3. > QoS: 1
  4. >
  5. > Payload: "hello"

Then inspect the DynamoDB table, verify a new record has been inserted:

image

And from the rule list, verify that the “Matched” column has increased to 1:

image