Repository navigation
M7, schemas part 4: JSON and Avro - #34
Merged
Merged
Conversation
The fourth of the schema pull requests (docs/7-api.md §8): Java V5's Schema.json(Class) and Schema.avro(Class), through serde and apache-avro, each a feature on by default. Tested on the simulated cluster and against a 5.0.1 broker with Java's schemas, Java's V5 evolution test included. Client crate: - Schema::json() and Schema::avro() for a record type that serde serializes and apache-avro describes (#[derive(AvroSchema)]). Both tell the broker an Avro schema, as Java's do: no name, the derived definition, and Java's two properties, with __alwaysAllowNull false, as an Option field is the nullable one. JSON values go through serde_json and read whichever version wrote them, as in Java. - Avro values are datums. A consumer of Avro, alone or as a key's or a value's, nested key/value schemas included, fetches the schema of each version as a generic consumer does, and reads each message with it: apache-avro resolves it into the consumer's schema, which fills in what a later version adds and takes the fields in the writer's order, which Java's reflection picks. A key/value schema hands each part its part of the writer's. - Definitions are parsed once each. Java's parser takes names that apache-avro refuses, such as the namespace ending in $ that older Java clients give a nested class's record: a definition's names become valid Avro names first. - Generic records of JSON and Avro: GenericValue::Json and GenericValue::Avro hold serde_json's and apache-avro's values, and GenericRecord gains Java V5's fields() and field(name), which gives an Avro union's branch, as Java's record holds it. - apache-avro 0.23 (Apache-2.0) and serde are optional dependencies, behind the json and avro features, both on by default. The library doesn't take apache-avro's derive; applications and the tests do. Integration tests against Pulsar 5.0.1: JSON and Avro records both ways, typed and as generic records; and Java's V5 evolution test across the two clients, under BACKWARD: Java writes the first version, the segment splits, Rust writes the second, both clients read every record with the second version, and a version that requires its new field is refused. SchemaTool gains json, avro, avro-v1 and avro-v2. Docs: docs/7-api.md §8, PLAN.md's M7.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The fourth of five pull requests for M7's schemas (see
PLAN.md): JSON and Avro, Java V5'sSchema.json(Class)andSchema.avro(Class), throughserdeand apache-avro, each behind a feature that is on by default. Protobuf follows.What's in it
Client crate
Schema::json()andSchema::avro(), for a record type thatserdeserializes and apache-avro describes(
#[derive(AvroSchema)]). Both tell the broker an Avro schema, as Java's do: no name, the derived definition, andJava's two properties. An
Optionfield is a nullable one, with a default of null, as Java's reflection makes them.serde_json. Like Java's, they read whichever version wrote them: a field the type doesn'thave is left, and an
Optionfield the message doesn't have isNone.schema/avro.rs). A consumer of Avro, alone or asa key's or a value's, fetches the schema of each version the way a generic consumer does, from PR 3's
consumer/writers.rs. apache-avro then resolves each message into the consumer's schema: it fills in what a laterversion adds, and takes the fields in the writer's order. That matters even without evolution, since Java's
reflection orders a record's fields its own way. A key/value schema hands each part its part of the writer's
schema.
schema/record.rs). Java's parser takes names that apache-avro refuses, suchas the namespace ending in
$that older Java clients give a nested class's record. A definition's names andnamespaces become valid Avro ones before it's parsed; fields keep theirs.
GenericValue::JsonandGenericValue::Avroholdserde_json's andapache-avro's values, read with the writer's schema.
GenericRecordgains Java V5'sfields(), in the writerschema's order, and
field(name), which gives an Avro union's branch without the union, as Java's record holds it.Dependencies
serde, optional, behind thejsonandavrofeatures. Both features takeapache-avro, as both describe their records in Avro, and both are on by default, like the codecs.
Integration tests
SchemaTool.javagainsjsonandavro, a record of a name and an age, andavro-v1andavro-v2, the twoversions of Java's evolution test.
Docs
docs/7-api.md§8: JSON and Avro.PLAN.md's M7.Choices worth a look
derive. Applications enable it themselves, since the derive's codenames
apache_avro; the tests take it as a dev-dependency. The crate re-exports neither apache-avro norserde_json, so an application depends on apache-avro 0.23 itself, for the derive and the trait to match.GenericValueholdsserde_json::Valueandapache_avro::types::Value. That puts both crates' types in thepublic API before the first release, rather than mapping every Avro type onto our own enum.
value()isapache-avro's record, unions included;
field(name)gives a union's branch, as Java's does.__alwaysAllowNullis"false". Java records how it derived a schema in two properties; here the nullablefields are the
Optionones. Only Java'sSchemaDefinitionreads the property back, not the broker or a decoder.differs from Rust's for the same record, so a consumer can't assume a message was written with its own schema.
Noneasnull,serde's default, where Java leaves null fields out. Each reads the other._.apache-avro's own validators are process-wide, and set once by the first schema derived or parsed, so a library
can't swap them reliably.
with an Avro part reads its parts' schemas from the writer's for each message. Both are small next to a send; the
benchmarks can say whether they matter.
Testing
version once;
and Rust writes the second; both clients read all four records with the second version; a version that requires
its new field is refused; and the topic keeps one schema history with two versions.
jsonoravroalone), rustdoc, the 541workspace tests, cargo-deny and the 54 integration tests pass. The bridge test that main ignores stays ignored.
covered.