PartII
 
实现单词统计
实现 main/wc.go 两个函数 mapF() 、 reduceF()
单词是任意字母连续序列, 由unicode.IsLetter 决定字母
测试数据 pg-*.txt 放在 ~/6.824/src/main
$ cd 6.824 $ export "GOPATH=$PWD" $ cd "$GOPATH/src/main" $ go run wc.go master sequential pg-*.txt # command-line-arguments ./wc.go:14: missing return at end of function ./wc.go:21: missing return at end of function
mapF()函数 输入文件名和文件内容,输出 mapreduce.KeyValue 类型切片
reduceF() 函数将调用每个 key 和 切片mapreduce.KeyValu ,返回 单个 key 和 总计数
测试命令
$ cd "$GOPATH/src/main"
$ time go run wc.go master sequential pg-*.txt
master: Starting Map/Reduce task wcseq
Merge: read mrtmp.wcseq-res-0
Merge: read mrtmp.wcseq-res-1
Merge: read mrtmp.wcseq-res-2
master: Map/Reduce task completed
2.59user 1.08system 0:02.81elapsed
查看结果文件
$ sort -n -k2 mrtmp.wcseq | tail -10
that: 7871
it: 7987
in: 8415
was: 8578
a: 13382
of: 13536
I: 14296
to: 16079
and: 23612
the: 29748
 
删除多余的中间文件
rm mrtmp.*
提供测试脚本
bash ./test-wc.sh
master@master:~/study/6.824/src/main$ ./test-wc.sh
-bash: ./test-wc.sh: Permission denied
增加执行权限
chmod 764 *sh

wc.go

package main

import (
"fmt"
"mapreduce"
"os"
"strings"
"unicode"
"strconv"
) //
// The map function is called once for each file of input. The first
// argument is the name of the input file, and the second is the
// file's complete contents. You should ignore the input file name,
// and look only at the contents argument. The return value is a slice
// of key/value pairs.
//
func mapF(filename string, contents string) []mapreduce.KeyValue {
// Your code here (Part II). f := func(c rune) bool {
return !unicode.IsLetter(c)
}
rst := make([]mapreduce.KeyValue, ) keys := strings.FieldsFunc(contents, f)
for _, key := range keys {
kv := mapreduce.KeyValue{ Key: key, Value:""}
rst = append(rst, kv)
}
return rst
} //
// The reduce function is called once for each key generated by the
// map tasks, with a list of all the values created for that key by
// any map task.
//
func reduceF(key string, values []string) string {
// Your code here (Part II).
cnt := for _ , val := range values { i , err := strconv.Atoi(val);
if err != nil {
panic(err)
}
cnt += i
} rst := strconv.Itoa(cnt)
return rst
} // Can be run in 3 ways:
// 1) Sequential (e.g., go run wc.go master sequential x1.txt .. xN.txt)
// 2) Master (e.g., go run wc.go master localhost:7777 x1.txt .. xN.txt)
// 3) Worker (e.g., go run wc.go worker localhost:7777 localhost:7778 &)
func main() {
if len(os.Args) < {
fmt.Printf("%s: see usage comments in file\n", os.Args[])
} else if os.Args[] == "master" {
var mr *mapreduce.Master
if os.Args[] == "sequential" {
mr = mapreduce.Sequential("wcseq", os.Args[:], , mapF, reduceF)
} else {
mr = mapreduce.Distributed("wcseq", os.Args[:], , os.Args[])
}
mr.Wait()
} else {
mapreduce.RunWorker(os.Args[], os.Args[], mapF, reduceF, , nil)
}
}
 

