Skip to content

Commit 5595ade

Browse files
Meggielqkclaude
andcommitted
docs: add Databricks data integration documentation
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 11dbdce commit 5595ade

16 files changed

Lines changed: 435 additions & 0 deletions
191 KB
Loading
360 KB
Loading
303 KB
Loading
192 KB
Loading
491 KB
Loading
404 KB
Loading
181 KB
Loading
Lines changed: 218 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,218 @@
1+
# Ingest MQTT Data into Databricks
2+
3+
[Databricks](https://www.databricks.com/) is a unified data analytics platform built on Apache Spark, designed for large-scale data engineering, machine learning, and collaborative analytics. EMQX integrates with Databricks by writing MQTT data into an Amazon S3 bucket managed by Databricks, which Databricks can then query directly through external locations.
4+
5+
This page provides a detailed introduction to the data integration between EMQX and Databricks and offers practical guidance on the connector and Sink creation.
6+
7+
## How It Works
8+
9+
Databricks data integration in EMQX is built on top of the Amazon S3 integration. EMQX writes MQTT data into an S3 bucket managed by Databricks. Databricks accesses this bucket via an external location, allowing direct SQL queries over the stored data.
10+
11+
![EMQX Databricks Data Integration](./assets/emqx-integration-databricks.jpg)
12+
13+
The specific workflow is as follows:
14+
15+
1. **Device Connection to EMQX**: IoT devices trigger an online event upon successfully connecting via the MQTT protocol. The event includes device ID, source IP address, and other property information.
16+
2. **Device Message Publishing and Receiving**: Devices publish telemetry and status data through specific topics. EMQX receives the messages and compares them within the rules engine.
17+
3. **Rules Engine Processing Messages**: The built-in rules engine processes messages and events from specific sources based on topic matching. It matches corresponding rules and processes messages and events, such as data format transformation, filtering specific information, or enriching messages with context information.
18+
4. **Writing to Amazon S3**: The rule triggers the Amazon S3 Sink to write the processed data into the S3 bucket associated with the Databricks workspace.
19+
5. **Databricks Reads from S3**: Databricks queries the data stored in the S3 bucket directly via an external location, enabling real-time analytics and machine learning workflows.
20+
21+
## Features and Benefits
22+
23+
Using Databricks data integration in EMQX can bring the following features and advantages to your business:
24+
25+
- **Message Transformation**: Messages can undergo extensive processing and transformation in EMQX rules before being written to S3, facilitating subsequent storage and analysis.
26+
- **Flexible Data Operations**: With the Amazon S3 Sink, specific fields of data can be conveniently written into the Databricks-managed S3 bucket, supporting dynamic object key configuration for flexible data storage.
27+
- **Unified Analytics Platform**: By integrating EMQX with Databricks, IoT data becomes immediately available for SQL analytics, machine learning, and data engineering pipelines within the Databricks workspace.
28+
- **Low-Cost Long-Term Storage**: Leveraging S3 as the underlying storage provides a highly available, reliable, and cost-effective data store, suitable for large-scale IoT workloads.
29+
30+
## Before You Start
31+
32+
This section introduces the preparations required before creating the Amazon S3 connector and Sink for Databricks in EMQX.
33+
34+
### Prerequisites
35+
36+
Before proceeding, make sure you are familiar with the following:
37+
38+
#### EMQX Concepts:
39+
40+
- [Rule Engine](./rules.md): Understand how rules define the logic for extracting and transforming data from MQTT messages.
41+
- [Data Integration](./data-bridges.md): Understand the concept of connectors and sinks in EMQX data integration.
42+
43+
#### Databricks Concepts:
44+
45+
- **Workspace**: A Databricks workspace is the environment where you access all Databricks assets.
46+
- **External Location**: A Databricks feature that maps an external S3 path so that data stored there can be queried directly using SQL.
47+
- **Storage Credential**: An access credential in Databricks that grants permission to read and write an external storage location.
48+
49+
### Set Up Databricks on AWS Marketplace
50+
51+
This section uses subscribing to Databricks on AWS Marketplace as an example deployment.
52+
53+
1. Subscribe to Databricks on the [AWS Marketplace](https://aws.amazon.com/marketplace/). You will be guided to create a Databricks account and a Databricks workspace.
54+
55+
![Databricks on AWS Marketplace](./assets/databricks-marketplace.png)
56+
57+
2. Once subscribed, create a workspace. Select a region and storage option, then click **Create**.
58+
59+
![Create Databricks Workspace](./assets/databricks-create-workspace.png)
60+
61+
After the workspace is created, it will appear in the **Workspaces** list. Note the S3 bucket name automatically provisioned for the workspace (for example, `databricks-workspace-stack-142ec-bucket`). This bucket will be used to store MQTT data from EMQX.
62+
63+
![Databricks Workspaces](./assets/databricks-workspaces.png)
64+
65+
3. Open the workspace, go to **Catalog** -> **External locations** to create an external location that points to the S3 path where EMQX will write data.
66+
67+
![Databricks External Locations](./assets/databricks-external-locations.png)
68+
69+
Click **Create location**, set the **Storage type** to `S3`, enter the **URL** as `s3://databricks-workspace-stack-142ec-bucket/emqx-iot-data-new`, and select a **Storage credential**.
70+
71+
![Create External Location](./assets/databricks-create-external-locations.png)
72+
73+
4. Obtain the AWS access credentials (Access Key ID and Secret Access Key) for the IAM user or role that has read/write access to the S3 bucket. These credentials will be used to configure the EMQX connector.
74+
75+
With the Databricks workspace and S3 bucket configured, you are now ready to create the connector and Sink in EMQX.
76+
77+
## Create a Connector
78+
79+
Before adding the Amazon S3 Sink, you need to create the corresponding connector.
80+
81+
1. Go to the Dashboard **Integration** -> **Connector** page.
82+
2. Click the **Create** button in the top right corner.
83+
3. Select **Amazon S3** as the connector type and click **Next**.
84+
4. Enter a name for the connector. The name must start with a letter or number and can contain letters, numbers, hyphens, or underscores. In this example, enter `my-databricks`.
85+
5. Enter the connection information:
86+
- **Host**: Enter the S3 endpoint for the AWS region where your Databricks workspace is deployed, formatted as `s3.{region}.amazonaws.com`.
87+
- **Port**: Enter `443`.
88+
- **Access Key ID** and **Secret Access Key**: Enter the AWS access credentials obtained in [Set Up Databricks on AWS Marketplace](#set-up-databricks-on-aws-marketplace).
89+
6. Use the default values for the remaining settings.
90+
7. Before clicking **Create**, you can click **Test Connectivity** to test if the connector can connect to the S3 service.
91+
8. Click the **Create** button at the bottom to complete the connector creation.
92+
93+
You have now completed the connector creation and will proceed to create a rule and Sink for specifying the data to be written into the Databricks-managed S3 bucket.
94+
95+
## Create a Rule with Amazon S3 Sink
96+
97+
This section demonstrates how to create a rule in EMQX to process messages from the source MQTT topic `t/#` and write the processed results to the Databricks-managed S3 bucket through the configured Sink.
98+
99+
1. Go to the Dashboard **Integration** -> **Rules** page.
100+
2. Click the **Create** button in the top right corner.
101+
3. Enter the rule ID `my_rule`, and input the following rule SQL in the SQL editor:
102+
103+
```sql
104+
SELECT
105+
*
106+
FROM
107+
"t/#"
108+
```
109+
110+
::: tip
111+
112+
If you are new to SQL, you can click **SQL Examples** and **Enable Debug** to learn and test the rule SQL results.
113+
114+
:::
115+
116+
4. Add an action, select `Amazon S3` from the **Action Type** dropdown list, keep the action dropdown as the default `create action` option, or choose a previously created Amazon S3 action from the action dropdown. Here, create a new Sink and add it to the rule.
117+
118+
5. Enter the Sink's name and description.
119+
120+
6. Select the `my-databricks` connector created earlier from the connector dropdown. You can also click the create button next to the dropdown to quickly create a new connector in the pop-up box. The required configuration parameters can be found in [Create a Connector](#create-a-connector).
121+
122+
7. Set the **Bucket** by entering `databricks-workspace-stack-142ec-bucket`. This field also supports `${var}` format placeholders, but ensure the corresponding bucket exists in S3.
123+
124+
8. Select **ACL** as needed, specifying the access permission for the uploaded object.
125+
126+
9. Select the **Upload Method**:
127+
128+
- **Direct Upload**: Each time the rule is triggered, data is uploaded directly to S3 according to the preset object key and content. This method is suitable for storing binary or large text data.
129+
- **Aggregated Upload**: This method packages the results of multiple rule triggers into a single file (such as a CSV file) and uploads it to S3, making it suitable for storing structured data. It can reduce the number of files and improve write efficiency.
130+
131+
The configuration parameters differ for each method. Please configure according to the selected method:
132+
133+
:::: tabs type
134+
135+
::: tab Direct Upload
136+
137+
Direct Upload requires configuring the following fields:
138+
139+
- **Object Key**: Defines the object's location to be uploaded to the bucket. It supports placeholders in the format of `${var}` and can use `/` to specify storage directories. Here, enter `emqx-iot-data-new/${clientid}_${timestamp}.json`, where `${clientid}` is the client ID and `${timestamp}` is the timestamp of the message.
140+
- **Object Content**: By default, this is in JSON text format containing all fields. It supports placeholders in the format of `${var}`. Here, enter `${payload}` to use the message body as the object content.
141+
142+
:::
143+
144+
::: tab Aggregate Upload
145+
146+
Aggregate Upload requires configuring the following parameters:
147+
148+
- **Object Key**: Used to specify the storage path of the object. The following variables can be used:
149+
150+
- **`${action}`**: Action name (required).
151+
- **`${node}`**: Name of the EMQX node performing the upload (required).
152+
- **`${datetime.{format}}`**: Start date and time of the aggregation, with the format specified by the `{format}` string (required):
153+
- **`${datetime.rfc3339utc}`**: RFC3339 date and time in UTC format.
154+
- **`${datetime.rfc3339}`**: RFC3339 date and time in local time zone format.
155+
- **`${datetime.unix}`**: Unix timestamp.
156+
- **`${datetime_until.{format}}`**: End date and time of the aggregation, with format options as above.
157+
- **`${sequence}`**: Sequence number for aggregated uploads within the same time interval (required).
158+
159+
- **Aggregation Type**: Currently, CSV and JSON Lines are supported.
160+
- `CSV`: Data will be written to S3 in comma-separated CSV format.
161+
- `JSON Lines`: Data will be written to S3 in [JSON Lines](https://jsonlines.org/) format.
162+
163+
- **Column Order** (applies only when the Aggregation Type is `CSV`): Adjust the order of rule result columns through a dropdown selection.
164+
165+
- **Max Records**: When the maximum number of records is reached, the aggregation of a single file will be completed and uploaded.
166+
167+
- **Time Interval**: When the time interval is reached, even if the maximum number of records has not been reached, the aggregation of a single file will be completed and uploaded.
168+
169+
:::
170+
171+
::::
172+
173+
10. **Fallback Actions (Optional)**: If you want to improve reliability in case of message delivery failure, you can define one or more fallback actions. See [Fallback Actions](./data-bridges.md#fallback-actions) for more details.
174+
175+
11. Expand **Advanced Settings** and configure the advanced setting options as needed (optional). For more details, refer to [Advanced Settings](#advanced-settings).
176+
177+
12. Use the default values for the remaining settings. Click the **Create** button to complete the Sink creation. After successful creation, the page will return to the rule creation, and the new Sink will be added to the rule actions.
178+
179+
13. Back on the rule creation page, click the **Create** button to complete the entire rule creation process.
180+
181+
You have now successfully created the rule. You can see the newly created rule on the **Rules** page and the new Amazon S3 Sink on the **Actions (Sink)** tab.
182+
183+
## Test the Rule
184+
185+
Use MQTTX to publish a message to the topic `t/1`:
186+
187+
```bash
188+
mqttx pub -i emqx_c -t t/1 -m '{ "msg": "hello Databricks" }'
189+
```
190+
191+
After sending a few messages, in your Databricks workspace, right-click **Workspace**, select **Create** -> **Notebook** to create a new notebook.
192+
193+
![Create Notebook](./assets/databricks-create-notebook.png)
194+
195+
In the notebook, run a SQL query against the external location to verify the data has been ingested successfully:
196+
197+
```sql
198+
SELECT * FROM json.`s3://databricks-workspace-stack-142ec-bucket/emqx-iot-data-new/`
199+
```
200+
201+
![Databricks Query Result](./assets/databricks-query-result.png)
202+
203+
## Advanced Settings
204+
205+
This section delves into the advanced configuration options available for the Amazon S3 Sink. In the Dashboard, when configuring the Sink, you can expand **Advanced Settings** to adjust the following parameters based on your specific needs.
206+
207+
| Field Name | Description | Default Value |
208+
| -------------------------------- | ------------------------------------------------------------ | --------------- |
209+
| **Buffer Pool Size** | Specifies the number of buffer worker processes, which are allocated to manage the data flow between EMQX and S3. | `16` |
210+
| **Request TTL** | Specifies the maximum duration, in seconds, that a request is considered valid once it enters the buffer. | `45` |
211+
| **Health Check Interval** | Specifies the time interval (in seconds) for the Sink to perform automatic health checks on its connection with S3. | `15` seconds |
212+
| **Health Check Interval Jitter** | A uniform random delay added on top of the base health check interval to reduce the chance that multiple nodes initiate health checks at the same time. | `0` millisecond |
213+
| **Health Check Timeout** | Specify the timeout duration for the connector to perform automatic health checks on its connection with S3. | `60` seconds |
214+
| **Max Buffer Queue Size** | Specifies the maximum number of bytes that can be buffered by each buffer worker process in the S3 Sink. | `256` MB |
215+
| **Query Mode** | Allows you to choose between `synchronous` or `asynchronous` request modes to optimize message transmission. | `Asynchronous` |
216+
| **In-flight Window** | Controls the maximum number of in-flight queue requests that can exist simultaneously during Sink communication with S3. | `100` |
217+
| **Min Part Size** | The minimum chunk size for part uploads after aggregation is complete. | `5MB` |
218+
| **Max Part Size** | The maximum chunk size for part uploads. | `5GB` |
191 KB
Loading
360 KB
Loading

0 commit comments

Comments
 (0)