一、输入格式

(1)输入分片记录

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

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

③InputFormat接口负责生成分片;

源码位置:org.apache.hadoop.mapreduce.lib.input包(新)

org.apache.hadoop.mapred.lib 包(旧)

查看其中FileInputFormat类中的getSplits()方法;

computeSplitSize()函数决定分片大小;

各种输入类的结构关系图:

(2)文件输入

抽象类:FileInputFormat

①FileInputFormat是所有使用文件作为数据源的InputFormat实现的基类;

②FileInputFormat输入数据格式的分配大小由数据块大小决定;

抽象类:CombineFileInputFormat

①可以使用CombineFileInputFormat来合并小文件;

②因为CombineFileInputFormat是一个抽象类,使用的时候需要创建一个CombineFileInputFormat的实体类,并且实现getRecordReader()的方法;

③避免文件分割的方法:

A.数据块大小尽可能大,这样使文件的大小小于数据块的大小,就不用进行分片;

B.继承FileInputFormat,并且重载isSplitable()方法;

(3)文本输入

类名:TextInputFormat

①TextInputFormat是默认的InputFormat,每一行数据就是一条记录;

②TextInputFormat的key是LongWritable类型的,存储该行在整个文件的偏移量,value是每行的数据内容,Text类型;

③输入分片与HDFS数据块关系:TextInputFormat每一条记录就是一行,很有可能某一行跨数据块存放;

类名:KeyValueInputFormat类

可以通过key为行号的方式来知道记录的行号,并且可以通过key.value.separator.in.input设置key与value的分割符;

类名:NLineInputFormat类

可以设置每个mapper处理的行数,可以通过mapred.line.input.format.lienspermap属性设置;

(4)二进制输入

类名:SequenceFileInputFormat

SequenceFileAsTextInputFormat

SequenceFileAsBinaryInputFormat

由于SequenceFile能够支持Splittable,所以能够作为mapreduce输入文件的格式,能够很方便的得到已经含有,value>的分片;

(5)多文件输入

类名:MultipleInputs

①MultipleInputs能够提供多个输入数据类型;

②通过addInputPath()方法来设置多路径;

(6)数据库格式输入

类名:DBInputFormat

①DBInputFormat是一个使用JDBC并且从关系型数据库中读取数据的一种输入格式;

②避免过多的数据库连接;

③HBase中的TableInputFormat可以让MapReduce程序访问HBase表里的数据;

实验部分:

新建项目TestMRInputFormat,新建包com.mr,导入相关依赖包

实验①,以SequenceFile作为输入,故预先运行SequenceFileWriter.java产生一个b.seq文件;

新建类:TestInputFormat1.java(基于WordCount.java修改):

package com.mr;

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.output.FileOutputFormat;

import org.apache.hadoop.util.GenericOptionsParser;

public class TestInputFormat {

public static class TokenizerMapper

extends Mapper< IntWritable, Text, Text, IntWritable>{

private final static IntWritable one = new IntWritable(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());

context.write(word, one);

}

}

}

public static class IntSumReducer

extends Reducer {

private IntWritable result = new IntWritable();

public void reduce(Text key, Iterable 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 {

Configuration conf = new Configuration();

String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();

if (otherArgs.length != 2) {

System.err.println("Usage: wordcount ");

System.exit(2);

}

Job job = new Job(conf, "word count");

job.setJarByClass(TestInputFormat.class);

job.setMapperClass(TokenizerMapper.class);

job.setCombinerClass(IntSumReducer.class);

job.setReducerClass(IntSumReducer.class);

job.setOutputKeyClass(Text.class);

job.setOutputValueClass(IntWritable.class);

job.setInputFormatClass(SequenceFileInputFormat.class);//输入格式的设定

FileInputFormat.addInputPath(job, new Path(otherArgs[0]));

FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));

System.exit(job.waitForCompletion(true) ? 0 : 1);

}

}

Eclipse中运行,参数配置如下图:

输出统计结果如下:

实验②,多种来源输入:

TestInputFormat2.java:

package com.mr;

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.lib.input.TextInputFormat;

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.MultipleInputs;

import org.apache.hadoop.mapreduce.lib.input.SequenceFileInputFormat;

import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import org.apache.hadoop.util.GenericOptionsParser;

public class TestInputFormat2 {

public static class Mapper1  //第一个mapper类

extends Mapper<<font color="#ed1c24">LongWritable, Text, Text, IntWritable>{

private final static IntWritable one = new IntWritable(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());

context.write(word, one);

}

}

}

public static class Mapper2 extends  //第二个mapper类

Mapper {

private final static IntWritable one = new IntWritable(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());

context.write(word, one);

}

}

}

public static class IntSumReducer

