【问题标题】:Conversion of JSON to Avro failed: Failed to convert JSON to Avro: Unknown union branch将 JSON 转换为 Avro 失败:无法将 JSON 转换为 Avro:未知联合分支
【发布时间】:2020-02-10 19:31:06
【问题描述】:

我正在尝试使用 Kafka-rest 服务将 JSON 消息发送到 Kafka 主题,以将 JSON 序列化为 Avro 对象,但 JSON 消息未能被 Kafka-rest 接受,并出现以下错误:

Conversion of JSON to Avro failed: Failed to convert JSON to Avro: Unknown union branch postId

我怀疑我使用的 Avro 架构存在问题,因为它是具有可为空字段的嵌套记录类型。

Avro 架构:

{
  "type": "record",
  "name": "ExportRequest",
  "namespace": "com.example.avro.model",
  "fields": [
    {
      "name": "context",
      "type": {
        "type": "map",
        "values": {
          "type": "string",
          "avro.java.string": "String"
        },
        "avro.java.string": "String"
      }
    },
    {
      "name": "exportInfo",
      "type": {
        "type": "record",
        "name": "ExportInfo",
        "fields": [
          {
            "name": "exportId",
            "type": {
              "type": "string",
              "avro.java.string": "String"
            }
          },
          {
            "name": "exportType",
            "type": {
              "type": "string",
              "avro.java.string": "String"
            }
          },
          {
            "name": "exportQuery",
            "type": {
              "type": "record",
              "name": "ExportQuery",
              "fields": [
                {
                  "name": "postExport",
                  "type": [
                    "null",
                    {
                      "type": "record",
                      "name": "PostExport",
                      "fields": [
                        {
                          "name": "postId",
                          "type": {
                            "type": "string",
                            "avro.java.string": "String"
                          }
                        },
                        {
                          "name": "isCommentIncluded",
                          "type": "boolean"
                        }
                      ]
                    }
                  ],
                  "default": null
                },
                {
                  "name": "feedExport",
                  "type": [
                    "null",
                    {
                      "type": "record",
                      "name": "FeedExport",
                      "fields": [
                        {
                          "name": "accounts",
                          "type": {
                            "type": "array",
                            "items": {
                              "type": "string",
                              "avro.java.string": "String"
                            }
                          }
                        },
                        {
                          "name": "recordTypes",
                          "type": {
                            "type": "array",
                            "items": {
                              "type": "string",
                              "avro.java.string": "String"
                            }
                          }
                        },
                        {
                          "name": "actions",
                          "type": {
                            "type": "array",
                            "items": {
                              "type": "string",
                              "avro.java.string": "String"
                            }
                          }
                        },
                        {
                          "name": "contentTypes",
                          "type": {
                            "type": "array",
                            "items": {
                              "type": "string",
                              "avro.java.string": "String"
                            }
                          }
                        },
                        {
                          "name": "startDate",
                          "type": "long"
                        },
                        {
                          "name": "endDate",
                          "type": "long"
                        },
                        {
                          "name": "advancedSearch",
                          "type": [
                            "null",
                            {
                              "type": "record",
                              "name": "AdvancedSearchExport",
                              "fields": [
                                {
                                  "name": "allOfTheWords",
                                  "type": {
                                    "type": "array",
                                    "items": {
                                      "type": "string",
                                      "avro.java.string": "String"
                                    }
                                  }
                                },
                                {
                                  "name": "anyOfTheWords",
                                  "type": {
                                    "type": "array",
                                    "items": {
                                      "type": "string",
                                      "avro.java.string": "String"
                                    }
                                  }
                                },
                                {
                                  "name": "noneOfTheWords",
                                  "type": {
                                    "type": "array",
                                    "items": {
                                      "type": "string",
                                      "avro.java.string": "String"
                                    }
                                  }
                                },
                                {
                                  "name": "hashtags",
                                  "type": {
                                    "type": "array",
                                    "items": {
                                      "type": "string",
                                      "avro.java.string": "String"
                                    }
                                  }
                                },
                                {
                                  "name": "keyword",
                                  "type": {
                                    "type": "string",
                                    "avro.java.string": "String"
                                  }
                                },
                                {
                                  "name": "exactPhrase",
                                  "type": {
                                    "type": "string",
                                    "avro.java.string": "String"
                                  }
                                }
                              ]
                            }
                          ],
                          "default": null
                        }
                      ]
                    }
                  ],
                  "default": null
                }
              ]
            }
          }
        ]
      }
    }
  ]
}

Json 消息:

   {
        "context": {
            "user_id": "1",
            "group_id": "1",
            "organization_id": "1"
        },
        "exportInfo": {
            "exportId": "93874dd7-35d7-4f1f-8cf8-051c606d920b",
            "exportType": "json",
            "exportQuery": {
                "postExport": {
                    "postId": "dd",
                    "isCommentIncluded": false
                },
                "feedExport": {
                    "accounts": [
                        "1677143852565319"
                    ],
                    "recordTypes": [],
                    "actions": [],
                    "contentTypes": [],
                    "startDate": 0,
                    "endDate": 0,
                    "advancedSearch": {
                        "allOfTheWords": [
                            "string"
                        ],
                        "anyOfTheWords": [
                            "string"
                        ],
                        "noneOfTheWords": [
                            "string"
                        ],
                        "hashtags": [
                            "string"
                        ],
                        "keyword": "string",
                        "exactPhrase": "string"
                    }
                }
            }
        }
    }

如果有人能帮助我了解问题所在,我将不胜感激。

【问题讨论】:

    标签: apache-kafka avro confluent-schema-registry


    【解决方案1】:

    你的 JSON 和 Avro 看起来都不错。

    您正面临这个问题,因为 JSON 不符合 Avro's JSON encoding spec

    所以,如果你相应地转换你的 JSON,它会看起来像这样

    {
      "context": {
        "user_id": "1",
        "group_id": "1",
        "organization_id": "1"
      },
      "exportInfo": {
        "exportId": "93874dd7-35d7-4f1f-8cf8-051c606d920b",
        "exportType": "json",
        "exportQuery": {
          "postExport": {
            "com.example.avro.model.PostExport": {
              "postId": "dd",
              "isCommentIncluded": false
            }
          },
          "feedExport": {
            "com.example.avro.model.FeedExport": {
              "accounts": [
                "1677143852565319"
              ],
              "recordTypes": [],
              "actions": [],
              "contentTypes": [],
              "startDate": 0,
              "endDate": 0,
              "advancedSearch": {
                "com.example.avro.model.AdvancedSearchExport": {
                  "allOfTheWords": [
                    "string"
                  ],
                  "anyOfTheWords": [
                    "string"
                  ],
                  "noneOfTheWords": [
                    "string"
                  ],
                  "hashtags": [
                    "string"
                  ],
                  "keyword": "string",
                  "exactPhrase": "string"
                }
              }
            }
          }
        }
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-13
      • 2020-05-20
      • 2020-10-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-07
      相关资源
      最近更新 更多