Cassandra Backend
After EMQX version 3.1, a powerful rule engine is introduced to replace plug-ins. It is recommended that you use it. See Save data to Cassandra to setup Save data to Cassandra in rule engine.
Config file: etc/plugins/emqx_backend_cassa.conf
Multi node Cassandra cluster is supported:
Configure Cassandra Persistence Hooks
backend.cassa.hook.client.connected.1 = {"action": {"function": "on_client_connected"}, "pool": "pool1"}
## Subscribe Lookup Record
backend.cassa.hook.client.connected.2 = {"action": {"function": "on_subscription_lookup"}, "pool": "pool1"}
## Client DisConnected Record
backend.cassa.hook.client.disconnected.1 = {"action": {"function": "on_client_disconnected"}, "pool": "pool1"}
## Lookup Unread Message QOS > 0
backend.cassa.hook.session.subscribed.1 = {"topic": "#", "action": {"function": "on_message_fetch"}, "pool": "pool1"}
## Lookup Retain Message
backend.cassa.hook.session.subscribed.2 = {"action": {"function": "on_retain_lookup"}, "pool": "pool1"}
## Store Publish Message QOS > 0
backend.cassa.hook.message.publish.1 = {"topic": "#", "action": {"function": "on_message_publish"}, "pool": "pool1"}
## Delete Acked Record
backend.cassa.hook.session.unsubscribed.1= {"topic": "#", "action": {"cql": ["delete from acked where clientid = ${clientid} and topic = ${topic}"]}, "pool": "pool1"}
## Store Retain Message
backend.cassa.hook.message.publish.2 = {"topic": "#", "action": {"function": "on_message_retain"}, "pool": "pool1"}
## Delete Retain Message
backend.cassa.hook.message.publish.3 = {"topic": "#", "action": {"function": "on_retain_delete"}, "pool": "pool1"}
backend.cassa.hook.message.acked.1 = {"topic": "#", "action": {"function": "on_message_acked"}, "pool": "pool1"}
## Get offline messages
### "offline_opts": Get configuration for offline messages
### max_returned_count: Maximum number of offline messages get at a time
### time_range: Get only messages in the current time range
## backend.cassa.hook.session.subscribed.1 = {"topic": "#", "action": {"function": "on_message_fetch"}, "offline_opts": {"max_returned_count": 500, "time_range": "2h"}, "pool": "pool1"}
## If you need to store Qos0 messages, you can enable the following configuration
## Tip: When the following configuration is enabled, 'on_message_fetch' needs to be disabled, otherwise qos1, qos2 messages will be stored twice
## backend.cassa.hook.message.publish.4 = {"topic": "#", "action": {"function": "on_message_store"}, "pool": "pool1"}
Description of Cassandra Persistence Hooks
CQL Parameters Description
Customized CQL command parameters includes:
hook | Parameter | Example (${name} in CQL represents available parameter |
---|---|---|
client.connected | clientid | insert into conn(clientid) values(${clientid}) |
client.disconnected | clientid | insert into disconn(clientid) values(${clientid}) |
session.subscribed | clientid, topic, qos | insert into sub(topic, qos) values(${topic}, ${qos}) |
session.unsubscribed | clientid, topic | delete from sub where topic = ${topic} |
message.publish | msgid, topic, payload, qos, clientid | insert into msg(msgid, topic) values(${msgid}, ${topic}) |
message.acked | msgid, topic, clientid | insert into ack(msgid, topic) values(${msgid}, ${topic}) |
message.delivered | msgid, topic, clientid | insert into delivered(msgid, topic) values(${msgid}, ${topic}) |
Cassandra backend supports CLQ in ‘action’:
## After a client is connected to the EMQX server, it executes a CQL command(multiple command also supported):
backend.cassa.hook.client.connected.3 = {"action": {"cql": ["insert into conn(clientid) values(${clientid})"]}, "pool": "pool1"}
Initializing Cassandra
Create KeySpace:
CREATE KEYSPACE mqtt WITH REPLICATION = { 'class' : 'SimpleStrategy', 'replication_factor' : 1 };
USE mqtt;
Import Cassandra tables:
cqlsh -e "SOURCE 'emqx_backend_cassa.cql'"
KeySpace is free of choice
Cassandra Client Connection Table
mqtt.client stores client connection states:
CREATE TABLE mqtt.client (
clientid text,
node text,
state int,
connected timestamp,
disconnected timestamp,
PRIMARY KEY(clientid)
);
Query a client’s connection state:
select * from mqtt.client where clientid = ${clientid};
If client ‘test’ is online:
Client ‘test’ is offline:
select * from mqtt.client where clientid = 'test';
clientid | connected | disconnected | node | state
-----------+---------------------------------+---------------------------------+---------------+-------
test | 2017-02-14 08:27:29.872000+0000 | 2017-02-14 08:27:35.872000+0000 | emqx@127.0.0.1| 0
Cassandra Subscription Table
mqtt.sub stores subscriptions of clients:
CREATE TABLE mqtt.sub (
clientid text,
topic text,
qos int,
PRIMARY KEY(clientid, topic)
);
Client ‘test’ subscribes to topic ‘test_topic1’ and ‘test_topic2’:
insert into mqtt.sub(clientid, topic, qos) values('test', 'test_topic1', 1);
insert into mqtt.sub(clientid, topic, qos) values('test', 'test_topic2', 2);
select * from mqtt_sub where clientid = ${clientid};
Query subscriptions of client ‘test’:
clientid | topic | qos
test | test_topic1 | 1
test | test_topic2 | 2
mqtt.msg stores MQTT messages:
CREATE TABLE mqtt.msg (
topic text,
msgid text,
sender text,
qos int,
retain int,
payload blob,
arrived timestamp,
PRIMARY KEY(topic, msgid)
) WITH CLUSTERING ORDER BY (msgid DESC);
Query messages published by a client:
Query messages published by client ‘test’:
select * from mqtt_msg where sender = 'test';
topic | msgid | arrived | payload | qos | retain | sender
-------+----------------------+---------------------------------+--------------+-----+--------+--------
hello | 2PguFrHsrzEvIIBdctmb | 2017-02-14 09:07:13.785000+0000 | Hello world! | 1 | 0 | test
world | 2PguFrHsrzEvIIBdctmb | 2017-02-14 09:07:13.785000+0000 | Hello world! | 1 | 0 | test
Cassandra Retained Message Table
mqtt.retain stores retained messages:
CREATE TABLE mqtt.retain (
topic text,
msgid text,
PRIMARY KEY(topic)
);
Query retained messages:
select * from mqtt_retain where topic = ${topic};
Query retained messages with topic ‘retain’:
select * from mqtt_retain where topic = 'retain';
topic | msgid
--------+----------------------
retain | 2PguFrHsrzEvIIBdctmb
Cassandra Acknowledgement Table
CREATE TABLE mqtt.acked (
clientid text,
topic text,
msgid text,
PRIMARY KEY(clientid, topic)
Enable Cassandra Backend
./bin/emqx_ctl plugins load emqx_backend_cassa