-
Notifications
You must be signed in to change notification settings - Fork 4
/
DeserializeAvro.java
40 lines (35 loc) · 1.38 KB
/
DeserializeAvro.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package com.politrons.avro;
import org.apache.avro.file.DataFileReader;
import org.apache.avro.io.DatumReader;
import org.apache.avro.io.Decoder;
import org.apache.avro.io.DecoderFactory;
import org.apache.avro.specific.SpecificDatumReader;
import java.io.File;
import java.io.IOException;
public class DeserializeAvro {
public static void fromFile() {
try {
File avroOutput = new File("avro-person.avro");
DatumReader<AvroPerson> bdPersonDatumReader = new SpecificDatumReader(AvroPerson.class);
DataFileReader<AvroPerson> dataFileReader = new DataFileReader<>(avroOutput, bdPersonDatumReader);
AvroPerson p = null;
while (dataFileReader.hasNext()) {
p = dataFileReader.next(p);
System.out.println(p);
}
} catch (IOException e) {
System.out.println("Error reading Avro");
}
}
public static AvroPerson fromByteArray(byte[] avroPersonData) {
AvroPerson avroPerson = null;
try {
DatumReader<AvroPerson> personReader = new SpecificDatumReader<>(AvroPerson.class);
Decoder binaryDecoder = DecoderFactory.get().binaryDecoder(avroPersonData, null);
avroPerson = personReader.read(null, binaryDecoder);
} catch (IOException e) {
e.printStackTrace();
}
return avroPerson;
}
}