输入格式

1、输入分片与记录

2、文件输入

3、文本输入

4、二进制输入

5、多文件输入

6、数据库格式输入

1、输入分片与记录

1、JobClient通过指定的输入文件的格式来生成数据分片InputSplit。

2、一个分片不是数据本身,而是可分片数据的引用

3、InputFormat接口负责生成分片。

InputFormat 负责处理MR的输入部分,有三个作用:

验证作业的输入是否规范。

把输入文件切分成InputSplit。

提供RecordReader 的实现类。把InputSplit读到Mapper中进行处理。

2、文件输入

抽象类:FilelnputFormat

1、FilelnputFormat是全部使用文件作为数据源的InputFormat实现的基类。

2、FilelnputFormat输入数据格式的分片大小由数据块大小决定

FileInputFormat保存作为job输入的全部文件。并实现了对输入文件计算splits的方法。

至于获得记录的方法是有不同的子类——TextInputFormat进行实现的。

package org.apache.hadoop.mapreduce.lib.input;
public abstract class FileInputFormat<K, V> extends InputFormat<K, V> {
protected long computeSplitSize(long blockSize, long minSize,long maxSize) {
return Math.max(minSize, Math.min(maxSize, blockSize));
} /*Generate the list of files and make them into FileSplits.*/
public List<InputSplit> getSplits(JobContext job) throws IOException {
long minSize = Math.max(getFormatMinSplitSize(), getMinSplitSize(job));
long maxSize = getMaxSplitSize(job);
......
long blockSize = file.getBlockSize();
long splitSize = computeSplitSize(blockSize, minSize, maxSize);
......
}
/*Get the minimum split size*/
public static long getMinSplitSize(JobContext job) {
return job.getConfiguration().getLong(SPLIT_MINSIZE, 1L);
} /*Get the maximum split size.*/
public static long getMaxSplitSize(JobContext context) {
return context.getConfiguration().getLong(SPLIT_MAXSIZE,Long.MAX_VALUE);
} //是否分片
/*
Is the given filename splitable? Usually, true, but if the file is stream compressed, it will not be.
<code>FileInputFormat</code> implementations can override this and return <code>false</code> to ensure that individual input files are never split-up so that {@link Mapper}s process entire files.
*/
protected boolean isSplitable(JobContext context, Path filename) {
return true;//默认须要分片
} }
自己定义输入格式

假设我们不须要分片,那我们就须要对isSplitable方法进行重写

1、继承FileInputFormat基类。

2、重写里面的getSplits(JobContext context)方法。

3、重写createRecordReader(InputSplit split,TaskAttemptContext context)方法。

具体样例:

http://blog.csdn.net/scgaliguodong123_/article/details/46492039

InputSplit

在运行mapreduce之前,原始数据被切割成若干split。每一个split作为一个map任务的输入,在map运行过程中split会被分解成一个个记录(key-value对), map会依次处理每一个记录。

FileInputFormat仅仅划分比HDFS block大的文件,所以FileInputFormat划分

的结果是这个文件或者是这个文件里的一部分。

假设一个文件的大小比block小,将不会被划分,这也是Hadoop处理大文件

的效率要比处理非常多小文件的效率高的原因。

当Hadoop处理非常多小文件(文件大小小于hdfs block大小)的时候。因为

FileInputFormat不会对小文件进行划分,所以每一个小文件都会被当做一个split并分配一个map任务,导致效率底下。

比如:一个1G的文件。会被划分成16个64MB的split,并分配16个map任务处

理,而10000个100kb的文件会被10000个map任务处理。

Map任务的数量?

一个InputSplit相应一个Map task。

InputSplit的大小是由Math.max(minSize,Math.min(maxSize, blockSize))决定。

单节点一般10-100个map task。map task运行时长不建议低于1 分钟,否

则效率低。

抽象类:CombineFilelnputFormat

1、能够使用CombineFilelnputFormat来合并小文件。

2、因为CombineFilelnputFormat是一个抽象类,使用的时候须要创建一个

CombineFilelnputFormat的实体类,而且实现getRecordReader()的方法。

3、避免文件分法的方法:

A、数据块大小尽可能大。这样使文件的大小小于数据块的大小,就不用进行分片。(这样的方式不太友好)

B、继承FilelnputFormat,而且重写isSplitable()方法。

job.setInputFormatClass(CombineTextInputFormat.class);

Hadoop2.6.0 CombineTextInputFormat源代码:

