输入格式

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进行实现的。

  1. package org.apache.hadoop.mapreduce.lib.input;
  2. public abstract class FileInputFormat<K, V> extends InputFormat<K, V> {
  3. protected long computeSplitSize(long blockSize, long minSize,long maxSize) {
  4. return Math.max(minSize, Math.min(maxSize, blockSize));
  5. }
  6. /*Generate the list of files and make them into FileSplits.*/
  7. public List<InputSplit> getSplits(JobContext job) throws IOException {
  8. long minSize = Math.max(getFormatMinSplitSize(), getMinSplitSize(job));
  9. long maxSize = getMaxSplitSize(job);
  10. ......
  11. long blockSize = file.getBlockSize();
  12. long splitSize = computeSplitSize(blockSize, minSize, maxSize);
  13. ......
  14. }
  15. /*Get the minimum split size*/
  16. public static long getMinSplitSize(JobContext job) {
  17. return job.getConfiguration().getLong(SPLIT_MINSIZE, 1L);
  18. }
  19. /*Get the maximum split size.*/
  20. public static long getMaxSplitSize(JobContext context) {
  21. return context.getConfiguration().getLong(SPLIT_MAXSIZE,Long.MAX_VALUE);
  22. }
  23. //是否分片
  24. /*
  25. Is the given filename splitable? Usually, true, but if the file is stream compressed, it will not be.
  26. <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.
  27. */
  28. protected boolean isSplitable(JobContext context, Path filename) {
  29. return true;//默认须要分片
  30. }
  31. }
自己定义输入格式

假设我们不须要分片,那我们就须要对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()方法。

  1. job.setInputFormatClass(CombineTextInputFormat.class);

Hadoop2.6.0 CombineTextInputFormat源代码:

  1. package org.apache.hadoop.mapreduce.lib.input;
  2. /* Input format that is a <code>CombineFileInputFormat</code>-equivalent for <code>TextInputFormat</code>.*/
  3. public class CombineTextInputFormat
  4. extends CombineFileInputFormat<LongWritable,Text> {
  5. public RecordReader<LongWritable,Text> createRecordReader(InputSplit split,
  6. TaskAttemptContext context) throws IOException {
  7. return new CombineFileRecordReader<LongWritable,Text>(
  8. (CombineFileSplit)split, context, TextRecordReaderWrapper.class);
  9. }
  10. /*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>.*/
  11. private static class TextRecordReaderWrapper
  12. extends CombineFileRecordReaderWrapper<LongWritable,Text> {
  13. // this constructor signature is required by CombineFileRecordReader
  14. public TextRecordReaderWrapper(CombineFileSplit split,
  15. TaskAttemptContext context, Integer idx)
  16. throws IOException, InterruptedException {
  17. super(new TextInputFormat(), split, context, idx);
  18. }
  19. }
  20. }

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为空。

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

类名:NLineInputFormat

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

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

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

4、二进制输入

输入类:

  1. SequenceFileInputFormat keyvaluesequencefile格式输入。
  2. SequenceFileAsTextInputFormat
  3. SequenceFileAsBinaryInputFormat keyvalue以原始二进制的格式输入。

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

SequenceFile处理、压缩处理。

5、多文件输入

类名:MultipleInputs

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

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

6、数据库格式输入

类名:DBInputFormat

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

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

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

