-
Notifications
You must be signed in to change notification settings - Fork 294
/
csv_write.go
68 lines (60 loc) · 1.35 KB
/
csv_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
package main
import (
"fmt"
"log"
"github.com/xitongsys/parquet-go-source/local"
"github.com/xitongsys/parquet-go/writer"
)
func main() {
var err error
md := []string{
"name=Name, type=BYTE_ARRAY, convertedtype=UTF8, encoding=PLAIN_DICTIONARY",
"name=Age, type=INT32",
"name=Id, type=INT64",
"name=Weight, type=FLOAT",
"name=Sex, type=BOOLEAN",
}
//write
fw, err := local.NewLocalFileWriter("csv.parquet")
if err != nil {
log.Println("Can't open file", err)
return
}
pw, err := writer.NewCSVWriter(md, fw, 4)
if err != nil {
log.Println("Can't create csv writer", err)
return
}
num := 10
for i := 0; i < num; i++ {
data := []string{
fmt.Sprintf("%s_%d", "Student Name", i),
fmt.Sprintf("%d", 20+i%5),
fmt.Sprintf("%d", i),
fmt.Sprintf("%f", 50.0+float32(i)*0.1),
fmt.Sprintf("%t", i%2 == 0),
}
rec := make([]*string, len(data))
for j := 0; j < len(data); j++ {
rec[j] = &data[j]
}
if err = pw.WriteString(rec); err != nil {
log.Println("WriteString error", err)
}
data2 := []interface{}{
"Student Name",
int32(20 + i%5),
int64(i),
float32(50.0 + float32(i)*0.1),
i%2 == 0,
}
if err = pw.Write(data2); err != nil {
log.Println("Write error", err)
}
}
if err = pw.WriteStop(); err != nil {
log.Println("WriteStop error", err)
}
log.Println("Write Finished")
fw.Close()
}