Skip to main content
Streaming pipeline

Set up Kafka

If you don’t have Kafka already setup, here is a quick guide on how to run and use Kafka locally:

Using Kafka locally

  1. In your terminal, clone this repository: git clone https://github.com/wurstmeister/kafka-docker.git.
  2. Change directory into that repository: cd kafka-docker.
  3. Edit the docker-compose.yml file to match this:
  4. Start Docker:
    If you encounter the error command not found: docker-compose, try running the following command instead:
  5. Start a terminal session in the running container:
  6. Create a topic:
  7. List all available topics in Kafka instance:
  8. Start a producer on topic named test:
  9. Send messages to the topic named test by typing the following JSON strings in the terminal (Note that Kafka messages in Mage are assumed to be in JSON format):
  10. Open another terminal and start a consumer on the topic named test:
  1. The output should look something like this:
Original source of instructions.

Build streaming pipeline

Start Mage

Using Kafka locally in a Docker container

Start Mage using Docker. Run the following command to run Docker in network mode:
Change the environment variables argument depending on your cloud provider.
If the network named kafka-docker_default doesn’t exist, create a new network:
Check that it exists:

If you can’t connect to Kafka locally in a Docker container using Mage in a Docker container, do the following:
  1. Clone Mage: git clone https://github.com/mage-ai/mage-ai.git.
  2. Change directory into Mage: cd mage-ai.
  3. Edit the docker-compose.yml file to match this:
  4. Run the following script in your terminal: ./scripts/dev.sh.
This will run Mage in development mode; which runs it in a Docker container using docker compose instead of docker run.

Using Kafka without a Docker container

Start Mage using Docker. If you haven’t done this before, refer to the setup guide.

Create a new pipeline

  1. Open Mage in your browser.
  2. Click + New pipeline, then select Streaming.
  3. Add a data loader block, select Kafka, and paste the following:
    1. By default, the bootstrap_server is set to localhost:9092. If you’re running Mage in a docker container, the bootstrap_server should be kafka:9093.
    2. Messages are consumed from source in micro batch mode for better efficiency. The default batch size is 100. You can adjust the batch size in the source config.
  4. Add a transformer block and paste the following:
  5. Add a data exporter block, select OpenSearch and paste the following:
    1. Change the host to match your OpenSearch domain’s endpoint.
    2. Change the index_name to match the index you want to export data into.

Test pipeline

Open the streaming pipeline you just created, and in the right side panel near the bottom, click the button Execute pipeline to test the pipeline. You should see an output like this:

Publish messages using Python

  1. Open a terminal on your local workstation.
  2. Install kafka-python:
  3. Open a Python shell and write the following code to publish messages:
Once you run the code snippet above, go back to your streaming pipeline in Mage and the output should look like this:

Consume messages using Python

If you want to programmatically consume messages from a Kafka topic, here is a code snippet:

Run in production

  1. Create a trigger.
  2. Once trigger is created, click the Start trigger button at the top of the page to make the streaming pipeline active.