avro使用schema生成java文件

时间:2022-07-23
本文章向大家介绍avro使用schema生成java文件,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

schema文件

{
    "namespace": "com.ric",
    "type": "record",
    "name": "Customer",
    "fields": [
        {"name": "id", "type": "int"},
        {"name": "name",  "type": "string"},
        {"name": "email", "type": ["null", "string"], "default" : "null"},
        {"name":"images","type":[{"type":"string","avro.java.string":"String"},"null"],"default":"null"}
    ]
}

下载avro-tools-1.8.2.jar工具,可以通过maven来下载

    <dependency>
      <groupId>org.apache.avro</groupId>
      <artifactId>avro-tools</artifactId>
      <version>1.8.2</version>
    </dependency>

cmd下执行如下指令,生成schema对应的entity

指令格式:
java -jar /path/to/avro-tools-1.8.0.jar compile schema <schema file> <destination>
最佳实践
java -jar avro-tools-1.8.2.jar compile schema Customer.avsc .