Search code examples
avro

org.apache.avro.AvroTypeException: Unknown union branch refentCodifStdId


I'm using this avsc content:

{
  "type" : "record",
  "name" : "ReferentialMessageType",
  "fields" : [ {
    "name" : "type",
    "type" : "string"
  }, 
  {
    "name" : "dictionnaire",
    "type" : [ "null", {
      "type" : "record",
      "name" : "dictionnaireType",
      "fields" : [ {
        "name" : "refentDictId",
        "type" : [ "null", "string" ],
        "default" : null
      }, {
        "name" : "refentDictCode",
        "type" : [ "null", "string" ],
        "default" : null
      }, {
        "name" : "refentDictLibelleCrt",
        "type" : [ "null", "string" ],
        "default" : null
      }, {
        "name" : "refentDictLibelleLng",
        "type" : [ "null", "string" ],
        "default" : null
      }, {
        "name" : "refentDictDateFin",
        "type" : [ "null", {
          "type" : "long",
          "logicalType" : "timestamp-millis"
        } ],
        "default" : null
      }, {
        "name" : "refentDictPere",
        "type" : [ "null", "string" ],
        "default" : null
      }, {
        "name" : "refentDictDateDbt",
        "type" : [ "null", {
          "type" : "long",
          "logicalType" : "timestamp-millis"
        } ],
        "default" : null
      } ]
    } ],
    "default" : null
  }, {
    "name" : "codification",
    "type" : [ "null", {
      "type" : "record",
      "name" : "codificationType",
      "fields" : [ {
        "name" : "refentCodifStdId",
        "type" : [ "null", "string" ],
        "default" : null
      }, {
        "name" : "refentDictId",
        "type" : [ "null", "string" ],
        "default" : null
      }, {
        "name" : "refentCodifStdCode",
        "type" : [ "null", "string" ],
        "default" : null
      }, {
        "name" : "refentCodifStdRng",
        "type" : [ "null", "double" ],
        "default" : null
      }, {
        "name" : "refentCodifStdLibCrt",
        "type" : [ "null", "string" ],
        "default" : null
      }, {
        "name" : "refentCodifStdLibLng",
        "type" : [ "null", "string" ],
        "default" : null
      }, {
        "name" : "refentCodifStdDateDbt",
        "type" : [ "null", {
          "type" : "long",
          "logicalType" : "timestamp-millis"
        } ],
        "default" : null
      }, {
        "name" : "refentCodifStdDateFin",
        "type" : [ "null", {
          "type" : "long",
          "logicalType" : "timestamp-millis"
        } ],
        "default" : null
      }, {
        "name" : "refentCodifStdPere",
        "type" : [ "null", "string" ],
        "default" : null
      } ]
    } ],
    "default" : null
  } ]
}

and trying to publish this message on kafka:

{ "type":"insert", "dictionnaire":null, "codification":{ "refentCodifStdId":"8", "refentDictId":"2", "refentCodifStdCode":"AAAA", "refentCodifStdLibCrt":"BBBB", "refentCodifStdPere":"2" } }

But I receive the following error:

org.apache.avro.AvroTypeException: Unknown union branch refentCodifStdId
    at org.apache.avro.io.JsonDecoder.readIndex(JsonDecoder.java:434)
    at org.apache.avro.io.ResolvingDecoder.readIndex(ResolvingDecoder.java:282)
    at org.apache.avro.generic.GenericDatumReader.readWithoutConversion(GenericDatumReader.java:187)
    at org.apache.avro.generic.GenericDatumReader.read(GenericDatumReader.java:160)
    at org.apache.avro.generic.GenericDatumReader.readField(GenericDatumReader.java:259)
    at org.apache.avro.generic.GenericDatumReader.readRecord(GenericDatumReader.java:247)
    at org.apache.avro.generic.GenericDatumReader.readWithoutConversion(GenericDatumReader.java:179)
    at org.apache.avro.generic.GenericDatumReader.read(GenericDatumReader.java:160)
    at org.apache.avro.generic.GenericDatumReader.read(GenericDatumReader.java:153)
    at org.akhq.modules.AvroSerializer.fromJsonToAvro(AvroSerializer.java:63)
    at org.akhq.modules.AvroSerializer.toAvro(AvroSerializer.java:48)
    at org.akhq.repositories.RecordRepository.produce(RecordRepository.java:528)
    at org.akhq.controllers.TopicController.produce(TopicController.java:141)
    at org.akhq.controllers.$TopicControllerDefinition$$exec4.invokeInternal(Unknown Source)

Could you please help to understand what is wrong ?

Regards


Solution

  • I think you need a JSON like below so that the type is explicitly called out for the union fields:

    {
      "type": "insert",
      "dictionnaire": null,
      "codification": {
        "codificationType": {
            "refentCodifStdId": {
                "string": "8"
            },
            "refentDictId":  {
                "string": "2"
            },
            "refentCodifStdCode":  {
                "string": "AAAA"
            },
            "refentCodifStdLibCrt": {
                "string": "BBBB"
            },
            "refentCodifStdPere": {
                "string": "2"
            }
         }
      }
    }