MapReduce编程练习(三),按要求不同文件名输出结果
问题:按要求文件名输出结果,比如这里我要求对一个输入文件中的WARN,INFO,ERROR,的信息项进行分析,并分别输入到对应的以WARN,INFO。ERROR和OTHER开头的结果文件中,其中结果文件包含对应的相关信息。
输入文件:
输入文件为hadoop的一些logs日志信息文件,比如:
示例程序:
package com.map.splitFile;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.regex.Pattern;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.FileSystem;
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.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.mapreduce.lib.output.MultipleOutputs;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
public class SplitFilesToResult extends Configured{
@SuppressWarnings("deprecation")
public static void main(String[] args) {
String in = "/SplitFilesToResult/input";
String out = "/SplitFilesToResult/output";
Job job;
try {
//删除hdfs目录
SplitFilesToResult wc2 = new SplitFilesToResult();
wc2.removeDir(out);
job = new Job(new Configuration(), "wordcount Job");
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class);
job.setMapperClass(mapperString.class);
job.setReducerClass(reduceStatistics.class);
//定义附加的输出文件
MultipleOutputs.addNamedOutput(job,"INFO",TextOutputFormat.class,Text.class,Text.class);
MultipleOutputs.addNamedOutput(job,"ERROR",TextOutputFormat.class,Text.class,Text.class);
MultipleOutputs.addNamedOutput(job,"WARN",TextOutputFormat.class,Text.class,Text.class);
MultipleOutputs.addNamedOutput(job,"OTHER",TextOutputFormat.class,Text.class,Text.class);
FileInputFormat.addInputPath(job, new Path(in));
FileOutputFormat.setOutputPath(job, new Path(out));
job.waitForCompletion(true);
FileSystem fs = FileSystem.get(new URI("hdfs://localhost:9000"), new Configuration());
fs.delete(new Path("/SplitFilesToResult/output/part-r-00000"));
} catch (IOException e) {
e.printStackTrace();
} catch (URISyntaxException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
@SuppressWarnings("deprecation")
public void removeDir(String filePath) throws IOException, URISyntaxException{
String url = "hdfs://localhost:9000";
FileSystem fs = FileSystem.get(new URI(url), new Configuration());
fs.delete(new Path(filePath));
}
}
/**
* 重写maptask使用的map方法
* @author nange
*
*/
class mapperString extends Mapper<LongWritable, Text, Text, Text>{
//设置正则表达式的编译表达形式
public static Pattern PATTERN = Pattern.compile(" ");
@Override
protected void map(LongWritable key, Text value, Context context)
throws IOException, InterruptedException {
String[] words = PATTERN.split(value.toString());
System.out.println("********" + value.toString());
if(words.length >= 2){
if(words.length == 2){
context.write(new Text("ERROR"), new Text(value.toString()));
}else if(words[0].equals("at")){
context.write(new Text("ERROR"), new Text(value.toString()));
}else{
context.write(new Text(words[2]), new Text(value.toString()));
}
}else
context.write(new Text("OTHER"), new Text(value.toString()));
}
}
/**
* 对单词做统计
* @author nange
*
*/
class reduceStatistics extends Reducer<Text, Text, Text, Text>{
//将结果输出到多个文件或多个文件夹
private MultipleOutputs<Text,Text> mos;
//创建MultipleOutputs对象
protected void setup(Context context) throws IOException,InterruptedException {
mos = new MultipleOutputs<Text, Text>(context);
}
@Override
protected void reduce(Text key, Iterable<Text> values, Context context)
throws IOException, InterruptedException {
for(Text t: values){
//使用MultipleOutputs对象输出数据
if(key.toString().equals("INFO")){
mos.write("INFO", "", t);
}else if(key.toString().equals("ERROR")){
mos.write("ERROR", "", t);
}else if(key.toString().equals("WARN")){
//输出到hadoop/hadoopfile-r-00000文件
mos.write("WARN", "", t, "WARN");
}else{
mos.write("OTHER", "", t);
}
}
}
//关闭MultipleOutputs对象
protected void cleanup(Context context) throws IOException,InterruptedException {
mos.close();
}
}
MapReduce编程练习(三),按要求不同文件名输出结果的更多相关文章
- mapreduce编程--(准备篇)
mapreduce编程准备 学习mapreduce编程之前需要做一些概念性的了解,这是做的一些课程学习笔记,以便以后时不时的翻出来学习下,之前看过一篇文章大神们都是时不时的翻出基础知识复习下,我也做点 ...
- 三、MapReduce编程实例
前文 一.CentOS7 hadoop3.3.1安装(单机分布式.伪分布式.分布式 二.JAVA API实现HDFS MapReduce编程实例 @ 目录 前文 MapReduce编程实例 前言 注意 ...
- Hadoop MapReduce编程 API入门系列之压缩和计数器(三十)
不多说,直接上代码. Hadoop MapReduce编程 API入门系列之小文件合并(二十九) 生成的结果,作为输入源. 代码 package zhouls.bigdata.myMapReduce. ...
- MapReduce编程模型详解(基于Windows平台Eclipse)
本文基于Windows平台Eclipse,以使用MapReduce编程模型统计文本文件中相同单词的个数来详述了整个编程流程及需要注意的地方.不当之处还请留言指出. 前期准备 hadoop集群的搭建 编 ...
- Javascript模块化编程(三):require.js的用法
Javascript模块化编程(三):require.js的用法 原文地址:http://www.ruanyifeng.com/blog/2012/11/require_js.html 作者: 阮一峰 ...
- mapreduce编程模型你知道多少?
上次新霸哥给大家介绍了一些hadoop的相关知识,发现大家对hadoop有了一定的了解,但是还有很多的朋友对mapreduce很模糊,下面新霸哥将带你共同学习mapreduce编程模型. mapred ...
- hadoop2.2编程:使用MapReduce编程实例(转)
原文链接:http://www.cnblogs.com/xia520pi/archive/2012/06/04/2534533.html 从网上搜到的一篇hadoop的编程实例,对于初学者真是帮助太大 ...
- MapReduce 编程模型
一.简单介绍 1.MapReduce 应用广泛的原因之中的一个在于它的易用性.它提供了一个因高度抽象化而变得异常简单的编程模型. 2.从MapReduce 自身的命名特点能够看出,MapReduce ...
- 暴力破解MD5的实现(MapReduce编程)
本文主要介绍MapReduce编程模型的原理和基于Hadoop的MD5暴力破解思路. 一.MapReduce的基本原理 Hadoop作为一个分布式架构的实现方案,它的核心思想包括以下几个方面:HDFS ...
随机推荐
- Turtlebot3入门教程-系统-SBC软件设置(ubuntu20.04)
本文针对如何在树莓派3上安装ubuntu20.04系统和软件进行讲解 树莓派3接上显示屏和鼠标后,开机后继续安装软件包 详细步骤如下: (1)系统安装 (2)ROS安装 (3)TurboBot3依赖的 ...
- ASP.NET Core路由中间件[2]: 路由模式
一个Web应用本质上体现为一组终结点的集合.终结点则体现为一个暴露在网络中可供外界采用HTTP协议调用的服务,路由的作用就是建立一个请求URL模式与对应终结点之间的映射关系.借助这个映射关系,客户端可 ...
- GitHub上的开源复刻:暗黑破坏神2
现在的00后少年可能已经不太了解<暗黑破坏神2>这款由暴雪打造的经典游戏. 该游戏上市的时候,国内个人家用电脑还没有那么普及,网络游戏也没有那么风靡,现在的孩子可能无法想象,那时候网吧里的 ...
- spark进行相同列的join时,只留下A与B关系,不要B与A
一.问题需求: 近期需要做一个商品集合的相关性计算,需要将所有商品进行两两组合笛卡尔积,但spark自带的笛卡尔积会造成过多重复,而且增加join量 假如商品集合里面有: aa aa bb b ...
- Jquery UI Autocomplete 在mvc中应用
首先添加引用 <link href="~/Content/themes/base/jquery-ui.css" rel="stylesheet" type ...
- 【递归】P1157组合的输出
题目相关 题目描述 排列与组合是常用的数学方法,其中组合就是从n个元素中抽出r个元素(不分顺序且 r ≤n),我们可以简单地将n个元素理解为自然数1,2,-,n从中任取r个数. 现要求你输出所有组合. ...
- Laya 踩坑日记-BitmapFont 不显示空格
项目中有用到艺术字,美术通过 bmfont64 将字体导给我了,结果发现在应用上 空格不显示 如图: 今天去深究了一下这个问题,发现是底层没封装好,然后自己改了一下下面是改过的 BitmapFont ...
- 编译安装 nginx -1.14.2
编译安装 nginx -1.14.2 1 ) 下载nginx-1.14.2 源码包: wget http://nginx.org/download/nginx-1.14.2.tar.gz 2 ) 编译 ...
- ASP.NET Core错误处理中间件[2]: 开发者异常页面
<呈现错误信息>通过几个简单的实例演示了如何呈现一个错误页面,该过程由3个对应的中间件来完成.下面先介绍用来呈现开发者异常页面的DeveloperExceptionPageMiddlewa ...
- C# 关机/重启/注销计算机
一.调用 shutdown.exe 执行操作 调用 shutdown.exe 执行计算机关机.重启.注销操作,还可以设置多长时间后执行操作,代码如下: 1 /// <summary> 2 ...