This library provides a way of using
the Confluent Schema Registry in a way that is
compliant with the Java client. Since
Karapace is API compatible it could also be used with this
library. The release notes can be found
Consuming/decoding and producing/encoding is supported. It's also possible to provide the schema to use when decoding.
You can also include references when decoding. Without a schema provided, the latest schema with the same
It's supposed to be feature complete compared to the Java version. If anything is missing or not working as expected please create an issue or start a discussion on github discussions. An example of using this library async with protobuf to produce data to Kafka can be found in ksqlDB-GraphQL-poc. A blog with a bit of background on this library can be found titled confluent Schema Registry and Rust
schema_registry_converter.rs is available on crates.io. It is recommended to look there for the newest and more elaborate documentation. It has a couple of feature flags, be sure to set them correctly.
To use it to convert using avro async use:
For simplicity there are
easy variants that internally have an arc.
Making it easier to use at the price of some overhead. To use the
easy variants add the
easy feature and use the
structs that start with
Easy in the name to do the conversions.
...and see the docs for how to use it.
All the converters also have a blocking (non async) version, in that case use something like:
If you need to use both in a project you can use something like, but have to be weary you import the correct paths depending on your use.
For consuming messages encoded with the schema registry, you need to fetch the correct schema from the schema registry to transform it into a record. For clarity, error handling is omitted from the diagram.
For producing messages which can be properly consumed by other clients, the proper id needs to be encoded with the message. To get the correct id, it might be necessary to register a new schema. For clarity, error handling is omitted from the diagram.
Example with consumer and producer using Avro (blocking)
Examples which does both consuming/decoding and producing/encoding. To use structs with Avro they must have an implementation
of either the
serde::Serialize trait to work. The examples are especially useful to update
from the 1.x.x version, when starting you probably want to use the async versions.
Example using to post schema to schema registry
Relation to related libraries
The avro part of the conversion is handled by avro-rs. As such, I don't include tests for every possible schema. While I used rdkafka in combination to successfully consume from and produce to kafka, and while it's used in the example, this crate has no direct dependency on it. All this crate does is convert [u8] <-> Some Value (based on converter used). With Json and Protobuf some other dependencies are pulled in, by using said features. I have tried to encapsulate all the errors in the SRCError type. So even when you get a pannic/error that's an SRCError it could be an error from one of the dependencies. Please make sure you are using the library correctly, and the error is not caused by a depency, before creating an issue.
Due to mockito, used for mocking the schema registry responses, being run in a separate thread, tests have to be run
--test-threads=1 for example like
cargo +stable test --color=always --features avro,json,proto_decoder,proto_raw -- --nocapture --test-threads=1
The integration tests require a Kafka cluster running on the default ports. It will create topics, register schema's,
produce and consume some messages. They are only included when compiled with the
kafka_test feature, so to include
them in testing
cargo +stable test --all-features --color=always -- --nocapture --test-threads=1 needs to be run.
The 'prepare_integration_test.sh' script can be used to create the 3 topics needed for the tests. To ensure Java
compatibility it's also needed to run
the schema-registry-test-app docker image.
This project is licensed under either of
- Apache License, Version 2.0, (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or #404)
at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in Schema Registry Converter by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.