Kafka Consumer
Avro Records
Schema Registry
Schema ID
Data Streaming

How to find the schema id from schema registry used for avro records, when reading from kafka consumer

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 Kafka, an open-source stream-processing software platform, is widely used for building real-time data pipelines and streaming apps. Schema Registry, often used with Kafka, manages Avro schemas and their versions ensuring data consistency and compatibility. This article will explore how to extract a schema ID from the Schema Registry when consuming Avro records from Kafka.

Understanding Avro and Schema Registry

Avro is a data serialization system that relies on schemas for structuring data. These schemas define the type and structure of the data stored in Avro files. Avro's integration with Kafka allows for serialized data to be efficiently and compactly transmitted over Kafka streams.

Confluent Schema Registry stores Avro Schemas and allows for schema evolution, maintaining backward, forward, or full compatibility. Each schema in the registry is associated with a unique ID, which helps applications understand how to deserialize messages.

Kafka Consumer and Avro Deserialization

When a Kafka Consumer reads a message serialized in Avro format, the message typically includes a schema ID, serialized data, and possibly other metadata. This schema ID is crucial for schema resolution, letting the consumer find the appropriate schema from the Registry to deserialize the data.

Extracting Schema ID in a Kafka Consumer

Configuration

Before consuming messages, ensure that your Kafka consumer is correctly configured to interact with the Schema Registry. Here's a typical configuration in Java:

java
1Properties props = new Properties();
2props.put("bootstrap.servers", "localhost:9092");
3props.put("group.id", "test-group");
4props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
5props.put("value.deserializer", "io.confluent.kafka.serializers.KafkaAvroDeserializer");
6props.put("schema.registry.url", "http://localhost:8081");

Consuming Messages

When consuming the Avro message, the Kafka Avro Deserializer automatically retrieves the schema using the schema ID encoded in the message. To extract this schema ID explicitly, you can modify the deserializer or access it indirectly via the deserialized data:

java
1KafkaConsumer<String, GenericRecord> consumer = new KafkaConsumer<>(props);
2consumer.subscribe(Collections.singletonList("your-topic"));
3
4while (true) {
5    ConsumerRecords<String, GenericRecord> records = consumer.poll(Duration.ofMillis(100));
6    for (ConsumerRecord<String, GenericRecord> record : records) {
7        GenericRecord value = record.value();
8
9        // Extracting schema details from the record
10        String schemaId = String.valueOf(value.getSchema().hashCode()); // Assuming schema is hashed to serve as ID
11        System.out.println("Schema ID: " + schemaId);
12
13        // Optionally, process the record
14        // ProcessRecord(value);
15    }
16}

Challenges and Solutions

Sometimes, directly accessing the schema ID as above might not be possible. In such cases, consider these alternatives:

  • Custom Deserializer: Create your own deserializer by extending the existing Kafka Avro Deserializer to expose the schema ID.
  • Schema Registry Client: Use the Schema Registry client to fetch all schemas and find your specific schema by comparing schema details.

Key Points Summary

Key ComponentRole/Functionality
Apache KafkaDistributes and manages real-time data streams.
AvroFacilitates data serialization with a schema-based approach.
Schema RegistryMaintains and versions schemas; assigns unique IDs to schemas.
Kafka ConsumerSubscribes to and processes messages from Kafka topics.
Schema IDA unique identifier for a schema, used to deserialize Avro records.

Conclusion

Extracting the schema ID when consuming Avro records from Kafka involves understanding the integration of Kafka with Avro and Schema Registry. By configuring your Kafka consumer correctly and using appropriate deserialization tactics, you can efficiently manage schema versions and ensure data integrity across your real-time data streams.

Further Reading:

  • Apache Kafka Documentation
  • Confluent Schema Registry Documentation
  • Avro Serialization and Deserialization Techniques

Course
Beginner
27 lessons
10 hours
System Design Fundamentals

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

View the course
System Design practice on Codemia

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

Practice system design