maven实现avro序列化

一、新建maven工程

二、导入pom依赖、插件

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
    </dependency>
    <dependency>
      <groupId>org.apache.avro</groupId>
      <artifactId>avro</artifactId>
      <version>1.8.2</version>
    </dependency>
    <dependency>
      <groupId>org.apache.avro</groupId>
      <artifactId>avro-tools</artifactId>
      <version>1.8.2</version>
    </dependency>
    <dependency>
      <groupId>org.apache.avro</groupId>
      <artifactId>avro-maven-plugin</artifactId>
      <version>1.8.2</version>
    </dependency>
    <dependency>
      <groupId>org.apache.avro</groupId>
      <artifactId>avro-compiler</artifactId>
      <version>1.8.2</version>
    </dependency>
    <dependency>
      <groupId>org.apache.avro</groupId>
      <artifactId>avro-ipc</artifactId>
      <version>1.8.2</version>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.1</version>
        <configuration>
          <!-- 一般而言,target与source是保持一致的,但是,有时候为了让程序能在其他版本的jdk中运行(对于低版本目标jdk,源代码中不能使用低版本jdk中不支持的语法),会存在target不同于source的情况 -->
          <source>1.8</source> <!-- 源代码使用的JDK版本 -->
          <target>1.8</target> <!-- 需要生成的目标class文件的编译版本 -->
          <encoding>UTF-8</encoding><!-- 字符集编码 -->
        </configuration>

      </plugin>
      <plugin>
        <groupId>org.apache.avro</groupId>
        <artifactId>avro-maven-plugin</artifactId>
        <version>1.8.2</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>
三、在${project.basedir}/src/main/avro/下新建User.avsc文件

编辑User.avsc文件
{
"namespace":"com.blb",
"type":"record",
"name":"User",
"fields":
[
{"name":"username","type":"string"},
{"name":"age","type":"int"}
]
}

四、编译

方式1、可以在maven下的Lifesycle里双击compile

方式2、选中pom.xml文件,右键,选择Maven,点击 generate Resources and Update Folders

编译成功后会在Java下生成User类

五、实例

新建测试类

package com.blb;

import static org.junit.Assert.assertTrue;

import org.apache.avro.file.DataFileReader;
import org.apache.avro.file.DataFileWriter;
import org.apache.avro.io.DatumReader;
import org.apache.avro.io.DatumWriter;
import org.apache.avro.specific.SpecificDatumReader;
import org.apache.avro.specific.SpecificDatumWriter;
import org.junit.Test;

import java.io.File;

public class AppTest
{
    //序列化示例
    @Test
    public  void write() throws Exception{

        User u1 = new User("Ken Tompson",194375);
        User u2 = new User("丹尼斯·里奇",194170);

        DatumWriter<User> dw = new SpecificDatumWriter<>(User.class);
        DataFileWriter<User> dfw = new DataFileWriter<>(dw);
        // 创建底层的文件输出通道 
        // schema - 序列化类的模式
        // path - 文件路径
        dfw.create(u1.getSchema(),new File("d://1.txt"));
        // 把对象数据写到文件中
        dfw.append(u1);
        dfw.append(u2);
        dfw.close();
    }


    //反序列化示例
    @Test
    public  void read()throws Exception {
        DatumReader<User> dr = new SpecificDatumReader<>(User.class);
        DataFileReader<User> dfr = new DataFileReader(new File("d://1.txt"), dr);
        //--通过迭代器,迭代出对象数据
        while (dfr.hasNext()) {
            System.out.println(dfr.next());
        }
    }
}

 

posted @ 2020-04-14 13:04  羊想蓝天  阅读(597)  评论(0编辑  收藏  举报