当前位置: 首页 > 知识库问答 >
问题:

将JSON转换为Avro失败:无法将JSON转化为Avro:未知联合分支

柴凌
2023-03-14

我试图使用Kafka rest服务将JSON序列化为Avro对象,将JSON消息发送到Kafka主题,但Kafka rest无法接受JSON消息,错误如下:

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"
                    }
                }
            }
        }
    }

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

共有1个答案

荆学民
2023-03-14

JSON和Avro看起来都不错。

您面临这个问题是因为JSON不符合Avro的JSON编码规范。

因此,如果您相应地转换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"
            }
          }
        }
      }
    }
  }
}
 类似资料:
  • 有一个网站这样做,但我想要一个图书馆或CLI。 谢了!

  • 我为简单的类层次结构自动生成了Avro模式: 看起来是这样的: 此模式适用于使用普通Avro API将数据从JSON读取到。我尝试实现的下一件事是使用将所有此类对象存储到单个拼花文件中: 此代码在第一行失败 难怪AvroSchemaConverter包含以下代码行: 我的模式类型是UNION。非常感谢将此UNION模式映射(合并)到RECORD模式或任何其他建议的任何想法/帮助。 解决方案 1)使

  • 我试图构建一个系统,从Kafka读取json数据(无模式),将其转换为avro并将其推送到s3。 我已经能够使用kstream和KSQL实现json到avro的转换。我想知道使用Kafka Connect的自定义转换是否可以实现同样的效果。 这是我迄今为止所尝试的: 其中avro_schema是avsc文件中指定的架构名称。 我不确定这是否是正确的方法,但我面临的问题是,当调用newRecord(

  • 问题内容: 有没有一种方法可以在不使用Python定义架构的情况下将JSON字符串转换为Avro?还是只有Java才能处理的事情? 问题答案: Apache Avro™1.7.6入门(Python) :

  • 我试图将一个JSON有效负载转换为Avro以发布到一个Kafka主题。但是,当我执行Dataweave转换时,我会得到一个“no type”错误。我不确定是什么导致了这个错误。我最初认为这可能是由于转换不知道入站有效负载上的MIME类型。因此,我已经确保将其设置为,但这并没有什么不同。 Avro模式

  • 问题内容: 我想将Intent的Extras Bundle转换为JSONObject,以便可以将其传递给JavaScript。 有没有快速或最佳的方法来进行此转换?如果不是所有可能的捆绑包都能正常工作,那就没关系了。 问题答案: 您可以用来获取捆绑软件包含的密钥列表。然后,您可以遍历这些键,并将每个键值对添加到中: 请注意,这将需要您抓住一个。 编辑: 有人指出,以前的代码不能很好地处理和键入。如