问题:按要求文件名输出结果,比如这里我要求对一个输入文件中的WARN,INFO,ERROR,的信息项进行分析,并分别输入到对应的以WARN,INFO。ERROR和OTHER开头的结果文件中,其中结果文件包含对应的相关信息。

输入文件:

    输入文件为hadoop的一些logs日志信息文件,比如:

示例程序:

  1. package com.map.splitFile;
  2. import java.io.IOException;
  3. import java.net.URI;
  4. import java.net.URISyntaxException;
  5. import java.util.regex.Pattern;
  6. import org.apache.hadoop.conf.Configuration;
  7. import org.apache.hadoop.conf.Configured;
  8. import org.apache.hadoop.fs.FileSystem;
  9. import org.apache.hadoop.fs.Path;
  10. import org.apache.hadoop.io.IntWritable;
  11. import org.apache.hadoop.io.LongWritable;
  12. import org.apache.hadoop.io.Text;
  13. import org.apache.hadoop.mapreduce.Job;
  14. import org.apache.hadoop.mapreduce.Mapper;
  15. import org.apache.hadoop.mapreduce.Reducer;
  16. import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
  17. import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
  18. import org.apache.hadoop.mapreduce.lib.output.MultipleOutputs;
  19. import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
  20. public class SplitFilesToResult extends Configured{
  21. @SuppressWarnings("deprecation")
  22. public static void main(String[] args) {
  23. String in = "/SplitFilesToResult/input";
  24. String out = "/SplitFilesToResult/output";
  25. Job job;
  26. try {
  27. //删除hdfs目录
  28. SplitFilesToResult wc2 = new SplitFilesToResult();
  29. wc2.removeDir(out);
  30. job = new Job(new Configuration(), "wordcount Job");
  31. job.setOutputKeyClass(Text.class);
  32. job.setOutputValueClass(Text.class);
  33. job.setMapperClass(mapperString.class);
  34. job.setReducerClass(reduceStatistics.class);
  35. //定义附加的输出文件
  36. MultipleOutputs.addNamedOutput(job,"INFO",TextOutputFormat.class,Text.class,Text.class);
  37. MultipleOutputs.addNamedOutput(job,"ERROR",TextOutputFormat.class,Text.class,Text.class);
  38. MultipleOutputs.addNamedOutput(job,"WARN",TextOutputFormat.class,Text.class,Text.class);
  39. MultipleOutputs.addNamedOutput(job,"OTHER",TextOutputFormat.class,Text.class,Text.class);
  40. FileInputFormat.addInputPath(job, new Path(in));
  41. FileOutputFormat.setOutputPath(job, new Path(out));
  42. job.waitForCompletion(true);
  43. FileSystem fs = FileSystem.get(new URI("hdfs://localhost:9000"), new Configuration());
  44. fs.delete(new Path("/SplitFilesToResult/output/part-r-00000"));
  45. } catch (IOException e) {
  46. e.printStackTrace();
  47. } catch (URISyntaxException e) {
  48. e.printStackTrace();
  49. } catch (ClassNotFoundException e) {
  50. e.printStackTrace();
  51. } catch (InterruptedException e) {
  52. e.printStackTrace();
  53. }
  54. }
  55. @SuppressWarnings("deprecation")
  56. public void removeDir(String filePath) throws IOException, URISyntaxException{
  57. String url = "hdfs://localhost:9000";
  58. FileSystem fs = FileSystem.get(new URI(url), new Configuration());
  59. fs.delete(new Path(filePath));
  60. }
  61. }
  62. /**
  63. * 重写maptask使用的map方法
  64. * @author nange
  65. *
  66. */
  67. class mapperString extends Mapper<LongWritable, Text, Text, Text>{
  68. //设置正则表达式的编译表达形式
  69. public static Pattern PATTERN = Pattern.compile(" ");
  70. @Override
  71. protected void map(LongWritable key, Text value, Context context)
  72. throws IOException, InterruptedException {
  73. String[] words = PATTERN.split(value.toString());
  74. System.out.println("********" + value.toString());
  75. if(words.length >= 2){
  76. if(words.length == 2){
  77. context.write(new Text("ERROR"), new Text(value.toString()));
  78. }else if(words[0].equals("at")){
  79. context.write(new Text("ERROR"), new Text(value.toString()));
  80. }else{
  81. context.write(new Text(words[2]), new Text(value.toString()));
  82. }
  83. }else
  84. context.write(new Text("OTHER"), new Text(value.toString()));
  85. }
  86. }
  87. /**
  88. * 对单词做统计
  89. * @author nange
  90. *
  91. */
  92. class reduceStatistics extends Reducer<Text, Text, Text, Text>{
  93. //将结果输出到多个文件或多个文件夹
  94. private MultipleOutputs<Text,Text> mos;
  95. //创建MultipleOutputs对象
  96. protected void setup(Context context) throws IOException,InterruptedException {
  97. mos = new MultipleOutputs<Text, Text>(context);
  98. }
  99. @Override
  100. protected void reduce(Text key, Iterable<Text> values, Context context)
  101. throws IOException, InterruptedException {
  102. for(Text t: values){
  103. //使用MultipleOutputs对象输出数据
  104. if(key.toString().equals("INFO")){
  105. mos.write("INFO", "", t);
  106. }else if(key.toString().equals("ERROR")){
  107. mos.write("ERROR", "", t);
  108. }else if(key.toString().equals("WARN")){
  109. //输出到hadoop/hadoopfile-r-00000文件
  110. mos.write("WARN", "", t, "WARN");
  111. }else{
  112. mos.write("OTHER", "", t);
  113. }
  114. }
  115. }
  116. //关闭MultipleOutputs对象
  117. protected void cleanup(Context context) throws IOException,InterruptedException {
  118. mos.close();
  119. }
  120. }

