Hadoop 1.x的Shuffle源码分析之3

2024-06-11 09:58
文章标签 分析 源码 hadoop shuffle

本文主要是介绍Hadoop 1.x的Shuffle源码分析之3,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

shuffle有两种,一种是在内存存储数据,另一种是在本地文件存储数据,两者几乎一致。


以本地文件进行shuffle的过程为例:

mapOutput = shuffleToDisk(mapOutputLoc, input, filename, compressedLength)

shuffleToDisk函数如下:

private MapOutput shuffleToDisk(MapOutputLocation mapOutputLoc,InputStream input,Path filename,long mapOutputLength) throws IOException {
        // Find out a suitable location for the output on local-filesystem
        //在本地文件系统做输出,输出文件的pathPath localFilename = lDirAlloc.getLocalPathForWrite(filename.toUri().getPath(), mapOutputLength, conf);
        //创建Map输出MapOutput mapOutput = new MapOutput(mapOutputLoc.getTaskId(), mapOutputLoc.getTaskAttemptId(), conf, localFileSys.makeQualified(localFilename), mapOutputLength);// Copy data to local-disk
        //从input读取数据,写入到本地文件,这个input是http连接创建的流式输入OutputStream output = null;long bytesRead = 0;try {output = rfs.create(localFilename);byte[] buf = new byte[64 * 1024];int n = -1;try {n = input.read(buf, 0, buf.length);} catch (IOException ioe) {readError = true;throw ioe;}while (n > 0) {bytesRead += n;shuffleClientMetrics.inputBytes(n);output.write(buf, 0, n);// indicate we're making progressreporter.progress();try {n = input.read(buf, 0, buf.length);} catch (IOException ioe) {readError = true;throw ioe;}}LOG.info("Read " + bytesRead + " bytes from map-output for " +mapOutputLoc.getTaskAttemptId());
          //正常取完数据,关闭。output.close();input.close();} catch (IOException ioe) {LOG.info("Failed to shuffle from " + mapOutputLoc.getTaskAttemptId(), ioe);// Discard the map-output
          try {mapOutput.discard();} catch (IOException ignored) {LOG.info("Failed to discard map-output from " + mapOutputLoc.getTaskAttemptId(), ignored);}mapOutput = null;// Close the streamsIOUtils.cleanup(LOG, input, output);// Re-throwthrow ioe;}// Sanity check
        //检查读取是否正常if (bytesRead != mapOutputLength) {try {mapOutput.discard();} catch (Exception ioe) {// IGNORED because we are cleaning upLOG.info("Failed to discard map-output from " + mapOutputLoc.getTaskAttemptId(), ioe);} catch (Throwable t) {String msg = getTaskID() + " : Failed in shuffle to disk :" + StringUtils.stringifyException(t);reportFatalError(getTaskID(), t, msg);}mapOutput = null;throw new IOException("Incomplete map output received for " +mapOutputLoc.getTaskAttemptId() + " from " +mapOutputLoc.getOutputLocation() + " (" + bytesRead + " instead of " + mapOutputLength + ")");}return mapOutput;}

所以说,这一段shuffle的本质就是,从http的输入流读取数据,然后存放在本地文件系统的磁盘文件,写完之后,把taskId, jobid,本地文件名等等诸多参数放在MapOutput对象记录下来,然后返回一个MapOutput对象。


java的代码很直接,没有花花绕的东东,除了略有一点冗长,实在没什么缺点  :)

这篇关于Hadoop 1.x的Shuffle源码分析之3的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1050845

相关文章

Python实现无痛修改第三方库源码的方法详解

《Python实现无痛修改第三方库源码的方法详解》很多时候,我们下载的第三方库是不会有需求不满足的情况,但也有极少的情况,第三方库没有兼顾到需求,本文将介绍几个修改源码的操作,大家可以根据需求进行选择... 目录需求不符合模拟示例 1. 修改源文件2. 继承修改3. 猴子补丁4. 追踪局部变量需求不符合很

Spring事务中@Transactional注解不生效的原因分析与解决

《Spring事务中@Transactional注解不生效的原因分析与解决》在Spring框架中,@Transactional注解是管理数据库事务的核心方式,本文将深入分析事务自调用的底层原理,解释为... 目录1. 引言2. 事务自调用问题重现2.1 示例代码2.2 问题现象3. 为什么事务自调用会失效3

找不到Anaconda prompt终端的原因分析及解决方案

《找不到Anacondaprompt终端的原因分析及解决方案》因为anaconda还没有初始化,在安装anaconda的过程中,有一行是否要添加anaconda到菜单目录中,由于没有勾选,导致没有菜... 目录问题原因问http://www.chinasem.cn题解决安装了 Anaconda 却找不到 An

Spring定时任务只执行一次的原因分析与解决方案

《Spring定时任务只执行一次的原因分析与解决方案》在使用Spring的@Scheduled定时任务时,你是否遇到过任务只执行一次,后续不再触发的情况?这种情况可能由多种原因导致,如未启用调度、线程... 目录1. 问题背景2. Spring定时任务的基本用法3. 为什么定时任务只执行一次?3.1 未启用

C++ 各种map特点对比分析

《C++各种map特点对比分析》文章比较了C++中不同类型的map(如std::map,std::unordered_map,std::multimap,std::unordered_multima... 目录特点比较C++ 示例代码 ​​​​​​代码解释特点比较1. std::map底层实现:基于红黑

Spring、Spring Boot、Spring Cloud 的区别与联系分析

《Spring、SpringBoot、SpringCloud的区别与联系分析》Spring、SpringBoot和SpringCloud是Java开发中常用的框架,分别针对企业级应用开发、快速开... 目录1. Spring 框架2. Spring Boot3. Spring Cloud总结1. Sprin

Spring 中 BeanFactoryPostProcessor 的作用和示例源码分析

《Spring中BeanFactoryPostProcessor的作用和示例源码分析》Spring的BeanFactoryPostProcessor是容器初始化的扩展接口,允许在Bean实例化前... 目录一、概览1. 核心定位2. 核心功能详解3. 关键特性二、Spring 内置的 BeanFactory

MyBatis-Plus中Service接口的lambdaUpdate用法及实例分析

《MyBatis-Plus中Service接口的lambdaUpdate用法及实例分析》本文将详细讲解MyBatis-Plus中的lambdaUpdate用法,并提供丰富的案例来帮助读者更好地理解和应... 目录深入探索MyBATis-Plus中Service接口的lambdaUpdate用法及示例案例背景

MyBatis-Plus中静态工具Db的多种用法及实例分析

《MyBatis-Plus中静态工具Db的多种用法及实例分析》本文将详细讲解MyBatis-Plus中静态工具Db的各种用法,并结合具体案例进行演示和说明,具有很好的参考价值,希望对大家有所帮助,如有... 目录MyBATis-Plus中静态工具Db的多种用法及实例案例背景使用静态工具Db进行数据库操作插入

Go使用pprof进行CPU,内存和阻塞情况分析

《Go使用pprof进行CPU,内存和阻塞情况分析》Go语言提供了强大的pprof工具,用于分析CPU、内存、Goroutine阻塞等性能问题,帮助开发者优化程序,提高运行效率,下面我们就来深入了解下... 目录1. pprof 介绍2. 快速上手:启用 pprof3. CPU Profiling:分析 C