Pykafka Integration with Python Flask

Apache Kafka is an open-source stream-processing software platform developed by the Apache Software Foundation, written in Scala and Java. The project aims to provide a unified, high-throughput, low-latency platform for handling real-time data feeds. we can discuss Apache Kafka and how Python programmers can use it for building distributed systems. In this blog we can learn what Kafka is? and how we can help to understand the need for a tool like Kafka, and then get started with it.

Pykafka Integration with Python/Flask

PyKafka is a programmer-friendly Kafka client for Python. It includes Python implementations of Kafka producers and consumers, which are optionally backed by a C extension, built on librdkafka(

PyKafka’s primary goal is to provide a similar level of abstraction to the JVM Kafka client( using idioms familiar to Python programmers and exposing the most Pythonic API possible...

Kafka cluster


                                                       Kafka Integration with python

Business Requirement
The main objective of this project is to build a live map of London with real-time updates. We will use apache Kafka, javascript and python(flask Pykafk and JSON)

Note:  In this document, we explained step by step integration between Python/flask and Kafka (to show live map) using pykafka.

Steps :

  • Download java

We can download java from below URL-

download Java

Once java is downloaded need to install it..and set the path in the environment variable that is present in advance system settings.

  • Click on advance system settings.
  • Click on environment variables.

Install Java

Click on an environment variable, and then click on system variable and set the path of java

Install location_Java


And in user variable click on the path then edit button and set the java path.


Once the path is set now you can check the java version by entering the cmd

>> java – version in command prompt.

2) Download Apache Kafka by clicking on the below URL.


Once Kafka has downloaded, unzip it

Open the command prompt and go to the directory where you unzip Kafka folder 

And hit the cmd


Hit enter, if you get something like this.

install kafka

Congrats you have successfully installed Kafka in your windows.

Kafka location

We need to set the Kafka path in an environment variable for proper use of Kafka server

How to start Kafka in windows:

Step1: Go to the directory where Kafka is installed. 

Step2: Make a folder called data

Step3: Under data again create two folders Kafka and another one is zookeeper..we need to this folder for storing logs

Step4: We need to modify the zookeeper path n file 

F:kafka_2.12-2.2.0config under this directory

Kafka_ string


Step5: need to modify the Kafka server path F:kafka_2.12-2.2.0config under server properties.

kafka log

Step6: Need to start zookeeper server by entering the following cmd in cmd prompt

>>F:kafka_2.12-2.2.0inwindows>zookeeper-server-start.bat ../../config/ 

If you see the following screen then your zookeeper server is up to running.

zookeeper server

Step7: Need to start kafka server by entering the following cmd in cmd prompt

>>F:kafka_2.12-2.2.0inwindows>kafka-server-start.bat ../../config/ 

If you see the following screen then your Kafka server is up to running.

running kafka server

Step8: Need to create a topic by run the following cmd in cmd prompt

>>F:kafka_2.12-2.2.0inwindows>kafka-topics.bat --zookeeper --topic test_topic --create -- partitions 1 -- replication-factor 1

Created topic test topic

Step9: start producer by entering the following cmd.

>>F:kafka_2.12-2.2.0inwindows>kafka-console-producer.bat --broker-list localhost:9092 --topic test_topic




Step10: Start a consumer by entering the following cmd.

>>F:kafka_2.12-2.2.0inwindows>kafka-console-consumer.bat --bootstrap-server localhost:9092 --topic test_topic --from-beginning

Local host

Connect to flask:
>>pip install pykafka

Here we are generating live bus location

So we need to generate a map API by hit the below URL once you successfully logged in. you see an access token in your map box dashboard copy it

import kafka client

kafka producer

index html


var mymap ='mapid').setView([51.505, -0.09], 13); 


{ attribution: 'Map data © OpenStreetMap 

contributors, CC-BY-SA, Imagery © Mapbox', 

maxZoom: 18, 

id: 'mapbox.streets', 




mapMarkers1 = []; 

mapMarkers2 = []; 

mapMarkers3 = []; 

var source = new EventSource('/topic/TOPICNAME'); //ENTER YOUR TOPICNAME HERE 

source.addEventListener('message', function(e){ 


obj = JSON.parse(; 


if(obj.busline == '00001') { 

for (var i = 0; i < mapMarkers1.length; i++) { 



marker1 = L.marker([obj.latitude, obj.longitude]).addTo(mymap); 



if(obj.busline == '00002') { 

for (var i = 0; i < mapMarkers2.length; i++) { 



marker2 = L.marker([obj.latitude, obj.longitude]).addTo(mymap); 



if(obj.busline == '00003') { 

for (var i = 0; i < mapMarkers3.length; i++) { 


marker3 = L.marker([obj.latitude, obj.longitude]).addTo(mymap); 


}, false);

Final Output:

google location

Here is the final output, where we can find a live map of London with real-time updates.

google map

Thanks for reading....

We have Highly-focused study material in our online academia and it's really helpful for our candidates and professionals, our professional online Data science, machine learning courses are a mixture of study materials, real-world scenario, use cases, and hands-on experience and we will be providing an industry project to our candidates or professionals, so that candidates can easily prepare himself for any specific role. We have recommended our experienced experts and real-time professionals for training and guidance out there, as well as they,  are sharing his industry experience with candidates. 

For more guidance please reach out to us, we can share a real-time experience.     

Share: -19  -16  -8 

Thoughts on “Pykafka Integration with Python Flask””

Leave a Reply

Your email address will not be published. Required fields are marked *


Recent Post

Top Trending Courses