1

I've got problem with sending message using Spring-Kafka API to topic on brokers running on confluent platform. I am using AVRO schema along with apache-avro-plugin for maven to generate Java objects from schema.

The schema is being registered properly in a correct format in Confluent Schema Registry but when I am sending the message using confluent KafkaAvroSerializer the union types in a message appear in a "weird" format.

Here is my AVRO schema

{
  "namespace": "com.mynamespace",
  "name": "MessageValue",
  "doc": "Kafka message value",
  "type": "record",
  "fields": [
    {
      "name": "metadata",
      "type": {
        "name": "Metadata",
        "doc": "metadata model description",
        "type": "record",
        "fields": [
          {
            "name": "eventVersion",
            "type": [
               "null",
               "string"
            ]
          },
          {
            "name": "eventTime",
            "type": "string"
          },
          {
            "name": "eventSource",
            "type": "string"
          },
          {
            "name": "eventKey",
            "type": "string"
          }
        ]
      }
    }
  ]
}

Here is a serialized JSON object that was sent to topic:

{
  "metadata": {
    "eventVersion": {
      "string": "1.0"
    },
    "eventTime": "2019-01-01T09:00",
    "eventSource": "Channel1",
    "eventKey": "SomeKey"
  }
}

My problem is the field that is of UNION type. It is being serialized in a way that it appears to be separate json object with field:

string: "1.0"

I've tried changing my schema to not be of UNION type like

{
   "name": "eventVersion",
   "type": "string",
   "default": "null"
}

But then I always had to sent the field because it made the field mandatory.

Also I cannot find anything relevant in their documentation about it. Have anyone had this problem and maybe know how to make the serialized object to look like this:

{
  "metadata": {
    "eventVersion": "1.0",
    "eventTime": "2019-01-01T09:00",
    "eventSource": "Channel1",
    "eventKey": "SomeKey"
  }
}

EDIT @Dean Van Greunen Typos were made by me typing here the question and cutting the bussiness information from schema that I cannot shar. In the code the schema was fine. The problem is speicified in the question. Serialized JSON object that is sent to kafka looks like this:

{
  "metadata": {
    "eventVersion": {
      "string": "1.0"
    },
    "eventTime": "2019-01-01T09:00",
    "eventSource": "Channel1",
    "eventKey": "SomeKey"
  }
}

The "eventVersion field is my problem.

...
"eventVersion":{
    "string" : "1.0"
}
...

It looks like this field is separate object with field called "string and value "1.0". I know it's limitation due to UNION avro type but I want to know if there's a workaround to make it look like this:

{
  "metadata": {
    "eventVersion": "1.0",
    "eventTime": "2019-01-01T09:00",
    "eventSource": "Channel1",
    "eventKey": "SomeKey"
  }
}

Here the "eventVersion" field is simple type String

Cysiekk
  • 33
  • 5

1 Answers1

0

unsure of the issue, please update your question; however i have spot a few mistakes, see below.

you're AVRO Scheme is incorrect. the first element in the fields is missing a ,

{
  "namespace": "com.mynamespace",
  "name": "MessageValue",
  "doc": "Kafka message value",
  "type": "record",
  "fields": [
    {
      "name": "metadata",
      "type": {
        "name": "Metadata",
        "doc": "metadata model description",
        "type": "record",
        "fields": [
          {
            "name": "eventVersion",
            "type": [
               "null",
               "string"
            ]
          },
          {
            "name": "eventTime",
            "type": "string"
          },
          {
            "name": "eventSource",
            "type": "string"
          },
          {
            "name": "eventKey",
            "type": "string"
          }
        ]
      }
    }
  ]
}

there is a spelling mistake for default.

{
   "name": "eventVersion",
   "type": "string",
   "default": "null"
}
Dean Van Greunen
  • 5,060
  • 2
  • 14
  • 28