avro序列化和反序列化——利用avsc文件方式

主要参考:
https://blog.csdn.net/u013054888/article/details/64444908
上一篇中(https://blog.csdn.net/wild46cat/article/details/80963111),我们在构建序列化和反序列化的时候,我们必须有avro编译出来的类文件,其实我们只要avsc的schema文件就可以进行序列化和反序列化。

序列化:

package com.xueyou.avro.demo;

import org.apache.avro.Schema;
import org.apache.avro.file.DataFileWriter;
import org.apache.avro.generic.GenericData;
import org.apache.avro.generic.GenericDatumWriter;
import org.apache.avro.generic.GenericRecord;
import org.apache.avro.io.DatumWriter;

import java.io.File;

public class App3 {
    public static void main(String[] args) throws Exception {
        Schema schema = new Schema.Parser().parse(new File("./src/main/avro/user.avsc"));
        GenericRecord user1 = new GenericData.Record(schema);
        user1.put("name", "Alyssa");
        user1.put("favorite_number", 256);

        GenericRecord user2 = new GenericData.Record(schema);
        user2.put("name", "Ben");
        user2.put("favorite_number", 7);
        user2.put("favorite_color", "red");

        File file = new File("users2.avro");
        System.out.println(user1);
        System.out.println(user2);
        DatumWriter datumWriter = new GenericDatumWriter(schema);
        DataFileWriter dataFileWriter = new DataFileWriter(datumWriter);
        dataFileWriter.create(schema, file);
        dataFileWriter.append(user1);
        dataFileWriter.append(user2);
        dataFileWriter.close();
    }
}

运行结果:

avro序列化和反序列化——利用avsc文件方式_第1张图片


反序列化:

package com.xueyou.avro.demo;

import org.apache.avro.Schema;
import org.apache.avro.file.DataFileReader;
import org.apache.avro.generic.GenericDatumReader;
import org.apache.avro.generic.GenericRecord;
import org.apache.avro.io.DatumReader;

import java.io.File;

public class App4 {
    public static void main(String[] args) throws Exception {
        Schema schema = new Schema.Parser().parse(new File("./src/main/avro/user.avsc"));
        File file = new File("users2.avro");
        DatumReader datumReader = new GenericDatumReader(schema);
        DataFileReader dataFileReader = new DataFileReader(file, datumReader);

        GenericRecord user = null;

        while (dataFileReader.hasNext()) {
            // Reuse user object by passing it to next(). This saves us from
            // allocating and garbage collecting many objects for files with
            // many items.
            user = dataFileReader.next(user);
            System.out.println(user);
        }
    }
}

运行结果:

avro序列化和反序列化——利用avsc文件方式_第2张图片

你可能感兴趣的:(avro)