MapReduce自定义InputFormat,RecordReader
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的更多相关文章
- 【Hadoop离线基础总结】MapReduce自定义InputFormat和OutputFormat案例
MapReduce自定义InputFormat和OutputFormat案例 自定义InputFormat 合并小文件 需求 无论hdfs还是mapreduce,存放小文件会占用元数据信息,白白浪费内 ...
- MapReduce自定义InputFormat和OutputFormat
一.自定义InputFormat 需求:将多个小文件合并为SequenceFile(存储了多个小文件) 存储格式:文件路径+文件的内容 c:/a.txt I love Beijing c:/b.txt ...
- MapReduce之自定义InputFormat
在企业开发中,Hadoop框架自带的InputFormat类型不能满足所有应用场景,需要自定义InputFormat来解决实际问题. 自定义InputFormat步骤如下: (1)自定义一个类继承Fi ...
- MapReduce 重要组件——Recordreader组件 [转]
(1)以怎样的方式从分片中读取一条记录,每读取一条记录都会调用RecordReader类: (2)系统默认的RecordReader是LineRecordReader,如TextInputFormat ...
- MapReduce 重要组件——Recordreader组件
(1)以怎样的方式从分片中读取一条记录,每读取一条记录都会调用RecordReader类: (2)系统默认的RecordReader是LineRecordReader,如TextInputFormat ...
- 自定义InputFormat和OutputFormat案例
一.自定义InputFormat InputFormat是输入流,在前面的例子中使用的是文件输入输出流FileInputFormat和FileOutputFormat,而FileInputFormat ...
- Hadoop案例(六)小文件处理(自定义InputFormat)
小文件处理(自定义InputFormat) 1.需求分析 无论hdfs还是mapreduce,对于小文件都有损效率,实践中,又难免面临处理大量小文件的场景,此时,就需要有相应解决方案.将多个小文件合并 ...
- 自定义inputformat和outputformat
1. 自定义inputFormat 1.1 需求 无论hdfs还是mapreduce,对于小文件都有损效率,实践中,又难免面临处理大量小文件的场景,此时,就需要有相应解决方案 1.2 分析 小文件的优 ...
- Hadoop_28_MapReduce_自定义 inputFormat
1. 自定义inputFormat 1.1.需求: 无论hdfs还是mapreduce,对于小文件都有损效率,实践中,又难免面临处理大量小文件,此时就需要有相应解决方案; 1.2.分析: 小文件的优化 ...
随机推荐
- 洛谷—— P1118 [USACO06FEB]数字三角形Backward Digit Su…
https://www.luogu.org/problem/show?pid=1118#sub 题目描述 FJ and his cows enjoy playing a mental game. Th ...
- Python中可避免读写乱码的一个强慷慨法
昨天在帮同学解析一批从网络上爬取的文件时,遇到一个奇葩的问题,文件本身的编码是gbk,Eclipse编辑环境的默认编码是utf8,使用常规的open方法批量打开文件时,某些文件里存在一些不可被gbk识 ...
- ds1302模块的一个arduino程序
/* * 读写DS1302 时钟芯片 * @author Yangtf * 很棒的文档 http://www.21ic.com/jichuzhishi/datasheet/DS1302/data/18 ...
- Vue里父子组间的通讯
父组件代码 <template> <div> <child @child-say="listenToBoy" :mes=count></c ...
- linux终端下一些“风骚”的按键操作及Linux终端命令
linux终端下一些"风骚"的按键操作 <backspace> 删除 <ctrl-l> 清空屏幕, 相当于clear tab ...
- wepy小程序实现列表分页上拉加载(1)
使用wepy开发微信小程序商城第一篇:项目初始化 使用wepy开发微信小程序商城第二篇:路由配置和页面结构 列表页效果图: 1.新建列表页 (1)在pages里面新建一个list.wpy文件 初始代码 ...
- [Debug] Use Snippets to Store Behaviors in Chrome DevTools
Using the Snippets tab in the source devtool you can define and run arbitrary pieces of code in your ...
- javascript进阶教程第三章--匿名和闭包--案例实战
javascript进阶教程第三章--匿名和闭包--案例实战 一.学习任务 通过几个小练习回顾学过的知识点 二.实例 练习1: 实例描述:打开页面后规定时间内弹出一个新窗口,新窗口指定时间后自动关闭. ...
- 数据结构与算法实验题 6.1 s_sin’s bonus
数据结构与算法实验题 6.1 s_sin's bonus ★实验任务 正如你所知道的 s_sin 是一个非常贪玩的人 QAQ(如果你非常讨厌他请直接从第二段开 始看),并且令人感到非常遗憾的是,他是一 ...
- UVA 11732 - strcmp() Anyone? 字典树
传送门:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a ...