-
Notifications
You must be signed in to change notification settings - Fork 290
/
Copy pathword_count_in_go.go
48 lines (39 loc) · 1.45 KB
/
word_count_in_go.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
package main
import (
"flag"
"github.com/chrislusf/gleam/distributed"
"github.com/chrislusf/gleam/flow"
"github.com/chrislusf/gleam/gio"
"github.com/chrislusf/gleam/gio/mapper"
"github.com/chrislusf/gleam/gio/reducer"
"github.com/chrislusf/gleam/plugins/file"
)
var (
isDistributed = flag.Bool("distributed", false, "run in distributed or not")
isDockerCluster = flag.Bool("onDocker", false, "run in docker cluster")
verbose = flag.Bool("verbose", false, "print out actual mapper and reducer function names")
filename = flag.String("f", "/etc/passwd", "the file to process")
)
func main() {
if *verbose {
gio.ListRegisteredFunctions()
}
// flag.Parse() // optional, since gio.Init() will call this also.
gio.Init() // If the command line invokes the mapper or reducer, execute it and exit.
f := flow.New("top5 words in passwd").
Read(file.Txt(*filename, 1)).
Map("tokenize", mapper.Tokenize). // invoke the registered "tokenize" mapper function.
Pipe("debugWithPipe", "tee debug.txt").
Map("addOne", mapper.AppendOne). // invoke the registered "addOne" mapper function.
ReduceByKey("sum", reducer.SumInt64). // invoke the registered "sum" reducer function.
Sort("sortBySum", flow.OrderBy(2, true)).
Top("top5", 5, flow.OrderBy(2, false)).
Printlnf("%s\t%d")
if *isDistributed {
f.Run(distributed.Option())
} else if *isDockerCluster {
f.Run(distributed.Option().SetMaster("master:45326"))
} else {
f.Run()
}
}