MongDB日志分析
Result文件数据说明:
Ip:106.39.41.166,(城市)
Date:10/Nov/2016:00:01:02 +0800,(日期)
Day:10,(天数)
Traffic: 54 ,(流量)
Type: video,(类型:视频video或文章article)
Id: 8701(视频或者文章的id)
测试要求:
1、 数据清洗:按照进行数据清洗,并将清洗后的数据导入 MongDB数据库中。
两阶段数据清洗:
(1)第一阶段:把需要的信息从原始日志中提取出来
ip: 199.30.25.88
time: 10/Nov/2016:00:01:03 +0800
traffic: 62
文章: article/11325
视频: video/3235
(2)第二阶段:根据提取出来的信息做精细化操作
ip--->城市 city(IP)
date--> time:2016-11-10 00:01:03
day: 10
traffic:62
type:article/video
id:11325
- package DataClean;
- import java.io.IOException;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- import java.util.Locale;
- import org.apache.hadoop.conf.Configuration;
- import org.apache.hadoop.fs.Path;
- 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.input.TextInputFormat;
- import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
- import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
- public class dataclean{
- public static class Map extends Mapper<Object,Text,Text,Text>{
- public static final SimpleDateFormat FORMAT = new SimpleDateFormat("d/MMM/yyyy:HH🇲🇲ss", Locale.ENGLISH); // 原时间格式
- public static final SimpleDateFormat dateformat1 = new SimpleDateFormat("yyyy-MM-dd-HH🇲🇲ss");// 现时间格式
- private static Date parseDateFormat(String string) { // 转换时间格式
- Date parse = null;
- try {
- parse = FORMAT.parse(string);
- } catch (Exception e) {
- e.printStackTrace();
- }
- return parse;
- }
- private static Text newKey = new Text();
- private static Text newvalue = new Text();
- public void map(Object key,Text value,Context context) throws IOException, InterruptedException{
- String line = value.toString();
- System.out.println(line);
- String arr[] = line.split(",");
- newKey.set(arr[0]);
- final int first = arr[1].indexOf("");
- final int last = arr[1].indexOf(" +0800");
- String time = arr[1].substring(first + 1, last).trim();
- Date date = parseDateFormat(time);
- arr[1] = dateformat1.format(date);
- newvalue.set(arr[1]+" "+arr[2]+" "+arr[3]+" "+arr[4]+" "+arr[5]);
- context.write(newKey,newvalue);
- }
- }
- public static class Reduce extends Reducer<Text, Text, Text, Text> {
- protected void reduce(Text key, Iterable<Text> values, Context context)throws IOException, InterruptedException {
- for(Text text : values){
- context.write(key,text);
- }
- }
- }
- public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
- Configuration conf = new Configuration();
- System.out.println("start");
- Job job=Job.getInstance(conf);
- job.setJobName("filter");
- job.setJarByClass(shujuqingxi.class);
- job.setMapperClass(Map.class);
- job.setReducerClass(Reduce.class);
- job.setOutputKeyClass(Text.class);
- job.setOutputValueClass(Text.class);
- job.setInputFormatClass(TextInputFormat.class);
- job.setOutputFormatClass(TextOutputFormat.class);
- Path in=new Path("hdfs://192.168.57.100:9000/testhdfs1026/result.txt");
- Path out=new Path("hdfs://192.168.57.100:9000/testhdfs1026/result");
- FileInputFormat.addInputPath(job, in);
- FileOutputFormat.setOutputPath(job, out);
- boolean flag = job.waitForCompletion(true);
- System.out.println(flag);
- System.exit(flag? 0 : 1);
- }
- }
(3)MongDB 数据库表结构:
create table data(mip String,
mtime String,
mday String,
mtraffic String,
mtype String,
mid String)
ROW format delimited fields terminated by ',' STORED AS TEXTFILE;
创建数据表:
2、数据处理:
·统计最受欢迎的视频/文章的Top10访问次数 (video/article)
- create table id_count as
- select word,count(*) as cnt from
- (select explode(split(mid,' ')) as word from data) w
- group by word
- order by cnt desc;
·按照地市统计最受欢迎的Top10课程 (ip)
- create table ip_count as
- select word,count(*) as cnt from
- (select explode(split(mip,' ')) as word from data) w
- group by word
- order by cnt desc;
·按照流量统计最受欢迎的Top10课程 (traffic)
- create table traffic_count as select mid,
- sum(mtraffic) as cnt from data
- group by mid order by cnt desc;
2、 数据可视化:将统计结果倒入MySql数据库中,通过图形化展示的方式展现出来。
Id_count
Ip_count
Traffic_count
柱状图:
折线图:
堆叠柱状图:
数据视图:
平铺图: