Skip to content

Sync Protobuf schemas over Avro and JSON Schema

Different schema formats on each side: the plan, with and without replacing.

Terminal recording of kurkik kafka sync

The recording shows kurkik kafka sync on the demo sandbox; its data differs from this example.

What you get if you do not ask for the risky part:

$ kurkik kafka sync a.yaml b --schemas --dry-run
Sync a.yaml into b - Apache Kafka, cluster MkU3OEVBNTcwNTJENDM2Qk
owns: the whole cluster - the document names no x-kurkik.kafka.server.topicPrefixes; access, as x-kurkik.kafka.server.acl declares it
plan: <digest>
Run one sync per cluster at a time: two at once each plan against a state the other is changing.
Nothing to change.
left alone (8)
not decided customers.updated groupId
Nothing about this can be checked against the system, so sync cannot bring it into line. Operation 'receive_customers.updated' declares no consumer group, so there is nothing a broker could confirm.
not decided orders.created groupId
Nothing about this can be checked against the system, so sync cannot bring it into line. Operation 'receive_orders.created' declares no consumer group, so there is nothing a broker could confirm.
not decided customers.updated action
Nothing about this can be checked against the system, so sync cannot bring it into line. A broker cannot know who publishes to 'customers.updated', so operation 'send_customers.updated' is not checked.
not decided orders.created action
Nothing about this can be checked against the system, so sync cannot bring it into line. A broker cannot know who publishes to 'orders.created', so operation 'send_orders.created' is not checked.
schema differs customers.updated schemaFormat --overwrite --replace-schemas
schema differs orders.created schemaFormat --overwrite --replace-schemas
and 2 differences sync does not act on - servers, messages, undeclared settings; kurkik kafka diff shows them
0 changes
exit 0

Nothing changes yet. --dry-run only shows what would happen.

