-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathrunner.go
141 lines (121 loc) · 3.43 KB
/
runner.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
package crunch
import (
"bufio"
"flag"
"io"
"io/ioutil"
"log"
"os"
"path"
"runtime/pprof"
"strings"
)
type Runner struct {
Opts CrunchOpts
}
func readFile(path string, defaultContent string) string {
if path != "" {
content, err := ioutil.ReadFile(path)
if err != nil {
log.Fatalf("Cannot find hive template: %s.\nError: %s", path, err)
}
return string(content)
}
return defaultContent
}
func startCPUProfileIfEnabled(path string) {
if path != "" {
f, err := os.Create(path)
if err != nil {
log.Fatal(err)
}
pprof.StartCPUProfile(f)
}
}
func NewRunner() *Runner {
return &Runner{Opts: CrunchOpts{}}
}
func NewRunnerWithOpts(opts CrunchOpts) *Runner {
return &Runner{Opts: opts}
}
func (self *Runner) Flags() {
flag.StringVar(&self.Opts.CpuProfile, "crunch.cpuprofile", "", "Turn on CPU profiling and write to the specified file.")
flag.StringVar(&self.Opts.PigTemplateFile, "crunch.pigtemplate", "", "Custom Pig template for stub generation.")
flag.StringVar(&self.Opts.HiveTemplateFile, "crunch.hivetemplate", "", "Custom Hive template for stub generation.")
flag.StringVar(&self.Opts.StubsOutPath, "crunch.stubs", "", "Generate stubs and output to given path, and exit.")
flag.BoolVar(&self.Opts.Investigate, "crunch.investigate", false, "Investigation mode - easy output to see processing result.")
}
func (self *Runner) HandleCliWithFlags(row *Row) bool {
self.Flags()
flag.Parse()
return self.HandleCli(row)
}
func (self *Runner) HandleCli(row *Row) bool {
if self.Opts.StubsOutPath != "" {
self.GenerateStubs(row)
return false
}
return true
}
func (self *Runner) GenerateStubs(row *Row) {
hiveTempl := readFile(self.Opts.HiveTemplateFile, TMPL_HIVE)
pigTempl := readFile(self.Opts.PigTemplateFile, TMPL_PIG)
outPath := "."
if self.Opts.StubsOutPath != "" {
outPath = self.Opts.StubsOutPath
}
schema := NewSchema()
hiveTempl = strings.Replace(hiveTempl, "%%schema%%", schema.Hive(row), -1)
pigTempl = strings.Replace(pigTempl, "%%schema%%", schema.Pig(row), -1)
pigTempl = strings.Replace(pigTempl, "%%process%%", path.Base(os.Args[0]), -1)
pigout := path.Join(outPath, "crunch.pig")
ioutil.WriteFile(pigout, []byte(pigTempl), 0666)
log.Printf("Generated: %s", pigout)
hiveout := path.Join(outPath, "crunch.hql")
log.Printf("Generated: %s", hiveout)
ioutil.WriteFile(hiveout, []byte(hiveTempl), 0666)
}
func (self *Runner) JsonRowProcessor(row *Row, in io.Reader, out io.Writer) {
startCPUProfileIfEnabled(self.Opts.CpuProfile)
if self.Opts.CpuProfile != "" {
defer pprof.StopCPUProfile()
}
transform := NewTransformer()
var writer DataWriter
writer = NewTsvWriter(out, row)
if self.Opts.Investigate {
writer = NewInvestigateWriter(out, row)
}
stdin := bufio.NewReader(in)
for {
line, err := stdin.ReadString('\n')
if err != nil {
return
}
data := transform.FromJson(line)
reader := NewMapReader(data)
row.Write(reader, writer)
}
}
func (self *Runner) CsvRowProcessor(row *Row, in io.Reader, out io.Writer) {
startCPUProfileIfEnabled(self.Opts.CpuProfile)
if self.Opts.CpuProfile != "" {
defer pprof.StopCPUProfile()
}
transform := NewTransformer()
var writer DataWriter
writer = NewTsvWriter(out, row)
if self.Opts.Investigate {
writer = NewInvestigateWriter(out, row)
}
stdin := bufio.NewReader(in)
for {
line, err := stdin.ReadString('\n')
if err != nil {
return
}
data := transform.FromCsv(line)
reader := NewArrayReader(data)
row.Write(reader, writer)
}
}