6.824——实验一Part II: Single-worker word count

这部分很简单,前面我们是调用的代码已经写好的mapF函数和reduceF,所以这一部分就相当于要我们自己实现自定义的map和reduce函数,map函数用来生成键值对序列,reduce函数用来统计词频。

  • map函数

    func mapF(filename string, contents string) []mapreduce.KeyValue {
    	// Your code here (Part II).
    	words := strings.FieldsFunc(contents, func(r rune) bool {
    		return !unicode.IsLetter(r)
    	})
    	res := []mapreduce.KeyValue{}
    	for _, w := range words {
    		res = append(res, mapreduce.KeyValue{w, ""})
    	}
    	return res
    }
    
  • reduce函数

    return strconv.Itoa(len(values))
    
发布了69 篇原创文章 · 获赞 10 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/JustKian/article/details/100921743