Schema registry
xkafka can write records with schemas kept in a Confluent Schema Registry, and read them back, on the JVM, Scala.js and Scala Native. It supports Avro, JSON Schema and Protobuf. Records are written in Confluent's wire format, so Java applications using Confluent's serializers and deserializers read what xkafka writes, and xkafka reads what they write.
Dependencies
// Avro serdes, the registry client they use, and http4s's Ember client to reach the registry
libraryDependencies ++= Seq(
"io.github.pmbmegalom" %%% "xkafka-avro" % "0.7.1",
"io.github.pmbmegalom" %%% "xkafka-schema-registry-http4s" % "0.7.1",
"org.http4s" %%% "http4s-ember-client" % "0.23.38"
)
xkafka-avrogives you Avro serdes for your types,xkafka-json-schemaJSON Schema serdes, andxkafka-protobufProtobuf serdes. Add the ones you use. Each brings inxkafka-schema-registry, the registry client.- If your types come from vulcan, avro4s, avrohugger, ScalaPB or smithy4s, see Using types from other libraries for the module to add. To generate classes from Avro or JSON Schema files, see Generating classes from schema files.
xkafka-schema-registry-http4slets the registry client use any http4s client. Ember is shown here, but any http4s client works. To use an HTTP client that is not http4s's, see Another HTTP client.
On Scala Native, Ember needs the s2n-tls
library, even when the registry is reached over plain HTTP. Install it wherever
the program is built and run. On macOS, Homebrew's s2n package provides it.
Ubuntu does not package it, so build it from source there.
Connecting to the registry
The registry is reached through your own HTTP client, which decides on TLS, authentication, proxies and timeouts:
// Connect to the registry at localhost:8081 through an Ember client
import cats.data.NonEmptyList
import cats.effect.{IO, Resource}
import org.http4s.ember.client.EmberClientBuilder
import xkafka.schemaregistry.{RegistrySettings, SchemaRegistry}
import xkafka.schemaregistry.http4s.Http4sTransport
val registry: Resource[IO, SchemaRegistry[IO]] =
for
client <- EmberClientBuilder.default[IO].build
settings <- Resource.eval(RegistrySettings.from(NonEmptyList.one("http://localhost:8081")).liftTo[IO])
registry <- Resource.eval(SchemaRegistry.of(settings, Http4sTransport(client)))
yield registry
To authenticate, wrap the client so it adds the credentials to every request,
and pass the wrapped client to Http4sTransport. A registry URL holding
credentials, such as https://user:password@registry, is refused, since no
client would send them from there:
// Send basic authentication with every request to the registry
import cats.effect.IO
import org.http4s.BasicCredentials
import org.http4s.client.Client
import org.http4s.headers.Authorization
def authenticated(client: Client[IO], user: String, password: String): Client[IO] =
Client(request => client.run(request.putHeaders(Authorization(BasicCredentials(user, password)))))
RegistrySettings.from takes:
- one or more registry URLs, each
httporhttps. A request that cannot reach one is sent to the next. latestCachedFor: how long a subject's latest version is cached. By default it is cached until the program ends, as Confluent's serializers cache it. UsewithLatestCachedFor(duration)to see a new latest version withinduration, andwithoutLatestExpiryto go back to the default.
A URL that is not http or https, or a negative duration, is refused;
liftTo raises the refusal as an InvalidRegistrySetting.
Schemas, and the ids they are registered under, never change, so they are cached for good: each schema is fetched once, however many records use it.
Another HTTP client
The registry client only needs a function that sends one request and gives back the answer:
type Transport[F[_]] = RegistryRequest => F[RegistryResponse]
A RegistryRequest has a method, an absolute URL, headers and an optional JSON
body; a RegistryResponse has the status code and the body. Fail the effect
only when the registry cannot be reached: an error status is a response like any
other. Pass your function to SchemaRegistry.of in place of Http4sTransport.
Writing and reading records
Each format has a codec, which you derive for your type, and an object that makes serdes in three ways:
ofwrites and reads your own type, with the schema its codec derives.genericwrites and reads values of a schema your program only learns as it runs.asWrittenreads any record with the schema it was written with, for tools that show or copy records.
| Avro | JSON Schema | Protobuf | |
|---|---|---|---|
| Module | xkafka-avro |
xkafka-json-schema |
xkafka-protobuf |
| Package | xkafka.avro |
xkafka.jsonschema |
xkafka.protobuf |
| Codec | AvroCodec |
JsonSchemaCodec |
ProtobufCodec |
| Serdes | AvroSerde |
JsonSchemaSerde |
ProtobufSerde |
| A record's fields are found by | name, resolved against your schema | name | number |
| Values of a schema known at run time | AvroValue |
circe's Json |
ProtobufValue |
Here an Avro serde is given to a producer; JsonSchemaSerde.of and
ProtobufSerde.of are used the same way:
// Write orders as Avro values, registering their schema under "orders-value" on the first write
import java.time.Instant
import cats.effect.{IO, Resource}
import xkafka.*
import xkafka.avro.{AvroCodec, AvroSerde}
import xkafka.schemaregistry.{Part, SchemaRegistry}
final case class Line(sku: String, quantity: Int = 1) derives AvroCodec
final case class Order(id: String, placedAt: Instant, lines: List[Line], note: Option[String] = None) derives AvroCodec
def orders(registry: SchemaRegistry[IO], client: ClientSettings): Resource[IO, KafkaProducer[IO, String, Order]] =
for
serde <- Resource.eval(AvroSerde.of[IO, Order](registry, Part.Value))
settings <- Resource.eval(ProducerSettings.from(client, Serializer.utf8[IO], serde.serializer).liftTo[IO])
producer <- KafkaClient[IO].producer(settings)
yield producer
The serde is a Serde: a producer takes its serializer, a consumer its
deserializer, and a streams program the serde itself. Use Part.Key for a
serde of keys. of and generic also take a SubjectNaming, which names the
subject a schema is kept under (see Subjects), and a Writing,
which chooses the schema values are written with (see
Which schema is written).
Each record carries the id of the schema it was written with. Reading fetches that schema, so records written with an older or newer version of your type are read. What changes a reader accepts depends on the format; each format's section says.
A record without a value, such as a tombstone, fails to read with
Deserializer.NullValueException. Read with serde.option, or
serde.deserializer.option, to get None for it; writing None through
serde.option writes a record without a value.
Avro
Derived Avro schemas
derives AvroCodec makes a schema from the type:
| Scala | Avro |
|---|---|
Boolean, Int, Long, Float, Double, String |
boolean, int, long, float, double, string |
fs2.Chunk[Byte], Array[Byte] |
bytes |
Option[A] |
a union of null and A, null first; an option of an option is refused, since both nones would be null |
List, Vector, Seq, Set |
array |
Map[String, A] |
map |
java.util.UUID |
string with logical type uuid |
java.time.Instant |
long with logical type timestamp-millis |
java.time.LocalDate |
int with logical type date |
java.time.LocalTime |
long with logical type time-micros |
java.time.LocalDateTime |
long with logical type local-timestamp-micros |
| a case class | a record of its fields |
a case class marked @AvroFixed(size), of one fs2.Chunk[Byte] or Array[Byte] field |
a fixed of size bytes |
| an enum or sealed trait whose cases are all objects | an enum of the cases' names |
| any other sealed trait or enum | a union of its cases' records, an object's having no fields |
- A record is named after its class, in the namespace of its package and the
objects it is in, so
Here.LineandThere.Lineare two records. A generic class's name carries its type arguments in full,Box[List[Int]]beingBox__List__Int, where the class is derived for those arguments. - Two types given one name, for example with
@SchemaName, make no schema, so making a serde of them fails withRegistryException.NoSchema. So do two cases of a sealed trait given one name, which would be one union branch twice, and a type in no namespace that the schema names again inside a namespace holding a type of the same name, where Avro reads the name as that type's. - A field's Scala default is its default in the schema. An optional field whose
default is a value has that value's branch first in its union, since Avro takes
a union's default from its first branch. A default the schema cannot place is
left out: one of the record its field is in, or one holding a value of a
union's later branch inside it, such as
Inner(Some(5))for a field of a record with an optional field. A schema registered by another application may give a union field a default of any branch, as Avro 1.12 allows. - A key or value that is bytes on its own is written after the header as it is, with no length before it, as Confluent's serializers write it, here and in the vulcan and avro4s serdes.
- For a recursive type, such as a linked list's node, put
derives AvroCodecon the type itself. - Writing a
fixedvalue of the wrong size fails. Avro gives afixedno documentation, so@SchemaDocon one fails to compile. - Mark an enum, or a sealed trait whose cases are all objects, with
@AvroEnumDefault("NAME")to make the caseNAMEits default: a record written with a symbol your enum does not have reads as that case, so other applications can add symbols. A name that is not one of its cases, or a type whose cases are not all objects, fails to compile.
AvroCodec.instantMicros, AvroCodec.instantNanos,
AvroCodec.localTimeMillis and AvroCodec.localDateTimeMillis write those
types with the other precisions Avro has. Make one the given codec to use it:
// Write instants to the microsecond
import java.time.Instant
import xkafka.avro.AvroCodec
given AvroCodec[Instant] = AvroCodec.instantMicros
final case class Reading(sensor: String, at: Instant) derives AvroCodec
Decimal numbers have no codec by default, since their precision and scale are part of the schema. Give one:
// Write prices with at most 10 digits, 2 of them after the point
import xkafka.avro.AvroCodec
given AvroCodec[BigDecimal] = AvroCodec.decimal(precision = 10, scale = 2)
final case class Price(sku: String, amount: BigDecimal) derives AvroCodec
If fields need different precisions, mark each with @SchemaDecimal. It
covers every BigDecimal in the field, including inside an Option or a list,
and overrides any given AvroCodec[BigDecimal]:
// Write prices to the cent and weights to the gram
import xkafka.avro.AvroCodec
import xkafka.schemaregistry.SchemaDecimal
final case class Parcel(
@SchemaDecimal(precision = 10, scale = 2) price: BigDecimal,
@SchemaDecimal(precision = 8, scale = 3) weights: List[BigDecimal]
) derives AvroCodec
A number with more digits, or more digits after the point, fails to write; it is not rounded.
For a field of a union type, such as Cash | Card, give the union its codec
with AvroCodec.union, listing its types in the order of the schema's union:
// Pay in cash or by card
import xkafka.avro.AvroCodec
final case class Cash(cents: Long) derives AvroCodec
final case class Card(number: String) derives AvroCodec
given AvroCodec[Cash | Card] = AvroCodec.union[(Cash, Card)]
final case class Payment(by: Cash | Card, refundedBy: Option[Cash | Card] = None) derives AvroCodec
A value is written as the first type in the union that it matches, and a
record is read by its name, so two records with the same fields stay apart. An Option of
a union type is the union with null first.
On Scala.js, numbers do not carry their Scala type: a Double that is a whole
number is also an Int, and one that a Float holds exactly is also a Float.
A union of number types, such as Int | Double, writes such a value as the
first of those types it lists, and reads it back as the same number.
For a type of your own, map it to one that has a codec:
// Write a customer id as the text it wraps
import xkafka.avro.AvroCodec
final case class CustomerId(value: String)
given AvroCodec[CustomerId] = AvroCodec[String].imap(CustomerId(_))(_.value)
emap does the same if reading can refuse a value.
If a type must have the schema of a file you keep, for example one other
applications also write with, make its codec with matching. If the derived
schema differs from the file's, making a serde fails with
RegistryException.NoSchema, and the error shows both schemas:
// A visit, whose schema must be the one in visit.avsc
import xkafka.avro.AvroCodec
import xkafka.schemaregistry.{SchemaDoc, SchemaNamespace}
val visitAvsc =
"""{"type": "record", "name": "Visit", "namespace": "com.example", "doc": "A visit to the shop",
| "fields": [{"name": "id", "type": "string"}, {"name": "pages", "type": "int", "default": 1}]}""".stripMargin
@SchemaNamespace("com.example")
@SchemaDoc("A visit to the shop")
final case class Visit(id: String, pages: Int = 1)
given AvroCodec[Visit] = AvroCodec.derived[Visit].matching(visitAvsc)
Avro records of other versions
Reading converts each record from the schema it was written with to your codec's schema, as Avro's schema resolution does:
- a field the record lacks takes its default;
- a field the codec lacks is skipped;
- numbers are widened, as an
intread as along.
A record that cannot be resolved this way fails with
RegistryException.Unserializable.
Avro values known only at run time
If a program does not know the schema until it runs, such as a tool that
copies or shows any topic's records, it works with AvroValues, which describe
any Avro value, and AvroSchemas.
AvroSerde.asWritten reads each record as it was written, together with the
schema its writer used:
// Read any record of any schema, and show its schema and value
import cats.effect.IO
import xkafka.{Deserializer, Headers, Topic}
import xkafka.avro.{AvroSchemaJson, AvroSerde, AvroWritten}
import xkafka.schemaregistry.SchemaRegistry
def show(registry: SchemaRegistry[IO], topic: Topic, bytes: fs2.Chunk[Byte]): IO[String] =
AvroSerde.asWritten[IO](registry).flatMap: (reading: Deserializer[IO, AvroWritten]) =>
reading.deserialize(topic, Headers.empty, Some(bytes)).map: written =>
s"${AvroSchemaJson.render(written.schema)}: ${written.value}"
AvroSerde.generic writes values of a schema you hold, and reads any record
converted to that schema, as a derived serde does. AvroSerde.genericText
does the same for a schema you hold as text, and registers that text as it is.
Use it for a schema another client registered, such as one Kafka Connect
registered with properties of its own, which xkafka's schemas do not keep:
// Write a line of an order, with a schema read from its JSON as the program runs
import cats.effect.IO
import xkafka.Serde
import xkafka.avro.{AvroSchemaJson, AvroSerde, AvroValue, FullName}
import xkafka.schemaregistry.{Part, SchemaRegistry}
val text = """{"type":"record","name":"Line","namespace":"com.example","fields":[{"name":"sku","type":"string"},{"name":"quantity","type":"int"}]}"""
def lines(registry: SchemaRegistry[IO]): IO[Serde[IO, AvroValue]] =
IO.fromEither(AvroSchemaJson.parse(text).left.map(new IllegalArgumentException(_))).flatMap: schema =>
AvroSerde.generic[IO](registry, schema, Part.Value)
def linesAsWritten(registry: SchemaRegistry[IO]): IO[Serde[IO, AvroValue]] = AvroSerde.genericText[IO](registry, text, Part.Value)
val line = AvroValue.Record(FullName(Some("com.example"), "Line"), Vector("sku" -> AvroValue.String("apple"), "quantity" -> AvroValue.Int(3)))
A value that does not fit the schema fails to write with
RegistryException.Unserializable. A value of a logical type is its underlying
value: a date is the AvroValue.Int of its days since 1970-01-01.
JSON Schema
Derive a codec for your type, and make a serde from it:
// Write orders as JSON, registering their JSON Schema under "orders-value" on the first write
import java.time.Instant
import cats.effect.IO
import xkafka.Serde
import xkafka.jsonschema.{JsonSchemaCodec, JsonSchemaSerde}
import xkafka.schemaregistry.{Part, SchemaRegistry}
final case class Line(sku: String, quantity: Int = 1) derives JsonSchemaCodec
final case class Order(id: String, placedAt: Instant, lines: List[Line], note: Option[String] = None) derives JsonSchemaCodec
def orders(registry: SchemaRegistry[IO]): IO[Serde[IO, Order]] = JsonSchemaSerde.of[IO, Order](registry, Part.Value)
Records match what Confluent's KafkaJsonSchemaSerializer writes.
Derived JSON schemas
derives JsonSchemaCodec makes a JSON Schema of draft 7 from the type:
| Scala | JSON Schema | Written as |
|---|---|---|
Boolean, String |
boolean, string |
as they are |
Int, Long |
integer |
numbers |
Float, Double, BigDecimal |
number |
numbers; infinities and NaN are refused; @SchemaDecimal changes nothing |
fs2.Chunk[Byte] |
string with contentEncoding base64 |
base64 text |
Option[A] |
oneOf null and A |
null for None; an option of an option is refused, since both nones would be null |
List, Vector, Seq, Set |
array |
arrays |
Map[String, A] |
object whose properties are all A |
objects |
java.util.UUID |
string with format uuid |
text |
java.time.Instant |
string with format date-time |
ISO-8601 text, such as 2026-11-10T07:00:00Z |
java.time.LocalDate |
string with format date |
ISO-8601 text, such as 2026-11-10 |
java.time.LocalTime, LocalDateTime |
string |
ISO-8601 text, such as 07:00, with seconds where they are not zero |
| a case class | object of its fields, closed to others |
objects |
| an enum or sealed trait whose cases are all objects | string with enum of the cases' names |
the case's name |
| any other sealed trait or enum | oneOf its cases' objects |
the case's object, with its name in the property type |
- An object's
titleis the type's full name, in the namespace of its package and the objects it is in. WithSubjectNaming.RecordName, the subject is that title. - A field with a default, or an optional one, may be left out of a record; the others are required.
- Objects are closed: their schema allows no other properties. Under the registry's default compatibility rules, that lets you add a field with a default, or an optional one, as a new version of the schema, and refuses to let you remove a field.
- A case of a sealed trait must not have a field named
type: writing it fails. - These make no schema, so making a serde of them fails with
RegistryException.NoSchema: two cases of a sealed trait or enum with one name, such asV1.CreatedandV2.Created, whichtypecould not tell apart; two types of one name that hold themselves, as a generic class derived once for every type argument gives; and an option holding an option, or a schema written by hand that may takenull, whichnullwould be a value of twice. A schema may takenullunless a keyword rules it out, such as atypeorenumwithoutnull, sincenullmust satisfy every keyword, so{"minimum": 0}takes it. Neither can a schema written by hand that names its own definitions by reference, or that is written for a draft other than draft 7, be held in a derived type, in an option, a collection or a map: the schema holding it is for draft 7, and its references would name that schema's definitions.
For a schema written by hand, give its JSON with circe's codecs for its values, which must write what the schema describes:
// A temperature written as a bare number, with a schema written by hand
import io.circe.{Decoder, Encoder, Json}
import xkafka.jsonschema.JsonSchemaCodec
final case class Celsius(degrees: Double)
given JsonSchemaCodec[Celsius] =
JsonSchemaCodec.from(Json.obj("type" -> Json.fromString("number")), Encoder[Double].contramap(_.degrees), Decoder[Double].map(Celsius(_)))
For a type of your own, map it to one that has a codec with imap or emap,
as for Avro.
JSON records of other versions
Reading takes a record's JSON as it is, whatever version of the schema it was written with:
- a field the record lacks takes its default, or
Noneif it is optional; - a field is also read under any name
@SchemaAliasgives it; - properties your type does not have are ignored.
xkafka does not validate values against the schema when writing or reading. Confluent's serializers do not validate by default either.
JSON values known only at run time
JsonSchemaSerde.generic writes circe Json values of a schema you hold, as
they are, and reads each record's JSON as it is:
// Write a line of an order as JSON, with a schema read from its text as the program runs
import cats.effect.IO
import io.circe.{jawn, Json}
import xkafka.Serde
import xkafka.jsonschema.JsonSchemaSerde
import xkafka.schemaregistry.{Part, SchemaRegistry}
val text = """{"type":"object","title":"com.example.Line","properties":{"sku":{"type":"string"},"quantity":{"type":"integer"}}}"""
def lines(registry: SchemaRegistry[IO]): IO[Serde[IO, Json]] =
IO.fromEither(jawn.parse(text)).flatMap(schema => JsonSchemaSerde.generic[IO](registry, schema, Part.Value))
val line = Json.obj("sku" -> Json.fromString("apple"), "quantity" -> Json.fromInt(3))
With SubjectNaming.RecordName, the subject is the schema's title. JsonSchemaSerde.asWritten reads each record as a JsonSchemaWritten:
its JSON, with the schema it was written with.
Protobuf
Derive a codec for your type, numbering every field with @ProtobufField, and
make a serde from it:
// Write orders as Protobuf messages, registering their .proto file under "orders-value" on the first write
import java.time.Instant
import cats.effect.IO
import xkafka.Serde
import xkafka.protobuf.{ProtobufCodec, ProtobufField, ProtobufSerde}
import xkafka.schemaregistry.{Part, SchemaNamespace, SchemaRegistry}
final case class Line(@ProtobufField(1) sku: String, @ProtobufField(2) quantity: Int = 1) derives ProtobufCodec
@SchemaNamespace("com.example")
final case class Order(
@ProtobufField(1) id: String,
@ProtobufField(2) placedAt: Instant,
@ProtobufField(3) lines: List[Line],
@ProtobufField(4) note: Option[String] = None
) derives ProtobufCodec
def orders(registry: SchemaRegistry[IO]): IO[Serde[IO, Order]] = ProtobufSerde.of[IO, Order](registry, Part.Value)
The schema registered for Order is this .proto file, which
ProtobufCodec[Order].schema also gives:
syntax = "proto3";
package com.example;
import "google/protobuf/timestamp.proto";
message Order {
string id = 1;
.google.protobuf.Timestamp placedAt = 2;
repeated Line lines = 3;
optional string note = 4;
}
message Line {
string sku = 1;
optional int32 quantity = 2;
}
Records are written as Confluent's KafkaProtobufSerializer writes them. A
Protobuf record is a message, so the type you write must be a case class or a
sealed trait; for any other type, ProtobufSerde.of fails with
RegistryException.NoSchema.
Field numbers
Records carry each field's number, not its name, so the numbers are what must stay the same as your type changes:
- every field needs a number. A field without one, two fields sharing one, or a number Protobuf does not allow (outside 1 to 536870911, or from 19000 to 19999) fails to compile;
- renaming a field, or moving it, changes nothing a reader sees;
- once a field is removed, do not give its number to another field.
Derived Protobuf schemas
derives ProtobufCodec declares, in proto3:
| Scala | Protobuf |
|---|---|
Boolean, Int, Long, Float, Double, String |
bool, int32, int64, float, double, string |
fs2.Chunk[Byte] |
bytes |
Option[A] |
an optional field (a message field needs no label, since a message already records whether it is set) |
List, Vector, Seq, Set |
a repeated field, packed if it holds numbers |
Map[String, A] |
map<string, A> |
java.util.UUID, BigDecimal |
string, holding the value as text; @SchemaDecimal changes nothing |
java.time.Instant |
google.protobuf.Timestamp |
java.time.LocalDate, LocalTime, LocalDateTime |
string, holding ISO-8601 text, such as 2026-11-10 |
| a case class | a message of its fields |
| an enum or sealed trait whose cases are all objects | an enum, whose values carry its name and theirs, as STATUS_OPEN |
| any other sealed trait or enum | a message whose one field, of a oneof, is the case's message |
- Every message and enum is declared in the one file, in the package of the message you write, by its simple name. Two types with the same name in different namespaces cannot share the file.
- Number each case of a sealed trait or enum with
@ProtobufFieldon the case, as on a field. An enum needs a case numbered 0, or it fails to compile: a field of it that a record lacks reads as that case. - A field with a Scala default is labelled
optionaland written even if it is zero, so a record that lacks it reads the default. Other fields are written as proto3 writes them: left out if they are zero (0,"",false, an empty list, an enum's case numbered 0), and read as zero if a record lacks them. Lists, maps and options do not use a default: a record that lacks one reads it as empty orNone. - Protobuf has no field for a list of lists, a list or map of options, or an
option of a list.
ProtobufCodec[A].schemasays which field ofAholds one, andProtobufSerde.offails withRegistryException.NoSchema.
ProtobufCodec.sint32 and ProtobufCodec.sint64 write numbers near zero,
negative or not, in fewer bytes than int32 and int64, which take ten bytes for
any negative number. fixed32, fixed64, sfixed32 and sfixed64 write every
number in four or eight bytes. Make one the given codec to use it:
// Write a change of stock, often negative, as a sint32
import xkafka.protobuf.{ProtobufCodec, ProtobufField}
given ProtobufCodec[Int] = ProtobufCodec.sint32
final case class StockChange(@ProtobufField(1) sku: String, @ProtobufField(2) change: Int) derives ProtobufCodec
For a type of your own, map it to one that has a codec with imap or emap,
as for Avro.
Protobuf records of other versions
Reading takes a record's fields by number, whatever version of the schema it was written with:
- a field the record lacks takes its default, or its zero if it has none;
- fields your type does not have are skipped;
- a record whose enum field holds a number your enum lacks fails to read with
RegistryException.Unserializable.
Under the registry's default compatibility rules, adding a field is a compatible change, and changing a field's type is not.
Protobuf values known only at run time
If a program does not know the schema until it runs, such as a tool that
copies or shows any topic's records, it works with ProtobufValues, which
describe any Protobuf value, and ProtobufSchema.Files, which describe .proto
files.
ProtobufSerde.asWritten reads each record as it was written, together with
the .proto file its writer registered. If that file imports files registered
under other subjects, they are fetched from the registry. The files Confluent's
serializers know without references need no fetching: Protobuf's own, such as
google/protobuf/timestamp.proto and google/protobuf/descriptor.proto, Google's
common types under google/type/, such as date.proto and money.proto, and
Confluent's confluent/meta.proto and confluent/type/decimal.proto, which
Kafka Connect's records import.
// Read any Protobuf record of any schema, and show its message and one of its fields
import cats.effect.IO
import xkafka.{Headers, Topic}
import xkafka.protobuf.ProtobufSerde
import xkafka.schemaregistry.SchemaRegistry
def show(registry: SchemaRegistry[IO], topic: Topic, bytes: fs2.Chunk[Byte]): IO[String] =
ProtobufSerde.asWritten[IO](registry).flatMap: reading =>
reading.deserialize(topic, Headers.empty, Some(bytes)).map(written => s"${written.value.name}: ${written.value.field("id")}")
ProtobufSerde.generic writes messages of a .proto file you hold, which
ProtobufSchemaText.parse reads from its text, and reads records by field
number, as a derived serde does:
// Write a line of an order, with a .proto file read as the program runs
import cats.effect.IO
import xkafka.Serde
import xkafka.protobuf.{ProtobufSchemaText, ProtobufSerde, ProtobufValue}
import xkafka.schemaregistry.{Part, SchemaRegistry}
val text = "syntax = \"proto3\"; package com.example; message Line { string sku = 1; int32 quantity = 2; }"
def lines(registry: SchemaRegistry[IO]): IO[Serde[IO, ProtobufValue]] =
IO.fromEither(ProtobufSchemaText.parse(text).left.map(new IllegalArgumentException(_))).flatMap: file =>
ProtobufSerde.generic[IO](registry, file, "com.example.Line", Part.Value)
val line = ProtobufValue.Message("com.example.Line", Vector("sku" -> ProtobufValue.String("apple"), "quantity" -> ProtobufValue.Int32(3)))
The file may import only those known files, and must declare the message you
name; otherwise ProtobufSerde.generic fails with RegistryException.NoSchema.
ProtobufSchemaText.render turns a ProtobufSchema.File back into text.
A message is named by its full name, such as com.example.Line, and holds its
fields by name, in the order the file declares them; field("sku") gives one.
When a record is read:
- a proto3 field without a label, such as
int32 quantity = 2;, that the record lacks reads as its zero value, and a list or map as empty; - any other field the record lacks, such as one labelled
optional, a message, or a field of aoneof, is left out of the message; - fields whose numbers the file does not declare are kept in the message's
unknownfields, and written back as they were, so copying a record loses nothing.
Each number field holds the value its type calls for:
| Protobuf | ProtobufValue |
|---|---|
int32, sint32, sfixed32 |
Int32 |
int64, sint64, sfixed64 |
Int64 |
uint32, fixed32 |
UInt32, a Long from 0 to 4294967295 |
uint64, fixed64 |
UInt64, a BigInt from 0 to 2^64 - 1 |
An enum's value is Enum("STATUS_PAID"), by name, or EnumNumber(7) if a
record holds a number the enum has no name for, such as one written by a newer
version of the schema.
Writing fails with RegistryException.Unserializable if a value does not fit
its message: it has a field the message lacks, a value of the wrong type, two
fields of one oneof, an unsigned number out of range, or lacks a proto2
required field.
ProtobufSchemaText.parse reads proto2 and proto3 files. It ignores services
and options, except a field's packed and default, and refuses files that
declare groups or extensions, or are written in an edition of Protobuf.
Names and documentation
These annotations, in xkafka.schemaregistry, change what a derived schema
calls things, for every format. Each that takes an argument takes a constant, a
literal or a value the compiler folds to one, such as a final val; any other
argument fails to compile:
| Annotation | On | Effect |
|---|---|---|
@SchemaName("name") |
a type or field | names it in place of its Scala name |
@SchemaNamespace("com.example") |
a type | puts it in this namespace in place of its package's and objects', or in none for @SchemaNamespace("") |
@SchemaDoc("text") |
a type or field | documents it |
@SchemaAlias("Old") |
a type or field | another name it was known by, so records written under that name are read |
@SchemaNoDefault |
a field | leaves its Scala default out of the schema |
// An order renamed from Purchase, in the namespace com.example, with a field renamed for the schema
import java.time.Instant
import xkafka.avro.AvroCodec
import xkafka.schemaregistry.{SchemaAlias, SchemaDoc, SchemaName, SchemaNamespace}
@SchemaNamespace("com.example")
@SchemaAlias("Purchase")
@SchemaDoc("An order a customer placed")
final case class Order(id: String, @SchemaName("placed_at") placedAt: Instant) derives AvroCodec
In JSON Schema, a type's name and namespace make its title, and a type's
aliases are not used: JSON Schema has no way to record them. A field's aliases
are read as other names for the field.
In Protobuf, documentation becomes comments in the .proto file, and the namespace of the message you write is the file's package. Aliases are not used: records name fields by number, so a renamed field or type needs no alias.
Subjects
The registry keeps each schema under a subject. SubjectNaming chooses the
subject's name, as Confluent's subject name strategies do:
SubjectNaming |
Subject | Confluent's strategy |
|---|---|---|
TopicName (default) |
orders-value or orders-key |
TopicNameStrategy |
RecordName |
com.example.Order, the record's full name, the JSON Schema's title or the Protobuf message's full name |
RecordNameStrategy |
TopicRecordName |
orders-com.example.Order |
TopicRecordNameStrategy |
RecordName and TopicRecordName need a named record: writing a type that has
none, such as a String, an enum or a fixed type in Avro, or a JSON Schema
without a title, fails with RegistryException.Unserializable when written.
Subject.from refuses an empty subject, and . and .., which would make
the registry read another path.
Which schema is written
of and generic take a Writing:
Register(default): writes with your codec's schema, registering it under the subject on the first write if it is not there yet, as Confluent'sauto.register.schemasdoes. Use it when your application owns the schema. If the subject's compatibility rule refuses the schema, writing fails withRegistryException.Refused, with status 409 and error code 40901.AlreadyRegistered: writes with your codec's schema, which must already be registered. Use it when schemas are registered ahead of time, for example by a deployment pipeline, and applications must not register them. Writing fails withRegistryException.Unserializableif the schema is not registered.Latest: writes with the subject's latest version, as Confluent'suse.latest.versiondoes. Use it when another team owns the schema. With Avro, the value is read into the latest version as Avro resolves your codec's schema into it: fields it lacks are left out, those it adds take their defaults, a field it renames is found through its alias, and anintis written as itslong. JSON is written as your codec writes it. A Protobuf message is written as your codec writes it, using the message in the latest version's file that has your type's name. If the file has no such message, writing fails. Writing also fails withRegistryException.Unserializableif the subject has no versions.Pinned(id): writes with the schema registered asid. Use it when every writer must use one exact schema.
With Avro, including the vulcan and avro4s serdes, Latest and Pinned refuse
a schema that would change what a value means: one with a decimal of another
scale or fewer digits than your codec writes, a time in another unit, such as
microseconds for your milliseconds, or text where your codec writes bytes.
Writing then fails with RegistryException.Unserializable.
Registry operations
SchemaRegistry also offers the registry's operations directly: schema(id),
register, lookup, latest, version, and compatibility, which tells
whether a schema could be registered as a subject's next version, with the
registry's reasons if it could not.
lookup, latest and version return None if the registry does not have
the subject, the version, or the schema under that subject. schema(id) of an
id the registry does not have fails with RegistryException.Refused.
Errors
Failures are RegistryExceptions:
| Exception | When |
|---|---|
Unreachable |
none of the registry's URLs could be reached |
Refused |
the registry refused a request; code is its error code, such as 40901 for an incompatible schema |
Unreadable |
the registry's answer could not be read, such as one from a proxy in front of it |
Unserializable |
a value could not be written, or a record read, with its schema |
NoSchema |
your type has no schema in the serde's format, such as a list of lists in Protobuf |
A record nested more than 100 deep, such as a list of 101 nodes that each hold
the next, is also Unserializable, written or read, in Avro and JSON Schema with
xkafka's own codecs. A Protobuf record may hold 100 messages below its own, as
protobuf-java reads it, counting a oneof's message and each map entry as one, so
a list of 102 nodes is refused there. A record read through ScalaPB or smithy4s,
whose readers count no depth, is held to the same limits before they read it.
So are:
- an Avro record whose arrays or maps claim more items than its bytes can hold, or that has bytes left after its value, with xkafka's codecs or with vulcan's and avro4s's;
- a JSON or Protobuf record whose text is not UTF-8. An Avro record's text is read with such bytes replaced.
The serdes of vulcan and avro4s write and read nested records as their libraries do. Those of ScalaPB and smithy4s hold what they write to the same limits as what they read, so they write nothing that reading refuses, and refuse bytes that are not a message of their type before their libraries read them.
A value an error names is cut short after 200 characters, with its full length.
Settings the registry client refuses raise InvalidRegistrySetting, and a
record without a value raises Deserializer.NullValueException (see
Writing and reading records).
Not supported
- Avro and JSON Schema schemas that name schemas registered under other subjects
(schema references): records written with them are refused when reading, and
LatestorPinnedcannot write with them. Protobuf records with such schemas are read: derived serdes read by field number and do not need the other schemas, andProtobufSerde.asWrittenfetches them. - Generic Protobuf serdes, and serdes of messages ScalaPB generated, of files that import files other than those Confluent's serializers know.
- Protobuf files that declare groups or extensions, or are written in an edition of Protobuf.