$ kurkik kafka sync a.yaml b --schemas --overwrite --replace-schemas --dry-run
Sync a.yaml into b - Apache Kafka, cluster MkU3OEVBNTcwNTJENDM2Qk
owns: the whole cluster - the document names no x-kurkik.kafka.server.topicPrefixes; access, as x-kurkik.kafka.server.acl declares it
plan: <digest>
Run one sync per cluster at a time: two at once each plan against a state the other is changing.
changes (2)
± replace customers.updated-value schemaFormat JSON Schema -> Protobuf [soft-deletes the subject's versions]
The value schema of customers.updated is Protobuf in the document and JSON Schema in the registry. sync converts no schema from one format to another, whatever their structure. Replacing soft-deletes customers.updated-value's 1 version(s) and registers the document's 1: a client resolving an old version by number no longer finds it. The rollback registers them again, under new numbers.
± replace orders.created-value schemaFormat Avro -> Protobuf [soft-deletes the subject's versions]
The value schema of orders.created is Protobuf in the document and Avro in the registry. sync converts no schema from one format to another, whatever their structure. Replacing soft-deletes orders.created-value's 1 version(s) and registers the document's 1: a client resolving an old version by number no longer finds it. The rollback registers them again, under new numbers.
note: Replacing a subject soft-deletes every version it has and registers the document's: a producer or consumer that looks a schema up by version number stops finding the old ones. Ids stay resolvable. The rollback registers the old versions again, in order, under new numbers.
left alone (6)
not decided customers.updated groupId
Nothing about this can be checked against the system, so sync cannot bring it into line. Operation 'receive_customers.updated' declares no consumer group, so there is nothing a broker could confirm.
not decided orders.created groupId
Nothing about this can be checked against the system, so sync cannot bring it into line. Operation 'receive_orders.created' declares no consumer group, so there is nothing a broker could confirm.
not decided customers.updated action
Nothing about this can be checked against the system, so sync cannot bring it into line. A broker cannot know who publishes to 'customers.updated', so operation 'send_customers.updated' is not checked.
not decided orders.created action
Nothing about this can be checked against the system, so sync cannot bring it into line. A broker cannot know who publishes to 'orders.created', so operation 'send_orders.created' is not checked.
and 2 differences sync does not act on - servers, messages, undeclared settings; kurkik kafka diff shows them
applying needs: DESCRIBE and DESCRIBE_CONFIGS on the topics it reads; write access to the schema registry's subject customers.updated-value; write access to the schema registry's subject orders.created-value
2 changes, 2 destroy
rehearsal
[?] not attempted replace customers.updated-value schemaFormat JSON Schema -> Protobuf
not rehearsed: it has no validate-only form
[?] not attempted replace orders.created-value schemaFormat Avro -> Protobuf
not rehearsed: it has no validate-only form
The cluster would accept every request it was shown. 2 could not be rehearsed - Kafka has no validate-only deletion or ACL change, and a schema registry no validate-only write. Nothing was changed.
exit 2
$ kurkik kafka sync a.yaml b --schemas --overwrite --replace-schemas --yes
Sync a.yaml into b - Apache Kafka, cluster MkU3OEVBNTcwNTJENDM2Qk
owns: the whole cluster - the document names no x-kurkik.kafka.server.topicPrefixes; access, as x-kurkik.kafka.server.acl declares it
plan: <digest>
Run one sync per cluster at a time: two at once each plan against a state the other is changing.
changes (2)
± replace customers.updated-value schemaFormat JSON Schema -> Protobuf [soft-deletes the subject's versions]
The value schema of customers.updated is Protobuf in the document and JSON Schema in the registry. sync converts no schema from one format to another, whatever their structure. Replacing soft-deletes customers.updated-value's 1 version(s) and registers the document's 1: a client resolving an old version by number no longer finds it. The rollback registers them again, under new numbers.
± replace orders.created-value schemaFormat Avro -> Protobuf [soft-deletes the subject's versions]
The value schema of orders.created is Protobuf in the document and Avro in the registry. sync converts no schema from one format to another, whatever their structure. Replacing soft-deletes orders.created-value's 1 version(s) and registers the document's 1: a client resolving an old version by number no longer finds it. The rollback registers them again, under new numbers.
note: Replacing a subject soft-deletes every version it has and registers the document's: a producer or consumer that looks a schema up by version number stops finding the old ones. Ids stay resolvable. The rollback registers the old versions again, in order, under new numbers.
left alone (6)
not decided customers.updated groupId
Nothing about this can be checked against the system, so sync cannot bring it into line. Operation 'receive_customers.updated' declares no consumer group, so there is nothing a broker could confirm.
not decided orders.created groupId
Nothing about this can be checked against the system, so sync cannot bring it into line. Operation 'receive_orders.created' declares no consumer group, so there is nothing a broker could confirm.
not decided customers.updated action
Nothing about this can be checked against the system, so sync cannot bring it into line. A broker cannot know who publishes to 'customers.updated', so operation 'send_customers.updated' is not checked.
not decided orders.created action
Nothing about this can be checked against the system, so sync cannot bring it into line. A broker cannot know who publishes to 'orders.created', so operation 'send_orders.created' is not checked.
and 2 differences sync does not act on - servers, messages, undeclared settings; kurkik kafka diff shows them
applying needs: DESCRIBE and DESCRIBE_CONFIGS on the topics it reads; write access to the schema registry's subject customers.updated-value; write access to the schema registry's subject orders.created-value
2 changes, 2 destroy
applied
[+] done replace customers.updated-value schemaFormat JSON Schema -> Protobuf
[+] done replace orders.created-value schemaFormat Avro -> Protobuf
Applied 2 of 2 changes - 6 left alone
snapshot: ~/.local/state/kurkik/sync/b/b-<time>.snapshot.yaml
rollback: ~/.local/state/kurkik/sync/b/b-<time>.rollback.yaml
undo: kurkik kafka sync revert ~/.local/state/kurkik/sync/b/b-<time>.snapshot.yaml
exit 0

Written before the first change.

rollback.yaml
system: "Apache Kafka"
target: "MkU3OEVBNTcwNTJENDM2Qk"
createdAt: "<time>"
document: "~/.local/state/kurkik/sync/b/b-<time>.snapshot.yaml"
entries:
- forAction: "replace-schemas:customers.updated:schemaFormat"
undo: "RESTORE_REPLACED_SCHEMAS"
target: "customers.updated"
field: "schemaFormat"
note: "sync replaced this subject's versions. Undoing it soft-deletes the ones it\
\ registered and registers the old ones again, in order, under new version numbers."
definition:
"1.format": "PROTOBUF"
"1.schema": "syntax = \"proto3\";\npackage com.shop.customers;\n\nmessage Customer\
\ {\n string id = 1;\n string email = 2;\n}\n"
count: "1"
old.1.format: "JSON"
old.1.schema: "{\"$schema\":\"http://json-schema.org/draft-07/schema#\",\"title\"\
:\"Customer\",\"type\":\"object\",\"properties\":{\"id\":{\"type\":\"string\"\
}},\"required\":[\"id\"]}"
old.1.version: "1"
old.count: "1"
part: "VALUE"
registered: "2"
subject: "customers.updated-value"
- forAction: "replace-schemas:orders.created:schemaFormat"
undo: "RESTORE_REPLACED_SCHEMAS"
target: "orders.created"
field: "schemaFormat"
note: "sync replaced this subject's versions. Undoing it soft-deletes the ones it\
\ registered and registers the old ones again, in order, under new version numbers."
definition:
"1.format": "PROTOBUF"
"1.schema": "syntax = \"proto3\";\npackage com.shop.orders;\n\nmessage OrderCreated\
\ {\n string id = 1;\n int64 total = 2;\n}\n"
count: "1"
old.1.format: "AVRO"
old.1.schema: "{\"type\":\"record\",\"name\":\"OrderCreated\",\"namespace\":\"\
com.shop.orders\",\"fields\":[{\"name\":\"id\",\"type\":\"string\"},{\"name\"\
:\"total\",\"type\":\"long\"}]}"
old.1.version: "1"
old.count: "1"
part: "VALUE"
registered: "2"
subject: "orders.created-value"

Download rollback.yaml

$ kurkik kafka sync revert ~/.local/state/kurkik/sync/b/b-<time>.snapshot.yaml b --force --yes
Revert b to b-<time>.snapshot.yaml - Apache Kafka, cluster MkU3OEVBNTcwNTJENDM2Qk
owns: what the run being undone changed, as its rollback records it
plan: <digest>
Run one sync per cluster at a time: two at once each plan against a state the other is changing.
changes (2)
± replace orders.created-value schemaFormat Protobuf -> Avro [soft-deletes the subject's versions]
Soft-deletes orders.created-value's 1 version(s) and registers the 1 it held before, in order, under new numbers.
± replace customers.updated-value schemaFormat Protobuf -> JSON Schema [soft-deletes the subject's versions]
Soft-deletes customers.updated-value's 1 version(s) and registers the 1 it held before, in order, under new numbers.
note: Replacing a subject soft-deletes every version it has and registers the document's: a producer or consumer that looks a schema up by version number stops finding the old ones. Ids stay resolvable. The rollback registers the old versions again, in order, under new numbers.
applying needs: DESCRIBE and DESCRIBE_CONFIGS on the topics it reads; write access to the schema registry's subject orders.created-value; write access to the schema registry's subject customers.updated-value
2 changes, 2 destroy
applied
[+] done replace orders.created-value schemaFormat Protobuf -> Avro
[+] done replace customers.updated-value schemaFormat Protobuf -> JSON Schema
Applied 2 of 2 changes
snapshot: ~/.local/state/kurkik/sync/b/b-<time>.snapshot.yaml
rollback: ~/.local/state/kurkik/sync/b/b-<time>.rollback.yaml
undo: kurkik kafka sync revert ~/.local/state/kurkik/sync/b/b-<time>.snapshot.yaml
exit 0

After the revert, compared with how it was before: Drift: 0 changed · 0 unmanaged · 0 missing (no drift) - 32 matched

source.yaml, 186 lines
asyncapi: 3.0.0
info:
title: a Apache Kafka API
version: 1.0.0-SNAPSHOT
description: "Generated by reading the a cluster. Cluster id: MkU3OEVBNTcwNTJENDM2Qk."
servers:
broker-1:
host: a-kafka-1:9092
protocol: kafka
protocolVersion: "4.4"
bindings:
kafka:
bindingVersion: 0.5.0
schemaRegistryUrl: http://registry-a:8080
schemaRegistryVendor: confluent
x-kurkik:
kafka:
server:
schemaRegistryUrl: http://registry-a:8080
schemaRegistryVendor: confluent
security:
- security: PLAINTEXT
securityProtocol: PLAINTEXT
acl: []
groups:
- groupId: schema-registry
groupType: classic
state: STABLE
clientIds: []
topics: []
configuration: {}
brokerConfiguration:
advertised.listeners: "INTERNAL://kafka-1:19092,EXTERNAL://a-kafka-1:9092"
authorizer.class.name: org.apache.kafka.metadata.authorizer.StandardAuthorizer
broker.id: "1"
controller.listener.names: CONTROLLER
controller.quorum.voters: 1@kafka-1:9093
group.initial.rebalance.delay.ms: "0"
inter.broker.listener.name: INTERNAL
listener.security.protocol.map: "INTERNAL:PLAINTEXT,EXTERNAL:PLAINTEXT,CONTROLLER:PLAINTEXT"
listeners: "INTERNAL://0.0.0.0:19092,EXTERNAL://0.0.0.0:9092,CONTROLLER://0.0.0.0:9093"
min.insync.replicas: "1"
node.id: "1"
offsets.topic.replication.factor: "1"
process.roles: "broker,controller"
transaction.state.log.min.isr: "1"
transaction.state.log.replication.factor: "1"
clientConfiguration:
bootstrap.servers: a-kafka-1:9092
security.protocol: PLAINTEXT
groupConfiguration: {}
producerConfiguration: {}
consumerConfiguration: {}
channels:
customers.updated:
address: customers.updated
messages:
customers.updated-value:
payload:
schemaFormat: application/vnd.google.protobuf;version=3
schema: |
syntax = "proto3";
package com.shop.customers;
message Customer {
string id = 1;
string email = 2;
}
name: customers.updated-value
title: customers.updated
summary: "Schema read from the Confluent Schema Registry: subject customers.updated-value,\
\ version 1."
bindings:
kafka:
bindingVersion: 0.5.0
schemaIdLocation: payload
schemaIdPayloadEncoding: confluent
schemaLookupStrategy: TopicNameStrategy
x-kurkik:
kafka:
message:
schemaIdLocation: payload
schemaIdPayloadEncoding: confluent
schemaLookupStrategy: TopicNameStrategy
schemaCompatibility: BACKWARD
bindings:
kafka:
bindingVersion: 0.5.0
partitions: 3
replicas: 1
topicConfiguration:
cleanup.policy:
- compact
x-kurkik:
kafka:
channel:
topic: customers.updated
partitions: 3
replicas: 1
topicConfiguration:
cleanup.policy: compact
orders.created:
address: orders.created
messages:
orders.created-value:
payload:
schemaFormat: application/vnd.google.protobuf;version=3
schema: |
syntax = "proto3";
package com.shop.orders;
message OrderCreated {
string id = 1;
int64 total = 2;
}
name: orders.created-value
title: orders.created
summary: "Schema read from the Confluent Schema Registry: subject orders.created-value,\
\ version 1."
bindings:
kafka:
bindingVersion: 0.5.0
schemaIdLocation: payload
schemaIdPayloadEncoding: confluent
schemaLookupStrategy: TopicNameStrategy
x-kurkik:
kafka:
message:
schemaIdLocation: payload
schemaIdPayloadEncoding: confluent
schemaLookupStrategy: TopicNameStrategy
schemaCompatibility: BACKWARD
bindings:
kafka:
bindingVersion: 0.5.0
partitions: 3
replicas: 1
topicConfiguration:
retention.ms: 604800000
x-kurkik:
kafka:
channel:
topic: orders.created
partitions: 3
replicas: 1
topicConfiguration:
retention.ms: "604800000"
operations:
receive_customers.updated:
action: receive
channel:
$ref: "#/channels/customers.updated"
x-kurkik:
kafka:
operation:
security: []
consumerConfiguration: {}
groupConfiguration: {}
receive_orders.created:
action: receive
channel:
$ref: "#/channels/orders.created"
x-kurkik:
kafka:
operation:
security: []
consumerConfiguration: {}
groupConfiguration: {}
send_customers.updated:
action: send
channel:
$ref: "#/channels/customers.updated"
x-kurkik:
kafka:
operation:
security: []
producerConfiguration: {}
send_orders.created:
action: send
channel:
$ref: "#/channels/orders.created"
x-kurkik:
kafka:
operation:
security: []
producerConfiguration: {}

Download source.yaml

How to build this setup yourself

Brokers

Cluster a

Terminal window
kafka-topics.sh --bootstrap-server a-kafka-1:9092 --create --topic orders.created --partitions 3 --replication-factor 1 --config retention.ms=604800000
kafka-topics.sh --bootstrap-server a-kafka-1:9092 --create --topic customers.updated --partitions 3 --replication-factor 1 --config cleanup.policy=compact
api=http://registry-a:8080; type='Content-Type: application/vnd.schemaregistry.v1+json' # confluentinc/cp-schema-registry:7.9.0
curl -X POST -H "$type" "$api/subjects/orders.created-value/versions" --data '{"schemaType":"PROTOBUF","schema":"syntax = \"proto3\";\npackage com.shop.orders;\n\nmessage OrderCreated {\n string id = 1;\n int64 total = 2;\n}\n"}'
curl -X POST -H "$type" "$api/subjects/customers.updated-value/versions" --data '{"schemaType":"PROTOBUF","schema":"syntax = \"proto3\";\npackage com.shop.customers;\n\nmessage Customer {\n string id = 1;\n string email = 2;\n}\n"}'

Cluster b

Terminal window
kafka-topics.sh --bootstrap-server b-kafka-1:9092 --create --topic orders.created --partitions 3 --replication-factor 1 --config retention.ms=604800000
kafka-topics.sh --bootstrap-server b-kafka-1:9092 --create --topic customers.updated --partitions 3 --replication-factor 1 --config cleanup.policy=compact
api=http://registry-b:8080; type='Content-Type: application/vnd.schemaregistry.v1+json' # confluentinc/cp-schema-registry:7.9.0
curl -X POST -H "$type" "$api/subjects/orders.created-value/versions" --data '{"schemaType":"AVRO","schema":"{\"type\":\"record\",\"name\":\"OrderCreated\",\"namespace\":\"com.shop.orders\",\"fields\":[{\"name\":\"id\",\"type\":\"string\"},{\"name\":\"total\",\"type\":\"long\"}]}"}'
curl -X POST -H "$type" "$api/subjects/customers.updated-value/versions" --data '{"schemaType":"JSON","schema":"{\"$schema\":\"http://json-schema.org/draft-07/schema#\",\"title\":\"Customer\",\"type\":\"object\",\"properties\":{\"id\":{\"type\":\"string\"}},\"required\":[\"id\"]}"}'

Sync

Terminal window
kurkik conn add kafka a --bootstrap a-kafka-1:9092 --registry-url http://registry-a:8080 --registry-vendor confluent
kurkik conn add kafka b --bootstrap b-kafka-1:9092 --registry-url http://registry-b:8080 --registry-vendor confluent
kurkik kafka export a --acls --groups --stdout > a.yaml # source.yaml
kurkik kafka sync a.yaml b --schemas --dry-run # plan-without.txt
kurkik kafka sync a.yaml b --schemas --overwrite --replace-schemas --dry-run # plan.txt (and -o json: plan.json)
kurkik kafka sync a.yaml b --schemas --overwrite --replace-schemas --yes # apply.txt, and the rollback: rollback.yaml
kurkik kafka sync revert ~/.local/state/kurkik/sync/b/b-<time>.snapshot.yaml b --force --yes # revert.txt

All flags: kurkik kafka sync · more Apache Kafka sync examples