-
Notifications
You must be signed in to change notification settings - Fork 1
/
write.go
91 lines (76 loc) · 1.77 KB
/
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
package main
import (
"fmt"
"os"
"path"
"strings"
"sync"
"time"
)
func write(target *Target, transformChannel <-chan []map[string]interface{}, wg *sync.WaitGroup) {
defer wg.Done()
if target.Split == nil {
writeAll(target, transformChannel)
} else {
writeSplit(target, transformChannel)
}
}
func writeAll(target *Target, transformChannel <-chan []map[string]interface{}) {
t := time.Now()
var fileName string
if args.out == "" {
fileName = path.Join(defaultOutputDir, args.target, day(t)+".json")
} else {
fileName = args.out
}
err := os.MkdirAll(path.Dir(fileName), 0777)
check(err)
file, err := os.Create(fileName)
check(err)
defer file.Close()
rows := 0
for data := range transformChannel {
for _, obj := range data {
err := jsonWriteln(file, obj)
check(err)
}
rows += len(data)
if !args.quiet {
fmt.Printf("%d rows written\r", rows)
}
}
if !args.quiet {
fmt.Printf("%d rows written in %d seconds\n", rows, int(time.Since(t).Seconds()))
}
}
func writeSplit(target *Target, transformChannel <-chan []map[string]interface{}) {
t := time.Now()
var dir string
if args.out == "" {
dir = path.Join(defaultOutputDir, args.target)
} else {
dir = path.Join(args.out, args.target)
}
if !strings.HasSuffix(dir, "/") {
dir = fmt.Sprintf("%s/", dir)
}
err := os.MkdirAll(path.Dir(dir), 0777)
check(err)
s := newSplitter(target, dir)
rows := 0
for data := range transformChannel {
for _, obj := range data {
file := s.split(obj)
err := jsonWriteln(file, obj)
check(err)
}
rows += len(data)
if !args.quiet {
fmt.Printf("%d rows written, %d files\r", rows, len(s.files))
}
}
s.close()
if !args.quiet {
fmt.Printf("%d rows written to %d files in %d seconds\n", rows, len(s.files), int(time.Since(t).Seconds()))
}
}