-
Notifications
You must be signed in to change notification settings - Fork 13
/
main.go
115 lines (95 loc) · 2.08 KB
/
main.go
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package main
//go:generate parquetgen -input main.go -type Person -package main
import (
"encoding/json"
"flag"
"log"
"os"
)
var (
rd = flag.String("read", "", "read a parquet file")
)
func main() {
flag.Parse()
if *rd != "" {
read()
} else {
write()
}
}
func write() {
f, err := os.Create("people.parquet")
if err != nil {
log.Fatal(err)
}
defer f.Close()
w, err := NewParquetWriter(f, MaxPageSize(100))
if err != nil {
log.Fatal(err)
}
for i := 0; i < 2000; i++ {
w.Add(newPerson(i))
}
// Every call to w.Write flushes data to disk (because
// f is *os.File) and creates a new RowGroup.
if err := w.Write(); err != nil {
log.Fatal(err)
}
for i := 2000; i < 4000; i++ {
w.Add(newPerson(i))
}
if err := w.Write(); err != nil {
log.Fatal(err)
}
if err := w.Close(); err != nil {
log.Fatal(err)
}
}
func read() {
f, err := os.Open(*rd)
if err != nil {
log.Fatal(err)
}
defer f.Close()
r, err := NewParquetReader(f)
if err != nil {
log.Fatal(err)
}
enc := json.NewEncoder(os.Stdout)
for r.Next() {
var p Person
r.Scan(&p)
enc.Encode(p)
}
if err := r.Error(); err != nil {
log.Fatal(err)
}
}
// Being is split out only to show how embedded structs
// are handled.
type Being struct {
ID int32 `parquet:"id"`
Age *int32 `parquet:"age"`
}
// Hobby is used to demonstrate the use of nested fields.
type Hobby struct {
Name string `parquet:"name"`
Difficulty *int32 `parquet:"difficulty"`
}
// Person is used in this example as the type that is written to a parquet file
type Person struct {
Being
Happiness int64 `parquet:"happiness"`
Sadness *int64 `parquet:"sadness"`
Code *string `parquet:"code"`
Funkiness float32 `parquet:"funkiness"`
Lameness *float32 `parquet:"lameness"`
Keen *bool `parquet:"keen"`
Birthday uint32 `parquet:"birthday"`
Anniversary *uint64 `parquet:"anniversary"`
Difficulty *int32 `parquet:"difficulty"`
Hobby *Hobby `parquet:"hobby"`
Friends []Being `parquet:"friends"`
// Secret will not be part of parquet.
Secret string `parquet:"-"`
}