Python
Kafka
Avro
Data Deserialization
Coding Tutorials

How to decode/deserialize Avro with Python from Kafka

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Apache Avro is a data serialization system that integrates seamlessly with Apache Kafka to facilitate efficient data exchange in distributed systems. Python, being a versatile programming language, offers libraries to handle both Kafka and Avro. Deserializing Avro data from Kafka in Python involves extracting Kafka messages and converting the binary Avro data into a readable format. This article will guide you through the processes involved, from setting up your environment to code implementation.

Prerequisites

To follow along, ensure you have:

  • Apache Kafka and Zookeeper running
  • Avro schema used for encoding the data
  • Python environment setup
  • Required Python libraries: confluent_kafka and fastavro

You can install the necessary Python packages using pip:

bash
pip install confluent-kafka fastavro

Setup Kafka Producer

Before you can decode Avro data, you need a Kafka producer to publish Avro-encoded messages. Here is a simple Kafka producer in Python that uses Avro serialization.

  1. Define Avro schema:
json
1{
2  "type": "record",
3  "name": "User",
4  "fields": [
5    {"name": "name", "type": "string"},
6    {"name": "age", "type": "int"}
7  ]
8}
  1. Kafka producer script:
python
1from confluent_kafka import Producer
2import json
3from fastavro import writer, parse_schema
4
5# Define the Avro schema
6schema = {
7    "type": "record",
8    "name": "User",
9    "fields": [
10        {"name": "name", "type": "string"},
11        {"name": "age", "type": "int"}
12    ]
13}
14
15parsed_schema = parse_schema(schema)
16
17# Function to encode with Avro
18def encode_user(user):
19    bytes_writer = io.BytesIO()
20    writer(bytes_writer, parsed_schema, [user])
21    return bytes_writer.getvalue()
22
23# Kafka configuration
24conf = {'bootstrap.servers': "localhost:9092"}
25producer = Producer(**conf)
26
27# Send data
28user = {"name": "John", "age": 25}
29producer.produce(topic='users', value=encode_user(user))
30producer.flush()

Setup Kafka Consumer to Decode Avro

Now, set up a Kafka consumer in Python that will fetch the Avro-encoded messages and decode them:

python
1from confluent_kafka import Consumer, KafkaError
2import fastavro
3import io
4
5# Kafka Consumer configuration
6conf = {
7    'bootstrap.servers': "localhost:9092",
8    'group.id': "group1",
9    'auto.offset.reset': "earliest"
10}
11consumer = Consumer(**conf)
12consumer.subscribe(['users'])
13
14# Function to decode Avro message
15def decode_message(message):
16    message_bytes = io.BytesIO(message)
17    message_bytes.seek(0)
18    return fastavro.schemaless_reader(message_bytes, parsed_schema)
19
20# Poll messages
21while True:
22    msg = consumer.poll(1.0)
23    
24    if msg is None:
25        continue
26    if msg.error():
27        if msg.error().code() == KafkaError._PARTITION_EOF:
28            continue
29        else:
30            print(msg.error())
31            break
32    
33    user = decode_message(msg.value())
34    print("Decoded Avro record: {}".format(user))
35
36consumer.close()

Explanation

In this example:

  • Kafka Producer creates and sends messages encoded in the Avro format. Each message is a serialized form of the User record.
  • Kafka Consumer reads the messages, deserializing the Avro-encoded User data using fastavro, which is known for its performance and ease of use.

Table Summary: Steps & Components

Step/ComponentFunction/Role
Avro SchemaSchema definition for data serialization.
fastavro LibraryPython library for fast Avro serialization and deserialization.
confluent-kafkaKafka client library that provides producer and consumer classes.
ProducerPublishes messages to Kafka topic.
ConsumerConsumes messages from Kafka topic.
SchemaUsed for data validation and serialization guidelines.

Additional Tips

  • Schema Management: For larger projects, consider using a schema registry to manage version control and maintain compatibility.
  • Security: Implement security best practices like SSL/TLS context settings for Kafka clients to protect data in transit.
  • Performance: Monitor the performance implications of serialization and deserialization on system throughput and latency.

By implementing the above setup, you will able to effectively utilize Kafka and Avro in Python applications, helping in building scalable and efficient real-time data processing pipelines.


Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.