单机简易版mapReduce 实现

go;gutter:true;collapse:false import "fmt" import "6.824/mr" import "plugin" import "os" import "log" import "io/ioutil" import "sort"</p> <p>// for sorting by key.</p> <p>type ByKey []mr.KeyValue</p> <p>// for sorting by key.</p> <p>func (a ByKey) Len() int           { return len(a) } func (a ByKey) Swap(i, j int)      { a[i], a[j] = a[j], a[i] } func (a ByKey) Less(i, j int) bool { return a[i].Key < a[j].Key }</p> <p>func main() { if len(os.Args) < 3 { fmt.Fprintf(os.Stderr, "Usage: mrsequential xxx.so inputfiles...\n" ) os.Exit(1) }</p> <pre><code>mapf, reducef := loadPlugin(os.Args[1]) // // read each input file, // pass it to Map, // accumulate the intermediate Map output. // intermediate := []mr.KeyValue{} for _, filename := range os.Args[2:] { file, err := os.Open(filename) if err != nil { log.Fatalf( "cannot open %v" , filename) } content, err := ioutil.ReadAll(file) if err != nil { log.Fatalf( "cannot read %v" , filename) } file.Close() kva := mapf(filename, string(content)) intermediate = append(intermediate, kva...) } // // a big difference from real MapReduce is that all the // intermediate data is in one place, intermediate[], // rather than being partitioned into NxM buckets. // sort.Sort(ByKey(intermediate)) oname := "mr-out-0" ofile, _ := os.Create(oname) // // call Reduce on each distinct key in intermediate[], // and print the result to mr-out-0. // i := 0 for i < len(intermediate) { j := i + 1 for j < len(intermediate) && intermediate[j].Key == intermediate[i].Key { j++ } values := []string{} for k := i; k < j; k++ { values = append(values, intermediate[k].Value) } output := reducef(intermediate[i].Key, values) // this is the correct format for each line of Reduce output. fmt.Fprintf(ofile, "%v %v\n" , intermediate[i].Key, output) i = j } ofile.Close() </code></pre> <p>}</p> <p>// // load the application Map and Reduce functions // from a plugin file, e.g. ../mrapps/wc.so // func loadPlugin(filename string) ( func (string, string) []mr.KeyValue, func (string, []string) string) { p, err := plugin.Open(filename) if err != nil { log.Fatalf( "cannot load plugin %v" , filename) } xmapf, err := p.Lookup( "Map" ) if err != nil { log.Fatalf( "cannot find Map in %v" , filename) } mapf := xmapf.( func (string, string) []mr.KeyValue) xreducef, err := p.Lookup( "Reduce" ) if err != nil { log.Fatalf( "cannot find Reduce in %v" , filename) } reducef := xreducef.( func (string, []string) string)</p> <pre><code>return mapf, reducef </code></pre> <p>}

摘自 MIT6.824

Original: https://www.cnblogs.com/thotf/p/16413164.html
Author: thotf
Title: 单机简易版mapReduce 实现

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/582256/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

  • 自动化集成:Pipeline整合Docker+K8S

    前言:该系列文章,围绕持续集成:Jenkins+Docker+K8S相关组件,实现自动化管理源码编译、打包、镜像构建、部署等操作; 本篇文章主要描述流水线集成K8S用法。 一、背景…

    Linux 2023年5月27日
    0162
  • linux版powershell中,tab补全,linux外部命令参数名,的模块介绍

    关键字 linux powershell pwsh 补全 complete bash zsh 摘要:linux用户的福音!在linux版powershell中,补全linux外部命…

    Linux 2023年6月14日
    079
  • FinalShell—一体化服务器管理软件(SSH客户端)

    下面附上一些截图和官方连接: 官网:http://www.hostbuf.com/ FinalShell是一体化的的服务器,网络管理软件,不仅是ssh客户端,还是功能强大的开发,运…

    Linux 2023年5月28日
    068
  • docker安装redis

    Redis configuration file example. # Note that in order to read the configuration file, Red…

    Linux 2023年5月28日
    089
  • Linux基础02

    import paramiko class SSHProxy(object): def __init__(self, hostname, port, username, passw…

    Linux 2023年6月7日
    074
  • Ansible Playbook概览

    Ansible playbook 执行需要三步路执行: 1.编写playbook 2.定义主机清单文件 3.设置运行环境,写入配置文件 1.编写playbook Playbook使…

    Linux 2023年6月6日
    073
  • Linux vi/vim

    1.vi/vim快捷键位图 详细可以参考:https://www.runoob.com/w3cnote/all-vim-cheatsheat.html 2.vi/vim的三种模式 …

    Linux 2023年6月8日
    090
  • Linux磁盘管理

    一、磁盘管理 Linux 磁盘管理好坏直接关系到整个系统的性能问题。 Linux 磁盘管理常用的三个命令为 df、 du 和 fdisk。 df(英文全称:disk full):列…

    Linux 2023年5月27日
    089
  • Linux下腾达无线网卡U6的驱动安装

    U6无线网卡是rt8192eu芯片, 腾达驱动只支持到内核4.4左右; 5.0以上的内核就需要自己编译的。 终于找到了一个github修改版的驱动,经过验证运行正常。 https:…

    Linux 2023年6月14日
    075
  • SSH_远程终端

    SSH 远程服务 目的 Windwos 和 Linux 的终端控制系统or传送传送文件, 当然 Linux和Linux 以及 Windwos 和 Windwos 之间的通信都是OK…

    Linux 2023年6月7日
    082
  • 【原创】Linux PCI驱动框架分析(三)

    背 景 Read the fucking source code! –By 鲁迅 A picture is worth a thousand words. &#8211…

    Linux 2023年6月8日
    094
  • 小记:音频格式转化ByPython(下)

    上文中我们已经大致明白了pydub库的使用方法,今天的目标是写个爬虫爬取歌曲信息。 关于网络爬虫,Python的标准库里是有相应的包的,可以直接打开:https://docs.py…

    Linux 2023年6月8日
    097
  • Conky配置(中文备注)

    conkyrc地址:~/.conkyrc 需要注意的是,因为每个人的网卡都不同,所以在网络部分,例如 downspeed wlp0s20f3 ,后面的 wlp0s20f3 每个人都…

    Linux 2023年6月7日
    065
  • redis的间隔性速度慢的问题

    php操作redis,偶尔间歇性很慢.查看redis日志发现:Asynchronous AOF fsync is taking too long (disk is busy?). …

    Linux 2023年5月28日
    074
  • 开放平台架构指南

    1.前言 2010年前,大型社交网站如腾讯QQ、新浪微博都搭建了开放平台。中小型互联网公司接入开放平台,能够获取社交平台的海量用户,有效的降低获客成本,获得社交平台的其他能力。对于…

    Linux 2023年6月6日
    067
  • Centos7 禁用IPV6地址的方法

    404. 抱歉,您访问的资源不存在。 可能是网址有误,或者对应的内容被删除,或者处于私有状态。 代码改变世界,联系邮箱 contact@cnblogs.com 园子的商业化努力-困…

    Linux 2023年6月7日
    071
亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球