-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
165 lines (145 loc) · 3.09 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
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
package main
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"github.com/urfave/cli/v2"
"github.com/xitongsys/parquet-go-source/local"
"github.com/xitongsys/parquet-go/reader"
)
func main() {
var file, output, url string
app := &cli.App{
Flags: []cli.Flag{
&cli.StringFlag{
Name: "file",
Value: "file.parquet",
Aliases: []string{"f"},
Usage: "path of the parquet file",
Destination: &file,
},
&cli.StringFlag{
Name: "url",
Aliases: []string{"u"},
Usage: "get parquet file from url",
Destination: &url,
},
&cli.StringFlag{
Name: "output",
Value: "file",
Aliases: []string{"o"},
Usage: "output name of json file",
Destination: &output,
},
},
Commands: []*cli.Command{
{
Name: "readcolumns",
Usage: "add a task to the list",
Action: func(c *cli.Context) error {
if url != "" {
saveFile(url)
file = "dowloaded.parquet"
}
readcolumns(file)
return nil
},
},
{
Name: "tojson",
Usage: "convert parquet file to json",
Action: func(c *cli.Context) error {
if url != "" {
saveFile(url)
file = "dowloaded.parquet"
}
toJSON(file, output)
fmt.Println("JSON file created successfully", c.Args().First())
return nil
},
},
},
}
err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}
func toJSON(path, output string) {
var num int
fr, err := local.NewLocalFileReader(path)
if err != nil {
log.Println("Can't open file", err)
return
}
pr, err := reader.NewParquetReader(fr, nil, 4)
if err != nil {
log.Println("Can't create parquet reader", err)
return
}
num = int(pr.GetNumRows())
res, err := pr.ReadByNumber(num)
if err != nil {
log.Println("Can't read", err)
return
}
file, _ := json.MarshalIndent(res, "", " ")
err = ioutil.WriteFile(output+".json", file, 0644)
if err != nil {
log.Println("Can't Write the file", err)
return
}
}
func saveFile(url string) {
fileURL := url
if err := DownloadFile("dowloaded.parquet", fileURL); err != nil {
panic(err)
}
}
func DownloadFile(filepath string, url string) error {
// Get the data
resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
// Create the file
out, err := os.Create(filepath)
if err != nil {
return err
}
defer out.Close()
// Write the body to file
_, err = io.Copy(out, resp.Body)
return err
}
func readcolumns(path string) {
fr, err := local.NewLocalFileReader(path)
if err != nil {
log.Println("Can't open file", err)
return
}
pr, err := reader.NewParquetReader(fr, nil, 4)
if err != nil {
log.Println("Can't create parquet reader", err)
return
}
res, err := pr.ReadByNumber(2)
if err != nil {
log.Println("Can't read", err)
return
}
file, _ := json.MarshalIndent(res, "", " ")
var result []interface{}
json.Unmarshal([]byte(file), &result)
file2, _ := json.Marshal(result[0])
var result2 map[string]interface{}
json.Unmarshal(file2, &result2)
for key := range result2 {
fmt.Println(key)
}
}