Created
October 22, 2025 19:50
-
-
Save StevenJDH/7ed7b4b3690a40035591f228c1d0eb6b to your computer and use it in GitHub Desktop.
Example showing how a Kafka consumer can read an Avro message using Confluent Wire Format without integrating with the Schema Registry.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| * This file is part of Wire Format Consumer Gist <https://github.com/StevenJDH>. | |
| * Copyright (c) 2025 Steven Jenkins De Haro | |
| * | |
| * This source code is licensed under the MIT license found in the | |
| * LICENSE file in the root directory of this source tree or at | |
| * https://opensource.org/licenses/MIT. | |
| */ | |
| package io.github.stevenjdh.examples.avro; | |
| import com.example.messages.Greeting; | |
| import java.io.IOException; | |
| import java.nio.ByteBuffer; | |
| import java.util.Base64; | |
| import org.apache.avro.io.DatumReader; | |
| import org.apache.avro.io.Decoder; | |
| import org.apache.avro.io.DecoderFactory; | |
| import org.apache.avro.specific.SpecificDatumReader; | |
| /** | |
| * Basic requirements for pom.xml setup. | |
| * Java JDK: >=9 (Tested with 21) | |
| * Dependencies: org.apache.avro:avro:1.12.1 | |
| * Plugins: org.apache.avro:avro-maven-plugin:1.12.1 | |
| */ | |
| public class WireFormatAvroConsumer { | |
| public static void main(String[] args) throws IOException { | |
| /** | |
| * Alternative (Raw Bytes): | |
| * byte[] message = { 0x00, 0x00, 0x00, 0x00, 0x01, 0x18, 0x48, 0x65, 0x6c, | |
| * 0x6c, 0x6f, 0x20, 0x57, 0x6f, 0x72, 0x6c, 0x64, 0x21 }; | |
| */ | |
| byte[] message = Base64.getDecoder().decode("AAAAAAEYSGVsbG8gV29ybGQh"); | |
| ByteBuffer buffer = ByteBuffer.wrap(message); | |
| byte magicByte = buffer.get(); // Position goes from 0 -> 1. | |
| int schemaId = buffer.getInt(); // Reads bytes [1..4], position goes from 1 -> 5. | |
| int remainingBytes = buffer.remaining(); | |
| byte[] avroPayload = new byte[remainingBytes]; | |
| buffer.get(avroPayload); // Writes remaining bytes to byte array. | |
| // Requires class to be generated from avro schema using avro-maven-plugin. | |
| DatumReader<Greeting> reader = new SpecificDatumReader<>(Greeting.class); | |
| Decoder decoder = DecoderFactory.get().binaryDecoder(avroPayload, null); | |
| Greeting greeting = reader.read(null, decoder); | |
| System.out.println("Magic: " + magicByte); | |
| System.out.println("Schema ID: " + schemaId); | |
| System.out.println("Payload [Message Field]: " + greeting.getMessage()); | |
| } | |
| } |
Comments are disabled for this gist.