Skip to main content

Import an Apache Avro schema

The Avro importer (avro) reads Apache Avro schemas into the Catalog. It accepts both of Avro's surfaces: the JSON schema (.avsc) and the Avro IDL source (.avdl). Records, enums and fixed types become types. An IDL protocol with messages becomes an RPC API with one operation per message. You can then search, version, diff, lint and convert it like any other catalog item.

When to use it​

Use the Avro importer for the schemas behind your Kafka topics and any other Avro data: Avro 1.12 JSON schemas and Avro IDL protocols. Bring them in as files, or read them straight from a Confluent-compatible schema registry (Confluent Platform or Cloud, Karapace, Apicurio Registry's compatibility API). Each version is fingerprinted the way Avro libraries do it, and compared by the same rules a Confluent Schema Registry uses.

At a glance​

StepWhereDetails
Import an .avsc or .avdl file, or a file setImport to catalog → Avro, or apiome import avroImport it
Read one schema from a schema registryURL Import with a registry URL, or apiome import avro --registryImport from a schema registry
Mirror a whole registry, every subject and versionA schema-registry connectorSchema registry connector
See what the schema became: types, unions, logical typesThe catalog itemWhat the catalog item holds
Fingerprint each type by Parsing Canonical FormAutomatic on every importFingerprints
Compare two versions by the Avro schema resolution rulesTick two versions on the catalog item's Versions tab, or apiome registry compatCompatibility between versions
Lint defaults, unions, aliases and docsThe avro lint packLint
Convert to an OpenAPI projectConvert to OpenAPI on the catalog itemPer-format conversion packs
Export back to Avro, or push it to a registryExport on the catalog item; a push over the REST APIApache Avro from any source, Push to a schema registry

Import it​

Import the schema like any other catalog format: in the import wizard (Import to catalog → Avro), over the REST API (see Import a specification), or from the CLI:

apiome import avro ./user.avsc
apiome import avro ./orders.avdl
  • One file. Upload, paste or link the .avsc or .avdl file.
  • A file set. Upload a .zip, .tar.gz or .tar holding an IDL file and the files it imports.
  • A schema registry. Read one schema from a Confluent-compatible schema registry. See Import from a schema registry.

The CLI's test fixtures include the same orders schema in each of Avro's surfaces: orders-avro.avsc and orders-avro.avdl, in apiome-cli/tests/fixtures/. They are a quick way to try an import:

apiome import avro apiome-cli/tests/fixtures/orders-avro.avdl

Import from a schema registry​

A Confluent-compatible schema registry serves each schema it holds at a raw-schema route:

  • <registry>/subjects/<subject>/versions/<version>/schema, for one version of a subject.
  • <registry>/schemas/ids/<id>/schema, for a schema by its global id.

The wizard and the CLI both import the schema from that route, so the catalog item records it as its source. This imports one schema. To keep a whole registry in the catalog, with its credentials and every subject's history, mirror the registry instead.

In the import wizard​

  1. On the Catalog, click “Import to catalog” and choose URL Import.
  2. Enter a registry URL as the Document URL:
    • …/subjects/orders-value/versions/3, for one version.
    • …/subjects/orders-value/versions, for the subject's latest version.
    • …/schemas/ids/21, for a schema by id.
    • A raw-schema URL ending in /schema works too.
  3. Click “Fetch and detect”.

The first three routes answer with JSON about the schema: the schema wrapped in an envelope, or a list of version numbers. The wizard recognizes that answer, fetches the raw-schema route instead, and continues with the schema. An envelope for a JSON Schema or Protobuf schema is not followed.

The fetch runs in your browser, so the registry must allow requests from Apiome (CORS) and must not need a sign-in. If it does, use the CLI, or mirror the registry.

From the CLI​

Give the registry's base URL to --registry, and name the schema:

apiome import avro --registry https://registry.example.com:8081 --subject orders-value
apiome import avro --registry https://registry.example.com:8081 --subject orders-value --version 3
apiome import avro --registry https://registry.example.com:8081 --schema-id 21

--version is latest (the default) or a version number. The base URL may carry a path prefix, such as https://host/apis/ccompat/v7.

You can also name the subject the way a Kafka producer does, from its topic or record, with --strategy:

--strategySubjectExample
topic (the default)<topic>-value, or <topic>-key with --key--topic orders → orders-value
recordThe record's full name--record com.example.orders.Order
topic_record<topic>-<record>--topic orders --record com.example.orders.Order → orders-com.example.orders.Order
apiome import avro --registry https://registry.example.com:8081 --topic orders --key
apiome import avro --registry https://registry.example.com:8081 \
--strategy topic_record --topic orders --record com.example.orders.Order

These are Confluent's TopicNameStrategy, RecordNameStrategy and TopicRecordNameStrategy, the same names a mirrored registry records for each subject. --registry replaces the file or URL input: use one or the other. See the CLI reference for every option.

What the catalog item holds​

Both surfaces map the same way. Each named type becomes a catalog type, keyed by its full name (com.example.User).

AvroCatalog item
record (and IDL error)A record type, one field per Avro field, in order
A field's defaultThe field's default; an explicit null default counts as a default
enumAn enum type, one value per symbol, in order; its default symbol is kept
fixedA scalar type that keeps its size
["null", X]A nullable reference to X
Any other unionA union type, one member per branch
mapA map type, named after its values: Map_long, Map_array_string
arrayA list of its items
aliases of a type or field, a field's orderKept as written

A union branch can be a type named elsewhere (["Cat", "Dog"]), a type declared inline, a map or a primitive written as an object ({"type": "long"}). Each one becomes a member. An array branch has no name of its own, so it is listed as string.

Custom attributes ("x-owner": "team") are kept only from Avro IDL, where they are @annotations.

Logical types​

Every logical type in Avro 1.12 is read, on a field, behind ["null", X], or on a named fixed:

Logical typeWritten onRead as
dateintdate
time-millis / time-microsint / longtime
timestamp-millis / -micros / -nanoslongdate-time
local-timestamp-millis / -micros / -nanoslongdate-time
uuidstring, or fixed of 16 bytesuuid
decimalbytes or fixed, with precision and scaledecimal
big-decimalbytesdecimal
durationfixed of 12 bytesduration

The exact logical type is kept beside the format, so a timestamp-micros field is exported back as timestamp-micros, not as timestamp-millis.

The Avro specification says a reader ignores a logical type it does not know, or one written on the wrong type. Apiome does the same: it reads the field as its plain Avro type and marks the logical type unknown or invalid. These count as invalid:

  • A logical type on the wrong type, such as {"type": "string", "logicalType": "date"}.
  • A decimal with no precision, or a scale below 0 or above its precision.
  • A decimal on a fixed too small for its precision.
  • A uuid or duration on a fixed of the wrong size.

The mark is a field's (or fixed type's) avro_logical_type_ignored key. The logical type itself is still kept, so an export writes it back as it was.

