-
Notifications
You must be signed in to change notification settings - Fork 1
/
Main.java
65 lines (51 loc) · 2.07 KB
/
Main.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package com.guihaojin.demo;
import static org.apache.parquet.hadoop.ParquetFileWriter.Mode.OVERWRITE;
import static org.apache.parquet.hadoop.metadata.CompressionCodecName.SNAPPY;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.avro.reflect.ReflectData;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.parquet.avro.AvroParquetReader;
import org.apache.parquet.avro.AvroParquetWriter;
import org.apache.parquet.hadoop.ParquetReader;
import org.apache.parquet.hadoop.ParquetWriter;
public class Main {
public static void main(String[] args) throws IOException {
List<Team> teams = new ArrayList<>();
Team team1 = new Team("A");
team1.addMember(new Person("Alice", 1));
team1.addMember(new Person("Amy", 3));
Team team2 = new Team("B");
team2.addMember(new Person("Bob", 20));
team2.addMember(new Person("Blare", 14));
team2.addMember(new Person("Beep", 5));
teams.add(team1);
teams.add(team2);
Path dataFile = new Path("/tmp/demo.snappy.parquet");
// Write as Parquet file.
try (ParquetWriter<Team> writer = AvroParquetWriter.<Team>builder(dataFile)
.withSchema(ReflectData.AllowNull.get().getSchema(Team.class))
.withDataModel(ReflectData.get())
.withConf(new Configuration())
.withCompressionCodec(SNAPPY)
.withWriteMode(OVERWRITE)
.build()) {
for (Team team : teams) {
writer.write(team);
}
}
// Read from Parquet file.
try (ParquetReader<Team> reader = AvroParquetReader.<Team>builder(dataFile)
.withDataModel(new ReflectData(Team.class.getClassLoader()))
.disableCompatibility()
.withConf(new Configuration())
.build()) {
Team team;
while ((team = reader.read()) != null) {
System.out.println(team);
}
}
}
}