-
Notifications
You must be signed in to change notification settings - Fork 293
/
json_schema.go
171 lines (152 loc) · 3.93 KB
/
json_schema.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package main
import (
"log"
"github.com/xitongsys/parquet-go-source/local"
"github.com/xitongsys/parquet-go/parquet"
"github.com/xitongsys/parquet-go/reader"
"github.com/xitongsys/parquet-go/writer"
)
type Student struct {
NameIn string
Age int32
Id int64
Weight float32
Sex bool
Classes []string
Scores map[string][]float32
Ignored string
Friends []struct {
Name string
Id int64
}
Teachers []struct {
Name string
Id int64
}
}
var jsonSchema string = `
{
"Tag": "name=parquet_go_root, repetitiontype=REQUIRED",
"Fields": [
{"Tag": "name=name, inname=NameIn, type=BYTE_ARRAY, convertedtype=UTF8, repetitiontype=REQUIRED"},
{"Tag": "name=age, inname=Age, type=INT32, repetitiontype=REQUIRED"},
{"Tag": "name=id, inname=Id, type=INT64, repetitiontype=REQUIRED"},
{"Tag": "name=weight, inname=Weight, type=FLOAT, repetitiontype=REQUIRED"},
{"Tag": "name=sex, inname=Sex, type=BOOLEAN, repetitiontype=REQUIRED"},
{"Tag": "name=classes, inname=Classes, type=LIST, repetitiontype=REQUIRED",
"Fields": [{"Tag": "name=element, type=BYTE_ARRAY, convertedtype=UTF8, repetitiontype=REQUIRED"}]
},
{
"Tag": "name=scores, inname=Scores, type=MAP, repetitiontype=REQUIRED",
"Fields": [
{"Tag": "name=key, type=BYTE_ARRAY, convertedtype=UTF8, repetitiontype=REQUIRED"},
{"Tag": "name=value, type=LIST, repetitiontype=REQUIRED",
"Fields": [{"Tag": "name=element, type=FLOAT, repetitiontype=REQUIRED"}]
}
]
},
{
"Tag": "name=friends, inname=Friends, type=LIST, repetitiontype=REQUIRED",
"Fields": [
{"Tag": "name=element, repetitiontype=REQUIRED",
"Fields": [
{"Tag": "name=name, inname=Name, type=BYTE_ARRAY, convertedtype=UTF8, repetitiontype=REQUIRED"},
{"Tag": "name=id, inname=Id, type=INT64, repetitiontype=REQUIRED"}
]}
]
},
{
"Tag": "name=teachers, inname=Teachers, repetitiontype=REPEATED",
"Fields": [
{"Tag": "name=name, inname=Name, type=BYTE_ARRAY, convertedtype=UTF8, repetitiontype=REQUIRED"},
{"Tag": "name=id, inname=Id, type=INT64, repetitiontype=REQUIRED"}
]
}
]
}
`
func main() {
var err error
fw, err := local.NewLocalFileWriter("json_schema.parquet")
if err != nil {
log.Println("Can't create local file", err)
return
}
//write
pw, err := writer.NewParquetWriter(fw, jsonSchema, 4)
if err != nil {
log.Println("Can't create parquet writer", err)
return
}
pw.RowGroupSize = 128 * 1024 * 1024 //128M
pw.CompressionType = parquet.CompressionCodec_SNAPPY
num := 10
for i := 0; i < num; i++ {
stu := Student{
NameIn: "StudentName",
Age: int32(20 + i%5),
Id: int64(i),
Weight: float32(50.0 + float32(i)*0.1),
Sex: bool(i%2 == 0),
Classes: []string{"Math", "Physics"},
Scores: map[string][]float32{
"Math": []float32{89.5, 99.4},
"Physics": []float32{100.0, 95.3},
},
Friends: []struct {
Name string
Id int64
}{
struct {
Name string
Id int64
}{
Name: "Jack",
Id: 01,
},
},
Teachers: []struct {
Name string
Id int64
}{
struct {
Name string
Id int64
}{
Name: "Tom",
Id: 02,
},
},
}
if err = pw.Write(stu); err != nil {
log.Println("Write error", err)
}
}
if err = pw.WriteStop(); err != nil {
log.Println("WriteStop error", err)
return
}
log.Println("Write Finished")
fw.Close()
///read
fr, err := local.NewLocalFileReader("json_schema.parquet")
if err != nil {
log.Println("Can't open file")
return
}
pr, err := reader.NewParquetReader(fr, jsonSchema, 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()
}