Java对象与Avro相互转换

Java对象与Avro相互转换

  • 一、pom依赖和包引用
    • 1. pom依赖
    • 2. 包引用
  • 二、序列化和反序列化
    • 1. java对象序列化为avro字节数组
    • 2. 反序列化
  • 三、完整代码和测试用例

一、pom依赖和包引用

1. pom依赖

<dependencies>
    <dependency>
        <groupId>org.apache.avrogroupId>
        <artifactId>avroartifactId>
        <version>1.9.0version>
    dependency>
    <dependency>
        <groupId>com.alibabagroupId>
        <artifactId>fastjsonartifactId>
        <version>1.2.57version>
    dependency>
    <dependency>
        <groupId>junitgroupId>
        <artifactId>junitartifactId>
        <version>4.12version>
        <scope>testscope>
    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)