欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

kafka权威指南-Avro的使用总结

程序员文章站 2022-06-14 10:35:36
...
  1. pom.xml中引入Avro相关配置,包括jar包依赖和plugin,示例如下:

<dependencies>
        <dependency>
            <groupId>org.apache.avro</groupId>
            <artifactId>avro</artifactId>
            <version>1.8.1</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.avro</groupId>
                <artifactId>avro-maven-plugin</artifactId>
                <version>1.8.1</version>
                <executions>
                    <execution>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>schema</goal>
                        </goals>
                        <configuration>
                            <sourceDirectory>${project.basedir}/src/main/avro/</sourceDirectory>
                            <outputDirectory>${project.basedir}/src/main/java/</outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

  2.自定义xxxx.avsc后缀结尾的文件,文件格式如下:其中namespace表示解析后的avro文件的包地址,name为解析后的文件名

{
  "namespace": "top.leejay.kafka.kafkaSerializer.avro",
  "type": "record",
  "name": "Weather",
  "doc": "A weather reading.",
  "fields": [
    {
      "name": "station",
      "type": "string",
      "order": "ignore"
    },
    {
      "name": "time",
      "type": "long"
    },
    {
      "name": "temp",
      "type": "int"
    }
  ]
}

3.运行mvn complier编译即可

参考blog:https://blog.****.net/xmeng1/article/details/81049555

Avro文档地址:http://avro.apache.org/docs/1.8.1/gettingstartedjava.html

相关标签: Avro