MapReduce默认的InputFormat是TextInputFormat,且key是偏移量,value是文本,自定义InputFormat需要实现FileInputFormat,并重写createRecorder方法,如果需要还可以重写isSplitable()来设置是否切片,重写了createRecordReader还需要自定义RecordReader,InputFormat规定了key,value是什么,而RecordReader则是具体的读取逻辑,下面的例子是合并小文件,最终输出的k是文件路径,v是文件二进制字节

1.InputFormat

 /**
* 自定义InputFormat规定读取文件的k,v
* @author tele
*
*/
public class MyInputFormat extends FileInputFormat<NullWritable,BytesWritable>{
/**
* 设置不切片,把小文件作为一个整体
*/
@Override
protected boolean isSplitable(JobContext context, Path filename) {
return false;
} @Override
public RecordReader<NullWritable,BytesWritable> createRecordReader(InputSplit split, TaskAttemptContext context)
throws IOException, InterruptedException {
MyRecordReader recordReader = new MyRecordReader();
recordReader.initialize(split, context);
return recordReader;
}
}

2.RecordReader

 /**
* recordreader用于读取文件内容,输出文件内容即可,文件路径信息保存在split中
* @author tele
*
*/
public class MyRecordReader extends RecordReader<NullWritable,BytesWritable> {
FileSplit split;
BytesWritable value = new BytesWritable();
boolean flag = false;
Configuration conf;
int count = ; /**
* 初始化
*/
@Override
public void initialize(InputSplit split, TaskAttemptContext context) throws IOException, InterruptedException {
this.split = (FileSplit) split;
conf = context.getConfiguration(); conf = context.getConfiguration();
} /**
* 业务逻辑处理,这个方法用来判断是否还有文件内容需要读取,会进入两次,第一次读取内容存入value中,返回true,第二次调用返回false
* 只要返回true,就会调用getCurrentKey().getCurrentValue()把内容返回给map
*
*/
@Override
public boolean nextKeyValue() throws IOException, InterruptedException {
count++;
if(!flag) {
//获取fs
FileSystem fs = FileSystem.get(conf);
//开启流
Path path = this.split.getPath();
FSDataInputStream fsDataInputStream = fs.open(path);
long length = this.split.getLength();
byte[] buf = new byte[(int) length]; //读取
IOUtils.readFully(fsDataInputStream, buf, ,buf.length);
value.set(buf, , buf.length); //关闭流
IOUtils.closeStream(fsDataInputStream);
flag = true;
}else {
flag = false;
}
return flag;
} @Override
public NullWritable getCurrentKey() throws IOException, InterruptedException {
return NullWritable.get();
} @Override
public BytesWritable getCurrentValue() throws IOException, InterruptedException {
return value;
} @Override
public float getProgress() throws IOException, InterruptedException {
return flag?:;
} @Override
public void close() throws IOException { }
}

3.Mapper

 /**
* 把结果输出到SequenceFileOutPutFormat中,输出的key是文件路径,value为文件内容
* @author tele
*
*/
public class InputformatMapper extends Mapper<NullWritable, BytesWritable, Text,BytesWritable/*Text*/> {
Text k = new Text(); @Override
protected void map(NullWritable key, BytesWritable value,
Mapper<NullWritable, BytesWritable, Text, BytesWritable/*Text*/>.Context context)
throws IOException, InterruptedException {
FileSplit split = (FileSplit) context.getInputSplit();
Path path = split.getPath(); k.set(path.toString()); /* String result = new String(value.getBytes(),0,value.getLength());
context.write(k,new Text(result));*/ context.write(k, value);
}
}

4.Driver(由于输出的是字节,需要指定OutputFormat为SequenceFileOutputFormat)

 /**
* 驱动
* @author tele
*
*/
public class InputformatDriver {
public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
//1.获得job实例
Configuration conf = new Configuration();
Job job = Job.getInstance(conf); //2.关联class
job.setJarByClass(InputformatDriver.class);
job.setMapperClass(InputformatMapper.class); //4.设置format
job.setInputFormatClass(MyInputFormat.class);
//使用SequenceFileOutputFormat作为输出格式
job.setOutputFormatClass(SequenceFileOutputFormat.class); //5.数据类型
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(BytesWritable.class); // job.setOutputValueClass(Text.class); //6.设置输入与输出路径
FileInputFormat.setInputPaths(job,new Path(args[]));
FileOutputFormat.setOutputPath(job,new Path(args[])); //7.提交
boolean result = job.waitForCompletion(true);
System.exit(result?:);
}
}

