单机简易版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)

大家都在看

  • Golang 实现 Redis(6): 实现 pipeline 模式的 redis 客户端

    本文是使用 golang 实现 redis 系列的第六篇, 将介绍如何实现一个 Pipeline 模式的 Redis 客户端。 通常 TCP 客户端的通信模式都是阻塞式的: 客户端…

    Linux 2023年5月28日
    087
  • MSSQL中UPDATE与 INNER JOIN联用的语法结构

    | 0.16分钟 | 267.2字符 | 1、引言&背景 2、解决方案 3、声明与参考资料 | SCscHero | 2022/4/30 PM9:36 | 系列 | 已完成…

    Linux 2023年6月14日
    080
  • [20211215]提示precompute_subquery补充.txt

    [20211215]提示precompute_subquery补充.txt –//前几天测试precompute_subquery,我仔细想一下好像以前看书或者别人的b…

    Linux 2023年6月13日
    076
  • 在cmd中使用doskey来实现alias别名功能

    作为一枚网络工程师,经常就是面对一堆黑框框,也是就是终端。不同操作系统、不同厂家的目录,功能相同但是键入的命令又大不相同,这些差异化容易让脑子混乱。比如华为、思科、H3C、锐捷的设…

    Linux 2023年6月6日
    090
  • CKS考试心得分享

    CKS证书 考试相关 考试报名准备 CKS考试和CKA考试一样,已经开放中国大陆的考试。但区别是CKS目前没有中文题目,考试都是英文题目,唯一区别是CKS中文考试是中文老师监考,仅…

    Linux 2023年6月13日
    0114
  • 多用户共享文件

    假设有三个用户:Tom Jerry Bob.其中,tom和Jerry都属于market部,Bob属于tech部,请在/usr目录下创建两个用户共享的目录market和public,…

    Linux 2023年6月13日
    0103
  • 通过域名查找IP地址

    1,打开cmd 2,敲入命令:nslookup 3,输入域名,如,www.baidu.com 4,得到address即IP地址 Original: https://www.cnbl…

    Linux 2023年6月8日
    099
  • JDK 环境变量配置

    一、环境准备 Windows10 jdk-9.0.1 二、下载合适的JDK版本,安装JDK 三、环境变量配置 1、右键桌面上”我的电脑”>>&#…

    Linux 2023年6月8日
    090
  • 学习一下 Spring Security

    一、Spring Security 1、什么是 Spring Security? (1)基本认识Spring Security 是基于 Spring 框架,用于解决 Web 应用安…

    Linux 2023年6月11日
    098
  • Linux文本处理相关命令

    一、文本处理命令 Linux sort命令用于将文本文件内容加以排序。 sort 可针对文本文件的内容,以行为单位来排序。 语法格式如下: sort [参数]…[文件] 相关参…

    Linux 2023年5月27日
    098
  • 泛微 OA 前台 GetShell 复现

    自行搭建环境: 漏洞路径: /weaver/weaver.common.Ctrl/.css?arg0=com.cloudstore.api.service.Service_Chec…

    Linux 2023年5月28日
    0102
  • 6.19(junit–>在maven和Spring中的使用)

    写文章要不忘初心,今天也要继续努力~ 白盒测试:是一种测试用例设计方法,在这里盒子指的是被测试的软件,白盒,顾名思义即盒子是可视的,你可以清楚盒子内部的东西以及里面是如何运作的,因…

    Linux 2023年6月7日
    0106
  • MySQL主从复制的原理和实现

    垂直扩展: 横向扩展: 复制:使每一个节点都有相同的数据集 MySQL复制的实现:使用二进制日志来实现 提高性能(负载均衡)、 实现读写分离 实现数据备份的功能(实时备份) 高可用…

    Linux 2023年6月7日
    0104
  • Docker镜像管理基础

    Docker镜像管理基础 1、镜像的概念 镜像可以理解为应用程序的集装箱,而docker用来装卸集装箱。 docker镜像含有启动容器所需要的文件系统及其内容,因此,其用于创建并启…

    Linux 2023年6月7日
    0116
  • JAVA中如何将以Date型的数据保存到数据库以Datetime型的字段中

    用Timestamp就行了 recordOuttime是Date类型 import java.sql.Timestamp; Record record = recordMapper…

    Linux 2023年6月8日
    083
  • 输入输出函数

    IDLE name=input(‘输入’) print(name) print函数 print(1,2) print(1,2,sep=",") input函数 …

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