序列化之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