You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将AsyncAPI Schema转换为Avro Schema?

如何将AsyncAPI Schema转换为Avro Schema?

我有一个未做Schema校验的Kafka实现,Kafka生产者使用Avro Serializer向Kafka发布Person消息,通过avro-maven-plugin从Avro Schema生成Person POJO类。

我设计的Avro Schema如下:

{  
  "namespace": "com.example.kafka.messageproducer",  
  "type": "record",    
  "name": "PersonMessage",  
  "doc": "Schema for Person validation",  
  "fields": [    
    { "doc": "The firstName of a person.", "name": "firstName","type": "string" },    
    { "doc": "The lastName of a person", "name": "lastName","type": "string"},    
    { "doc": "The gender", "name": "gender", "type": "string"},    
    { "doc": "The age", "name": "age", "type": "int" }  
  ]
}

该Avro Schema已上传至Confluent Schema Registry,消息校验正常。Kafka文档采用AsyncAPI编写,我的目标是将AsyncAPI Schema转换为上述相同的Avro Schema。

我的AsyncAPI文件如下:

{
  "asyncapi": "2.6.0",
  "id": "urn:com:blaise:person",
  "info": {
    "title": "Person AsynchAPI",
    "version": "1.0.0"
  },
  "servers": {
    "non-prod-eu-west-3": {
      "url": "pkc-75m1o.europe-west3.gcp.confluent.cloud:9092",
      "protocol": "kafka"
    }
  },
  "defaultContentType": "application/json",
  "channels": {
    "person": {
      "publish": {
        "message": {
          "name": "personupload",
          "summary": "Inform the world about the new person created",
          "description": "Kafka message related to person\n* publish a message on kafka bus when a new person is created\n",
          "contentType": "application/json",
          "headers": {
            "$ref": "#/components/messageTraits/commonHeaders/headers"
          },
          "payload": {
            "$ref": "#/components/schemas/personPayload"
          }
        }
      }
    }
  },
  "components": {
    "messageTraits": {
      "commonHeaders": {
        "headers": {
          "type": "object",
          "properties": {
            "header1": {
              "type": "string",
              "description": "the first header"
            },
            "header2": {
              "type": "string",
              "description": "the second header"
            },
            "header3": {
              "type": "string",
              "description": "the third header"
            }
          }
        }
      }
    },
    "schemas": {
      "personPayload": {
        "type": "object",
        "properties": {
          "id": {
            "$ref": "#/components/schemas/personId"
          },
          "firstName": {
            "$ref": "#/components/schemas/firstName"
          },
          "lastName": {
            "$ref": "#/components/schemas/lastName"
          },
          "gender": {
            "$ref": "#/components/schemas/gender"
          },
          "age": {
            "$ref": "#/components/schemas/age"
          }
        },
        "required": [
          "firstName",
          "lastName"
        ]
      },
      "personId": {
        "type": "string",
        "format": "uuid",
        "description": "unique uuid to identify a person        \nexamples: \n- \"9acabc86-d5e7-41bd-a210-f10bb1984105\"\n"
      },
      "firstName": {
        "type": "string",
        "description": "The firstName of a person                \nexamples: \n- Robert\n"
      },
      "lastName": {
        "type": "string",
        "description": "The lastName of a person                \nexamples: \n- Lafange\n"
      },
      "gender": {
        "type": "string",
        "description": "The gender of a person           \n",
        "enum": [
          "MALE",
          "FEMALE"
        ]
      },
      "age": {
        "type": "integer",
        "format": "int32",
        "description": "The age of a person\n"
      }
    }
  }
}

我曾使用AsyncAPI Studio将其转换为JSON文件,但结果并非Avro Schema,转换结果如下:

{
  "asyncapi": "2.6.0",
  "id": "urn:com:blaise:person",
  "info": {
    "title": "Person AsynchAPI",
    "version": "1.0.0"   
  },
  "servers": {
    "non-prod-eu-west-3": {
      "url": "pkc-75m1o.europe-west3.gcp.confluent.cloud:9092",
      "protocol": "kafka"
    }
  },
  "defaultContentType": "application/json",
  "channels": {
    "person": {
      "publish": {
        "message": {
          "name": "personupload",
          "summary": "Inform the world about the new person created",
          "description": "Kafka message related to person\n* publish a message on kafka bus when a new person is created\n",
          "contentType": "application/json",
          "headers": {
            "$ref": "#/components/messageTraits/commonHeaders/headers"
          },
          "payload": {
            "$ref": "#/components/schemas/personPayload"
          }
        }
      }
    }
  },
  "components": {
    "messageTraits": {
      "commonHeaders": {
        "headers": {
          "type": "object",
          "properties": {
            "header1": {
              "type": "string",
              "description": "the first header"
            },
            "header2": {
              "type": "string",
              "description": "the second header"
            },
            "header3": {
              "type": "string",
              "description": "the third header"
            }
          }
        }
      }
    },
    "schemas": {
      "personPayload": {
        "type": "object",
        "properties": {
          "id": {
            "$ref": "#/components/schemas/personId"
          },
          "firstName": {
            "$ref": "#/components/schemas/firstName"
          },
          "lastName": {
            "$ref": "#/components/schemas/lastName"
          },
          "gender": {
            "$ref": "#/components/schemas/gender"
          },
          "age": {
            "$ref": "#/components/schemas/age"
          }
        },
        "required": [
          "firstName",
          "lastName"
        ]
      },
      "personId": {
        "type": "string",
        "format": "uuid",
        "description": "unique uuid to identify a person        \nexamples: \n- \"9acabc86-d5e7-41bd-a210-f10bb1984105\"\n"
      },
      "firstName": {
        "type": "string",
        "description": "The firstName of a person                \nexamples: \n- Robert\n"
      },
      "lastName": {
        "type": "string",
        "description": "The lastName of a person                 \nexamples: \n- Lafange\n"
      },
      "gender": {
        "type": "string",
        "description": "The gender of a person           \n",
        "enum": [
          "MALE",
          "FEMALE"
        ]
      },
      "age": {
        "type": "integer",
        "format": "int32",
        "description": "The age of a person\n"
      }
    }
  }
}

请问是否有方法将AsyncAPI YAML文件转换为Avro Schema文件?


内容的提问来源于stack exchange,提问作者blaiso

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.04 15:35:55