You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: en_US/data-integration/schema-registry.md
+133-2Lines changed: 133 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -11,15 +11,15 @@ Because of the variety of IoT device terminals and the different coding formats
11
11
The Schema Registry manages the Schema used for coding and decoding, processes the encoding or decoding requests, and returns the results. The Schema Registry in collaboration with the rule engine can be adapted for device access and rule design in
12
12
various scenarios.
13
13
14
-
EMQX Schema Registry currently supports codecs in below formats:
14
+
EMQX Schema Registry currently supports codecs in the below formats:
Avro and Protobuf are Schema-dependent data formats. The encoded data is binary and the decoded data is in [Map format](#rule-engine-internal-data-format-map). The decoded data can be used directly by the rule engine and other plugins. Schema Registry maintains Schema text for built-in encoding formats such as Avro and Protobuf.
21
21
22
-
JSON schema can be used to validate if input JSON object is following the schema definetions or if the JSON object output from the rule engine is valid before producing the data to downstream.
22
+
JSON schema can be used to validate if the input JSON object is following the schema definitions or if the JSON object output from the rule engine is valid before producing the data to downstream.
23
23
24
24
The diagram below shows an example of a Schema Registry application. Multiple devices report data in different formats, which are decoded by Schema Registry into a uniform internal format and then forwarded to the backend application.
25
25
@@ -105,3 +105,134 @@ The SQL statement above will match an MQTT message with the content of the paylo
105
105
106
106
**Note:** The `AS` clause is required to assign the decoded data to a key so that subsequent operations can be performed on it later.
107
107
108
+
109
+
## External Schema Registry
110
+
111
+
Starting with version 5.8.1, EMQX supports configuring an external Confluent Schema Registry (CSR). This feature allows users to dynamically retrieve schemas from external registries during rule processing, enabling efficient message encoding and decoding.
112
+
113
+
### Create External Schema Registry in Dashboard
114
+
115
+
You can configure an external schema registry directly through the EMQX Dashboard, making it easy to manage your schema integration.
116
+
117
+
Go to **Integration** -> **Schema** on EMQX Dashboard. Select the **External** tab on the Schema page.
118
+
119
+
Click the **Create** button at the upper right corner. Configure the following fields:
120
+
121
+
- **Name**: Enter an external schema registry name that will be used in the encoding and decoding functions.
122
+
- **Type**: Select the type of external schema registry. Currently, only `Confluent` is supported.
123
+
- **URL**: Enter the endpoint of your Confluent Schema Registry.
124
+
- **Authentication**: If you select `Basic auth`, enter the authentication credentials (username and password) for accessing the external registry.
125
+
126
+
Click **Create** after you complete the settings.
127
+
128
+
### Configure External Schema Registry via Configuration File
129
+
130
+
You can configure an external Confluent Schema Registry via the EMQX configuration file. Here’s an example of how to set it up:
131
+
132
+
133
+
```hcl
134
+
schema_registry {
135
+
external {
136
+
my_external_registry {
137
+
type = confluent
138
+
url = "https://confluent.registry.url:8081"
139
+
auth {
140
+
username = "myuser"
141
+
password = "secret"
142
+
}
143
+
}
144
+
}
145
+
}
146
+
```
147
+
148
+
In this example:
149
+
150
+
- `my_external_registry` is the name assigned to the external schema registry.
151
+
- `type = confluent` specifies the type of external registry.
152
+
- `url` is the endpoint of your Confluent Schema Registry.
153
+
- `auth` contains the authentication credentials (username and password) for accessing the external registry.
154
+
155
+
### Use External Schema Registry in Rule Engine
156
+
157
+
Once an external registry is configured, you can use several functions in the EMQX rule engine to encode and decode payloads using the schemas stored in the external registry.
158
+
159
+
The following functions utilize a configured external CSR:
In all function usage examples below, the following example values and variable names are used:
169
+
170
+
- `my_external_registry` is the name you assigned to the external registry in EMQX.
171
+
- `my_schema_id` is the schema ID registered in the CSR (always an integer in CSR).
172
+
- `my_local_avro_schema` is the name of a locally configured Avro schema in EMQX.
173
+
- `my_subject` is the subject name defined in the CSR.
174
+
175
+
#### Function Usage Examples
176
+
177
+
##### `avro_encode`
178
+
179
+
`avro_encode` encodes a payload using the schema ID from the external registry. The schema is retrieved dynamically at runtime and cached for subsequent runs. In Confluent Schema Registry, schema IDs are integers.
180
+
181
+
::: tip Note
182
+
183
+
When encoding, the payload must be in the internal data format of the rule engine, which is a decoded map. This is why `json_decode` is used in the example.
184
+
185
+
:::
186
+
187
+
Example:
188
+
189
+
```sql
190
+
select
191
+
-- 123 is the schema ID that is registered in CSR
192
+
avro_encode('my_external_registry', json_decode(payload), 123) as encoded
193
+
from 't'
194
+
```
195
+
196
+
##### `avro_decode`
197
+
198
+
This function decodes an Avro payload based on the specified schema ID from the external registry. The schema is dynamically fetched during runtime and cached for subsequent operations.
199
+
200
+
Example:
201
+
202
+
```sql
203
+
select
204
+
-- 123 is the schema ID that is registered in CSR
205
+
avro_decode('my_external_registry', payload, 123) as decoded
206
+
from 't'
207
+
```
208
+
209
+
##### `schema_encode_and_tag`
210
+
211
+
This function uses a locally registered Avro schema, an external CSR schema name, and a subject to encode a payload (already in internal map format), and to tag the resulting payload with a schema ID. The schema ID comes from registering the local schema to CSR.
212
+
213
+
Example:
214
+
215
+
```sql
216
+
select
217
+
schema_encode_and_tag(
218
+
'my_local_avro_schema',
219
+
'my_external_registry',
220
+
json_decode(payload),
221
+
'my_subject'
222
+
) as encoded
223
+
from 't'
224
+
```
225
+
226
+
##### `schema_decode_tagged`
227
+
228
+
This function uses a CSR name to decode a payload, assuming it is tagged with the schema ID retrieved from CSR.
0 commit comments