avro和java原生序列化的区别,序列化之protobuf与avro对比(Java)

序列化之protobuf与avro对比(Java)

package avro;

import java.io.File;

import org.apache.avro.Schema;

import org.apache.avro.file.DataFileReader;

import org.apache.avro.file.DataFileWriter;

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.DatumReader;

import org.apache.avro.io.DatumWriter;

/**

* @date 2017年8月13日22:15:32

* @author qiang(upupgo)

*

*/

public class AvroTest2 {

public static void main(String[] args) throws Exception {

String filePath = "D:/TCPLog.avsc";

Schema schema = new Schema.Parser().parse(new File(filePath));

GenericRecord tcpLog = new GenericData.Record(schema);

tcpLog.put("total_byteps", 1024);

tcpLog.put("flow_start_time", 1502415717L);

tcpLog.put("date", 1502415717L);

System.out.println(tcpLog);

// Serialize user1 and tcpLog to disk

File file = new File("D:/avro");

DatumWriter < GenericRecord > datumWriter = new GenericDatumWriter < GenericRecord > (schema);

DataFileWriter < GenericRecord > dataFileWriter = new DataFileWriter < GenericRecord > (datumWriter);

dataFileWriter.create(schema, file);

long timestart = System.currentTimeMillis();

dataFileWriter.append(tcpLog);

dataFileWriter.close();

long timeend = System.currentTimeMillis();

System.out.println(timeend - timestart);

// Deserialize users from disk

DatumReader < GenericRecord > datumReader = new GenericDatumReader < GenericRecord > (schema);

DataFileReader < GenericRecord > dataFileReader = new DataFileReader < >(new File("d:/avro"), datumReader);

GenericRecord tcpLogs = null;

long timestart1 = System.currentTimeMillis();

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.

tcpLogs = dataFileReader.next();

// System.out.println("xx"+tcpLogs);

}

long timeend1 = System.currentTimeMillis();

System.out.println("Deserialize" + (timeend1 - timestart1));

}

}

来源: http://www.cnblogs.com/upupgo/p/7354504.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值