MapReduce自定义InputFormat,RecordReader的更多相关文章

  1. 【Hadoop离线基础总结】MapReduce自定义InputFormat和OutputFormat案例

    MapReduce自定义InputFormat和OutputFormat案例 自定义InputFormat 合并小文件 需求 无论hdfs还是mapreduce,存放小文件会占用元数据信息,白白浪费内 ...

  2. MapReduce自定义InputFormat和OutputFormat

    一.自定义InputFormat 需求:将多个小文件合并为SequenceFile(存储了多个小文件) 存储格式:文件路径+文件的内容 c:/a.txt I love Beijing c:/b.txt ...

  3. MapReduce之自定义InputFormat

    在企业开发中,Hadoop框架自带的InputFormat类型不能满足所有应用场景,需要自定义InputFormat来解决实际问题. 自定义InputFormat步骤如下: (1)自定义一个类继承Fi ...

  4. MapReduce 重要组件——Recordreader组件 [转]

    (1)以怎样的方式从分片中读取一条记录,每读取一条记录都会调用RecordReader类: (2)系统默认的RecordReader是LineRecordReader,如TextInputFormat ...

  5. MapReduce 重要组件——Recordreader组件

    (1)以怎样的方式从分片中读取一条记录,每读取一条记录都会调用RecordReader类: (2)系统默认的RecordReader是LineRecordReader,如TextInputFormat ...

  6. 自定义InputFormat和OutputFormat案例

    一.自定义InputFormat InputFormat是输入流,在前面的例子中使用的是文件输入输出流FileInputFormat和FileOutputFormat,而FileInputFormat ...

  7. Hadoop案例(六)小文件处理(自定义InputFormat)

    小文件处理(自定义InputFormat) 1.需求分析 无论hdfs还是mapreduce,对于小文件都有损效率,实践中,又难免面临处理大量小文件的场景,此时,就需要有相应解决方案.将多个小文件合并 ...

  8. 自定义inputformat和outputformat

    1. 自定义inputFormat 1.1 需求 无论hdfs还是mapreduce,对于小文件都有损效率,实践中,又难免面临处理大量小文件的场景,此时,就需要有相应解决方案 1.2 分析 小文件的优 ...

  9. Hadoop_28_MapReduce_自定义 inputFormat

    1. 自定义inputFormat 1.1.需求: 无论hdfs还是mapreduce,对于小文件都有损效率,实践中,又难免面临处理大量小文件,此时就需要有相应解决方案; 1.2.分析: 小文件的优化 ...

随机推荐

  1. Excel 下拉菜单制作

    废话少说吧,以图明示: 图1 操作步骤(环境为Office 2013) 备注,第四步,可以选择页面中的数据源,也可以以“,”分割的方式输入字符串 图2 制作效果

  2. AndroidActivity跳转动画,让你的APP瞬间绚丽起来

    我们都知道绚丽的APP总会给用户耳目一新的感觉,为了抓住用户更大网络公司使出浑身解数让自己的产品更绚丽,而绚丽最简单的效果就是Activity跳转效果,不仅能够让用户看起来舒服,并且实现起来也特别简单 ...

  3. NLP系列(1)_从破译外星人文字浅谈自然语言处理的基础

    作者:龙心尘 &&寒小阳 时间:2016年1月. 出处: http://blog.csdn.net/longxinchen_ml/article/details/50543337, h ...

  4. 2、应用程序及驱动-poll和select使用说明

    1.poll机制(如果中断机制出问题了,poll机制是对中断机制的补充,比如等一个小孩,如果小孩生病了,因此隔一段时间应该去找他) poll机制就是给定一段时间,在这一段时间内程序处于睡眠状态一直等待 ...

  5. VS2012 打包部署程序

      一. 下载 InstallShield 2015(支持VS2012) VS2012没有自带打包工具,所以要先下载并安装一个打包工具.我采用微软提供的打包工具:  InstallShield2015 ...

  6. CSDN学院 免费技术答疑公开课,本周三场即将开播~~~

    为了酬谢广大学员.CSDN学院特推出免费技术答疑公开课,让您开启一段充实的学习之旅~ 本周三场即将开播! ----------------------------------------------- ...

  7. hdu 5078 Osu!(鞍山现场赛)

    Osu! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Sub ...

  8. ios开发多线程一:了解-NSOperation的基本使用

    #import "ViewController.h" @interface ViewController () @end @implementation ViewControlle ...

  9. RMAN之一:快速入门 分类: H2_ORACLE 2014-02-17 16:11 689人阅读 评论(0) 收藏

    1.数据导出基础 (1)创建datapump导出文件的目录对象并为相应用户授予权限. 出于安全考虑,不允许oracle用户直接在OS上进行文件的操作,而应通过directory对象指定. SQL> ...

  10. 小强的HTML5移动开发之路(51)——jquerymobile中改善页面访问速度

    在使用jQuery Mobile进行开发的时候可以选择单页模版和多页模版,在使用单页模版的时候从一个页面跳转到另一个页面的时候需要从服务器请求,用户会感到略有停顿.使用多页模版,可以改善页面跳转之间的 ...