过滤日志自定义日志输出路径(自定义OutputFormat)

1.需求分析

过滤输入的log日志中是否包含xyg

(1)包含xyg的网站输出到e:/xyg.log

(2)不包含xyg的网站输出到e:/other.log

2.数据准备

http://www.baidu.com
http://www.google.com
http://cn.bing.com
http://www.xyg.com
http://www.sohu.com
http://www.sina.com
http://www.sin2a.com
http://www.sin2desa.com
http://www.sindsafa.com

log.txt

输出预期:

http://www.xyg.com

xyg.txt

http://cn.bing.com
http://www.baidu.com
http://www.google.com
http://www.sin2a.com
http://www.sin2desa.com
http://www.sina.com
http://www.sindsafa.com
http://www.sohu.com

other.txt

3.代码实现

(1)自定义一个outputformat

package com.xyg.mapreduce.outputformat;
import java.io.IOException;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.RecordWriter;
import org.apache.hadoop.mapreduce.TaskAttemptContext;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; public class FilterOutputFormat extends FileOutputFormat<Text, NullWritable>{ @Override
public RecordWriter<Text, NullWritable> getRecordWriter(TaskAttemptContext job) throws IOException, InterruptedException { // 创建一个RecordWriter
return new FilterRecordWriter(job);
}
}

(2)具体的写数据RecordWriter

package com.xyg.mapreduce.outputformat;
import java.io.IOException;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.RecordWriter;
import org.apache.hadoop.mapreduce.TaskAttemptContext; public class FilterRecordWriter extends RecordWriter<Text, NullWritable> {
FSDataOutputStream atguiguOut = null;
FSDataOutputStream otherOut = null; public FilterRecordWriter(TaskAttemptContext job) {
// 1 获取文件系统
FileSystem fs; try {
fs = FileSystem.get(job.getConfiguration()); // 2 创建输出文件路径
Path atguiguPath = new Path("e:/xyg.log");
Path otherPath = new Path("e:/other.log"); // 3 创建输出流
atguiguOut = fs.create(atguiguPath);
otherOut = fs.create(otherPath);
} catch (IOException e) {
e.printStackTrace();
}
} @Override
public void write(Text key, NullWritable value) throws IOException, InterruptedException { // 判断是否包含“xyg”输出到不同文件
if (key.toString().contains("xyg")) {
atguiguOut.write(key.toString().getBytes());
} else {
otherOut.write(key.toString().getBytes());
}
} @Override
public void close(TaskAttemptContext context) throws IOException, InterruptedException {
// 关闭资源
if (atguiguOut != null) {
atguiguOut.close();
} if (otherOut != null) {
otherOut.close();
}
}
}

(3)编写FilterMapper

package com.xyg.mapreduce.outputformat;
import java.io.IOException;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper; public class FilterMapper extends Mapper<LongWritable, Text, Text, NullWritable>{ Text k = new Text(); @Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
// 1 获取一行
String line = value.toString(); k.set(line); // 3 写出
context.write(k, NullWritable.get());
}
}

(4)编写FilterReducer

package com.xyg.mapreduce.outputformat;
import java.io.IOException;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer; public class FilterReducer extends Reducer<Text, NullWritable, Text, NullWritable> { @Override
protected void reduce(Text key, Iterable<NullWritable> values, Context context) throws IOException, InterruptedException { String k = key.toString();
k = k + "\r\n"; context.write(new Text(k), NullWritable.get());
}
}

(5)编写FilterDriver

package com.xyg.mapreduce.outputformat;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; public class FilterDriver {
public static void main(String[] args) throws Exception { args = new String[] { "e:/inputoutputformat", "e:/output2" }; Configuration conf = new Configuration(); Job job = Job.getInstance(conf); job.setJarByClass(FilterDriver.class);
job.setMapperClass(FilterMapper.class);
job.setReducerClass(FilterReducer.class); job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(NullWritable.class); job.setOutputKeyClass(Text.class);
job.setOutputValueClass(NullWritable.class); // 要将自定义的输出格式组件设置到job中
job.setOutputFormatClass(FilterOutputFormat.class); FileInputFormat.setInputPaths(job, new Path(args[])); // 虽然我们自定义了outputformat,但是因为我们的outputformat继承自fileoutputformat
// 而fileoutputformat要输出一个_SUCCESS文件,所以,在这还得指定一个输出目录
FileOutputFormat.setOutputPath(job, new Path(args[])); boolean result = job.waitForCompletion(true);
System.exit(result ? : );
}
}

