-
Notifications
You must be signed in to change notification settings - Fork 293
/
local_nested.go
153 lines (137 loc) · 3.56 KB
/
local_nested.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package main
import (
"fmt"
"log"
"github.com/xitongsys/parquet-go-source/local"
"github.com/xitongsys/parquet-go/reader"
"github.com/xitongsys/parquet-go/writer"
)
type Student struct {
Name string `parquet:"name=name, type=BYTE_ARRAY, convertedtype=UTF8"`
Age int32 `parquet:"name=age, type=INT32"`
Weight *int32 `parquet:"name=weight, type=INT32"`
Classes *map[string][]*Class `parquet:"name=classes, type=MAP, keytype=BYTE_ARRAY, keyconvertedtype=UTF8"`
}
type Class struct {
Name string `parquet:"name=name, type=BYTE_ARRAY, convertedtype=UTF8"`
Id *int32 `parquet:"name=id, type=INT32"`
Required []string `parquet:"name=required, type=LIST, valuetype=BYTE_ARRAY, valueconvertedtype=UTF8"`
Ignored string
}
func (c Class) String() string {
id := "nil"
if c.Id != nil {
id = fmt.Sprintf("%d", *c.Id)
}
res := fmt.Sprintf("{Name:%s, Id:%v, Required:%s}", c.Name, id, fmt.Sprint(c.Required))
return res
}
func (s Student) String() string {
weight := "nil"
if s.Weight != nil {
weight = fmt.Sprintf("%d", *s.Weight)
}
cs := "{"
for key, classes := range *s.Classes {
s := string(key) + ":["
for _, class := range classes {
s += (*class).String() + ","
}
s += "]"
cs += s
}
cs += "}"
res := fmt.Sprintf("{Name:%s, Age:%d, Weight:%s, Classes:%s}", s.Name, s.Age, weight, cs)
return res
}
func writeNested() {
var err error
math01ID := int32(1)
math01 := Class{
Name: "Math1",
Id: &math01ID,
Required: make([]string, 0),
}
math02ID := int32(2)
math02 := Class{
Name: "Math2",
Id: &math02ID,
Required: make([]string, 0),
}
math02.Required = append(math02.Required, "Math01")
physics := Class{
Name: "Physics",
Id: nil,
Required: make([]string, 0),
}
physics.Required = append(physics.Required, "Math01", "Math02")
weight01 := int32(60)
stu01Class := make(map[string][]*Class)
stu01Class["Science1"] = make([]*Class, 0)
stu01Class["Science1"] = append(stu01Class["Science"], &math01, &math02)
stu01Class["Science2"] = make([]*Class, 0)
stu01Class["Science2"] = append(stu01Class["Science"], &math01, &math02)
stu01 := Student{
Name: "zxt",
Age: 18,
Weight: &weight01,
Classes: &stu01Class,
}
stu02Class := make(map[string][]*Class)
stu02Class["Science"] = make([]*Class, 0)
stu02Class["Science"] = append(stu02Class["Science"], &physics)
stu02 := Student{
Name: "tong",
Age: 29,
Weight: nil,
Classes: &stu02Class,
}
stus := make([]Student, 0)
stus = append(stus, stu01, stu02)
//write nested
fw, err := local.NewLocalFileWriter("nested.parquet")
if err != nil {
log.Println("Can't create file", err)
return
}
pw, err := writer.NewParquetWriter(fw, new(Student), 4)
if err != nil {
log.Println("Can't create parquet writer", err)
return
}
for _, stu := range stus {
if err = pw.Write(stu); err != nil {
log.Println("Write error", err)
return
}
}
if err = pw.WriteStop(); err != nil {
log.Println("WriteStop error", err)
}
fw.Close()
log.Println("Write Finished")
//read nested
fr, err := local.NewLocalFileReader("nested.parquet")
if err != nil {
log.Println("Can't open file", err)
return
}
pr, err := reader.NewParquetReader(fr, new(Student), 4)
if err != nil {
log.Println("Can't create parquet reader", err)
return
}
num := int(pr.GetNumRows())
for i := 0; i < num; i++ {
stus := make([]Student, 1)
if err = pr.Read(&stus); err != nil {
log.Println("Read error", err)
}
log.Println(stus)
}
pr.ReadStop()
fr.Close()
}
func main() {
writeNested()
}