MapReduce编程练习(三),按要求不同文件名输出结果的更多相关文章

  1. mapreduce编程--(准备篇)

    mapreduce编程准备 学习mapreduce编程之前需要做一些概念性的了解,这是做的一些课程学习笔记,以便以后时不时的翻出来学习下,之前看过一篇文章大神们都是时不时的翻出基础知识复习下,我也做点 ...

  2. 三、MapReduce编程实例

    前文 一.CentOS7 hadoop3.3.1安装(单机分布式.伪分布式.分布式 二.JAVA API实现HDFS MapReduce编程实例 @ 目录 前文 MapReduce编程实例 前言 注意 ...

  3. Hadoop MapReduce编程 API入门系列之压缩和计数器(三十)

    不多说,直接上代码. Hadoop MapReduce编程 API入门系列之小文件合并(二十九) 生成的结果,作为输入源. 代码 package zhouls.bigdata.myMapReduce. ...

  4. MapReduce编程模型详解(基于Windows平台Eclipse)

    本文基于Windows平台Eclipse,以使用MapReduce编程模型统计文本文件中相同单词的个数来详述了整个编程流程及需要注意的地方.不当之处还请留言指出. 前期准备 hadoop集群的搭建 编 ...

  5. Javascript模块化编程(三):require.js的用法

    Javascript模块化编程(三):require.js的用法 原文地址:http://www.ruanyifeng.com/blog/2012/11/require_js.html 作者: 阮一峰 ...

  6. mapreduce编程模型你知道多少?

    上次新霸哥给大家介绍了一些hadoop的相关知识,发现大家对hadoop有了一定的了解,但是还有很多的朋友对mapreduce很模糊,下面新霸哥将带你共同学习mapreduce编程模型. mapred ...

  7. hadoop2.2编程:使用MapReduce编程实例(转)

    原文链接:http://www.cnblogs.com/xia520pi/archive/2012/06/04/2534533.html 从网上搜到的一篇hadoop的编程实例,对于初学者真是帮助太大 ...

  8. MapReduce 编程模型

    一.简单介绍 1.MapReduce 应用广泛的原因之中的一个在于它的易用性.它提供了一个因高度抽象化而变得异常简单的编程模型. 2.从MapReduce 自身的命名特点能够看出,MapReduce ...

  9. 暴力破解MD5的实现(MapReduce编程)

    本文主要介绍MapReduce编程模型的原理和基于Hadoop的MD5暴力破解思路. 一.MapReduce的基本原理 Hadoop作为一个分布式架构的实现方案,它的核心思想包括以下几个方面:HDFS ...

随机推荐

  1. 11. const 修饰成员函数

    const 限定只读,对函数的实参进行保护 常数据成员:必须出现在类的定义体中,常数据成员必须进行初始化,并且不能被更新,但常数据成员的初始化只能通过构造函数的初始化列表进行 1. 常函数 成员函数加 ...

  2. kafka 0.8+spark offset 提交至mysql

    kafka版本:<kafka.version> 0.8.2.1</kafka.version> spark版本 <artifactId>spark-streamin ...

  3. Npoi XWPF Word 导出时插入图片无法显示 bug 完美解决

    一.来自客户的需求 最近客户来个新需求生成一个word 标签纸,并且需要在标签纸上插入一个logo,并且将erp 中的数据取出来自动写在文档上,不由得淡淡一笑,这不难呀! 于是乎我就写下了这样的代码: ...

  4. 网络爬虫第一步:通用代码框架(python版)

    import requests def getHTMLText(url):     try:         r=requests.get(url,timeout=30)         r.rais ...

  5. Macbook 安装Windows的完美教程

    [原文](http://www.melodydance.top/mac-win.html) 1. 背景 Windows相对于Mac市场占有率更高,对很多人来说Windows使用起来更方便,以至于很多人 ...

  6. 【SpringBoot1.x】SpringBoot1.x 开发热部署和监控管理

    SpringBoot1.x 开发热部署和监控管理 热部署 在开发中我们修改一个 Java 文件后想看到效果不得不重启应用,这导致大量时间花费,我们希望不重启应用的情况下,程序可以自动部署(热部署). ...

  7. 【Linux】kali 安装 python3 和 pip3(亲测有效)

    [Linux]kali 安装 python3 和 pip3 引言:   在使用kali的时候,经常会用到各种工具以及脚本,而大多数脚本都是以python编写的,但是烦就烦在python有2个版本,有些 ...

  8. Redis 5 配置 Redis sentinel(哨兵模式)

    先了解一下哨兵都 做了什么工作:Redis 的 Sentinel 系统用于管理多个 Redis 服务器(instance), 该系统执行以下三个任务: * 监控(Monitoring): Sentin ...

  9. Docker学习笔记之使用Docker数据卷

    Docker数据卷将数据存储到主机而非容器,多个容器需要共享数据时,常常使用数据卷. 1. 为容器设置数据卷(不指定主机目录) 2. 容器与主机之间.容器与容器之间共享数据卷(指定主机目录) 3. 使 ...

  10. 九:APP及其他资产

    APP提取一键反编译提取 APP抓数据包进行工具配合 各种第三方应用相关探针技术 各种服务器接口相关探针技术 APP提取及抓包及后续配合 某APK一键提取反编译 利用burp历史抓更多URL 某IP无 ...