Rule
The rule describes the three configurations of "where the data comes from", "how to filter and process the data", and "where the processing results go". A rule contains three factors:
- Trigger event: The rule is triggered by an event, and the event injects the context information (data source) of the event into the rule when triggered. The event type is specified through the
FROM
clause of SQL; - Processing rules (SQL): Use
SELECT
clause andWHERE
clause and built-in processing functions to filter and process data from context information; - Response action: If there is a processing result output, the rule will execute the corresponding action, such as persisting to the database, republishing the processed message, and forwarding the message to the message queue. A rule can configure multiple response actions.
Create rules
TIP
Before creating a rule, you need to ensure that you have added VPC peering connection, and created a resource
- We will attach the rule to the TimescaleDB resource as an example. Either create a rule directly from the notification or the resource detail page.
- In the following rule, we read the time up_timestamp when the message is reported, the client ID, the message body (Payload) from the temp_hum/emqx topic and the temperature and humidity from the message body respectively.
TIP
In EMQX Cloud, the rules uses SQL-like statements to select, filter, and process data. To learn how to write rules in SQL, you can refer to the following links:
SELECT
timestamp div 1000 AS up_timestamp, clientid AS client_id, payload.temp AS temp, payload.hum AS hum
FROM
"temp_hum/emqx"
- Create a new test SQL, click the switch button behind
SQL test
, fill in the corresponding test parameters, and finally click theSQL test
button.
- In the result text area, we will find the output data as expected. Then click the
Next
button to save the rule.
Create Actions
An action solves the problem of "where to send the processed data". It tells EMQX Cloud what to do with the data generated by the rule. Usually, we set up the configuration and SQL Template for the target resource.
EMQX Cloud will set the default values for the configuration. You can change them if needed.
Code the SQL Template, and click
Confirm
to create an action.
INSERT INTO temp_hum(up_timestamp, client_id, temp, hum) VALUES (to_timestamp(${up_timestamp}), ${client_id}, ${temp}, ${hum})
- One rule can be associated with a few actions. Add another action, and we can change the target resource. For example, we can forward the data to Kafka cluster as well as saving the data to RDS.
- If there is a failure on the main action, we can set up a fallback action to ensure a double check.
Rules Operations
Edit Rules
Click the edit icon to edit the rule. On this page, you can edit both the rule SQL Template and the actions.
View Monitoring Status
- Click the rule monitoring icon on the rule list page.
- The panel shows the detail of rule matching status.
Delete rules
In the resource detail page, you can delete the rules in the rule list.