Redis is introduced

Redis is a fully open source, free, high-performance key-value database that complies with the BSD protocol.

Compared with other key-value cache products, Redis has the following characteristics:

  • Redis has high performance, with a single-machine reading and writing speed of 100,000 levels.
  • Redis supports data persistence, saving data in memory to disk, which can be reloaded for use upon restart.
  • Redis not only supports simple key-value type data, but also provides the storage of list, set, zset, hash and other data structures.
  • Redis supports data backup, namely, data backup in master-slave mode.

Quick Start (Redis. IO /topics/quic…) To install Redis (version 5.0 at the time of this writing) and start the Redis server with the redis-server command.

The scene is introduced

This scenario requires that messages that meet the criteria specified by EMQ X be stored to Reids. To facilitate subsequent analysis and retrieval, message content needs to be split and stored.

In this scenario, the device reports the following information:

  • The report topic is CMD /state/: ID, where ID indicates the vehicle client id

  • The body of the message:

    {"id": "NXP 058659730253-963945118132721-22", // client id" speed": 32.12, // vehicle speed" direction": 198.33212, // driving direction "tachometer": 3211, // engine speed greater than 8000 need to store "synchronization ": 8.93, // instantaneous fuel consumption "location": {// GPS latitude and longitude data "LNG ": 116.296011," LAT ": 40.005091}, "TS ": 1563268202}Copy the code

When the reported engine speed value is greater than 8000, the current information is stored for subsequent analysis of the user’s vehicle usage.

Configuration instructions

Create a resource

Open EMQ X Dashboard, enter the resource page of the menu on the left, click the New button, and enter Redis server information to create resources.

Nodes in the EMQ X cluster may reside on different networks. After resources are created, click the status button in the list to view the resource connection status of each node. If resources on the node are unavailable, check whether the configurations are correct and the network connectivity is normal.

Create rules

Enter the rule page in the left menu and click The New button to create a rule. Trigger event message publishing is selected here to trigger the rule for data processing when the message is published.

After selecting the trigger event, we can see the optional fields and sample SQL on the interface:

Filter the required fields

The rule engine uses SQL statements to process rule conditions. In this business, we need to select all fields of the payload and use the paypay. fieldName format to select the fields, as well as the topic, qos and ID information of the message context.

SELECT
  payload.id as client_id, payload.speed as speed, 
  payload.tachometer as tachometer,
  payload.ts as ts, id
FROM
  "message.publish"
WHERE
  topic =~ 't/#'
Copy the code

Establish screening conditions

Use SQL statement WHERE sentence to filter conditions. In this business, we need to define two conditions:

  • Only deal withcmd/state/:idTheme, using the theme wildcard= ~topicTo filter:topic =~ 'cmd/state/+'
  • Only deal withtachometer > 8000“, using a comparator pairtachometerTo filter:payload.tachometer > 8000

The SQL obtained by combining the previous steps is as follows:

SELECT
  payload.id as client_id, payload.speed as speed, 
  payload.tachometer as tachometer,
  payload.ts as ts,
  id
FROM
  "message.publish"
WHERE
  topic =~ 'cmd/state/+'
  AND payload.tachometer > 8000
Copy the code

Use SQL test functionality for output testing

With the SQL test function, we can view the data output after the current SQL processing in real time. This function requires us to specify payload and other simulated raw data.

Payload data is as follows. Note that the tachometer value is changed to meet SQL conditions:

{
  "id": "NXP-058659730253-963945118132721-22"."speed": 32.12."direction": 198.33212."tachometer": 9001."dynamical": 8.93."location": {
    "lng": 116.296011."lat": 40.005091
  },
  "ts": 1563268202
}
Copy the code

Click the SQL test toggle button to change the topic and payload as the information in the scene. Click the Test button to view the data output:

The test output data is:

{
  "client_id": "NXP-058659730253-963945118132721-22"."id": "589A429E9572FB44B0000057C0001"."speed": 32.12."tachometer": 9001."ts": 1563268202
}
Copy the code

The test output is as expected, and we can proceed to the next step.

Add response actions to store messages to Redis

After the INPUT and output of SQL conditions are correct, we continue to add corresponding actions, configure to write SQL statements, and store the filtering results in Redis.

Click the Add button in the response action, select the Save data to Redis action, select the resource you just selected, populate the SQL statement with ${fieldName} syntax, insert the data into the database, and click the New button to complete the rule creation.

The SQL configuration for the action is as follows:

HMSET test client_id "${client_id}" speed "${speed}" tachometer "${tachometer}" ts "${ts}" msg_id "${msg_id}"
Copy the code

Create a hash table using Redis hash table structure with message ID

test

The expected results

We have successfully created a rule that contains a processing action that is expected to have the following effect:

  1. Equipment tocmd/state/:idWhen a subject reports a message, the message intachometerIf the value exceeds 8000, the SQL is matched in the rule listHas been hitThe number increases by 1;
  2. Redis will add a hash table named after the current Message ID with the same value as the current message.

Use the Websocket tool in Dashboard to test

Switch to tools –> Websocket page, connect to EMQ X using any message client, and send the following message on the message card after successful connection:

  • Topic: CMD/state/NXP – 058659730253-963945118132721-22

  • The body of the message:

    {
      "id": "NXP-058659730253-963945118132721-22"."speed": 32.12."direction": 198.33212."tachometer": 8081."dynamical": 8.93."location": {
        "lng": 116.296011."lat": 40.005091
      },
      "ts": 1563268202
    }
    Copy the code

Click the “send” button to check that the statistics value of the current rule has been hit is 1.

Redis command line check hash table records obtain the following data:

So far, we have implemented business development using the rules engine to store messages to Reids through the rules engine.


For more information, please visit our official website emqx. IO, or follow our open source project github.com/emqx/emqx. For more details, please visit our official documentation.