Fingerprints​

Every Avro catalog item records two hashes for each named type it declares. Each type is hashed together with the named types it uses.

  • Parsing Canonical Form (PCF). This is Avro's own schema identity. The schema is reduced to what a reader needs to decode its bytes: full names, types, fields in order, symbols, items, values and sizes. Everything else is stripped. Apiome hashes it two ways:

    • CRC-64-AVRO: the 64-bit fingerprint Avro libraries compute, shown as 16 hex digits.
    • SHA-256.

    Two schemas with the same PCF read and write the same binary data.

  • Semantic hash. A SHA-256 over the same normalized schema, keeping what PCF strips: doc, default, aliases, order, logicalType and custom attributes. Defaults and aliases matter when a schema evolves, so this hash changes whenever anything in the schema does.

ChangePCFSemantic hashNew catalog version
A doc editedSameChangesNo
Key order or whitespaceSameSameNo
A field's defaultSameChangesYes
A field's aliases or orderSameChangesYes
A field's type, a field added or removed, fields reorderedChangesChangesYes
A type renamed, an enum symbol addedChangesChangesYes

A doc-only edit is not a change to the contract: it creates no new version, and a diff between the two shows nothing breaking. An aliases or order edit does create one, because the catalog item keeps them.

The catalog item keeps the hashes under these keys:

KeyHolds
avro_pcfcrc64 and sha256 of each named type, keyed by full name (com.example.User)
avro_semantic_hashesThe semantic hash of each named type, keyed by full name
avro_rootThe full name of the document's root type

The schema's own fingerprint is its root type's. An IDL protocol has no single schema, so it has a fingerprint per type only.

note

The schema registry still matches schemas by the catalog's canonical fingerprint, not by PCF.

Compatibility between versions​

Avro data written with one schema is read with another, and the Avro schema resolution rules decide whether that works. Apiome applies them the way a Confluent Schema Registry does, in two places.

Breaking changes in a diff​

When you compare two versions of an Avro catalog item, each change is graded by which way the data can still be read:

What the check findsSeverityConfluent modes it breaks
The new schema cannot read data written with the old oneBreakingBACKWARD, FULL
Only the old schema cannot read data written with the new oneDangerousFORWARD, FULL
Each reads the other's dataSafeNone

For example:

ChangeSeverity
A field added without a defaultBreaking: new readers have nothing to fill it with
A field added with a defaultSafe
A field removed that had no defaultDangerous: old readers miss it
A field removed that had a defaultSafe
int widened to long, or a union branch addedDangerous
long narrowed to int, or a union branch removedBreaking
An enum symbol added (and the enum has no default)Dangerous
An enum symbol removedBreaking
A field renamed, keeping the old name in aliasesDangerous: new readers find it by the alias, old readers do not
The namespace changedSafe: Avro compares names without their namespace

Each grade's explanation names the modes it breaks and gives the registry's own message, such as READER_FIELD_MISSING_DEFAULT_VALUE. For an IDL protocol, every type both versions declare is compared; its operations and messages keep the general rules.

The schema registry​

A subject's compatibility level now applies to Avro schemas. A new version is checked when you register it, and an incompatible one is refused with HTTP 409, as a Confluent registry refuses it:

LevelThe new schema must…
BACKWARD (the default)read data written with the latest version
BACKWARD_TRANSITIVEread data written with every version
FORWARDbe readable by the latest version
FORWARD_TRANSITIVEbe readable by every version
FULL / FULL_TRANSITIVEboth, against the latest version or every version
NONEnothing; any schema registers

To test a schema without registering it, use apiome registry compat <subject> <file> (CLI reference) or the Confluent POST /compatibility/subjects/{subject}/versions route. The answer lists the same messages a Confluent registry gives with ?verbose=true. JSON Schema and Protobuf have no compatibility rules yet: under any level but NONE, a second version of those is refused with error code 42290.

Lint​

The avro lint pack checks the schema as written: defaults, union branches, aliases and docs that tools would drop. See Lint rules.