-
Notifications
You must be signed in to change notification settings - Fork 294
/
json_write.go
106 lines (98 loc) · 2.83 KB
/
json_write.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
package main
import (
"fmt"
"log"
"github.com/xitongsys/parquet-go-source/local"
"github.com/xitongsys/parquet-go/writer"
)
func main() {
var err error
md := `
{
"Tag":"name=parquet-go-root",
"Fields":[
{"Tag":"name=name, type=BYTE_ARRAY, convertedtype=UTF8, repetitiontype=OPTIONAL"},
{"Tag":"name=age, type=INT32"},
{"Tag":"name=id, type=INT64"},
{"Tag":"name=weight, type=FLOAT"},
{"Tag":"name=sex, type=BOOLEAN"},
{"Tag":"name=classes, type=LIST",
"Fields":[
{"Tag":"name=element, type=BYTE_ARRAY, convertedtype=UTF8"}
]
},
{"Tag":"name=scores, type=MAP",
"Fields":[
{"Tag":"name=key, type=BYTE_ARRAY, convertedtype=UTF8"},
{"Tag":"name=value, type=LIST",
"Fields":[{"Tag":"name=element, type=FLOAT"}]
}
]
},
{"Tag":"name=friends, type=LIST",
"Fields":[
{"Tag":"name=element",
"Fields":[
{"Tag":"name=name, type=BYTE_ARRAY, convertedtype=UTF8"},
{"Tag":"name=id, type=INT64"}
]
}
]
},
{"Tag":"name=teachers, repetitiontype=REPEATED",
"Fields":[
{"Tag":"name=name, type=BYTE_ARRAY, convertedtype=UTF8"},
{"Tag":"name=id, type=INT64"}
]
}
]
}
`
//write
fw, err := local.NewLocalFileWriter("json.parquet")
if err != nil {
log.Println("Can't create file", err)
return
}
pw, err := writer.NewJSONWriter(md, fw, 4)
if err != nil {
log.Println("Can't create json writer", err)
return
}
num := 10
for i := 0; i < num; i++ {
rec := `
{
"name":"%s",
"age":%d,
"id":%d,
"weight":%f,
"sex":%t,
"ignored":"ignored",
"classes":["Math", "Computer", "English"],
"scores":{
"Math":[99.5, 98.5, 97],
"Computer":[98,97.5],
"English":[100]
},
"friends":[
{"name":"friend1", "id":1},
{"name":"friend2", "id":2}
],
"teachers":[
{"name":"teacher1", "id":1},
{"name":"teacher2", "id":2}
]
}
`
rec = fmt.Sprintf(rec, "Student Name", 20+i%5, i, 50.0+float32(i)*0.1, i%2 == 0)
if err = pw.Write(rec); err != nil {
log.Println("Write error", err)
}
}
if err = pw.WriteStop(); err != nil {
log.Println("WriteStop error", err)
}
log.Println("Write Finished")
fw.Close()
}