Ingest Data into Oracle Database

EMQX supports integration with Oracle Database so you can save MQTT messages and client events to Oracle Database, or use events to trigger the update or removal of data to record the online status or online/offline of clients.

TIP

EMQX Enterprise Edition features. EMQX Enterprise Edition provides comprehensive coverage of key business scenarios, rich data integration, product-level reliability, and 24/7 global technical support. Experience the benefits of this enterprise-ready MQTT messaging platformIngest Data into Oracle Database - 图1 (opens new window) today.

Prerequisites

Feature List

Quick Start Tutorial

This section introduces how to use the Oracle Database data bridge with a practical tutorial, covering topics like how to install the Oracle Database server and create data tables, create data bridges and rules for forwarding data to Oracle Database, and test the data bridges and rules.

This tutorial assumes that you run both EMQX and Oracle Database on the local machine. If you have Oracle Database and EMQX running remotely, adjust the settings accordingly.

Install Oracle Database Server

Install Oracle Database server via Docker, and then run the docker image.

  1. # To start the Oracle Database docker image locally
  2. docker run --name oracledb -p 1521:1521 -d oracleinanutshell/oracle-xe-11g:1.0.0
  3. # To start the Oracle Database docker image remotely
  4. docker run --name oracledb -p 1521:1521 -e ORACLE_ALLOW_REMOTE=true -d oracleinanutshell/oracle-xe-11g:1.0.0
  5. # For performance concern, you may want to disable the disk asynch IO:
  6. docker run --name oracledb -p 1521:1521 -e ORACLE_DISABLE_ASYNCH_IO=true -d oracleinanutshell/oracle-xe-11g:1.0.0
  7. # Access the container
  8. docker exec -it oracledb bash
  9. # Connect to the default database "XE"
  10. # username: "system"
  11. # password: "oracle"
  12. sqlplus

Create Data Tables

Use the following SQL statements to create data table t_mqtt_msgs in Oracle Database for storing the message ID, client ID, topic, QoS, retain flag, message payload, and timestamp of every message.

  1. CREATE TABLE t_mqtt_msgs (
  2. msgid VARCHAR2(64),
  3. sender VARCHAR2(64),
  4. topic VARCHAR2(255),
  5. qos NUMBER(1),
  6. retain NUMBER(1),
  7. payload NCLOB,
  8. arrived TIMESTAMP
  9. );

Use the following SQL statements to create data table t_emqx_client_events in Oracle Database for storing the client ID, event type, and creation time of every event.

  1. CREATE TABLE t_emqx_client_events (
  2. clientid VARCHAR2(255),
  3. event VARCHAR2(255),
  4. created_at TIMESTAMP
  5. );

Create Oracle Database Data Bridges

Data bridges for message storage and event recording require different SQL templates. Therefore, you need to create 2 different data bridges to Oracle Database for message storage and event recording.

  1. Go to EMQX Dashboard, and click Integration -> Data Bridge.

  2. Click Create on the top right corner of the page.

  3. In the Create Data Bridge page, click to select Oracle Database, and then click Next.

  4. Input a name for the data bridge. The name should be a combination of upper/lower case letters and numbers.

  5. Input the connection information:

    • Server Host: Input http://127.0.0.1:1521, or the actual URL if the Oracle Database server is running remotely.
    • Database Name: Input XE.
    • Oracle Database SID: Input XE.
    • Username: Input system.
    • Password: Input oracle.
  6. Configure the SQL Template based on the feature to use.

    Note: This is a preprocessed SQL, so the fields should not be enclosed in quotation marks, and do not write a semicolon at the end of the statements.

    • To create a data bridge for message storage, use the SQL statement below:

      1. INSERT INTO t_mqtt_msgs(msgid, sender, topic, qos, retain, payload, arrived) VALUES(
      2. ${id},
      3. ${clientid},
      4. ${topic},
      5. ${qos},
      6. ${flags.retain},
      7. ${payload},
      8. TO_TIMESTAMP('1970-01-01 00:00:00', 'YYYY-MM-DD HH24:MI:SS') + NUMTODSINTERVAL(${timestamp}/1000, 'SECOND')
      9. )
    • To create a data bridge for online/offline status recording, use the SQL statement below:

      1. INSERT INTO t_emqx_client_events(clientid, event, created_at) VALUES (
      2. ${clientid},
      3. ${event},
      4. TO_TIMESTAMP('1970-01-01 00:00:00', 'YYYY-MM-DD HH24:MI:SS') + NUMTODSINTERVAL(${timestamp}/1000, 'SECOND')
      5. )
  7. Leave other options as default.

  8. Before clicking Create, you can click Test Connectivity to test that the bridge can connect to the Oracle Database server.

  9. Click Create to finish the creation of the data bridge.

    A confirmation dialog will appear and ask if you like to create a rule using this data bridge, you can click Create Rule to continue creating rules to specify the data to be saved into Oracle Database. For detailed steps, refer to Create Rules for Oracle Database Data Bridge.

Now the Oracle Database data bridge should appear in the data bridge list (Integration -> Data Bridge) with Resource Status as Connected.

Create Rules for Oracle Database Data Bridge

After you have successfully created the data bridges to Oracle Database, you can continue to create rules to specify the data to be saved into Oracle Database and rules for the online/offline status recording.

  1. Go to EMQX Dashboard, click Integration -> Rules.

  2. Click Create on the top right corner of the page.

  3. Input my_rule as the rule ID, and set the rules in the SQL Editor based on the feature to use:

    • To create a rule for message storage, input the following SQL syntax, which means the MQTT messages under topic t/# will be saved to Oracle Database.

      Note: If you want to specify your own SQL syntax, make sure that you have included all fields required by the data bridge in the SELECT part.

      1. SELECT
      2. *
      3. FROM
      4. "t/#"
    • To create a rule for online/offline status recording, input the following SQL syntax:

      1. SELECT
      2. *
      3. FROM
      4. "$events/client_connected", "$events/client_disconnected"
  4. Click the Add Action button, select Forwarding with Data Bridge from the dropdown list, and then select the Oracle Database data bridge just created. Click the Add button.

  5. Click the Create button to finish the setup.

Now you have successfully created the data bridges to Oracle Database. You can click Integration -> Flows to view the topology. It can be seen that the messages under topic t/# are sent and saved to Oracle Database after parsing by rule my_rule.

Test the Data Bridges and Rules

Use MQTTX to send a message to topic t/1 to trigger an online/offline event.

  1. mqttx pub -i emqx_c -t t/1 -m '{ "msg": "hello Oracle Database" }'

Check the running status of the two data bridges, there should be one new incoming and one new outgoing message.

Check whether the data is written into the t_mqtt_msgs data table.

  1. SELECT * FROM t_mqtt_msgs;
  2. MSGID SENDER TOPIC QOS RETAIN PAYLOAD ARRIVED
  3. -------------------------------- ------ ----- --- ------ ---------------------------------- ----------------------------
  4. 0005FA6CE9EF9F24F442000048100002 emqx_c t/1 0 0 { "msg": "hello Oracle Database" } 28-APR-23 08.22.51.760000 AM

Check whether the data is written into the t_emqx_client_events table.

  1. SELECT * FROM t_emqx_client_events;
  2. CLIENTID EVENT CREATED_AT
  3. -------- ------------------- ----------------------------
  4. emqx_c client.connected 28-APR-23 08.22.51.757000 AM
  5. emqx_c client.disconnected 28-APR-23 08.22.51.760000 AM