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.分析: 小文件的优化 ...
随机推荐
- 洛谷 P1097 统计数字
P1097 统计数字 题目描述 某次科研调查时得到了n个自然数,每个数均不超过1500000000(1.5*10^9).已知不相同的数不超过10000个,现在需要统计这些自然数各自出现的次数,并按照自 ...
- LeetCode Algorithm 133_Clone Graph
Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. OJ's ...
- python3 求斐波那契数列(Fibonacci sequence)
输出斐波那契数列的前多少个数. 利用函数 #!/usr/bin/env python # -*- coding:utf-8 -*- # Author:Hiuhung Wan # ----斐波那契数列( ...
- HDU2438 Turn the corner【三分法】【数学几何】
Turn the corner Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- js进阶 12-16 jquery如何实现通过点击按钮和按下组合键两种方式提交留言
js进阶 12-16 jquery如何实现通过点击按钮和按下组合键两种方式提交留言 一.总结 一句话总结:实现按下组合键提交留言是通过给input加keydown事件,判断按键的键码来实现的. 1.如 ...
- 函数的引用透明性(referential transparency)
1. 基础 初学程序设计时,比较容易混淆的两个概念是数学函数(math function)和程序中使用的函数. 在数学函数中 y=f(x),一个输入值有固定的输出值.例如,无论计算多少次,sinπ 的 ...
- 【转载】zookeeper数据模型
[转载请注明作者和原文链接, 如有谬误, 欢迎在评论中指正. ] ZooKeeper的数据结构, 与普通的文件系统极为类似. 见下图: 图片引用自developerworks 图中的每个节点称为一个 ...
- HIVE快速入门 分类: B4_HIVE 2015-06-06 11:27 59人阅读 评论(0) 收藏
(一)简单入门 1.创建一个表 create table if not exists ljh_emp( name string, salary float, gender string) commen ...
- php实现 计算字符串的距离
php实现 计算字符串的距离 一.总结 一句话总结:解决dp问题最好的方法是什么:分析出状态后 实例+画表. 1.解决dp问题最好的方法是什么? 分析出状态后 实例+画表 2.画图的好处? 画出来表之 ...
- 可直接复制粘贴的boostrap图标库网址
1:http://fontawesome.dashgame.com/ 2:http://www.kuiyu.net/art-34.html 3:http://www.bootcss.com/p/fon ...