Arvo序列化与反序列化

StringPair.avsc 模型

{
    "type":"record",
    "name":"StringPair",
    "doc":"a pair of strings.",
    "fields":[
        {"name":"left","type":"string"},
        {"name":"right","type":"string"}
    ]
}

例子:

package com.peidw;

import org.apache.avro.Schema;
import org.apache.avro.generic.GenericData;
import org.apache.avro.generic.GenericDatumReader;
import org.apache.avro.generic.GenericDatumWriter;
import org.apache.avro.generic.GenericRecord;
import org.apache.avro.io.*;

import java.io.*;

/**
 * Created by peidw on 2018-05-15.
 */
public class AvroParseDemo {
    public void writeAvro(){
        Schema.Parser prser=new Schema.Parser();
        try {
            Schema schema=prser.parse(this.getClass().getResourceAsStream("/StringPair.avsc"));
            //创建记录
            GenericRecord datum=new GenericData.Record(schema);
            datum.put("left","xxx");
            datum.put("right","Cyxxx");
            //输出文件

            ByteArrayOutputStream out=new ByteArrayOutputStream(  );
            DatumWriter<GenericRecord> writer=new GenericDatumWriter<GenericRecord>(schema);
            Encoder encoder = EncoderFactory.get().binaryEncoder(out,null);
            writer.write(datum,encoder);
            encoder.flush();
            out.writeTo(new FileOutputStream("d:\\avro.data"));
            out.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public void readAvro(){
        Schema.Parser prser=new Schema.Parser();
        try {
            Schema schema=prser.parse(this.getClass().getResourceAsStream("/StringPair.avsc"));
            //从文件读
            InputStream in=new FileInputStream("d:\\avro.data");
            DatumReader<GenericRecord> reader=new GenericDatumReader<GenericRecord>(schema);
            byte[] _ary_byte=new byte[in.available()];
            in.read(_ary_byte);
            Decoder decoder = DecoderFactory.get().binaryDecoder(_ary_byte,null);
            GenericRecord result=reader.read(null,decoder);
            System.out.println(result.get("left").toString());
            System.out.println(result.get("right").toString());



        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static void main(String[] args){
        AvroParseDemo apd=new AvroParseDemo();
        //apd.writeAvro();
        apd.readAvro();
    }
}

输出文件这部份也可以使用



序列化文件读入:





阅读更多
个人分类: hbase-hadoop
想对作者说点什么? 我来说一句
相关热词

没有更多推荐了,返回首页

关闭
关闭
关闭