Avro union - type information in resulting json

Viewed 1150

I have a part of a java application that writes an AVRO encoded object into a Kafka topic. I use the org.apache.kafka.clients.producer.Producer to send messages into the topic. The Producer is configured to use the io.confluent.kafka.serializers.KafkaAvroSerializer.

The AVRO schema contains the following field among others:

{
  "name": "field1",
  "type": [
    "null",
    "string"
  ],
  "default": null
}

Which basically means that it's a string field which can be null.

After sending an object to the topic, I inspect its content with the following terminal command:

./kafka-avro-console-consumer --bootstrap-server broker-host:9091 --topic test-topic-1
  --property schema.registry.url=http://schema-host:8081 --from-beginning

As I result I would expect to see a JSON that has the following attribute:

{"field1": "something"}

However, in reality I see this:

{"field1": {"string": "something"}}

Along with that, if I simply use a toString() method on the object extending the SpecificRecordBase before actually sending it to the topic, I see a JSON formatted as I expect.

Is there a way to actually get the first option in the topic, without this extra type information being a part of the JSON?

1 Answers

Is there a way to actually get the first option in the topic, without this extra type information being a part of the JSON?

There is not for avro-console-consumer. It is using GenericRecord, not SpecificRecord

Since the field is a union-type the Avro Record JSON-encoded output must include the type of the field for that value.

Related