forked from zeromicro/go-zero
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix mapreduce problem when reducer doesn't write
- Loading branch information
Showing
2 changed files
with
55 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package main | ||
|
||
import ( | ||
"log" | ||
"strconv" | ||
|
||
"github.com/tal-tech/go-zero/core/mr" | ||
) | ||
|
||
type User struct { | ||
Uid int | ||
Name string | ||
} | ||
|
||
func main() { | ||
uids := []int{111, 222, 333} | ||
res, err := mr.MapReduce(func(source chan<- interface{}) { | ||
for _, uid := range uids { | ||
source <- uid | ||
} | ||
}, func(item interface{}, writer mr.Writer, cancel func(error)) { | ||
uid := item.(int) | ||
user := &User{ | ||
Uid: uid, | ||
Name: strconv.Itoa(uid), | ||
} | ||
writer.Write(user) | ||
}, func(pipe <-chan interface{}, writer mr.Writer, cancel func(error)) { | ||
var users []*User | ||
for p := range pipe { | ||
users = append(users, p.(*User)) | ||
} | ||
// missing writer.Write(...), should not panic | ||
}) | ||
if err != nil { | ||
log.Print(err) | ||
return | ||
} | ||
log.Print(len(res.([]*User))) | ||
} |