Avro serialization is popular with Hadoop users but examples are so hard to find.
Can anyone help me with this sample code? I'm mostly interested in using the Reflect API to read/write into files and to use the Union and Null annotations.
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);
}
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…