一、输入格式

(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. MongoDB学习笔记一

    操作系统:Windows7 1.下载MongoDB 2.6.5服务端,并安装 网址:http://pan.baidu.com/s/1dDfoJAh 说明:网上很多都不需要安装的,这个需要安装. 2.添 ...

  2. XRecyclerView Scrapped or attached views may not be recycled

    将XRecyclerView布局设置为 android:layout_width="match_parent"android:layout_height="match_p ...

  3. [转] form.getForm().submit的用法及Ext.Ajax.request的小小区别

    原文地址:http://blog.csdn.net/hongleidy5000/article/details/7329325 if (!formDetail.getForm().isValid()) ...

  4. poi导出的excel的数字小数位过多?

    最近在使用Apache的POI组件对Excel进行操作,在对excel导出的时候,导出的数字本来只有两位小数,得到的结果就变成了很多位小数.如下面的图所示: 虽然对单元格使用了setCellStyle ...

  5. Angular双向数据绑定MVVM以及基本模式分析

    MVVM: angular的MVVM实现的是双向数据绑定,模型从服务器端抓取到数据,将数据通过控制器(controller)传递到视图(view)显示,视图数据发生变化时同样也会影响到模型数据的变化, ...

  6. Problems about trees

    Problems (1) 给一棵带边权的树,求遍历这棵树(每个节点至少经过一次)再回到起点的最短路程. 答案是显然的:边权之和的两倍. (2)给一棵带边权的树,求遍历这棵树(每个节点至少经过一次)的最 ...

  7. python3的基础练习题

    1. 执行 Python 脚本的两种方式 1)/usr/bin/python3 xx.py 2)python3 xx.py #注xx.py需要在内容里面调用由什么解释器执行 2. 简述位.字节的关系 ...

  8. [Unity] Shader - CG语言 流程控制语句

    CG语言中: 不支持 switch 语句(可以写,但不能很好的执行.) 循环语句中, 循环次数不能大于 1024 ,否则会报错. If...ELSE 条件判断语句: if (true) { } els ...

  9. LINQ驱动数据的查询功能

    一.LINQ概念 LINQ是微软在.NetFramework3.5中新加入的语言功能,在语言中以程序代码方式处理集合的能力. 1.1 LINQ VS 循环处理 在我刚工作时候,对于集合对象的处理一般是 ...

  10. IOS热更新-JSPatch实现原理+Patch现场恢复

    关于HotfixPatch 在IOS开发领域,由于Apple严格的审核标准和低效率,IOS应用的发版速度极慢,稍微大型的app发版基本上都在一个月以上,所以代码热更新(HotfixPatch)对于IO ...