Java 使用apacheavro反射
Avro序列化在Hadoop用户中很流行,但是很难找到示例 有人能帮我处理这个示例代码吗?我最感兴趣的是使用reflectapi读/写文件,以及使用Union和Null注释Java 使用apacheavro反射,java,reflection,avro,Java,Reflection,Avro,Avro序列化在Hadoop用户中很流行,但是很难找到示例 有人能帮我处理这个示例代码吗?我最感兴趣的是使用reflectapi读/写文件,以及使用Union和Null注释 public class Reflect { public class Packet { int cost; @Nullable TimeStamp stamp; public Packet(int cost, TimeStamp stamp){
public class Reflect {
public class Packet {
int cost;
@Nullable TimeStamp stamp;
public Packet(int cost, TimeStamp stamp){
this.cost = cost;
this.stamp = stamp;
}
}
public class TimeStamp {
int hour = 0;
int second = 0;
public TimeStamp(int hour, int second){
this.hour = hour;
this.second = second;
}
}
public static void main(String[] args) throws IOException {
TimeStamp stamp;
Packet packet;
stamp = new TimeStamp(12, 34);
packet = new Packet(9, stamp);
write(file, packet);
packet = new Packet(8, null);
write(file, packet);
file.close();
// open file to read.
packet = read(file);
packet = read(file);
}
}
下面是上述程序的一个版本 这也会对文件进行压缩
import java.io.File;
import org.apache.avro.Schema;
import org.apache.avro.file.DataFileWriter;
import org.apache.avro.file.DataFileReader;
import org.apache.avro.file.CodecFactory;
import org.apache.avro.io.DatumWriter;
import org.apache.avro.io.DatumReader;
import org.apache.avro.reflect.ReflectData;
import org.apache.avro.reflect.ReflectDatumWriter;
import org.apache.avro.reflect.ReflectDatumReader;
import org.apache.avro.reflect.Nullable;
public class Reflect {
public static class Packet {
int cost;
@Nullable TimeStamp stamp;
public Packet() {} // required to read
public Packet(int cost, TimeStamp stamp){
this.cost = cost;
this.stamp = stamp;
}
}
public static class TimeStamp {
int hour = 0;
int second = 0;
public TimeStamp() {} // required to read
public TimeStamp(int hour, int second){
this.hour = hour;
this.second = second;
}
}
public static void main(String[] args) throws Exception {
// one argument: a file name
File file = new File(args[0]);
// get the reflected schema for packets
Schema schema = ReflectData.get().getSchema(Packet.class);
// create a file of packets
DatumWriter<Packet> writer = new ReflectDatumWriter<Packet>(Packet.class);
DataFileWriter<Packet> out = new DataFileWriter<Packet>(writer)
.setCodec(CodecFactory.deflateCodec(9))
.create(schema, file);
// write 100 packets to the file, odds with null timestamp
for (int i = 0; i < 100; i++) {
out.append(new Packet(i, (i%2==0) ? new TimeStamp(12, i) : null));
}
// close the output file
out.close();
// open a file of packets
DatumReader<Packet> reader = new ReflectDatumReader<Packet>(Packet.class);
DataFileReader<Packet> in = new DataFileReader<Packet>(file, reader);
// read 100 packets from the file & print them as JSON
for (Packet packet : in) {
System.out.println(ReflectData.get().toString(packet));
}
// close the input file
in.close();
}
}
导入java.io.File;
导入org.apache.avro.Schema;
导入org.apache.avro.file.DataFileWriter;
导入org.apache.avro.file.DataFileReader;
导入org.apache.avro.file.CodecFactory;
导入org.apache.avro.io.DatumWriter;
导入org.apache.avro.io.DatumReader;
导入org.apache.avro.reflect.ReflectData;
导入org.apache.avro.reflect.ReflectDatumWriter;
导入org.apache.avro.reflect.ReflectDatumReader;
导入org.apache.avro.reflect.Nullable;
公开课反映{
公共静态类包{
国际成本;
@可空时间戳;
公共数据包(){}//需要读取
公共数据包(整数成本、时间戳){
成本=成本;
这个.邮票=邮票;
}
}
公共静态类时间戳{
整小时=0;
int秒=0;
公共时间戳(){}//需要读取
公共时间戳(整数小时,整数秒){
这个小时=小时;
这个秒=秒;
}
}
公共静态void main(字符串[]args)引发异常{
//一个参数:文件名
File File=新文件(args[0]);
//获取包的反射模式
Schema Schema=ReflectData.get().getSchema(Packet.class);
//创建一个包文件
DatumWriter=新的反射DatumWriter(Packet.class);
DataFileWriter out=新的DataFileWriter(writer)
.setCodec(CodecFactory.deflateCodec(9))
.create(模式、文件);
//将100个数据包写入文件,时间戳为空
对于(int i=0;i<100;i++){
append(新数据包(i,(i%2==0)?新时间戳(12,i):null);
}
//关闭输出文件
out.close();
//打开一个包文件
DatumReader=新的反射DatumReader(Packet.class);
DataFileReader in=新的DataFileReader(文件,读取器);
//从文件中读取100个数据包并将其打印为JSON
用于(数据包:in){
System.out.println(ReflectData.get().toString(数据包));
}
//关闭输入文件
in.close();
}
}
这要求所有类都有一个无参数构造函数(或者我错了?)。那么,如果我们想使用外部类(例如,我有一个使用LocalDate的简单案例),该怎么办?