How to use connector management¶
Context¶
You have external data stores that you want to read from and write to with ksqlDB, but you don’t want to write custom glue code to do it. ksqlDB is capable of using the vast ecosystem of Kafka Connect connectors through its SQL syntax. This functionality is called connector management.
In action¶
1 2 3 4 5 6 7 8 9 |
|
Modes¶
Before you can use connector management, you need to decide what mode you want to run connectors in. ksqlDB can run connectors in two different modes: embedded or external. This controls how and where the connectors are executed. The way in which you configure ksqlDB's server determines which mode it will use. All nodes in a single ksqlDB cluster must use the same mode.
Regardless of which mode you use, the syntax to create and use connectors is the same.
Embedded mode¶
In embedded mode, ksqlDB runs connectors directly on its servers. This is convenient because it reduces the number of moving parts that you need to manage in your infrastructure. Embedded mode is highly useful for development, testing, and production workloads that have light/moderate data volumes. Use this mode when you don't need to scale your ingest/egress capacity independently from your processing capacity. When you use embedded mode, ksqlDB server is actually running a Kafka Connect server in distributed mode.
Downloading connectors¶
Before you can use an embedded connector, you need to download it prior to starting ksqlDB. A downloaded connector package is essentially a set of jars that contain the code for interacting with the target data store.
The easiest way to download a connector is to use confluent-hub
, a utility program distributed by Confluent.
Create a directory for your connectors:
1 |
|
Run the following command to get the Voluble data generator connector:
1 |
|
After running this command, confluent-hub-components
should contain the Voluble jars. If you are running in clustered mode, you must install the connector on every server.
When you have all the connectors that you need, configure ksqlDB to find them.
Important
You must restart all of the ksqlDB servers to finish installing the new connectors.
Configuring ksqlDB¶
You control whether ksqlDB uses embedded mode by supplying the server configuration property ksql.connect.worker.config
with the path to a Kafka Connect configuration file. Although embedded mode eases the operational burden of running a full Kafka Connect cluster, it doesn't dilute Kafka Connect's power. Any property that you can configure a regular Kafka Connect cluster with can also be applied to embedded mode.
There are a number of properties that you must set to have a valid Connect setup. Refer to the Kafka Connect documentation to learn about the right properties to set. One critical property is ksql.connect.plugin.path
, which specifies the path to find the connector jars. If you're using Docker, use a volume to mount your connector jars from your host into the container.
Note
If you're deploying with Docker, you can skip setting ksql.connect.worker.config
. ksqlDB will look for environment variables prefixed with KSQL_CONNECT_
. If it finds any, it will remove the KSQL_
prefix and place them into a Connect configuration file. Embedded mode will use that configuration file. This is a convenience to avoid creating and mounting a separate configuration file.
To get started, here is a Docker Compose example with a server configured for embedded mode. All KSQL_
environment variables are converted automatically to server configuration properties. Any connectors installed on your host at confluent-hub-components
are loaded. Save this in a file named docker-compose.yml
:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
|
Bring up the stack with:
1 |
|
Launching a connector¶
Now that ksqlDB has a connector and is configured to run it in embedded mode, you can launch it. Start by running ksqlDB's CLI:
1 |
|
Starting a connector is as simple as giving it a name and properties. In this example, you launch the Voluble connector to source random events into a Kafka topic. Run the following SQL statement:
1 2 3 4 5 6 7 8 9 |
|
Here is what this ksqlDB statement does:
- ksqlDB interacts with Kafka Connect to create a new source connector named
s
. - Kafka Connect infers that
s
is a Voluble connector because of the value ofconnector.class
. Kafka Connect searches its plugin path to find a connector that matches the specified class. - ksqlDB passes the remaining properties directly to the Voluble connector so that it can configure itself.
- Voluble publishes a new event to topic
people
every500
milliseconds with a UUID key and a map value of two keys,name
andcreditCardNumber
.
The properties are the same that you would pass to a connector if it was running in a dedicated Connect cluster. You can pass any properties that the connector or Kafka Connect respects, like max.tasks
to scale the number of instances of the connector.
Check that the connector is working by printing the contents of the people
topic, which connector s
created.
1 |
|
Because the data is random, your output should look roughly like the following:
1 2 3 4 5 6 7 |
|
When you're done, you can drop the connector by running:
1 |
|
You can confirm that the connector is no longer running by looking at the output of SHOW CONNECTORS;
.
Logging¶
Embedded Kafka Connect logs messages inline with ksqlDB's server's log messages. View them by running the following command:
1 |
|
Introspecting embedded mode¶
Sometimes you might need a little more power to introspect how your connectors are behaving by interacting directly with the embedded Kafka Connect server. First, notice that ksqlDB is really just wrapping a regular Kafka Connect server. You can curl it and interact with its REST API just like any other Connect server.
1 |
|
Your output should resemble:
1 |
|
This can be really useful if you're having trouble getting a connector to load or need more insight into how connector tasks are behaving.
External mode¶
In external mode, ksqlDB communicates with an external Kafka Connect cluster. It's able to create and destroy connectors as needed. Use external mode when you have high volumes of input and output, or need to scale your ingest/egress capacity independently from your processing capacity.
External mode essentially works the same way as embedded mode, except connectors run outside of ksqlDB's servers. All that is needed is to configure ksqlDB server with the ksql.connect.url
property, indicating the address of the Connect server. Beyond that, you can manage connectors exactly as you would in embedded mode. No other configuration is needed.
This guide omits an example of setting up an external Kafka Connect cluster. Many great examples, like Apache Kafka's, have already been published.
Tear down the stack¶
When you're done, tear down the stack by running:
1 |
|