Skip to content

Add Avro single-object encoding for writing (#172) - #917

Open
fudianchn wants to merge 1 commit into
FasterXML:3.xfrom
fudianchn:write-avro-single-object-encoding
Open

fudianchn wants to merge 1 commit into
FasterXML:3.xfrom
fudianchn:write-avro-single-object-encoding

Conversation

@fudianchn

Copy link
Copy Markdown

AI disclosure: this change was prepared with AI coding agents, reviewed and revised line by line by me.

What

Enable single-object encoding on a writer.

mapper.writer(schema)
    .with(AvroWriteFeature.WRITE_SINGLE_OBJECT_ENCODING)
    .writeValueAsBytes(value);

Each root value gets its own header, even in a sequence. The default is raw Avro.

Why

Callers can send Jackson output directly to Apache Avro's BinaryMessageDecoder. The corresponding reader landed in #905.

How

The generator caches a 10-byte header using the marker bytes and the little-endian fingerprint of the original writer schema from @Vineet829's #905. The same encoder writes the header and datum. Writing a value with AVRO_FILE_OUTPUT also enabled raises an exception.

Explicit flush drains buffered messages even after the option is switched off. On close, the generator drains pending bytes and closes any stream it owns, even if the final write fails.

Root cause

Feature request. The writer previously supported raw Avro and object container files.

Testing

All 25 focused tests passed.

./mvnw -pl avro -Dtest=SingleObjectWrite172Test,SingleObjectEncoding343Test,FileFormatTest test

The default writer still writes 32 integers (88 bytes) in one underlying write on close. The six paired JMH runs were too noisy to establish equivalent throughput.

Verification of the original issue

On the base branch, Apache Avro rejected the record because its header was missing.

With the option enabled, Apache Avro 1.12.2 reads back ids = [3, 5]. Both encoders produce identical bytes. Two null values produce 20 bytes. An empty sequence produces zero.

Fixes #172

Signed-off-by: 付典 <fudianchn@gmail.com>
@cowtowncoder cowtowncoder added avro cla-received Marker to denote that there is a CLA for pr 3.4 labels Dec 3, 2026
@github-actions

github-actions Bot commented Dec 3, 2026

Copy link
Copy Markdown

🧪 Code Coverage Report

Coverage Type Coverage Change
📝 Instructions 80.98% 📈 +0.14%
🔀 Branches 75.55% 📈 +0.15%

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

3.4 avro cla-received Marker to denote that there is a CLA for pr

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support for Avro 'single-object encoding' headers in serialize/deserialize

2 participants