Get subscription relationship from Cassandra

Set up the Cassandra database and set the user name and password to root/public. Take MacOS X as an example:

  1. $ brew install cassandra
  2. ## Modify the configuration and disable anonymous authentication
  3. $ vim /usr/local/etc/cassandra/cassandra.yaml
  4. authenticator: PasswordAuthenticator
  5. authorizer: CassandraAuthorizer
  6. $ brew services start cassandra
  7. ## Create root user
  8. $ cqlsh -ucassandra -pcassandra
  9. create user root with password 'public' superuser;

Create the “mqtt” tablespace:

  1. $ cqlsh -uroot -ppublic
  2. CREATE KEYSPACE mqtt WITH replication = {'class': 'SimpleStrategy', 'replication_factor': '1'} AND durable_writes = true;

Create the mqtt_sub table:

  1. CREATE TABLE mqtt_sub (
  2. clientid text,
  3. topic text,
  4. qos int,
  5. PRIMARY KEY (clientid, topic)
  6. ) WITH CLUSTERING ORDER BY (topic ASC)
  7. AND bloom_filter_fp_chance = 0.01
  8. AND caching = {'keys': 'ALL', 'rows_per_partition': 'NONE'}
  9. AND comment = ''
  10. AND compaction = {'class': 'org.apache.cassandra.db.compaction.SizeTieredCompactionStrategy', 'max_threshold': '32', 'min_threshold': '4'}
  11. AND compression = {'chunk_length_in_kb': '64', 'class': 'org.apache.cassandra.io.compress.LZ4Compressor'}
  12. AND crc_check_chance = 1.0
  13. AND dclocal_read_repair_chance = 0.1
  14. AND default_time_to_live = 0
  15. AND gc_grace_seconds = 864000
  16. AND max_index_interval = 2048
  17. AND memtable_flush_period_in_ms = 0
  18. AND min_index_interval = 128
  19. AND read_repair_chance = 0.0
  20. AND speculative_retry = '99PERCENTILE';

TIP

The subscription relationship table structure cannot be modified. Please use the above SQL statement to create

Create rules:

Open EMQX DashboardGet subscription from Cassandra - 图1 (opens new window) and select the “Rules” tab on the left.

Then fill in the rule SQL:

  1. SELECT * FROM "$events/client_connected"

Get subscription from Cassandra - 图2

Related actions:

Select “Add Action” on the “Response Action” interface, and then select “Get Subscription List from Cassandra” in the “Add Action” drop-down box

Get subscription from Cassandra - 图3

Fill in the action parameters:

The action of “Get subscription list from Cassandra” requires one parameter:

1). Associated resources. The resource drop-down box is empty now, and you can click “New” in the upper right corner to create a Cassandra resource:

Get subscription from Cassandra - 图4

The “Create Resource” dialog box pops up

Get subscription from Cassandra - 图5

Fill in the resource configuration:

Fill in the real Cassandra server address and the values corresponding to other configurations, and then click the “Test Connection” button to ensure that the connection test is successful.

Finally click the “OK” button.

Get subscription from Cassandra - 图6

Return to the response action interface and click “OK”.

Get subscription from Cassandra - 图7

Return to the rule creation interface and click “Create”.

Get subscription from Cassandra - 图8

The rule has been created, and you can insert a subscription relationship into Cassandra through “cqlsh”:

  1. insert into mqtt_sub(clientid, topic, qos) values('test', 't1', 1);

Get subscription from Cassandra - 图9

Log in to the device whose clientid is test via Dashboard:

Get subscription from Cassandra - 图10

Check the “Subscription” list, and you can see that the Broker obtains the subscription relationship from Cassandra and subscribes as the agent device:

Get subscription from Cassandra - 图11