Create a source connector from MQTT to Apache Kafka®
The MQTT source
connector
copies messages from the MQTT topic into Apache Kafka® where they can be
transformed and read by multiple consumers. Then, the Stream Reactor
MQTT source connector creates a queue and binds it to the amq.topic
defined in the KCQL statement, then messages are copied to the Apache
Kafka® service.
See the full set of available parameters and configuration options in the connector's documentation.
The connector can be used to source messages from RabbitMQ® where RabbitMQ MQTT plugin is enabled.
Prerequisites
To set up a MQTT source connector, you need an Aiven for Apache Kafka service with Kafka Connect enabled or a dedicated Aiven for Apache Kafka Connect cluster.
The connector will write to a topic defined in the "connect.mqtt.kcql"
configuration, so either create the topic in your Kafka service, or
enable the auto_create_topic
parameter so that the topic will be
created automatically.
Also collect the following information about the source MQTT server upfront:
USERNAME
: The MQTT username to connectPASSWORD
: The password for the username selectedHOST
: The MQTT hostnamePORT
: MQTT port (usually 1883)KCQL_STATEMENT
: The KCQL statement to be used in the following format:INSERT INTO <your-kafka-topic> SELECT * FROM <your-mqtt-topic>
APACHE_KAFKA_HOST
: The hostname of the Apache Kafka service, only needed when using Avro as data formatSCHEMA_REGISTRY_PORT
: The Apache Kafka's schema registry port, only needed when using Avro as data formatSCHEMA_REGISTRY_USER
: The Apache Kafka's schema registry username, only needed when using Avro as data formatSCHEMA_REGISTRY_PASSWORD
: The Apache Kafka's schema registry user password, only needed when using Avro as data format
Setup a MQTT source connector with Aiven Console
The following example demonstrates how to setup an Apache Kafka MQTT source connector using the Aiven Console.
Define a Kafka Connect configuration file
Define the connector configurations in a file (we'll refer to it with
the name mqtt_source.json
) with the following content, creating a file
is not strictly necessary but allows to have all the information in one
place before copy/pasting them in the Aiven
Console:
{
"name": "CONNECTOR_NAME",
"connect.mqtt.hosts": "tcp://<HOST>:<PORT>",
"connect.mqtt.kcql": "KCQL_STATEMENT",
"connector.class": "com.datamountaineer.streamreactor.connect.mqtt.source.MqttSourceConnector",
"connect.mqtt.username": "USERNAME",
"connect.mqtt.password": "PASSWORD",
"key.converter": "org.apache.kafka.connect.json.JsonConverter",
"connect.mqtt.service.quality": "1",
"value.converter": "org.apache.kafka.connect.json.JsonConverter"
}
The configuration file contains the following entries:
name
: the connector name, replaceCONNECTOR_NAME
with the name to give to the connector.connect.mqtt.hosts
,connect.mqtt.kcql
,connect.mqtt.username
andconnect.mqtt.password
: source MQTT parameters collected in the prerequisite phase.key.converter
andvalue.converter
: The data converter used for this example JSON converter is used.connect.mqtt.service.quality
: Specifies theMqtt
quality of service.
See the dedicated documentation for the full list of parameters.
Create a Kafka Connect connector with the Aiven Console
To create an Apache Kafka Connect connector:
-
Log in to the Aiven Console and select the Aiven for Apache Kafka® or Aiven for Apache Kafka Connect® service where the connector needs to be defined.
-
Select Connectors from the left sidebar.
-
Select Create New Connector,
noteIt is enabled only for services with Kafka Connect enabled.
-
Select Stream Reactor MQTT Source Connector.
-
In the Common tab, locate the Connector configuration text box and select on Edit.
-
Paste the connector configuration (stored in the
mqtt_source.json
file) in the form. -
Select Apply.
To create the connector, access the Aiven Console and select the Aiven for Apache Kafka® or Aiven for Apache Kafka® Connect service where the connector needs to be defined, then:
noteThe Aiven Console parses the configuration file and fills the relevant UI fields. You can review the UI fields across the various tabs and change them if necessary. The changes will be reflected in JSON format in the Connector configuration text box.
-
After all the settings are correctly configured, select Create connector.
-
Verify the connector status under the Connectors screen.
-
Verify the presence of the data in the target Apache Kafka topic, the topic name is the one defined in the
KCQL_STATEMENT
.
You can also create connectors using the Aiven CLI command.