If you need to reduce output size (e.g., for Kafka or databases), use the AvroTurf::Messaging API. Instead of prepending the full schema to every message, it uses a schema ID. This requires a running Schema Registry service.
Note: You must explicitly require 'avro_turf/messaging' to use this API. The Messaging format is not compatible with the standard Avro data file API.
Key Methods:
encode: Automatically registers the schema if it's new. You can also specify a subject and version, or a schema_id directly.decode: Fetches the schema from the registry using the ID embedded in the message and caches it.decode_message: Returns a result object containing both the message and the schema_id used.
require 'avro_turf/messaging'
# Initialize with your registry URL
avro = AvroTurf::Messaging.new(registry_url: "http://my-registry:8081/")
# 1. Automatic registration
data = avro.encode({ "title" => "hello" }, schema_name: "greeting")
# 2. Encode using specific subject and version
data = avro.encode({ "title" => "hello" }, subject: 'greeting', version: 1)
# 3. Encode using a specific schema_id
data = avro.encode({ "title" => "hello" }, schema_id: 2)
# 4. Decode a message
decoded_data = avro.decode(data)
# 5. Decode and inspect metadata
result = avro.decode_message(data)
puts result.message # The decoded hash
puts result.schema_id # The ID used for encoding