0
0
Kafkadevops~5 mins

Deserialization in Kafka - Commands & Configuration

Choose your learning style9 modes available
Introduction
Deserialization is the process of converting data received from Kafka into a format your application can understand. It solves the problem of reading raw bytes from Kafka messages and turning them into usable objects.
When your application reads messages from Kafka topics and needs to convert them into objects or data structures.
When you want to process JSON or Avro formatted messages from Kafka in your consumer application.
When you need to ensure the data format matches what your application expects to avoid errors.
When integrating Kafka with systems that require specific data formats like strings, JSON, or custom objects.
When debugging message consumption issues caused by incorrect data formats or serialization mismatches.
Config File - consumer.properties
consumer.properties
bootstrap.servers=localhost:9092
key.deserializer=org.apache.kafka.common.serialization.StringDeserializer
value.deserializer=org.apache.kafka.common.serialization.StringDeserializer
group.id=my-consumer-group
enable.auto.commit=true
auto.commit.interval.ms=1000

This configuration file sets up a Kafka consumer.

bootstrap.servers tells the consumer where the Kafka server is.

key.deserializer and value.deserializer specify how to convert the message key and value from bytes to strings.

group.id identifies the consumer group.

enable.auto.commit and auto.commit.interval.ms control automatic offset commits.

Commands
This command starts a Kafka consumer that reads messages from 'example-topic' from the beginning. It uses StringDeserializer to convert message keys and values from bytes to readable strings.
Terminal
kafka-console-consumer --bootstrap-server localhost:9092 --topic example-topic --from-beginning --property print.key=true --property key.deserializer=org.apache.kafka.common.serialization.StringDeserializer --property value.deserializer=org.apache.kafka.common.serialization.StringDeserializer
Expected OutputExpected
key1: Hello World key2: Kafka is fun key3: Deserialization example
--bootstrap-server - Specifies the Kafka server address
--topic - Specifies the Kafka topic to consume from
--property key.deserializer - Sets the deserializer for message keys
This command starts a Kafka producer to send messages to 'example-topic'. It helps test deserialization by sending known messages.
Terminal
kafka-console-producer --broker-list localhost:9092 --topic example-topic
Expected OutputExpected
No output (command runs silently)
--broker-list - Specifies the Kafka server address
--topic - Specifies the Kafka topic to send messages to
Key Concept

If you remember nothing else from this pattern, remember: deserialization converts raw Kafka message bytes into usable data formats your application can work with.

Common Mistakes
Using the wrong deserializer class for the message format.
This causes errors or unreadable data because the bytes are interpreted incorrectly.
Always match the deserializer to the message format, like StringDeserializer for strings or AvroDeserializer for Avro data.
Not specifying deserializer properties when running Kafka console consumer.
Kafka defaults to byte arrays, so output will be unreadable raw bytes.
Use --property key.deserializer and value.deserializer flags to specify the correct deserializers.
Summary
Configure your Kafka consumer with the correct deserializer classes to convert message bytes into usable data.
Use kafka-console-consumer with deserializer properties to read and verify messages in a readable format.
Test deserialization by sending known messages with kafka-console-producer and consuming them.