go-zero/doc/mapreduce.md
2020-07-26 17:09:05 +08:00

29 lines
960 B
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# mapreduce用法
## Map
> channel是Map的返回值
由于Map是个并发操作如果不用range或drain的方式那么在使用返回值的时候可能Map里面的代码还在读写这个返回值可能导致数据不全或者`concurrent read write错误`
* 如果需要收集Map生成的结果那么使用如下方式
```
for v := range channel {
// v is with type interface{}
}
```
* 如果不需要收集结果那么就需要显式的调用mapreduce.Drain
```
mapreduce.Drain(channel)
```
## MapReduce
* mapper和reducer方法里可以调用cancel调用了cancel之后返回值会是`nil, false`
* mapper里面如果有item不写入writer那么这个item就不会被reduce收集
* mapper里面如果有处理item时panic那么这个item也不会被reduce收集
* reduce是单线程所有mapper出来的结果在这里串行处理
* reduce里面不写writer或者panic会导致返回`nil, false`