Writing a Kafka Producer Flask API

Byte Blog
2 min readApr 27, 2023

--

Apache Kafka is a distributed streaming platform that is widely used for building real-time data pipelines and streaming applications. Flask is a popular Python web framework that allows developers to build web applications quickly and easily. In this tutorial, we’ll show you how to create a Flask API that writes to a Kafka topic.

Prerequisites:

  • Python 3.6 or later
  • Flask
  • kafka-python

Step 1: Set up a Kafka cluster

To create a Kafka topic, you need to have access to a Kafka cluster. You can use a local Kafka cluster or a cloud-based Kafka cluster like Confluent Cloud or Amazon MSK. Follow the Kafka documentation to set up a Kafka cluster.

Step 2: Install required packages

Install Flask and kafka-python packages using pip:

pip install Flask kafka-python

Step 3: Create a Flask app

Create a Flask app and import the required packages:

from flask import Flask, request
from kafka import KafkaProducer

app = Flask(__name__)
producer = KafkaProducer(bootstrap_servers='localhost:9092')

The bootstrap_servers argument specifies the Kafka broker address.

Step 4: Create an endpoint to send messages to Kafka

Create a Flask endpoint that sends messages to Kafka:

@app.route('/send_message', methods=['POST'])
def send_message():
message = request.json
producer.send('my_topic', message)
return 'Message sent successfully'

The endpoint receives a JSON payload in the request body and sends the message to the my_topic Kafka topic.

Step 5: Run the Flask app

Run the Flask app using the following command:

if __name__ == '__main__':
app.run(debug=True)

This command starts the Flask app on a local server.

Step 6: Test the API

To test the Flask API, send a POST request to the /send_message endpoint with a JSON payload:

import requests

message = {'key': 'value'}
response = requests.post('http://localhost:5000/send_message', json=message)
print(response.content)

This code sends a message to the Flask API, which then writes the message to the my_topic Kafka topic.

In conclusion, creating a Flask API that writes to a Kafka topic is a straightforward process. By following the steps outlined in this tutorial, you can quickly set up a Flask app and write messages to a Kafka topic. This is just the beginning of what you can do with Kafka and Flask — you can use these technologies to build powerful streaming applications that process real-time data.

--

--