extends Reducer {

private IntWritable result = new IntWritable();

public void reduce(Text key, Iterable 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 {

Configuration conf = new Configuration();

Job job = new Job(conf, "word count");

job.setJarByClass(TestInputFormat2.class);

job.setReducerClass(IntSumReducer.class);

job.setOutputKeyClass(Text.class);

job.setOutputValueClass(IntWritable.class);

Path path1 = new Path("/a.txt");

Path path2 = new Path("/b.seq");

//多输入

MultipleInputs.addInputPath(job, path1,TextInputFormat.class, Mapper1.class);

MultipleInputs.addInputPath(job, path2,SequenceFileInputFormat.class, Mapper2.class);

FileOutputFormat.setOutputPath(job, new Path("/output2"));

System.exit(job.waitForCompletion(true) ? 0 : 1);

}

}

创建输入文本文件a.txt:

aaa bbb

ccc aaa

ddd eee

将项目打包为jar(不知道为什么eclipse中不能运行,还没找到原因,用jar命令可以运行):

File->Export->Runnable JAR file,命名jar文件为testMR.jar。

命令行中运行:

$hadoop jar testMR.jar com.mr.TestInputFormat2

输出统计结果如下:

二、输出格式

各种类关系结构图:

(1)文本输出

类名:TextOutputFormat

①默认的输出方式,key是LongWritable类型的,value是Text类型的;

②以“key \t value”的方式输出行;

(2)二进制输出

类名:SequenceFileOutputFormat

SequenceFileAsTextOutputFormat

SequenceFileAsBinaryOutputFormat

MapFileOutputFormat

(3)多文件输出

类名:MultipleOutputFormat

MultipleOutputs

区别:MultipleOutputs可以产生不同类型的输出;

(4)数据库输出

类名:DBOutputFormat

http://blog.sina.com.cn/s/blog_4438ac090101qfuh.html

MapReduce类型与格式(输入与输出)的更多相关文章

  1. CString中Format函数与格式输入与输出

    CString中Format函数与格式输入与输出 Format是一个非经常常使用.却又似乎非常烦的方法,下面是它的完整概貌.以供大家查询之用:   格式化字符串forma("%d" ...

  2. 1.java.io包中定义了多个流类型来实现输入和输出功能,

    1.java.io包中定义了多个流类型来实现输入和输出功能,可以从不同的角度对其进行分 类,按功能分为:(C),如果为读取的内容进行处理后再输出,需要使用下列哪种流?(G)   A.输入流和输出流 B ...

  3. c语言中double类型数据的输入和输出

    double a;scanf("%f",&a);   //应用scanf("%lf",&a);执行上面语句时,发现double类型的输入不能使用 ...

  4. mysql datetime类型 按格式在页面输出

    mysql datetime类型对应java Date类型   java.util.Date类型会显示时间戳 java.sql.Date 只显示年月日不显示时分秒 只需要重写get方法 就能按格式输出 ...

  5. Go基础结构与类型03---标准输入与输出

    package main import ( "fmt" "strconv" ) //每次接收一个用户输入 func main031() { //定义a, b两个 ...

  6. MapReduce输入输出类型、格式及实例

    输入格式 1.输入分片与记录 2.文件输入 3.文本输入 4.二进制输入 5.多文件输入 6.数据库格式输入 1.输入分片与记录 1.JobClient通过指定的输入文件的格式来生成数据分片Input ...

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

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

  8. MapReduce深入理解输入和输出格式(2)-输入和输出完全总结

    MapReduce太高深,性能也值得考虑,大家感兴趣的还是看看spark比较好. FileInputFormat类 FileInputFormat是所有使用文件为数据源的InputFormat实现的基 ...

  9. MapReduce的类型与格式

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

随机推荐

  1. Charles 从入门到精通

    特别说明:原文来自唐巧大神的博客 http://blog.devtang.com/2015/11/14/charles-introduction/ Charles 从入门到精通 文章目录 1. 目录及 ...

  2. Matplotlib 学习笔记

    注:该文是上了开智学堂数据科学基础班的课后做的笔记,主讲人是肖凯老师. 数据绘图 数据可视化的原则 为什么要做数据可视化? 为什么要做数据可视化?因为可视化后获取信息的效率高.为什么可视化后获取信息的 ...

  3. 回归树|GBDT|Gradient Boosting|Gradient Boosting Classifier

    已经好久没写了,正好最近需要做分享所以上来写两篇,这篇是关于决策树的,下一篇是填之前SVM的坑的. 参考文献: http://stats.stackexchange.com/questions/545 ...

  4. 浅谈系统架构<一>

    前言:博主刚刚从事于Web后端开发与学习不久,开发项目经验也是有限的.不过今天依旧将一些个人的想法记录下来,我的构想或许不太正确,还望各位大牛能给我多多建议. 首先:我们从编程开始讲起 博主是偏向于后 ...

  5. Spring系列之AOP实现的两种方式

    AOP常用的实现方式有两种,一种是采用声明的方式来实现(基于XML),一种是采用注解的方式来实现(基于AspectJ). 首先复习下AOP中一些比较重要的概念: Joinpoint(连接点):程序执行 ...

  6. Android Studio的配置问题——Intel HAXM is required to run this AVD,VT-x is disabled in BIOS;

    Intel HAXM is required to run this AVD,VT-x is disabled in BIOS;Enable VT-x in your BIOS security se ...

  7. 大熊君大话NodeJS之------Connect中间件模块(第一季)

    一,开篇分析 截止到今天来说,NodeJS系列文章已经有将近十篇了,让我们回顾一下: (1),大熊君大话NodeJS之开篇------Why NodeJS(将Javascript进行到底) (2),大 ...

  8. Ubuntu 14 中,SecureCRT、SecureFX个性化设置

    [SecureCRT 个性化设置] 打开设置路径:菜单栏 -> Opions -> Global Options -> General -> Default Session - ...

  9. Mac中安装Vim7.4

    Mac上的Vim Mac本身其实是预装了Vim的,但是目前的系统中都是Vim7.3版本的,而最新的Vim已经是7.4版了,因此为了能够使用最新版的vim,必须要对Mac中的vim要么升级,要么重装.在 ...

  10. CentOS 7.0,启用iptables防火墙

    CentOS 7.0默认使用的是firewall作为防火墙,这里改为iptables防火墙. 1.关闭firewall: systemctl stop firewalld.service #停止fir ...