Avro Union
These example show you how to use Unions.
Avro Schema
In this schema we create a field metric
that can be a TimerMetric
or CounterMetric
.
{
"type": "record",
"name": "Metric",
"namespace": "kafka.sandbox.avro",
"version": "1",
"fields": [
{
"name": "metricId",
"type": "string"
},
{
"name": "metricType",
"type": {
"type": "enum",
"name": "MetricType",
"symbols": [
"TIMER",
"COUNTER"
],
"default": "TIMER"
}
},
{
"name": "metric",
"type": [
{
"type": "record",
"name": "TimerMetric",
"namespace": "kafka.sandbox.avro",
"version": "1",
"fields": [
{
"name": "avg",
"type": "double"
}
]
},
{
"type": "record",
"name": "CounterMetric",
"namespace": "kafka.sandbox.avro",
"version": "1",
"fields": [
{
"name": "count",
"type": "long"
}
]
}
]
}
]
}
The Metric
java class will define an object (for the metric
field) instead of a specific type (TimerMetric
or CounterMetric
).
private java.lang.Object metric;
Setup
Create a topic:
kafka-topics --create --bootstrap-server kafka1:9092 \
--replication-factor 3 \
--partitions 3 \
--topic client.metrics
Produce
gradle kafka-avro-union-clients:run --args="produce client.metrics 100"
Consume
gradle kafka-avro-union-clients:run --args="consume client.metrics"