> ## Documentation Index
> Fetch the complete documentation index at: https://private-7c7dfe99-detect-table-modification.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

> Introduction to EMQX with ClickHouse

# Integrating EMQX with ClickHouse

export const Image = ({img, alt, size = "lg"}) => {
  const normalizedSize = ["sm", "md", "lg"].includes(size) ? size : "lg";
  return <div className={`ch-image-${normalizedSize}`}>
      <Frame>
        <img src={img} alt={alt} />
      </Frame>
    </div>;
};

<h2 id="connecting-emqx">
  Connecting EMQX
</h2>

[EMQX](https://www.emqx.com/en/try?product=enterprise) is an open source MQTT broker with a high-performance real-time message processing engine, powering event streaming for IoT devices at massive scale. As the most scalable MQTT broker, EMQX can help you connect any device, at any scale. Move and process your IoT data anywhere.

[EMQX Cloud](https://www.emqx.com/en/try?product=cloud) is an MQTT messaging middleware product for the IoT domain hosted by [EMQ](https://www.emqx.com/en). As the world's first fully managed MQTT 5.0 cloud messaging service, EMQX Cloud provides a one-stop O\&M colocation and a unique isolated environment for MQTT messaging services. In the era of the Internet of Everything, EMQX Cloud can help you quickly build industry applications for the IoT domain and easily collect, transmit, compute, and persist IoT data.

With the infrastructure provided by cloud providers, EMQX Cloud serves dozens of countries and regions around the world, providing low-cost, secure, and reliable cloud services for 5G and Internet of Everything applications.

<Image img="https://mintcdn.com/private-7c7dfe99-detect-table-modification/nH26ELfEvmC8yX3w/images/integrations/data-ingestion/emqx/emqx-cloud-artitecture.png?fit=max&auto=format&n=nH26ELfEvmC8yX3w&q=85&s=318b678842c532d322f5472083604a40" size="lg" border alt="EMQX Cloud Architecture diagram showing cloud infrastructure components" width="1384" height="952" data-path="images/integrations/data-ingestion/emqx/emqx-cloud-artitecture.png" />

<h3 id="assumptions">
  Assumptions
</h3>

* You're familiar with the [MQTT protocol](https://mqtt.org/), which is designed as an extremely lightweight publish/subscribe messaging transport protocol.
* You're using EMQX or EMQX Cloud for real-time message processing engine, powering event streaming for IoT devices at massive scale.
* You have prepared a Clickhouse Cloud instance to persist device data.
* We're using [MQTT X](https://mqttx.app/)  as an MQTT client testing tool to connect the deployment of EMQX Cloud to publish MQTT data. Or other methods connecting to the MQTT broker will do the job as well.

<h2 id="get-your-clickhouse-cloudservice">
  Get your ClickHouse Cloud service
</h2>

During this setup, we deployed the ClickHouse instance on AWS in N. Virginia (us-east -1), while an EMQX Cloud instance was also deployed in the same region.

<Image img="https://mintcdn.com/private-7c7dfe99-detect-table-modification/nH26ELfEvmC8yX3w/images/integrations/data-ingestion/emqx/clickhouse_cloud_1.webp?fit=max&auto=format&n=nH26ELfEvmC8yX3w&q=85&s=b8bc1f5286d637ba1880014711a9434e" size="sm" border alt="ClickHouse Cloud Service Deployment interface showing AWS region selection" width="1164" height="1734" data-path="images/integrations/data-ingestion/emqx/clickhouse_cloud_1.webp" />

During the setup process, you will also need to pay attention to the connection settings. In this tutorial, we choose "Anywhere", but if you apply for a specific location, you will need to add the [NAT gateway](https://docs.emqx.com/en/cloud/latest/vas/nat-gateway.html) IP address you got from your EMQX Cloud deployment to the whitelist.

<Image img="https://mintcdn.com/private-7c7dfe99-detect-table-modification/nH26ELfEvmC8yX3w/images/integrations/data-ingestion/emqx/clickhouse_cloud_2.webp?fit=max&auto=format&n=nH26ELfEvmC8yX3w&q=85&s=69b5fe3ace55fd40b1873a63ec16a0dd" size="sm" border alt="ClickHouse Cloud Connection Settings showing IP access configuration" width="1108" height="1250" data-path="images/integrations/data-ingestion/emqx/clickhouse_cloud_2.webp" />

Then you need to save your username and password for future use.

<Image img="https://mintcdn.com/private-7c7dfe99-detect-table-modification/nH26ELfEvmC8yX3w/images/integrations/data-ingestion/emqx/clickhouse_cloud_3.webp?fit=max&auto=format&n=nH26ELfEvmC8yX3w&q=85&s=e2035c0ff0069d8bf9a2d10607c885ef" size="sm" border alt="ClickHouse Cloud Credentials screen showing username and password" width="1050" height="1116" data-path="images/integrations/data-ingestion/emqx/clickhouse_cloud_3.webp" />

After that, you will get a running Click house instance. Click "Connect" to get the instance connection address of Clickhouse Cloud.

<Image img="https://mintcdn.com/private-7c7dfe99-detect-table-modification/nH26ELfEvmC8yX3w/images/integrations/data-ingestion/emqx/clickhouse_cloud_4.webp?fit=max&auto=format&n=nH26ELfEvmC8yX3w&q=85&s=63732f992bc0b21c70f36ed80a146606" size="lg" border alt="ClickHouse Cloud Running Instance dashboard with connection options" width="3834" height="1116" data-path="images/integrations/data-ingestion/emqx/clickhouse_cloud_4.webp" />

Click "Connect to SQL Console" to create database and table for integration with EMQX Cloud.

<Image img="https://mintcdn.com/private-7c7dfe99-detect-table-modification/nH26ELfEvmC8yX3w/images/integrations/data-ingestion/emqx/clickhouse_cloud_5.webp?fit=max&auto=format&n=nH26ELfEvmC8yX3w&q=85&s=76a8b5ae27da66ccbc00c607614f0d71" size="lg" border alt="ClickHouse Cloud SQL Console interface" width="3838" height="1860" data-path="images/integrations/data-ingestion/emqx/clickhouse_cloud_5.webp" />

You can refer to the following SQL statement, or modify the SQL according to the actual situation.

```sql theme={null}
CREATE TABLE emqx.temp_hum
(
   client_id String,
   timestamp DateTime,
   topic String,
   temp Float32,
   hum Float32
)
ENGINE = MergeTree()
PRIMARY KEY (client_id, timestamp)
```

<Image img="https://mintcdn.com/private-7c7dfe99-detect-table-modification/nH26ELfEvmC8yX3w/images/integrations/data-ingestion/emqx/clickhouse_cloud_6.webp?fit=max&auto=format&n=nH26ELfEvmC8yX3w&q=85&s=b945ad61bfadc2b003e43842d67e77bb" size="lg" border alt="ClickHouse Cloud Create Database and Table SQL query execution" width="3826" height="1284" data-path="images/integrations/data-ingestion/emqx/clickhouse_cloud_6.webp" />

<h2 id="create-an-mqtt-service-on-emqx-cloud">
  Create an MQTT service on EMQX Cloud
</h2>

Creating a dedicated MQTT broker on EMQX Cloud is as easy as a few clicks.

<h3 id="get-an-account">
  Get an account
</h3>

EMQX Cloud provides a 14-day free trial for both standard deployment and professional deployment for every account.

Start at the [EMQX Cloud sign up](https://accounts.emqx.com/signup?continue=https%3A%2F%2Fwww.emqx.com%2Fen%2Fcloud) page and click start free to register an account if you're new to EMQX Cloud.

<Image img="https://mintcdn.com/private-7c7dfe99-detect-table-modification/nH26ELfEvmC8yX3w/images/integrations/data-ingestion/emqx/emqx_cloud_sign_up.webp?fit=max&auto=format&n=nH26ELfEvmC8yX3w&q=85&s=2268e425cf2c5e21c1837366189adf51" size="lg" border alt="EMQX Cloud Signup Page with registration form" width="2744" height="1832" data-path="images/integrations/data-ingestion/emqx/emqx_cloud_sign_up.webp" />

<h3 id="create-an-mqtt-cluster">
  Create an MQTT cluster
</h3>

Once logged in, click on "Cloud console" under the account menu and you will be able to see the green button to create a new deployment.

<Image img="https://mintcdn.com/private-7c7dfe99-detect-table-modification/nH26ELfEvmC8yX3w/images/integrations/data-ingestion/emqx/emqx_cloud_create_1.webp?fit=max&auto=format&n=nH26ELfEvmC8yX3w&q=85&s=39b636a76eba576cea7114052455cbef" size="lg" border alt="EMQX Cloud Create Deployment Step 1 showing deployment options" width="3078" height="1844" data-path="images/integrations/data-ingestion/emqx/emqx_cloud_create_1.webp" />

In this tutorial, we will use the Professional deployment because only Pro version provides the data integration functionality, which can send MQTT data directly to ClickHouse without a single line of code.

Select Pro version and choose `N.Virginial` region and click `Create Now`. In just a few minutes, you will get a fully managed MQTT broker:

<Image img="https://mintcdn.com/private-7c7dfe99-detect-table-modification/nH26ELfEvmC8yX3w/images/integrations/data-ingestion/emqx/emqx_cloud_create_2.png?fit=max&auto=format&n=nH26ELfEvmC8yX3w&q=85&s=8f8c93f27581613f2ccc8fe4d2effcb5" size="lg" border alt="EMQX Cloud Create Deployment Step 2 showing region selection" width="3062" height="830" data-path="images/integrations/data-ingestion/emqx/emqx_cloud_create_2.png" />

Now click the panel to go to the cluster view. On this dashboard, you will see the overview of your MQTT broker.

<Image img="https://mintcdn.com/private-7c7dfe99-detect-table-modification/nH26ELfEvmC8yX3w/images/integrations/data-ingestion/emqx/emqx_cloud_overview.webp?fit=max&auto=format&n=nH26ELfEvmC8yX3w&q=85&s=98821d6c8c2464df764efe8f32cb18e8" size="lg" border alt="EMQX Cloud Overview Dashboard showing broker metrics" width="3162" height="1840" data-path="images/integrations/data-ingestion/emqx/emqx_cloud_overview.webp" />

<h3 id="add-client-credential">
  Add client credential
</h3>

EMQX Cloud doesn't allow anonymous connections by default，so you need add a client credential so you can use the MQTT client tool to send data to this broker.

Click 'Authentication & ACL' on the left menu and click 'Authentication' in the submenu. Click the 'Add' button on the right and give a username and password for the MQTT connection later. Here we will use `emqx` and `xxxxxx` for the username and password.

<Image img="https://mintcdn.com/private-7c7dfe99-detect-table-modification/nH26ELfEvmC8yX3w/images/integrations/data-ingestion/emqx/emqx_cloud_auth.webp?fit=max&auto=format&n=nH26ELfEvmC8yX3w&q=85&s=3e2671f6fa5e84a3c2177e6429829433" size="lg" border alt="EMQX Cloud Authentication Setup interface for adding credentials" width="3164" height="1400" data-path="images/integrations/data-ingestion/emqx/emqx_cloud_auth.webp" />

Click 'Confirm' and now we have a fully managed MQTT broker ready.

<h3 id="enable-nat-gateway">
  Enable NAT gateway
</h3>

Before we can start setting up the ClickHouse integration, we need to enable the NAT gateway first. By default, the MQTT broker is deployed in a private VPC, which can not send data to third-party systems over the public network.

Go back to the Overview page and scroll down to the bottom of the page where you will see the NAT gateway widget. Click the Subscribe button and follow the instructions. Note that NAT Gateway is a value-added service, but it also offers a 14-day free trial.

<Image img="https://mintcdn.com/private-7c7dfe99-detect-table-modification/nH26ELfEvmC8yX3w/images/integrations/data-ingestion/emqx/emqx_cloud_nat_gateway.webp?fit=max&auto=format&n=nH26ELfEvmC8yX3w&q=85&s=89a12426dc667b3c5f46697f659508d3" size="lg" border alt="EMQX Cloud NAT Gateway Configuration panel" width="2358" height="364" data-path="images/integrations/data-ingestion/emqx/emqx_cloud_nat_gateway.webp" />

Once it has been created, you will find the public IP address in the widget. Please note that if you select "Connect from a specific location" during ClickHouse Cloud setup, you will need to add this IP address to the whitelist.

<h2 id="integration-emqx-cloud-with-clickhouse-cloud">
  Integration EMQX Cloud with ClickHouse Cloud
</h2>

The [EMQX Cloud Data Integrations](https://docs.emqx.com/en/cloud/latest/data_integration/introduction.html) is used to configure the rules for handling and responding to EMQX message flows and device events. The Data Integrations not only provides a clear and flexible "configurable" architecture solution, but also simplifies the development process, improves user usability, and reduces the coupling degree between the business system and EMQX Cloud. It also provides a superior infrastructure for customization of EMQX Cloud's proprietary capabilities.

<Image img="https://mintcdn.com/private-7c7dfe99-detect-table-modification/nH26ELfEvmC8yX3w/images/integrations/data-ingestion/emqx/emqx_cloud_data_integration.png?fit=max&auto=format&n=nH26ELfEvmC8yX3w&q=85&s=1d34af0b64321c1fb3a0f3ec75c04463" size="lg" border alt="EMQX Cloud Data Integration Options showing available connectors" width="1746" height="812" data-path="images/integrations/data-ingestion/emqx/emqx_cloud_data_integration.png" />

EMQX Cloud offers more than 30 native integrations with popular data systems. ClickHouse is one of them.

<Image img="https://mintcdn.com/private-7c7dfe99-detect-table-modification/nH26ELfEvmC8yX3w/images/integrations/data-ingestion/emqx/data_integration_clickhouse.webp?fit=max&auto=format&n=nH26ELfEvmC8yX3w&q=85&s=3f376215a47e71b8811809c6c3f5fb06" size="lg" border alt="EMQX Cloud ClickHouse Data Integration connector details" width="3080" height="1902" data-path="images/integrations/data-ingestion/emqx/data_integration_clickhouse.webp" />

<h3 id="create-clickhouse-resource">
  Create ClickHouse resource
</h3>

Click "Data Integrations" on the left menu and click "View All Resources". You will find the ClickHouse in the Data Persistence section or you can search for ClickHouse.

Click the ClickHouse card to create a new resource.

* Note: add a note for this resource.
* Server address: this is the address of your ClickHouse Cloud service, remember don't forget the port.
* Database name: `emqx` we created in the above steps.
* User: the username for connecting to your ClickHouse Cloud service.
* Key: the password for the connection.

<Image img="https://mintcdn.com/private-7c7dfe99-detect-table-modification/nH26ELfEvmC8yX3w/images/integrations/data-ingestion/emqx/data_integration_resource.webp?fit=max&auto=format&n=nH26ELfEvmC8yX3w&q=85&s=3d9c3f66b34b307522d7e143a75a7ab2" size="lg" border alt="EMQX Cloud ClickHouse Resource Setup form with connection details" width="3468" height="1504" data-path="images/integrations/data-ingestion/emqx/data_integration_resource.webp" />

<h3 id="create-a-new-rule">
  Create a new rule
</h3>

During the creation of the resource, you will see a popup, and clicking 'New' will leads you to the rule creation page.

EMQX provides a powerful [rule engine](https://docs.emqx.com/en/cloud/latest/data_integration/rules.html) that can transform, and enrich the raw MQTT message before sending it to third-party systems.

Here's the rule used in this tutorial:

```sql theme={null}
SELECT
   clientid AS client_id,
   (timestamp div 1000) AS timestamp,
   topic AS topic,
   payload.temp AS temp,
   payload.hum AS hum
FROM
"temp_hum/emqx"
```

It will read the messages from the `temp_hum/emqx` topic and enrich the JSON object by adding client\_id, topic, and timestamp info.

So, the raw JSON you send to the topic:

```bash theme={null}
{"temp": 28.5, "hum": 0.68}
```

<Image img="https://mintcdn.com/private-7c7dfe99-detect-table-modification/nH26ELfEvmC8yX3w/images/integrations/data-ingestion/emqx/data_integration_rule_1.webp?fit=max&auto=format&n=nH26ELfEvmC8yX3w&q=85&s=88a959830c86daced95468e2f4b9b49c" size="md" border alt="EMQX Cloud Data Integration Rule Creation Step 1 showing SQL query" width="2356" height="1568" data-path="images/integrations/data-ingestion/emqx/data_integration_rule_1.webp" />

You can use the SQL test to test and see the results.

<Image img="https://mintcdn.com/private-7c7dfe99-detect-table-modification/nH26ELfEvmC8yX3w/images/integrations/data-ingestion/emqx/data_integration_rule_2.webp?fit=max&auto=format&n=nH26ELfEvmC8yX3w&q=85&s=4a4303691230b2b51076b5bfe4e175ba" size="md" border alt="EMQX Cloud Data Integration Rule Creation Step 2 showing test results" width="2344" height="1718" data-path="images/integrations/data-ingestion/emqx/data_integration_rule_2.webp" />

Now click on the "NEXT" button. This step is to tell EMQX Cloud how to insert refined data into your ClickHouse database.

<h3 id="add-a-response-action">
  Add a response action
</h3>

If you have only one resource, you don't need to modify the 'Resource' and 'Action Type'.
You only need to set the SQL template. Here's the example used for this tutorial:

```bash theme={null}
INSERT INTO temp_hum (client_id, timestamp, topic, temp, hum) VALUES ('${client_id}', ${timestamp}, '${topic}', ${temp}, ${hum})
```

<Image img="https://mintcdn.com/private-7c7dfe99-detect-table-modification/nH26ELfEvmC8yX3w/images/integrations/data-ingestion/emqx/data_integration_rule_action.webp?fit=max&auto=format&n=nH26ELfEvmC8yX3w&q=85&s=facbab3611e3d205d41ef5356631f733" size="md" border alt="EMQX Cloud Data Integration Rule Action Setup with SQL template" width="2310" height="1586" data-path="images/integrations/data-ingestion/emqx/data_integration_rule_action.webp" />

This is a template for inserting data into Clickhouse, you can see the variables are used here.

<h3 id="view-rules-details">
  View rules details
</h3>

Click "Confirm" and "View Details". Now, everything should be well set. You can see the data integration works from rule details page.

<Image img="https://mintcdn.com/private-7c7dfe99-detect-table-modification/nH26ELfEvmC8yX3w/images/integrations/data-ingestion/emqx/data_integration_details.webp?fit=max&auto=format&n=nH26ELfEvmC8yX3w&q=85&s=6a4fc128d359d129e528f70b35c9ed9e" size="md" border alt="EMQX Cloud Data Integration Rule Details showing configuration summary" width="2344" height="1950" data-path="images/integrations/data-ingestion/emqx/data_integration_details.webp" />

All the MQTT messages sent to the `temp_hum/emqx` topic will be persisted into your ClickHouse Cloud database.

<h2 id="saving-data-into-clickhouse">
  Saving Data into ClickHouse
</h2>

We will simulate temperature and humidity data and report these data to EMQX Cloud via the MQTT X and then use the EMQX Cloud Data Integrations to save the data into ClickHouse Cloud.

<Image img="https://mintcdn.com/private-7c7dfe99-detect-table-modification/j2pAbv7ihJZXp9qi/images/integrations/data-ingestion/emqx/work-flow.png?fit=max&auto=format&n=j2pAbv7ihJZXp9qi&q=85&s=68f459792135255d7055fb88859a66d5" size="lg" border alt="EMQX Cloud to ClickHouse Workflow diagram showing data flow" width="1209" height="559" data-path="images/integrations/data-ingestion/emqx/work-flow.png" />

<h3 id="publish-mqtt-messages-to-emqx-cloud">
  Publish MQTT messages to EMQX Cloud
</h3>

You can use any MQTT client or SDK to publish the message. In this tutorial, we will use [MQTT X](https://mqttx.app/), a user friendly MQTT client application provided by EMQ.

<Image img="https://mintcdn.com/private-7c7dfe99-detect-table-modification/nH26ELfEvmC8yX3w/images/integrations/data-ingestion/emqx/mqttx-overview.webp?fit=max&auto=format&n=nH26ELfEvmC8yX3w&q=85&s=7759004c44262077e88043a0b7e1998e" size="lg" border alt="MQTTX Overview showing the client interface" width="2880" height="1800" data-path="images/integrations/data-ingestion/emqx/mqttx-overview.webp" />

Click "New Connection" on MQTTX and fill the connection form:

* Name: Connection name. Use whatever name you want.
* Host: the MQTT broker connection address. You can get it from the EMQX Cloud overview page.
* Port: MQTT broker connection port. You can get it from the EMQX Cloud overview page.
* Username/Password: Use the credential created above, which should be `emqx` and `xxxxxx` in this tutorial.

<Image img="https://mintcdn.com/private-7c7dfe99-detect-table-modification/nH26ELfEvmC8yX3w/images/integrations/data-ingestion/emqx/mqttx-new.webp?fit=max&auto=format&n=nH26ELfEvmC8yX3w&q=85&s=fa4484193545dc962d445e7932a75b9a" size="lg" border alt="MQTTX New Connection Setup form with connection details" width="2174" height="1696" data-path="images/integrations/data-ingestion/emqx/mqttx-new.webp" />

Click the "Connect" button on top right and the connection should be established.

Now you can send messages to the MQTT broker using this tool.
Inputs:

1. Set payload format to "JSON".
2. Set to topic: `temp_hum/emqx` (the topic we just set in the rule)
3. JSON body:

```bash theme={null}
{"temp": 23.1, "hum": 0.68}
```

Click the send button on the right. You can change the temperature value and send more data to MQTT broker.

The data sent to EMQX Cloud should be processed by the rule engine and inserted into ClickHouse Cloud automatically.

<Image img="https://mintcdn.com/private-7c7dfe99-detect-table-modification/nH26ELfEvmC8yX3w/images/integrations/data-ingestion/emqx/mqttx-publish.webp?fit=max&auto=format&n=nH26ELfEvmC8yX3w&q=85&s=ac610fd2aab9e977dc530a77e752e2f3" size="lg" border alt="MQTTX Publish MQTT Messages interface showing message composition" width="2880" height="1800" data-path="images/integrations/data-ingestion/emqx/mqttx-publish.webp" />

<h3 id="view-rules-monitoring">
  View rules monitoring
</h3>

Check the rule monitoring and add one to the number of success.

<Image img="https://mintcdn.com/private-7c7dfe99-detect-table-modification/j2pAbv7ihJZXp9qi/images/integrations/data-ingestion/emqx/rule_monitor.webp?fit=max&auto=format&n=j2pAbv7ihJZXp9qi&q=85&s=786be0e774200d62d23cf00e6cac5857" size="lg" border alt="EMQX Cloud Rule Monitoring dashboard showing message processing metrics" width="2866" height="1066" data-path="images/integrations/data-ingestion/emqx/rule_monitor.webp" />

<h3 id="check-the-data-persisted">
  Check the data persisted
</h3>

Now it's time to take a look at the data on the ClickHouse Cloud. Ideally, the data you send using MQTTX will go to the EMQX Cloud and persist to the ClickHouse Cloud's database with the help of native data integration.

You can connect to the SQL console on ClickHouse Cloud panel or use any client tool to fetch data from your ClickHouse. In this tutorial, we used the SQL console.
By executing the SQL:

```bash theme={null}
SELECT * FROM emqx.temp_hum;
```

<Image img="https://mintcdn.com/private-7c7dfe99-detect-table-modification/nH26ELfEvmC8yX3w/images/integrations/data-ingestion/emqx/clickhouse_result.webp?fit=max&auto=format&n=nH26ELfEvmC8yX3w&q=85&s=64b72da938a557b5fd7c7689063e9730" size="lg" border alt="ClickHouse Query Results showing persisted IoT data" width="3830" height="900" data-path="images/integrations/data-ingestion/emqx/clickhouse_result.webp" />

<h3 id="summary">
  Summary
</h3>

You didn't write any piece of code, and now have the MQTT data move from EMQX cloud to ClickHouse Cloud. With EMQX Cloud and ClickHouse Cloud, you don't need to manage the infra and just focus on writing you IoT applications with data storied securely in ClickHouse Cloud.
