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. LeetCode Algorithm 03_Longest Substring Without Repeating Characters

    Given a string, find the length of the longest substring without repeating characters. For example, ...

  2. 如何在本地运行查看github上的开源项目

    看中了一款很多星星的github的项目,想把这个项目拉到自己的电脑上运行查看项目效果,该怎么做?示例:我们今天要看的 github项目地址:https://github.com/lzxb/vue-cn ...

  3. 原生js大总结二

    011.if语句的优化   1.把次数多的条件和执行结果放到最前面   2.减少第一次无用的判断,可以用嵌套判断   3.判断语句禁止出现三次嵌套     012.谈谈你对switch的理解   1. ...

  4. mahout測试朴素贝叶斯分类样例

    对于这个測试建议大家先理解原理,这里我画了例如以下的示意图 接下来就依照例如以下的细节来输入指令測试: 首先前提是Hadoop安装并启动,mahout已经安装了. <strong>< ...

  5. 10.9 android输入系统_APP跟输入系统建立联系和Dispatcher线程_分发dispatch

    12. 输入系统_APP跟输入系统建立联系_InputChannel和Connection核心: socketpair // 第9课第3节_输入系统_必备Linux编程知识_任意进程双向通信(scok ...

  6. OC学习篇之---类的定义

    OC中类的相关知识 OC和C的最大区别就是具有了面向对象的功能,那么说到面向对象,就不得不说类这个概念了,如果学过Java的话,那么对类和对象的概念就不陌生了,因为Java是非常纯正的面向对象设计语言 ...

  7. ImageView的圆角半径

    // 设置imageview的圆角半径 UIImageView *imageView = (UIImageView *)[cell viewWithTag:tag]; imageView.layer. ...

  8. [Angular] Learn Angular Multi-Slot Content Projection

    Now for au-modal component, we pass in tow component though contenct projection: <au-modal class= ...

  9. Oracle-18-select语句初步&amp;SQL中用算术表达式&amp;别名的使用&amp;连接运算符%distinct&amp;where子句

    一.一般SELECT语句的格式例如以下: 1.查询指定表的全部列 select * from 表名 [where 条件] [group by 分组列名] [having 聚合函数] [order by ...

  10. js读取json,纠结。。。

    什么是json.先小抄一段:  JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.採用全然独立于语言的文本格式, 是理想的数据交换格式,同一时候,JSO ...