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

Java对象与Avro相互转换

程序员文章站 2024-03-16 08:56:46
...

一、pom依赖和包引用

1. pom依赖

<dependencies>
    <dependency>
        <groupId>org.apache.avro</groupId>
        <artifactId>avro</artifactId>
        <version>1.9.0</version>
    </dependency>
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>fastjson</artifactId>
        <version>1.2.57</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>
</dependencies>

2. 包引用

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.avro.Schema;
import org.apache.avro.generic.GenericData;
import org.apache.avro.generic.GenericDatumWriter;
import org.apache.avro.generic.GenericRecord;
import org.apache.avro.io.BinaryDecoder;
import org.apache.avro.io.BinaryEncoder;
import org.apache.avro.io.DecoderFactory;
import org.apache.avro.io.EncoderFactory;
import org.apache.avro.specific.SpecificDatumReader;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Map;

二、序列化和反序列化

1. java对象序列化为avro字节数组

public static byte[] mapToByte(String schemaStr, Map<String, Object> data) {
    try (ByteArrayOutputStream out = new ByteArrayOutputStream(1024 * 2 * 1024)) {
        Schema schema = new Schema.Parser().parse(schemaStr);
        GenericRecord record = new GenericData.Record(schema);
        GenericDatumWriter<GenericRecord> writer = new GenericDatumWriter<>(schema);
        EncoderFactory encoderFactory = EncoderFactory.get();
        BinaryEncoder encoder = encoderFactory.binaryEncoder(out, null);
        writeDataToRecord(schemaStr, record, data);
        writer.write(record, encoder);
        encoder.flush();
        out.flush();
        return out.toByteArray();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

private static void writeDataToRecord(String schemaStr, GenericRecord record, Map<String, Object> data) {
    JSONObject jsonObject = JSONObject.parseObject(schemaStr);
    JSONArray fields = jsonObject.getJSONArray("fields");
    for (int i = 0; i < fields.size(); i++) {
        String name = fields.getJSONObject(i).getString("name");
        record.put(name, data.get(name));
    }
}

2. 反序列化

public static GenericRecord byteToRecord(String schemaStr, byte[] byteData) throws IOException {
    Schema schema = new Schema.Parser().parse(schemaStr);
    GenericRecord record = new GenericData.Record(schema);
    SpecificDatumReader<GenericRecord> reader = new SpecificDatumReader<>(schema);
    BinaryDecoder decoder = DecoderFactory.get().binaryDecoder(byteData, null);
    while (!decoder.isEnd()) {
        reader.read(record, decoder);
    }
    return record;
}

三、完整代码和测试用例

参见我的github

相关标签: java avro