LAB1 partII的更多相关文章

  1. MIT 6.824学习笔记4 Lab1

    现在我们准备做第一个作业Lab1啦 wjk大神也在做6.824,可以参考大神的笔记https://github.com/zzzyyyxxxmmm/MIT6824_Distribute_System P ...

  2. 6.828 lab1 bootload

    MIT6.828 lab1地址:http://pdos.csail.mit.edu/6.828/2014/labs/lab1/ 第一个练习,主要是让我们熟悉汇编,嗯,没什么好说的. Part 1: P ...

  3. Machine Learning #Lab1# Linear Regression

    Machine Learning Lab1 打算把Andrew Ng教授的#Machine Learning#相关的6个实验一一实现了贴出来- 预计时间长度战线会拉的比較长(毕竟JOS的7级浮屠还没搞 ...

  4. ucore lab1 bootloader学习笔记

    ---恢复内容开始--- 开机流程回忆 以Intel 80386为例,计算机加电后,CPU从物理地址0xFFFFFFF0(由初始化的CS:EIP确定,此时CS和IP的值分别是0xF000和0xFFF0 ...

  5. LAB1 partV

    partV 创建文档反向索引.word -> document 与 前面做的 单词统计类似,这个是单词与文档位置的映射关系. mapF 文档解析相同,返回信息不同而已. reduceF 返回归约 ...

  6. 6.824 LAB1 环境搭建

    MIT 6.824 LAB1 环境搭建 vmware 虚拟机 linux ubuntu server   安装 go 官方安装步骤: 下载此压缩包并提取到 /usr/local 目录,在 /usr/l ...

  7. 软件测试:lab1.Junit and Eclemma

    软件测试:lab1.Junit and Eclemma Task: Install Junit(4.12), Hamcrest(1.3) with Eclipse Install Eclemma wi ...

  8. MIT 6.824 lab1:mapreduce

    这是 MIT 6.824 课程 lab1 的学习总结,记录我在学习过程中的收获和踩的坑. 我的实验环境是 windows 10,所以对lab的code 做了一些环境上的修改,如果你仅仅对code 感兴 ...

  9. 清华大学OS操作系统实验lab1练习知识点汇总

    lab1知识点汇总 还是有很多问题,但是我觉得我需要在查看更多资料后回来再理解,学这个也学了一周了,看了大量的资料...还是它们自己的80386手册和lab的指导手册觉得最准确,现在我就把这部分知识做 ...

随机推荐

  1. 实践作业4 Web测试(软件评测)

    经过我们小组的讨论之后,我们选择的待检测产品为产品三:学校相关网站. 我们测的是华中科技大学软件学院官方网站和华中科技大学计算机学院官方网站. 我们比较的有: 一.功能缺陷一:网页显示信息不全 英文网 ...

  2. <input > -- radio

    radio 单选按钮的使用.(注意:两个radio的name属性是相同的,才能表现为互斥) </head> <body> <p>这是input标签的用法</p ...

  3. c++ 集合的增删改查,与两集合的合并 缺陷(空间大小不灵活)

    #if 1 #include <iostream> #include <stdlib.h> using namespace std; class List { public: ...

  4. Oracle中计算两个日期时间的差

    --方法1 select floor((sysdate - to_date('2006-09-01 08:00:00', 'yyyy-mm-dd hh24:mi:ss'))) as sDays fro ...

  5. servlet之servlet容器(一)

    1.servlet容器 ·servlet容器为javaweb应用提供运行时环境,负责管理servlet和jsp的生命周期以及管理它们的共享数据 ·servlet容器中的文件目录结构 ·tomcat是一 ...

  6. 【oacle入门】表空间类型

    系统表空间 系统表空间包括SYSTEM和SYSAUX表空间,系统表空间是所有数据库必须且自动创建的,一般存放在Oracle的数据字典表及相应数据. 永久表空间 永久表空间用户保存永久性数据,如系统数据 ...

  7. python学习笔记——(一)基础设置

    python的学习,今天就开始上开发环境Pycharm,这样以后在调试,使用和学习起来就方便很多. 我用的是JetBrains PyCharm Community Edition 2018.1.1 x ...

  8. ubuntu apt 安装 mpv

    安装 curl -s https://non-gnu.uvt.nl/debian/uvt_key.gpg | sudo apt-key add - sudo add-apt-repository &q ...

  9. 阿里云物联网套件(iot)设备间通信(M2M)在web端的实践

    之前通过nodejs连接到阿里云物联网mqtt,后又用浏览器连接,总结一下: 由于项目是SPA,使用webpack,关键代码: 同样使用mqtt.js之前先install: npm install - ...

  10. 开发Canvas 绘画应用(四):实现拖拽绘画

    在开发Canvas绘画应用(三):实现对照绘画中,我们实现了视图引导的第一部分,这一篇我们来完成第二部分,即将图片直接拖到画布上进行绘画. ✁ 拖放如何实现? [拖放的基本概念]:创建一个绝对定位的元 ...