Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/apache-kafka/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Apache kafka 将JSON转换为Avro失败:将JSON转换为Avro失败:未知联合分支_Apache Kafka_Avro_Confluent Schema Registry - Fatal编程技术网

Apache kafka 将JSON转换为Avro失败:将JSON转换为Avro失败:未知联合分支

Apache kafka 将JSON转换为Avro失败:将JSON转换为Avro失败:未知联合分支,apache-kafka,avro,confluent-schema-registry,Apache Kafka,Avro,Confluent Schema Registry,我试图使用Kafka rest服务将JSON序列化为Avro对象,向Kafka主题发送JSON消息,但Kafka rest无法接受JSON消息,错误如下: Conversion of JSON to Avro failed: Failed to convert JSON to Avro: Unknown union branch postId 我怀疑我使用的Avro模式存在问题,因为它是一种带有可空字段的嵌套记录类型 Avro模式: { "type": "record", "name"

我试图使用Kafka rest服务将JSON序列化为Avro对象,向Kafka主题发送JSON消息,但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"
                    }
                }
            }
        }
    }

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

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

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