Lab 5 of 8 · Kafka Connect: moving data in and out
Stream a file through Kafka
Run Kafka Connect in standalone mode with a file source and a file sink, and watch new lines flow through Kafka within seconds.
- Time
- 60 minutes
- Cost
- Free.
What you learn
Kafka Connect moves data between Kafka and other systems without custom code. You run a Connect worker with the file connectors that ship with Kafka, stream lines from a file into a topic and out to another file, and manage connectors over its REST API.
- Source and sink connectors, workers and tasks
- Standalone and distributed mode
- Converters: how bytes become records
- Checking connector status over the REST API
In the AI era: AI can draft connector configs for databases and cloud services, but it invents properties that do not exist. Check every property against the connector’s own documentation and test on lab data first.
Get set up
Commands use Homebrew in the Terminal app. Already have a tool? Skip it.
- A terminalOfficial install page ↗
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"Use the Terminal app. The command installs Homebrew, which the other macOS commands use.
- Multipass (a free Ubuntu server on your computer)Official install page ↗
brew install --cask multipassCheck it worked:
multipass version
Before you start
Start your server and
cd ~/kafka-streaming-lab.
Step by step
1 Write the worker settings
The worker connects to Kafka, treats records as plain strings, and loads the file connectors that ship inside the Kafka image. Create
connect/worker.properties.bootstrap.servers=localhost:9092 key.converter=org.apache.kafka.connect.storage.StringConverter value.converter=org.apache.kafka.connect.storage.StringConverter offset.storage.file.filename=/tmp/connect.offsets plugin.path=/opt/kafka/libs/connect-file-4.1.0.jar2 Write the source connector
It reads new lines from
/data/signups.txtand writes each one to thesignupstopic. Createconnect/signups-source.properties.name=signups-file-source connector.class=org.apache.kafka.connect.file.FileStreamSourceConnector tasks.max=1 file=/data/signups.txt topic=signups3 Write the sink connector
It reads the
signupstopic and appends each record to/data/signups-copy.txt. Createconnect/signups-sink.properties.name=signups-file-sink connector.class=org.apache.kafka.connect.file.FileStreamSinkConnector tasks.max=1 file=/data/signups-copy.txt topics=signups4 Start the Connect worker
The worker runs in its own container from the same Kafka image.
datais shared with it as/data.mkdir -p data && printf "ana@example.com\nben@example.com\n" > data/signups.txt && chmod -R a+rw data docker run -d --name connect --network host \ -v "$PWD/connect":/connect -v "$PWD/data":/data \ apache/kafka:4.1.0 /opt/kafka/bin/connect-standalone.sh \ /connect/worker.properties /connect/signups-source.properties /connect/signups-sink.properties sleep 25 curl -s localhost:8083/connectorsYou should see:["signups-file-source","signups-file-sink"].5 Watch a new line flow through
Append a line to the source file and read the copy a few seconds later. It went file, Kafka topic, file.
echo "cat@example.com" >> data/signups.txt sleep 8 cat data/signups-copy.txt kafka console-consumer --topic signups --from-beginning --timeout-ms 5000You should see:All three addresses appear in the copy and in the topic.
6 Check status over the REST API
This is how you would monitor connectors in production.
curl -s localhost:8083/connectors/signups-file-source/status docker stop connect && docker rm connectYou should see:"state":"RUNNING"for the connector and its task.7 Write your notes and push
Create
notes/05-connect.md(do not commit thedatafolder), then commit.# Kafka Connect - Source vs sink: ... - What the converter does: ... - Where the source keeps its position: ... - A real connector I would use at work (for example a database or S3) and why: ...8 Commit and push
Save your connector configs.
echo "data/" >> .gitignore git add .gitignore connect notes git commit -m "Add Kafka Connect file source and sink" git push
You are done when
The REST API listed both connectors as running.
A line appended to the source file appeared in the copy.
Your connector configs are on GitHub.
We check your public GitHub repository and give you a page to share with recruiters, and can add the project to your resume.