package org.apache.hadoop.mapreduce.lib.input;
/* Input format that is a <code>CombineFileInputFormat</code>-equivalent for <code>TextInputFormat</code>.*/
public class CombineTextInputFormat
extends CombineFileInputFormat<LongWritable,Text> { public RecordReader<LongWritable,Text> createRecordReader(InputSplit split,
TaskAttemptContext context) throws IOException {
return new CombineFileRecordReader<LongWritable,Text>(
(CombineFileSplit)split, context, TextRecordReaderWrapper.class);
} /*A record reader that may be passed to <code>CombineFileRecordReader</code> so that it can be used in a <code>CombineFileInputFormat</code>-equivalent for <code>TextInputFormat</code>.*/
private static class TextRecordReaderWrapper
extends CombineFileRecordReaderWrapper<LongWritable,Text> {
// this constructor signature is required by CombineFileRecordReader
public TextRecordReaderWrapper(CombineFileSplit split,
TaskAttemptContext context, Integer idx)
throws IOException, InterruptedException {
super(new TextInputFormat(), split, context, idx);
}
}
}

3、文本输入

类名:TextlnputFormat

1、TextlnputFormat是默认的lnputFormat,每一行数据就是一条记录

2、TextlnputFormat的key是LongWritable类型的。存储该行在整个文件的偏移量,value是每行的数据内容,Text类型。

3、输入分片与HDFS数据块关系:TextlnputFormat每一条记录就是一行,非常有可能某一行跨数据块存放。默认以\n或回车键作为一行记录。

4、TextInputFormat继承了FileInputFormat。

类名:KeyValueTextInputFormat

能够通过设置key为行号的方式来知道记录的行号,而且能够通过key.value.separator.in.input设置key与value的切割符。

当输入数据的每一行是两列,并用tab分离的形式的时候,KeyValueTextInputformat处理这样的格式的文件非常适合。

假设行中有分隔符,那么分隔符前面的作为key,后面的作为value。假设行中没有分隔符,那么整行作为key,value为空。

job.setInputFormatClass(KeyValueTextInputFormat.class);
//默认分隔符就是制表符
//conf.setStrings(KeyValueLineRecordReader.KEY_VALUE
_SEPERATOR, "\t")

类名:NLineInputFormat

能够设置每一个mapper处理的行数。能够通过mapred.line.input.format.lienspermap属性设置。

NLineInputformat能够控制在每一个split中数据的行数

//设置具体输入处理类
job.setInputFormatClass(NLineInputFormat.class);
//设置每一个split的行数
NLineInputFormat.setNumLinesPerSplit(job, Integer.parseInt(args[2]));

4、二进制输入

输入类:

SequenceFileInputFormat 将key和value以sequencefile格式输入。
SequenceFileAsTextInputFormat
SequenceFileAsBinaryInputFormat 将key和value以原始二进制的格式输入。

因为SequenceFile能够支持Splittable。所以能够作为mapreduce输入文件的格式,能够非常方便的得到己经含有<key,value>的分片。

SequenceFile处理、压缩处理。

5、多文件输入

类名:MultipleInputs

1、MultipleInputs能够提供多个输入数据类型。

2、通过addInputPath()方法来设置多路径。

6、数据库格式输入

类名:DBInputFormat

1、DBInputFormat是一个使用JDBC方式连接数据库,而且从关系型数据库中读取数据的一种输入格式。

2、有多个map会去连接数据库。有可能造成数据库崩溃,因此,避免过多的数据库连接。

3、HBase中的TablelnputFormat能够让MapReduce程序訪问HBase表里的数据。

实例单输入路径

[root@master liguodong]# hdfs dfs -cat /input.txt
hello you
hello everybody
hello hadoop
[root@master liguodong]# hdfs dfs -text /tmp.seq
15/06/10 21:17:11 INFO bzip2.Bzip2Factory: Successfully loaded & initialized native-bzip2 library system-native
15/06/10 21:17:11 INFO compress.CodecPool: Got brand-new decompressor [.bz2]
100 apache software
99 chinese good
98 james NBA
97 index pass
96 apache software
95 chinese good
94 james NBA
93 index pass
......
package mrinputformat;

