在旧版本的samples中,使用的是旧的api,mapred下面的MultiFileInputFormat,现在已经过时。

现在推荐使用mapreduce下面的CombineInputFormat来处理。

应用场景:

如果文件数量大,而且单个文件又比较小,若是使用FileInputFormat进行分片,则会根据一个文件生成一个分片,

每个分片又丢给一个maptask,这样maptask处理的内容太小,很快就完成了,利用率不高,因为maptask本身启动

处理所占的时间和资源消耗就超过了信息处理本身所占的时间。推荐一个maptask至少运行一分钟左右。

解决方案:

使用combinefileinputformat来重定义了getSplits方法,这样可以根据我们指定的splitsize(一般是给定为blocksize大小,减少数据传输)

,打包多个小文件到一个inputsplit中去。这样减少了框架生成的maptask的数量。

示例:

例如我的englishwords目录下面有四个文件,使用wordcount示例来跑的话,默认生成4个maptask(不考虑失败又生成的maptask)一个reducetask.

使用旧版的api生成了2个maptask,使用新版的multiplefilewordcount示例生成了一个maptask.

CombineFileInputformat 中可以重写的一个重要方法是:

/**
* Specify the maximum size (in bytes) of each split. Each split is
* approximately equal to the specified size.
*/
protected void setMaxSplitSize(long maxSplitSize) {
this.maxSplitSize = maxSplitSize;
}

示例中又自己写了一个数据结构wordoffset, 是因为原来的只考虑一个文件(一个分片一个文件)中的信息,所以key是offset,value是当前行的值。

现在一个分片中会有多个文件,所以新的数据结构wordoffset就表示哪个文件的offset,这样更明晰。

有时候我们在项目中就需要自己定义maptask的参数。这个结构是需要实现writable接口的(可以序列化)。

使用CombineFileInputFormat最重要的就是实现 Reader的方法,Reader中最重要的就是next().

基本思路其实和单个文件的是类似的, 只是在这种情况下需要处理多个文件的情况,需要有一个index来标志是正在处理哪个文件。

一般在combineReader里面会有如下的代码:

public static class CombineFileLineRecordReader
extends RecordReader<WordOffset, Text> { private long startOffset; //offset of the chunk;
private long end; //end of the chunk;
private long pos; // current pos
private FileSystem fs;
private Path path;
private WordOffset key;
private Text value; private FSDataInputStream fileIn;
private LineReader reader; public CombineFileLineRecordReader(CombineFileSplit split,
TaskAttemptContext context, Integer index) throws IOException { this.path = split.getPath(index);
fs = this.path.getFileSystem(context.getConfiguration());
this.startOffset = split.getOffset(index);
this.end = startOffset + split.getLength(index);
boolean skipFirstLine = false; //open the file
fileIn = fs.open(path);
if (startOffset != 0) {
skipFirstLine = true;
--startOffset;
fileIn.seek(startOffset);
}
reader = new LineReader(fileIn);
if (skipFirstLine) { // skip first line and re-establish "startOffset".
startOffset += reader.readLine(new Text(), 0,
(int)Math.min((long)Integer.MAX_VALUE, end - startOffset));
}
this.pos = startOffset;
}

…………

Sample MultipleFileWordcount CombineFileInputFormat的更多相关文章

  1. Linux下UPnP sample分析

        一.UPnP简介   UPnP(Universal Plug and Play)技术是一种屏蔽各种数字设备的硬件和操作系统的通信协议.它是一种数字网络中间件技术,建立在TCP/IP.HTTP协 ...

  2. cocos2d-x for android配置 & 运行 Sample on Linux OS

    1.从http://www.cocos2d-x.org/download下载稳定版 比如cocos2d-x-2.2 2.解压cocos2d-x-2.2.zip,比如本文将其解压到 /opt 目录下 3 ...

  3. android studio2.2 的Find Sample Code点击没有反应

    1 . 出现的问题描述:           右键点击Find Sample Code后半天没有反应,然后提示 Samples are currently unavailable for :{**** ...

  4. jmeter(四)Sample之http请求

    启动jmeter,建立一个测试计划 这里再次说说怎么安装和启动jmeter吧,昨天下午又被人问到怎样安装和使用,我也是醉了:在我看来,百度能解决百分之八十的问题,特别是基础的问题... 安装:去官网下 ...

  5. jcaptcha sample 制作验证码

    Skip to end of metadata Created by marc antoine garrigue, last modified by Jeremy Waters on Feb 23, ...

  6. Python 对不均衡数据进行Over sample(重抽样)

    需要重采样的数据文件(Libsvm format),如heart_scale +1 1:0.708333 2:1 3:1 4:-0.320755 5:-0.105023 6:-1 7:1 8:-0.4 ...

  7. Basic linux command-with detailed sample

    Here I will list some parameters which people use very ofen, I will attach the output of the command ...

  8. 例子:RSS Reader Sample

    本例演示了Rss xml信息的获取,以及如何使用SyndicationFeed来进行符合Rss规范的xml进行解析. SyndicationFeed 解析完成后 可以得到SyndicationItem ...

  9. 例子:Background Audio Streamer Sample

    The Background Audio Streamer sample demonstrates how to create an app that uses a MediaStreamSource ...

随机推荐

  1. 第一个app.总结

    前记: 最近想整点外快,但是又没啥子技术,唉,学了一下android,想写点游戏啥的,,唉,可惜,美工,UI始终不行,代码也勉勉强强... 不过总的来说也是收获参半吧,也是有一些新的知识学到了嘛,至少 ...

  2. 基于Eclipse的Go语言可视化开发环境

    http://jingyan.baidu.com/article/d7130635032e2f13fdf475b8.html 基于Eclipse的Go语言可视化开发环境 | 浏览:2924 | 更新: ...

  3. 批量导数据之利器-load data[2016-07-11]

    由于天热,中午吃完饭后不再去逛了,感觉这段时间其实也是可以利用起来的,所以决定每天中午积累一些小的知识点.今天中午,先总结一下最近造数据用到手命令,load data. 使用这个命令的起源是因为最近要 ...

  4. 在Android设备上判断设备是否支持摄像头

    private boolean hasCamera(){ boolean hasCamera=false; PackageManager pm=getActivity().getPackageMana ...

  5. poi excel export 乱码

    1. Question Description: ~前端是get方式提交的,参数含有中文“测试” ~导出的excel,文件名正常, 而标题乱码 2. Solution: ~断点发现, 参数接收就是乱码 ...

  6. 设置php下载文件的超时时间

    使用curl 可以使用curl自己实现一个curl_file_get_contents函数 //CURLOPT_FOLLOWLOCATION TRUE 时将会根据服务器返回 HTTP 头中的 &quo ...

  7. ahjesus Unity3D XML注释被编译的问题

    public class XMLStringReader : MonoBehaviour { public string slectedItem; private bool editing = fal ...

  8. Linux里如何查找文件内容

    Linux查找文件内容的常用命令方法. 从文件内容查找匹配指定字符串的行: $ grep "被查找的字符串" 文件名例子:在当前目录里第一级文件夹中寻找包含指定字符串的.in文件g ...

  9. 设置让ASP.NET管道接收所有类型的请求

    在web.config文件添加如下一段配置: <configuration> <system.webServer> <modules runAllManagedModul ...

  10. SQL索引学习-聚集索引

    这篇接着我们的索引学习系列,这次主要来分享一些有关聚集索引的问题.上一篇SQL索引学习-索引结构主要是从一些基础概念上给大家分享了我的理解,没有实例,有朋友就提到了聚集索引的问题,这里列出来一下: 其 ...