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 ...
随机推荐
- 轻松理解UML用例图时序图类图的教程
摘自https://zhuanlan.zhihu.com/p/29874146 写在前面 当你老大扔给你这样的图,或者你需要完成某些功能而去看文档的时候发现以下类似这样的图会不会不(一)知(脸)所(懵 ...
- 死磕以太坊源码分析之MPT树-下
死磕以太坊源码分析之MPT树-下 文章以及资料请查看:https://github.com/blockchainGuide/ 上篇主要介绍了以太坊中的MPT树的原理,这篇主要会对MPT树涉及的源码进行 ...
- Mysql性能监控可视化
前言 操作系统以及Mysql数据库的实时性能状态数据尤为重要,特别是在有性能抖动的时候,这些实时的性能数据可以快速帮助你定位系统或Mysql数据库的性能瓶颈,镜像你在Linux系统上使用top.i ...
- 【Redis3.0.x】配置文件
Redis3.0.x 配置文件 概述 Redis 的配置文件位于Redis安装目录下,文件名为 redis.conf. 可以通过 CONFIG 命令查看或设置配置项. Redis 命令不区分大小写. ...
- mmal商城商品模块总结
学习目标 FTP服务器的对接 SpringMVC文件上传 流读取properties配置文件 抽象POJO.BO.VO对象之间的转换关系及解决思路 joda-time快速入门 静态代码块 mybati ...
- Linux 入门教程:基础操作 01
1.1 实验内容 实验楼环境介绍 常用 Shell 命令及快捷键 Linux 使用小技巧 1.2 实验知识点 Linux 基本命令 通配符的使用 查看帮助文档 终端的概念 通常我们在使用 Linux ...
- Java开发手册之工程结构
1.在线上生产环境,JVM 的 Xms 和 Xmx 设置一样大小的内存容量,避免在 GC 后调整堆大小带来的压力. 2.给 JVM 环境参数设置-XX:+HeapDumpOnOutOfMemoryEr ...
- 洛谷P1198 [JSOI2008]最大数(线段树/单调栈)
题目链接: https://www.luogu.org/problemnew/show/P1198 题目描述 现在请求你维护一个数列,要求提供以下两种操作: 1. 查询操作. 语法:Q L 功能:查询 ...
- 安装newman error:package exports for 'c:\nmp\node_modules\newman\node_module 解决办法
一.场景描述: 通过npm安装newman时,一直失败. 尝试了很多安装命令: npm install -g newman npm install -g newman --registry=http: ...
- 实现简易版德州扑克|学习麻瓜编程以项目为导向入门前端 HTML+CSS+JS
实现简易版德州扑克 1.先上达到网页效果图(简易版德州扑克) 网页分为发牌区和牌池,上面为发牌区,下面是牌池区 2. 代码实现 2.1 HTML和JS代码 ` <link rel="s ...