import java.io.IOException;
import java.util.StringTokenizer; import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.SequenceFileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; public class TestInputFormat { public static class TokenizerMapper
extends Mapper<IntWritable, Text, Text, IntWritable>{ private final static IntWritable one = new IntWritable(1);//1
private Text word = new Text(); public void map(IntWritable key, Text value, Context context
) throws IOException, InterruptedException
{
StringTokenizer itr = new StringTokenizer(value.toString());
while (itr.hasMoreTokens()) {
word.set(itr.nextToken());
//k v
context.write(word, one);
}
}
} public static class IntSumReducer
extends Reducer<Text,IntWritable,Text,IntWritable> {
private IntWritable result = new IntWritable(); public void reduce(Text key, Iterable<IntWritable> values,
Context context) throws IOException, InterruptedException {
int sum = 0;
for (IntWritable val : values)
{
sum += val.get();
}
result.set(sum);
context.write(key, result);
}
} public static void main(String[] args) throws Exception {
//1、配置
Configuration conf = new Configuration();
Job job = Job.getInstance(conf, "word count"); //2、打包运行必须运行的方法
job.setJarByClass(TestInputFormat.class); //3、输入路径
//hdfs://master:8020/tmp.seq
//hdfs://master:8020/output
FileInputFormat.addInputPath(job, new Path(args[0]));
//默认是TextInputFormat
job.setInputFormatClass(SequenceFileInputFormat.class); //4、Map
job.setMapperClass(TokenizerMapper.class); //5、Combiner
job.setCombinerClass(IntSumReducer.class); //6、Reducer
job.setReducerClass(IntSumReducer.class);
job.setOutputKeyClass(Text.class); //7、 输出路径
FileOutputFormat.setOutputPath(job, new Path(args[1])); //8、提交作业
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}

运行结果:

多输入路径方式

package mrinputformat;

import java.io.IOException;
import java.util.StringTokenizer; import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.MultipleInputs;
import org.apache.hadoop.mapreduce.lib.input.SequenceFileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; public class TestInputFormat { //採用TextInputFormat
public static class Mapper1
extends Mapper<LongWritable, Text, Text, IntWritable>{ private final static IntWritable one = new IntWritable(1);//1
private Text word = new Text(); public void map(LongWritable key, Text value, Context context
) throws IOException, InterruptedException
{
StringTokenizer itr = new StringTokenizer(value.toString());
while (itr.hasMoreTokens()) {
word.set(itr.nextToken());
//k v
context.write(word, one);
}
}
} //SequenceFileInputFormat
public static class Mapper2
extends Mapper<IntWritable, Text, Text, IntWritable>{ private final static IntWritable one = new IntWritable(1);//1
private Text word = new Text(); public void map(IntWritable key, Text value, Context context
) throws IOException, InterruptedException
{
StringTokenizer itr = new StringTokenizer(value.toString());
while (itr.hasMoreTokens()) {
word.set(itr.nextToken());
//k v
context.write(word, one);
}
}
} public static class IntSumReducer
extends Reducer<Text,IntWritable,Text,IntWritable> {
private IntWritable result = new IntWritable(); public void reduce(Text key, Iterable<IntWritable> values,
Context context) throws IOException, InterruptedException {
int sum = 0;
for (IntWritable val : values)
{
sum += val.get();
}
result.set(sum);
context.write(key, result);
}
} public static void main(String[] args) throws Exception {
//1、配置
Configuration conf = new Configuration();
Job job = Job.getInstance(conf, "word count"); //2、打包运行必须运行的方法
job.setJarByClass(TestInputFormat.class); //3、输入路径
//hdfs://master:8020/tmp.seq
//hdfs://master:8020/output
//单个输入路径
//FileInputFormat.addInputPath(job, new Path(args[0]));
//默认是TextInputFormat
//job.setInputFormatClass(SequenceFileInputFormat.class);
//4、Map
//job.setMapperClass(TokenizerMapper.class); //多个输入路径
Path path1 = new Path("hdfs://master:8020/input.txt");
Path path2 = new Path("hdfs://master:8020/tmp.seq");
MultipleInputs.addInputPath(job, path1, TextInputFormat.class,Mapper1.class);
MultipleInputs.addInputPath(job, path2, SequenceFileInputFormat.class,Mapper2.class); //5、Combiner
job.setCombinerClass(IntSumReducer.class); //6、Reducer
job.setReducerClass(IntSumReducer.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class); //7、 输出路径
FileOutputFormat.setOutputPath(job, new Path("hdfs://master:8020/output")); //8、提交作业
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}

运行结果:

输出格式

文本输出

TextOutputFormat

默认的输出格式。key是LongWritable,value是Text类型, key和value中间值用tab隔开的。

二进制输出

SequenceFileOutputFormat

将key和value以sequencefile格式输出。

SequenceFileAsBinaryOutputFormat

将key和value以原始二进制的格式输出。

MapFileOutputFormat

将key和value写入MapFile中。因为MapFile中的key是有序的,所以写入的时候必须保证记录是按key值顺序写入的。

多文件输出

MultipleOutputFormat
MultipleOutputs

默认情况下一个reducer会产生一个输出,可是有些时候我们想一个reducer产生多个输出。 MultipleOutputFormat和MultipleOutputs能够实现这个功能。

差别:MultipleOutputs能够产生不同类型的输出。

数据库格式输出

DBOutputFormat

MapReduce输入输出类型、格式及实例的更多相关文章

  1. mapreduce 输入输出类型

    默认的mapper是IdentityMapper,默认的reducer是IdentityReducer,它们将输入的键和值原封不动地写到输出中. 默认的partitioner是HashPartitin ...

  2. Hadoop MapReduce输入输出类型

    一.输入格式 1.输入分片split 一个分片对应一个map任务: 一个分片包含一个表(整个文件)上的若干行,而一条记录(单行)对应一行: 分片包含一个以字节为单位的长度 和 一组存储位置,分片不包含 ...

  3. MapReduce的类型与格式

    MapReduce的类型 默认的MR作业 默认的mapper是Mapper类,它将输入的键和值原封不动地写到输出中 默认的partitioner是HashPartitioner,它对每条记录的键进行哈 ...

  4. MapReduce工作机制——Word Count实例(一)

    MapReduce工作机制--Word Count实例(一) MapReduce的思想是分布式计算,也就是分而治之,并行计算提高速度. 编程思想 首先,要将数据抽象为键值对的形式,map函数输入键值对 ...

  5. 一、JAVA变量类型:①类变量与实例变量的异同点

    在JAVA中,变量使用前必须声明,格式如下: int a; //单个变量声明 int b, c, d; //多个变量一起声明 int e = 1, f = 2, g = 3; //声明时同时赋值(初始 ...

  6. MySQL 有输入输出参数的存储过程实例

    1.MySQL 有输入输出参数的存储过程实例 DELIMITER // DROP PROCEDURE IF EXISTS `test`.`p_getvalue` // CREATE PROCEDURE ...

  7. C# Directory.GetFiles()获取多个类型格式的文件

    第一种方式 System.IO.Directory.GetFiles()获取多个类型格式的文件 System.IO.Directory.GetFiles("c:\","( ...

  8. SpringMVC返回Json,自定义Json中Date类型格式

    http://www.cnblogs.com/jsczljh/p/3654636.html —————————————————————————————————————————————————————— ...

  9. MapReduce 的类型与格式【编写最简单的mapreduce】(1)

    hadoop mapreduce 中的map 和reduce 函数遵循下面的形式 map: (K1, V1) → list(K2, V2) reduce: (K2, list(V2)) → list( ...

随机推荐

  1. Open Cascade创建自己的MFC文档程序

    项目初始设置在Visual studio中创建一个单文档MFC项目(本例以MFCTest为名称): 在项目属性的VC++页面设置包含目录.库目录,在链接器的输入中添加OCC库目录下的所有.lib文件名 ...

  2. B6. Concurrent 内存模型与线程交互

    [概述]

  3. sftp ftp文件同步方案

    sftp ftp文件同步方案 1. 需求 1.1实现网关服务器的ftp服务器的/batchFileRequest目录下文件向徽商所使用的sftp服务器的/batchFileRequest目录同步文件 ...

  4. centos7 firewalld日常使用

    若生产中使用有docker,建议不要使用firewalld,改用iptables,用firewalld坑很多,暂时还未找到解决办法,在此做下记录: 说明:若加参数permanent为永久添加即添加至z ...

  5. 07 mongodb

    mongodb mongodb简介 简介 MongoDB是一个基于分布式文件存储的数据库.由C++语言编写.旨在为Web应用提供可扩展的高性能数据存储解决方案.    MongoDB是一个介于关系数据 ...

  6. mybatis完整sql调试

    问题描述 在使用mybatis进行开发的时候,由于可以动态拼接sql,这样大大方便了我们.但是也有一定的问题,当我们动态sql拼接的块很多的时候,我们要想从*mapper.xml中直接找出完整的sql ...

  7. finally块的问题(finally block does not complete normally)

    http://blog.csdn.net/chh_jiang/article/details/4557461 当finall块中包含return语句时,Eclipse会给出警告“finally blo ...

  8. list、tuple、dict内部功能释义

    一.list内部功能释义 运用频次:☆☆☆☆☆ 1. append():列表末尾插入元素 [示例] >>> L1=[11,22,33] >>> L1.append( ...

  9. python的unittest单元测试框架断言整理汇总

    自动化脚本最重要的是断言,正确设置断言以后才能帮助我们判断测试用例执行结果. 一.先说说unittest常用的断言吧 常用的就以下几个,网上一搜一大堆.python版本2.7以上都可以调用了. 断言语 ...

  10. IDEA将Maven项目中src源代码下的xml配置文件编译进classes

    遇到这样的情况,maven项目启动报错,src中某个包下面的xml文件找不到. eclipse编译项目会自动将xml配置文件编译进classes,IDEA却不行 在报错项目的pom.xml文件中添加: ...