Hadoop案例(五)过滤日志及自定义日志输出路径(自定义OutputFormat)的更多相关文章

  1. Flink FileSink 自定义输出路径——StreamingFileSink、BucketingSink 和 StreamingFileSink简单比较

    接上篇:Flink FileSink 自定义输出路径——BucketingSink 上篇使用BucketingSink 实现了自定义输出路径,现在来看看 StreamingFileSink( 据说是S ...

  2. Hadoop案例(一)之日志清洗

    日志清洗案例 一. 简单解析版 1)需求 去除日志中字段长度小于等于11的日志. 2)输入数据 /Sep/::: +] "-" "Mozilla/4.0 (compati ...

  3. 《手把手教你》系列基础篇(八十五)-java+ selenium自动化测试-框架设计基础-TestNG自定义日志-下篇(详解教程)

    1.简介 TestNG为日志记录和报告提供的不同选项.现在,宏哥讲解分享如何开始使用它们.首先,我们将编写一个示例程序,在该程序中我们将使用 ITestListener方法进行日志记录. 2.Test ...

  4. 《手把手教你》系列基础篇(九十五)-java+ selenium自动化测试-框架之设计篇-java实现自定义日志输出(详解教程)

    1.简介 前面宏哥一连几篇介绍如何通过开源jar包Log4j.jar.log4j2.jar和logback实现日志文件输出,Log4j和logback确实很强大,能生成三种日志文件,一种是保存到磁盘的 ...

  5. log4j分离日志输出 自定义过滤 自定义日志文件

    普通的log4j.properties 定义: ### set log levels ### log4j.rootLogger = debug,D,E ## Disable other log log ...

  6. ELK收集Nginx自定义日志格式输出

    1.ELK收集日志的有两种常用的方式: 1.1:不修改源日志格式,简单的说就是在logstash中转通过 grok方式进行过滤处理,将原始无规则的日志转换为规则日志(Logstash自定义日志格式) ...

  7. ATS配置自定义日志

    修改records.config,开启日志自定义功能 更改日志目录,默认日志存放在/var/log/trafficserver: CONFIG proxy.config.log.logfile_dir ...

  8. SpringBoot系列(十三)统一日志处理,logback+slf4j AOP+自定义注解,走起!

    往期精彩推荐 SpringBoot系列(一)idea新建Springboot项目 SpringBoot系列(二)入门知识 springBoot系列(三)配置文件详解 SpringBoot系列(四)we ...

  9. Nginx日志配置及日志分析脚本案例

    https://blog.csdn.net/bbwangj/article/details/82186162 nginx的log日志分为access log 和 error log 其中access ...

随机推荐

  1. python基础----递归函数(二分法、最大深度递归)

    递归函数 定义:在函数内部,可以调用其他函数.如果一个函数在内部调用自身本身,这个函数就是递归函数. #例子1 # age()=age()+ n= age(n)=age(n-)+ # age()=ag ...

  2. 随机抽样一致性算法(RANSAC)示例及源代码--转载

    转载自王先荣 http://www.cnblogs.com/xrwang/p/SampleOfRansac.html 作者:王先荣 大约在两年前翻译了<随机抽样一致性算法RANSAC>,在 ...

  3. git 还原某个文件到特定版本

    1.先使用 git log 查看需要还原的版本号 2.git checkout <版本号> <文件相对路径> 3.git commit -m "xxx"

  4. crontab 自动执行脚本

    crontab -e ================>自动执行某脚本!!!!!!! 1001 ls 1002 cd /home/wwwroot/default/ 1003 ls 1004 cr ...

  5. Ajax笔记-加强版

    AJAX :   Asynchronous JavaScript and XML 异步JavaScript和XML   用javascript异步形式去操作xml 进行数据交互   节省用户操作,时间 ...

  6. java请求url返回json

    package cn.it.test; import java.io.BufferedReader; import java.io.IOException; import java.io.InputS ...

  7. Codeforces 620C EDU C.Pearls in a Row ( set + greed )

    C. Pearls in a Row There are n pearls in a row. Let's enumerate them with integers from 1 to n from ...

  8. jQuery面向对象的写法

    定义的写法 //构造函数 function test(){ //construct code } //初始化方法 test.prototype.init = function(){ //init co ...

  9. python Multiprocessing 多进程应用

    在运维工作中,经常要处理大量数据,或者要跑一些时间比较长的任务,可能都需要用到多进程,不管是管理端下发任务,还是客户端执行任务,如果服务器配置还可以,跑多进程还是挺能解决问题的 Multiproces ...

  10. Crash Consistency : FSCK and Journaling

    现在开始今天的第三篇博客的撰写,不能扯淡了,好多任务啊.但是还是忍不住吐槽一下,之前选择这篇文章纯属是个意外,我把Crash看做了Cache,唉,要不然也就不用写这篇文章了. 1. 这篇博客讲什么? ...