@

OutputFormat接口实现类

OutputFormatMapReduce输出的基类,所有实现MapReduce输出都实现了OutputFormat接口。下面介绍几种常见的OutputFormat实现类。

  • 文本输出TextoutputFormat

    默认的输出格式是TextOutputFormat,它把每条记录写为文本行。它的键和值可以是任意类型,因为TextOutputFormat调用toString()方法把它们转换为字符串。

  • SequenceFileOutputFormat

    将SecquenceFileOutputFormat输出作为后续MapReduce任务的输入,这便是一种好的输出格式,因为它的格式紧凑,很容易被压缩。

  • 自定义OutputFormat

    根据用户需求,自定义实现输出。

自定义OutputFormat使用场景及步骤

使用场景

  • 为了实现控制最终文件的输出路径和输出格式,可以自定义OutputFormat。

    例如:要在一个MapReduce程序中根据数据的不同输出两类结果到不同目录,这类灵活的输出需求可以通过自定义OutputFormat来实现。
  • 自定义OutputFormat步骤

    (1)自定义一个类继承FileOutputFormat

    (2)改写RecordWriter,具体改写输出数据的方法write()

自定义OutputFormat 案例实操

需求

过滤输入的log日志,包含atguigu的网站输出到e:/atguigu.log,不包含atguigu的网站输出到e:/other.log。

输入数据



什么时候需要Reduce

①合并

②需要对数据排序

所以本案例不需要Reduce阶段,key-value不需要实现序列化

CustomOFMapper.java

public class CustomOFMapper extends Mapper<LongWritable, Text, String, NullWritable>{

	@Override
protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, String, NullWritable>.Context context) throws IOException, InterruptedException { String content = value.toString();
//value不需要,但是不能用Null这个关键字,要使用NullWritable对象
context.write(content+"\r\n", NullWritable.get());
} }

MyOutPutFormat.java

public class MyOutPutFormat extends FileOutputFormat<String, NullWritable>{

	@Override
public RecordWriter<String, NullWritable> getRecordWriter(TaskAttemptContext job)
throws IOException, InterruptedException {
return new MyRecordWriter(job);//传递job对象,才能在RecordWriter中获取配置
} }

MyRecordWriter.java

public class MyRecordWriter extends RecordWriter<String, NullWritable> {

	private Path atguiguPath=new Path("e:/atguigu.log");
private Path otherPath=new Path("e:/other.log"); private FSDataOutputStream atguguOS ;
private FSDataOutputStream otherOS ; private FileSystem fs; private TaskAttemptContext context; public MyRecordWriter(TaskAttemptContext job) throws IOException { context=job; Configuration conf = job.getConfiguration(); fs=FileSystem.get(conf); atguiguOS = fs.create(atguiguPath);
otherOS = fs.create(otherPath);
} // 将key-value写出到文件
@Override
public void write(String key, NullWritable value) throws IOException, InterruptedException { if (key.contains("atguigu")) {
atguguOS.write(key.getBytes());//写到atguigu.log
//统计输出的含有atguigu字符串的key-value个数
context.getCounter("MyCounter", "atguiguCounter").increment(1);
}else {
otherOS.write(key.getBytes());//写到other.log
context.getCounter("MyCounter", "otherCounter").increment(1);
}
} // 关闭流
@Override
public void close(TaskAttemptContext context) throws IOException, InterruptedException { if (atguguOS != null) {
IOUtils.closeStream(atguguOS);
} if (otherOS != null) {
IOUtils.closeStream(otherOS);
} if (fs != null) {
fs.close();
} }
}

CustomOFDriver.java

public class CustomOFDriver {

	public static void main(String[] args) throws Exception {

		Path inputPath=new Path("e:/mrinput/outputformat");
Path outputPath=new Path("e:/mroutput/outputformat"); //作为整个Job的配置
Configuration conf = new Configuration();
//保证输出目录不存在
FileSystem fs=FileSystem.get(conf); if (fs.exists(outputPath)) {
fs.delete(outputPath, true);
} // ①创建Job
Job job = Job.getInstance(conf); //重点,设置为自定义的输出格式
job.setJarByClass(CustomOFDriver.class); // ②设置Job
// 设置Job运行的Mapper,Reducer类型,Mapper,Reducer输出的key-value类型
job.setMapperClass(CustomOFMapper.class); // 设置输入目录和输出目录
FileInputFormat.setInputPaths(job, inputPath);
FileOutputFormat.setOutputPath(job, outputPath); // 设置输入和输出格式
job.setOutputFormatClass(MyOutPutFormat.class); // 取消reduce阶段。设置为0,默认为1
job.setNumReduceTasks(0); // ③运行Job
job.waitForCompletion(true); }
}

输出文件:



MapReduce之自定义OutputFormat的更多相关文章

  1. 第3节 mapreduce高级:7、自定义outputformat实现输出到不同的文件夹下面

    2.1 需求 现在有一些订单的评论数据,需求,将订单的好评与差评进行区分开来,将最终的数据分开到不同的文件夹下面去,数据内容参见资料文件夹,其中数据第九个字段表示好评,中评,差评.0:好评,1:中评, ...

  2. Hadoop案例(五)过滤日志及自定义日志输出路径(自定义OutputFormat)

    过滤日志及自定义日志输出路径(自定义OutputFormat) 1.需求分析 过滤输入的log日志中是否包含xyg (1)包含xyg的网站输出到e:/xyg.log (2)不包含xyg的网站输出到e: ...

  3. Hadoop_27_MapReduce_运营商原始日志增强(自定义OutputFormat)

    1.需求: 现有一些原始日志需要做增强解析处理,流程: 1. 从原始日志文件中读取数据(日志文件:https://pan.baidu.com/s/12hbDvP7jMu9yE-oLZXvM_g) 2. ...

  4. hadoop 自定义OutputFormat

    1.继承FileOutputFormat,复写getRecordWriter方法 /** * @Description:自定义outputFormat,输出数据到不同的文件 */ public cla ...

  5. 关于spark写入文件至文件系统并制定文件名之自定义outputFormat

    引言: spark项目中通常我们需要将我们处理之后数据保存到文件中,比如将处理之后的RDD保存到hdfs上指定的目录中,亦或是保存在本地 spark保存文件: 1.rdd.saveAsTextFile ...

  6. 关于MapReduce中自定义分区类(四)

    MapTask类 在MapTask类中找到run函数 if(useNewApi){       runNewMapper(job, splitMetaInfo, umbilical, reporter ...

  7. 关于MapReduce中自定义分组类(三)

    Job类  /**    * Define the comparator that controls which keys are grouped together    * for a single ...

  8. 关于MapReduce中自定义带比较key类、比较器类(二)——初学者从源码查看其原理

    Job类 /**   * Define the comparator that controls    * how the keys are sorted before they   * are pa ...

  9. 关于MapReduce中自定义Combine类(一)

    MRJobConfig      public static fina COMBINE_CLASS_ATTR      属性COMBINE_CLASS_ATTR = "mapreduce.j ...

随机推荐

  1. ADB-常见命令使用详解

    ADB命令使用详解 ADB是一个 客户端-服务器端 程序, 其中客户端是你用来操作的电脑, 服务器端是android设备. 1.连接android设置adb connect 设备名例如:adb con ...

  2. [斯坦福大学2014机器学习教程笔记]第六章-决策界限(decision boundary)

    这一节主要介绍的是决策界限(decision boundary)的概念,这个概念可以帮组我们更好地理解逻辑回归的假设函数在计算什么. 首先回忆一下上次写的公式. 现在让我们进一步了解这个假设函数在什么 ...

  3. 【一起学系列】之命令模式:封装一个简单Jedis

    意图 将一个请求封装为一个对象,从而使你可用不同的请求对客户进行参数化:对请求排队或记录请求日志,以及支持可撤销的操作. 命令模式的诞生 [产品]:开发小哥,来活啦,咱们需要设计一款遥控器,核心功能就 ...

  4. J.U.C体系进阶(二):juc-locks 锁框架

    Java - J.U.C体系进阶 作者:Kerwin 邮箱:806857264@qq.com 说到做到,就是我的忍道! juc-locks 锁框架 接口说明 Lock接口 类型 名称 void loc ...

  5. Python Ethical Hacking - VULNERABILITY SCANNER(8)

    Implementing Code To Discover XSS in Parameters 1. Watch the URL of the XSS reflected page carefully ...

  6. Python Ethical Hacking - WEB PENETRATION TESTING(5)

    Guessing Login Information on Login Pages Our target website: http://10.0.0.45/dvwa/login.php #!/usr ...

  7. T4 分配时间 题解

    问题描述 小王参加的考试是几门科目的试卷放在一起考,一共给 t 分钟来做.他现在已经知道每 门科目花的时间和得到的分数的关系,还有写名字要的时间(他写自己的名字很慢)请帮他 算一下他最高能得几分.总分 ...

  8. O、Θ、Ω、o、ω,别再傻傻分不清了!

    前言 本篇文章收录于专辑:http://dwz.win/HjK,点击解锁更多数据结构与算法的知识. 你好,我是彤哥,一个每天爬二十六层楼还不忘读源码的硬核男人. 前面几节,我们一起学习了算法的复杂度如 ...

  9. undefined reference to `typeinfo for xxx 报错

    编译成功了,链接的时候出现了这个报错 产生”undefined reference to `typeinfo for xxx’“最常见的原因就是基类的虚函数未实现了. 由于C++类的实现可以分布在多个 ...

  10. Python游戏编程入门 中文pdf扫描版|网盘下载内附地址提取码|

    Python是一种解释型.面向对象.动态数据类型的程序设计语言,在游戏开发领域,Python也得到越来越广泛的应用,并由此受到重视. 本书教授用Python开发精彩游戏所需的[]为重要的该你那.本书不 ...