PleinIQ
Menu
Language

Choose your language

This page is only in English for now. Choosing a language opens its home page.

Other languages

Your browser can translate any page into almost any language.

Other browsers: Open the browser menu and look for “Translate”.

Your resume, your downloads and lab commands are never translated.

Roadmaps / Kafka / Lab 5

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.

  • Install
    /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 ↗
    Install
    brew install --cask multipass

    Check it worked: multipass version

Before you start

  • Start your server and cd ~/kafka-streaming-lab.

Step by step

  1. 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.

    File: 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.jar
    
  2. 2 Write the source connector

    It reads new lines from /data/signups.txt and writes each one to the signups topic. Create connect/signups-source.properties.

    File: connect/signups-source.properties
    name=signups-file-source
    connector.class=org.apache.kafka.connect.file.FileStreamSourceConnector
    tasks.max=1
    file=/data/signups.txt
    topic=signups
    
  3. 3 Write the sink connector

    It reads the signups topic and appends each record to /data/signups-copy.txt. Create connect/signups-sink.properties.

    File: connect/signups-sink.properties
    name=signups-file-sink
    connector.class=org.apache.kafka.connect.file.FileStreamSinkConnector
    tasks.max=1
    file=/data/signups-copy.txt
    topics=signups
    
  4. 4 Start the Connect worker

    The worker runs in its own container from the same Kafka image. data is shared with it as /data.

    Run this on your lab server
    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/connectors
    You should see:

    ["signups-file-source","signups-file-sink"].

  5. 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.

    Run this on your lab server
    echo "cat@example.com" >> data/signups.txt
    sleep 8
    cat data/signups-copy.txt
    kafka console-consumer --topic signups --from-beginning --timeout-ms 5000
    You should see:

    All three addresses appear in the copy and in the topic.

  6. 6 Check status over the REST API

    This is how you would monitor connectors in production.

    Run this on your lab server
    curl -s localhost:8083/connectors/signups-file-source/status
    docker stop connect && docker rm connect
    You should see:

    "state":"RUNNING" for the connector and its task.

  7. 7 Write your notes and push

    Create notes/05-connect.md (do not commit the data folder), then commit.

    File: notes/05-connect.md
    # 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. 8 Commit and push

    Save your connector configs.

    Run this on your lab server
    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.

Finished? Get your proof page

We check your public GitHub repository and give you a page to share with recruiters, and can add the project to your resume.

Join PleinIQ Updates

What are you interested in?

Read how we handle your information in our privacy notice.