package main import ( "strings" "os" "github.com/chrislusf/gleam/flow" "github.com/chrislusf/gleam/gio" ) var ( MapperTokenizer = gio.RegisterMapper(tokenize) MapperAddOne = gio.RegisterMapper(addOne) ReducerSum = gio.RegisterReducer(sum) ) func main(){ gio.Init() // required, place it right after main() starts flow.New().TextFile("/etc/passwd"). Mapper(MapperTokenizer). // invoke the registered "tokenize" mapper function. Mapper(MapperAddOne). // invoke the registered "addOne" mapper function. ReducerBy(ReducerSum). // invoke the registered "sum" reducer function. Sort(flow.OrderBy(2, true)). Fprintf(os.Stdout, "%s %d\n").Run() } func tokenize(row []interface{}) error { line := string(row[0].([]byte)) for _, s := range strings.FieldsFunc(line, func(r rune) bool { return !('A' <= r && r <= 'Z' || 'a' <= r && r <= 'z' || '0' <= r && r <= '9') }) { gio.Emit(s) } return nil } func addOne(row []interface{}) error { word := string(row[0].([]byte)) gio.Emit(word, 1) return nil } func sum(x, y interface{}) (interface{}, error) { return x.(uint64) + y.(uint64), nil }
Thanks,
Pablo
--
You received this message because you are subscribed to a topic in the Google Groups "golang-nuts" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/golang-nuts/MDx44mMa7jE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.