Skip to content

Instantly share code, notes, and snippets.

@StevenJDH
Created October 22, 2025 19:47
Show Gist options
  • Save StevenJDH/90814aeb71973ac64184393300439bbf to your computer and use it in GitHub Desktop.
Save StevenJDH/90814aeb71973ac64184393300439bbf to your computer and use it in GitHub Desktop.
Example showing how a Kafka producer can send an Avro message using Confluent Wire Format without integrating with the Schema Registry.
/*
* This file is part of Wire Format Producer 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.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.Base64;
import java.util.HexFormat;
import org.apache.avro.io.DatumWriter;
import org.apache.avro.io.Encoder;
import org.apache.avro.io.EncoderFactory;
import org.apache.avro.specific.SpecificDatumWriter;
/**
* Basic requirements for pom.xml setup.
* Java JDK: >=17 or >=9 without HexFormat (Tested with 21)
* Dependencies: org.apache.avro:avro:1.12.1
* Plugins: org.apache.avro:avro-maven-plugin:1.12.1
*/
public class WireFormatAvroProducer {
public static void main(String[] args) throws IOException {
Greeting greeting = new Greeting("Hello World!");
byte magicByte = 0x00;
int schemaId = 1;
byte[] avroPayload;
try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
DatumWriter<Greeting> writer = new SpecificDatumWriter<>(greeting.getSchema());
Encoder encoder = EncoderFactory.get().binaryEncoder(out, null);
writer.write(greeting, encoder);
encoder.flush();
avroPayload = out.toByteArray(); // Serialize it to a byte array.
}
// Prepend the wire format.
ByteBuffer buffer = ByteBuffer.allocate(1 + 4 + avroPayload.length);
buffer.put(magicByte);
buffer.putInt(schemaId);
buffer.put(avroPayload);
// Ready for sending as a native-based Kafka client.
byte[] message = buffer.array();
// Ready for sending as a REST-based Kafka client.
String base64Message= Base64.getEncoder().encodeToString(message);
// Just to show the hex representation.
String hexMessage = HexFormat.ofDelimiter(" ").formatHex(message);
System.out.println("Avro Payload Length: " + avroPayload.length);
System.out.println("Message Length: " + message.length);
System.out.println("Message (base64): " + base64Message);
System.out.println("Message (hex): " + hexMessage);
}
}

Comments are disabled for this gist.