在 Java 中反序列化 Avro 中的嵌套通用记录 |使用联合时出现异常

Deserialize nested generic record in Avro in Java | getting exception while using union

我有以下嵌套对象类型的 avro 架构。

{   "type" : "record",   "name" : "StackFlow",   "namespace" :
> "com.example.stack.model",   "fields" : [ {
>     "name" : "prtn_id",
>     "type" : "string"   }, {
>     "name" : "wrk_unit_id",
>     "type" : "string"   }, {
>     "name" : "tableDataList",
>     "type" : {
>       "type" : "array",
>       "items" : {
>         "type" : "record",
>         "name" : "TableData",
>         "fields" : [ {
>           "name" : "table_id",
>           "type" : "string"
>         }, {
>           "name" : "record",
>           "type" : [ "null", {
>             "type" : "record",
>             "name" : "Record1",
>             "namespace" : "com.example.stack.model.record1",
>             "fields" : [ {
>               "name" : "key1",
>               "type" : "string"
>             }, {
>               "name" : "key2",
>               "type" : "string"
>             }, {
>               "name" : "key3",
>               "type" : "string"
>             }, {
>               "name" : "key4",
>               "type" : "string"
>             }, {
>               "name" : "key5",
>               "type" : "string"
>             } ]
>           }, {
>             "type" : "record",
>             "name" : "Record2",
>             "namespace" : "com.example.stack.model.record2",
>             "fields" : [ {
>               "name" : "key1",
>               "type" : "string"
>             }, {
>               "name" : "key6",
>               "type" : "string"
>             } ]
>           }, {
>             "type" : "record",
>             "name" : "Record3",
>             "namespace" : "com.example.stack.model.record3",
>             "fields" : [ {
>               "name" : "key1",
>               "type" : "string"
>             }, {
>               "name" : "key8",
>               "type" : "string"
>             }, {
>               "name" : "key9",
>               "type" : "string"
>             }, {
>               "name" : "key10",
>               "type" : "string"
>             }, {
>               "name" : "key11",
>               "type" : "string"
>             } ]
>           } ]
>         } ]
>       }
>     }   } ] }

这就是 maven avro 插件生成 类 的方式。

TableData包含以下两个变量:

   private java.lang.CharSequence table_id;
   private java.lang.Object record;

反序列化逻辑:

    Schema schema = new Schema.Parser().parse(getClass().getResourceAsStream("/Stack_Avro_Schema.avsc"));
    DatumReader<StackFlow> reader = new GenericDatumReader<>(schema);
    Decoder decoder = null;
    try {
        decoder = DecoderFactory.get()
                .jsonDecoder(schema, data);

        return reader.read(null, decoder);
    } catch (IOException e) {
        System.out.println("Deserialization error" + e.getMessage());
    }

我收到以下异常:

嵌套异常是 org.apache.avro.AvroTypeException:未知联合分支键 1]


以下是我正在使用的json:

{
    "prtn_id" : "SOME_ID_1",
    "wrk_unit_id" : "baseCode1",
    "tableDataList" : 
    [
        {
            "table_id" : "table_1",
            "tableRecord" :
                {
                    "key1" : "value",
                    "key2" : "value",
                    "key3" : "value",
                    "key4" : "value",
                    "key5" : "value"
                }
        },
        {
            "table_id" : "table_2",
            "tableRecord" :
                {
                    "key1" : "value",
                    "key6" : "value"
                }
        },
        {
            "table_id" : "table_3",
            "tableRecord" :
                {
                    "key1" : "value",
                    "key8" : "value",
                    "key9" : "value",
                    "key10" : "value",
                    "key11" : "value"
                }
        }
    ]
    
}

我没有给出解串器的完整逻辑吗?非常感谢任何指点。

这可以使用 Kafka Schema 注册表来实现。关闭此线程。