实例单输入路径

  1. [root@master liguodong]# hdfs dfs -cat /input.txt
  2. hello you
  3. hello everybody
  4. hello hadoop
  5. [root@master liguodong]# hdfs dfs -text /tmp.seq
  6. 15/06/10 21:17:11 INFO bzip2.Bzip2Factory: Successfully loaded & initialized native-bzip2 library system-native
  7. 15/06/10 21:17:11 INFO compress.CodecPool: Got brand-new decompressor [.bz2]
  8. 100 apache software
  9. 99 chinese good
  10. 98 james NBA
  11. 97 index pass
  12. 96 apache software
  13. 95 chinese good
  14. 94 james NBA
  15. 93 index pass
  16. ......
  1. package mrinputformat;
  2. import java.io.IOException;
  3. import java.util.StringTokenizer;
  4. import org.apache.hadoop.conf.Configuration;
  5. import org.apache.hadoop.fs.Path;
  6. import org.apache.hadoop.io.IntWritable;
  7. import org.apache.hadoop.io.Text;
  8. import org.apache.hadoop.mapreduce.Job;
  9. import org.apache.hadoop.mapreduce.Mapper;
  10. import org.apache.hadoop.mapreduce.Reducer;
  11. import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
  12. import org.apache.hadoop.mapreduce.lib.input.SequenceFileInputFormat;
  13. import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
  14. public class TestInputFormat {
  15. public static class TokenizerMapper
  16. extends Mapper<IntWritable, Text, Text, IntWritable>{
  17. private final static IntWritable one = new IntWritable(1);//1
  18. private Text word = new Text();
  19. public void map(IntWritable key, Text value, Context context
  20. ) throws IOException, InterruptedException
  21. {
  22. StringTokenizer itr = new StringTokenizer(value.toString());
  23. while (itr.hasMoreTokens()) {
  24. word.set(itr.nextToken());
  25. //k v
  26. context.write(word, one);
  27. }
  28. }
  29. }
  30. public static class IntSumReducer
  31. extends Reducer<Text,IntWritable,Text,IntWritable> {
  32. private IntWritable result = new IntWritable();
  33. public void reduce(Text key, Iterable<IntWritable> values,
  34. Context context) throws IOException, InterruptedException {
  35. int sum = 0;
  36. for (IntWritable val : values)
  37. {
  38. sum += val.get();
  39. }
  40. result.set(sum);
  41. context.write(key, result);
  42. }
  43. }
  44. public static void main(String[] args) throws Exception {
  45. //1、配置
  46. Configuration conf = new Configuration();
  47. Job job = Job.getInstance(conf, "word count");
  48. //2、打包运行必须运行的方法
  49. job.setJarByClass(TestInputFormat.class);
  50. //3、输入路径
  51. //hdfs://master:8020/tmp.seq
  52. //hdfs://master:8020/output
  53. FileInputFormat.addInputPath(job, new Path(args[0]));
  54. //默认是TextInputFormat
  55. job.setInputFormatClass(SequenceFileInputFormat.class);
  56. //4、Map
  57. job.setMapperClass(TokenizerMapper.class);
  58. //5、Combiner
  59. job.setCombinerClass(IntSumReducer.class);
  60. //6、Reducer
  61. job.setReducerClass(IntSumReducer.class);
  62. job.setOutputKeyClass(Text.class);
  63. //7、 输出路径
  64. FileOutputFormat.setOutputPath(job, new Path(args[1]));
  65. //8、提交作业
  66. System.exit(job.waitForCompletion(true) ? 0 : 1);
  67. }
  68. }

运行结果:

多输入路径方式

  1. package mrinputformat;
  2. import java.io.IOException;
  3. import java.util.StringTokenizer;
  4. import org.apache.hadoop.conf.Configuration;
  5. import org.apache.hadoop.fs.Path;
  6. import org.apache.hadoop.io.IntWritable;
  7. import org.apache.hadoop.io.LongWritable;
  8. import org.apache.hadoop.io.Text;
  9. import org.apache.hadoop.mapreduce.Job;
  10. import org.apache.hadoop.mapreduce.Mapper;
  11. import org.apache.hadoop.mapreduce.Reducer;
  12. import org.apache.hadoop.mapreduce.lib.input.MultipleInputs;
  13. import org.apache.hadoop.mapreduce.lib.input.SequenceFileInputFormat;
  14. import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
  15. import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
  16. public class TestInputFormat {
  17. //採用TextInputFormat
  18. public static class Mapper1
  19. extends Mapper<LongWritable, Text, Text, IntWritable>{
  20. private final static IntWritable one = new IntWritable(1);//1
  21. private Text word = new Text();
  22. public void map(LongWritable key, Text value, Context context
  23. ) throws IOException, InterruptedException
  24. {
  25. StringTokenizer itr = new StringTokenizer(value.toString());
  26. while (itr.hasMoreTokens()) {
  27. word.set(itr.nextToken());
  28. //k v
  29. context.write(word, one);
  30. }
  31. }
  32. }
  33. //SequenceFileInputFormat
  34. public static class Mapper2
  35. extends Mapper<IntWritable, Text, Text, IntWritable>{
  36. private final static IntWritable one = new IntWritable(1);//1
  37. private Text word = new Text();
  38. public void map(IntWritable key, Text value, Context context
  39. ) throws IOException, InterruptedException
  40. {
  41. StringTokenizer itr = new StringTokenizer(value.toString());
  42. while (itr.hasMoreTokens()) {
  43. word.set(itr.nextToken());
  44. //k v
  45. context.write(word, one);
  46. }
  47. }
  48. }
  49. public static class IntSumReducer
  50. extends Reducer<Text,IntWritable,Text,IntWritable> {
  51. private IntWritable result = new IntWritable();
  52. public void reduce(Text key, Iterable<IntWritable> values,
  53. Context context) throws IOException, InterruptedException {
  54. int sum = 0;
  55. for (IntWritable val : values)
  56. {
  57. sum += val.get();
  58. }
  59. result.set(sum);
  60. context.write(key, result);
  61. }
  62. }
  63. public static void main(String[] args) throws Exception {
  64. //1、配置
  65. Configuration conf = new Configuration();
  66. Job job = Job.getInstance(conf, "word count");
  67. //2、打包运行必须运行的方法
  68. job.setJarByClass(TestInputFormat.class);
  69. //3、输入路径
  70. //hdfs://master:8020/tmp.seq
  71. //hdfs://master:8020/output
  72. //单个输入路径
  73. //FileInputFormat.addInputPath(job, new Path(args[0]));
  74. //默认是TextInputFormat
  75. //job.setInputFormatClass(SequenceFileInputFormat.class);
  76. //4、Map
  77. //job.setMapperClass(TokenizerMapper.class);
  78. //多个输入路径
  79. Path path1 = new Path("hdfs://master:8020/input.txt");
  80. Path path2 = new Path("hdfs://master:8020/tmp.seq");
  81. MultipleInputs.addInputPath(job, path1, TextInputFormat.class,Mapper1.class);
  82. MultipleInputs.addInputPath(job, path2, SequenceFileInputFormat.class,Mapper2.class);
  83. //5、Combiner
  84. job.setCombinerClass(IntSumReducer.class);
  85. //6、Reducer
  86. job.setReducerClass(IntSumReducer.class);
  87. job.setOutputKeyClass(Text.class);
  88. job.setOutputValueClass(IntWritable.class);
  89. //7、 输出路径
  90. FileOutputFormat.setOutputPath(job, new Path("hdfs://master:8020/output"));
  91. //8、提交作业
  92. System.exit(job.waitForCompletion(true) ? 0 : 1);
  93. }
  94. }

