You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
go-zero/example/mapreduce/flatmap/flatmap.go

32 lines
618 B
Go

4 years ago
package main
import (
"fmt"
"zero/core/mr"
4 years ago
)
var (
persons = []string{"john", "mary", "alice", "bob"}
friends = map[string][]string{
"john": {"harry", "hermione", "ron"},
"mary": {"sam", "frodo"},
"alice": {},
"bob": {"jamie", "tyrion", "cersei"},
}
)
func main() {
var allFriends []string
for v := range mr.Map(func(source chan<- interface{}) {
4 years ago
for _, each := range persons {
source <- each
}
}, func(item interface{}, writer mr.Writer) {
4 years ago
writer.Write(friends[item.(string)])
}, mr.WithWorkers(100)) {
4 years ago
allFriends = append(allFriends, v.([]string)...)
}
fmt.Println(allFriends)
}