MapReduce 分析nginx日志例子
来源:互联网 发布:餐饮营业额数据分析 编辑:程序博客网 时间:2024/05/16 14:04
package com.sekorm.dataAnalysis.Main;
import java.io.IOException;
import java.util.List;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
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.util.GenericOptionsParser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.sekorm.bean.sekorm.NginxLogBean;
import com.sekorm.dataAnalysis.util.JacksonUtils;
import com.sekorm.dataAnalysis.util.ReadFileUtil;
public class Main {
private static final Logger log = LoggerFactory.getLogger(Main.class);
public static class MyMap extends Mapper<Object, Text, Text, IntWritable>{
private Text word = new Text();
private final static IntWritable one = new IntWritable(1);
public void map(Object key, Text value, Context context) throws IOException, InterruptedException{
NginxLogBean bean=JacksonUtils.toBean(value.toString(), NginxLogBean.class);
word.set(bean.getRemote_addr());
context.write(word, one);
}
}
public static class MyReducer extends Reducer<Text, IntWritable, Text, IntWritable> {
private IntWritable result = new IntWritable();
public void reduce(Text key, Iterable<IntWritable> values,Context context) throws IOException, InterruptedException {
int sum=0;
for(IntWritable val:values) {
sum+=val.get();
}
result.set(sum);
context.write(key, result);
}
}
public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
String[] argArray = new GenericOptionsParser(conf,args).getRemainingArgs();
if(argArray.length!=2){
log.info("需要两个参数");
log.info("job 强制退出");
System.exit(1);
}
Job job = Job.getInstance(conf);
job.setJarByClass(Main.class);
// job.setNumReduceTasks(0);
job.setMapperClass(MyMap.class);
job.setCombinerClass(MyReducer.class);
job.setReducerClass(MyReducer.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
FileSystem fs = FileSystem.get(conf);
List<Path> list=ReadFileUtil.getSubPath(fs,new Path(args[0]));
fs.close();
for(Path path:list){
FileInputFormat.addInputPath(job, path);
}
FileOutputFormat.setOutputPath(job, new Path(args[1]));
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}
import java.io.IOException;
import java.util.List;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
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.util.GenericOptionsParser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.sekorm.bean.sekorm.NginxLogBean;
import com.sekorm.dataAnalysis.util.JacksonUtils;
import com.sekorm.dataAnalysis.util.ReadFileUtil;
public class Main {
private static final Logger log = LoggerFactory.getLogger(Main.class);
public static class MyMap extends Mapper<Object, Text, Text, IntWritable>{
private Text word = new Text();
private final static IntWritable one = new IntWritable(1);
public void map(Object key, Text value, Context context) throws IOException, InterruptedException{
NginxLogBean bean=JacksonUtils.toBean(value.toString(), NginxLogBean.class);
word.set(bean.getRemote_addr());
context.write(word, one);
}
}
public static class MyReducer extends Reducer<Text, IntWritable, Text, IntWritable> {
private IntWritable result = new IntWritable();
public void reduce(Text key, Iterable<IntWritable> values,Context context) throws IOException, InterruptedException {
int sum=0;
for(IntWritable val:values) {
sum+=val.get();
}
result.set(sum);
context.write(key, result);
}
}
public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
String[] argArray = new GenericOptionsParser(conf,args).getRemainingArgs();
if(argArray.length!=2){
log.info("需要两个参数");
log.info("job 强制退出");
System.exit(1);
}
Job job = Job.getInstance(conf);
job.setJarByClass(Main.class);
// job.setNumReduceTasks(0);
job.setMapperClass(MyMap.class);
job.setCombinerClass(MyReducer.class);
job.setReducerClass(MyReducer.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
FileSystem fs = FileSystem.get(conf);
List<Path> list=ReadFileUtil.getSubPath(fs,new Path(args[0]));
fs.close();
for(Path path:list){
FileInputFormat.addInputPath(job, path);
}
FileOutputFormat.setOutputPath(job, new Path(args[1]));
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}
阅读全文
0 0
- MapReduce 分析nginx日志例子
- 日志分析 mapreduce sogou
- hadoop mapreduce分析访问日志
- hadoop mapreduce tomcat日志分析
- awk分析nginx日志
- nginx错误日志分析
- Linux Nginx日志分析
- awstats分析nginx日志
- nginx 日志分析
- nginx分析日志
- Nginx 日志分析
- nginx日志模块分析
- nginx日志分析
- Awstats分析Nginx日志
- nginx日志错误分析
- awstats分析nginx日志
- Awstats分析nginx日志
- goaccess分析nginx日志
- ADO.NET入门简介
- 阻止事件冒泡event.stopPropagation()
- windows下面安装Python和pip以便下载python库包
- 普及练习场 排序Ex 瑞士轮
- Markdown 大全(持续补充)
- MapReduce 分析nginx日志例子
- 编程的基础——C语言
- Loadrunner 字符串拼接的二种写法
- TensorFlow相关
- 先序中序还原二叉树 和 中序后序还原二叉树 总结
- 计算机图形学—几何变换
- Qt5.9编译MQTT错误解决办法
- 策略模式的演化使用
- Nginx入门