运行结果:

输出格式

文本输出

TextOutputFormat

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

二进制输出

SequenceFileOutputFormat

将key和value以sequencefile格式输出。

SequenceFileAsBinaryOutputFormat

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

MapFileOutputFormat

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

多文件输出

  1. MultipleOutputFormat
  2. 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. Asp.Net Core 入门(一)——Program.cs做了什么

    ASP.NET Core 是微软推出的一种全新的跨平台开源 .NET 框架,用于在 Windows.Mac 或 Linux 上生成基于云的新式 Web 应用程序.国内目前关于Asp.Net Core的 ...

  2. Android N requires the IDE to be running with Java 1.8 or later

      Android Studio需要两个JDK: ide jdk和project jdk: 前者是IDE本身运行使用的JDK. 后者用于编译Java代码 Project JDK 可以通过file-&g ...

  3. python之str (字符型)

    用途: 存储少量的数据,+ *int 切片, 其他操作方法 切片还是对其进行任何操作,获取的内容全部是strl类型 存储数据单一 格式: 在python中用引号引起来的就是字符串 '今天吃了没?' 1 ...

  4. JavaScript设计模式基础之面向对象的JavaScript(二)

    多态 多态的实际含义:同一操作作用与不同的对象上面,可以产生不同的解释和不同的执行结果,就是说,给不同的对象发送同一个消息 的时候,这些对象会根据这个消息分别给出不同的反馈 代码如下: class D ...

  5. VMware Workstation 14 UEFI启动

    1.新建虚拟机 完成后不要启动 修改虚拟机目录下的XXX.vmx文件 添加一行:firmware="efi" 然后启动UEFI安装系统.

  6. HTML5增加与改良的input元素

    h5中form表单中input新增的属性值 在HTML5中增加了许多新的标签和功能属性,今天我们来看一个Form表单在HTML5中新的使用方法.那么在HTML5中新加入的这个功能与之前咱们使用的功能区 ...

  7. CSS3--- 颜色

    1.RGB是一种色彩标准,是由红(R).绿(G).蓝(B)的变化以及相互叠加来得到各式各样的颜色.RGBA是在RGB的基础上增加了控制alpha透明度的参数. 语法:color:rgba(R,G,B, ...

  8. 由Java实现Valid Parentheses

    一.题目 Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the ...

  9. MySQL 初识

    一.MySQL介绍: MySQL是一个关系型数据库管理系统,由瑞典MySQL AB 公司开发,目前属于 Oracle 旗下产品.MySQL 是最流行的关系型数据库管理系统之一,在 WEB 应用方面,M ...

  10. pwnable.kr 之 passcode write up

    先看源码: #include <stdio.h> #include <stdlib.h> void login(){ int passcode1; int passcode2; ...