MongDB日志分析

Result文件数据说明:

Ip106.39.41.166,(城市)

Date10/Nov/2016:00:01:02 +0800,(日期)

Day10,(天数)

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--->城市 cityIP

date--> time:2016-11-10 00:01:03

day: 10

traffic:62

type:article/video

id:11325

  1. package DataClean;  
  2. import java.io.IOException;  
  3. import java.text.SimpleDateFormat;  
  4. import java.util.Date;  
  5. import java.util.Locale;  
  6. import org.apache.hadoop.conf.Configuration;  
  7. import org.apache.hadoop.fs.Path;  
  8. import org.apache.hadoop.io.Text;  
  9. import org.apache.hadoop.mapreduce.Job;  
  10. import org.apache.hadoop.mapreduce.Mapper;  
  11. import org.apache.hadoop.mapreduce.Reducer;  
  12. import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;  
  13. import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;  
  14. import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;  
  15. import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;  
  16. public class dataclean{  
  17. public static class Map extends Mapper<Object,Text,Text,Text>{  
  18. public static final SimpleDateFormat FORMAT = new SimpleDateFormat("d/MMM/yyyy:HH:mm:ss", Locale.ENGLISH); //原时间格式  
  19. public static final SimpleDateFormat dateformat1 = new SimpleDateFormat("yyyy-MM-dd-HH:mm:ss");//现时间格式  
  20. private  static Date parseDateFormat(String string) {         //转换时间格式  
  21. Date parse = null;  
  22. try {  
  23. parse = FORMAT.parse(string);  
  24. catch (Exception e) {  
  25. e.printStackTrace();  
  26. }  
  27. return parse;  
  28. }  
  29. private static Text newKey = new Text();  
  30. private static Text newvalue = new Text();  
  31. public void map(Object key,Text value,Context context) throws IOException, InterruptedException{  
  32. String line = value.toString();  
  33. System.out.println(line);  
  34. String arr[] = line.split(",");  
  35. newKey.set(arr[0]);  
  36. final int first = arr[1].indexOf("");  
  37. final int last = arr[1].indexOf(" +0800");  
  38. String time = arr[1].substring(first + 1, last).trim();  
  39. Date date = parseDateFormat(time);  
  40. arr[1] = dateformat1.format(date);  
  41. newvalue.set(arr[1]+" "+arr[2]+" "+arr[3]+" "+arr[4]+" "+arr[5]);  
  42. context.write(newKey,newvalue);  
  43. }  
  44. }  
  45. public static class Reduce extends Reducer<Text, Text, Text, Text> {  
  46. protected void reduce(Text key, Iterable<Text> values, Context context)throws IOException, InterruptedException {  
  47. for(Text text : values){  
  48. context.write(key,text);  
  49. }  
  50. }  
  51. }  
  52. public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {  
  53. Configuration conf = new Configuration();  
  54. System.out.println("start");  
  55. Job job=Job.getInstance(conf);   
  56. job.setJobName("filter");  
  57. job.setJarByClass(shujuqingxi.class);  
  58. job.setMapperClass(Map.class);  
  59. job.setReducerClass(Reduce.class);  
  60. job.setOutputKeyClass(Text.class);  
  61. job.setOutputValueClass(Text.class);          
  62. job.setInputFormatClass(TextInputFormat.class);  
  63. job.setOutputFormatClass(TextOutputFormat.class);  
  64. Path in=new Path("hdfs://192.168.57.100:9000/testhdfs1026/result.txt");  
  65. Path out=new Path("hdfs://192.168.57.100:9000/testhdfs1026/result");  
  66. FileInputFormat.addInputPath(job, in);  
  67. FileOutputFormat.setOutputPath(job, out);  
  68. boolean flag = job.waitForCompletion(true);  
  69. System.out.println(flag);  
  70. System.exit(flag? 0 : 1);  
  71. }  
  72. }  

 

 

 

 

 

 

 

3MongDB数据库表结构:

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

  1. create table id_count as  
  2. select word,count(*) as cnt from  
  3. (select explode(split(mid,' ')) as word from data) w      
  4. group by word  
  5. order by cnt desc;  

 

 

 

 

·按照地市统计最受欢迎的Top10课程 (ip

  1. create table ip_count as  
  2. select word,count(*) as cnt from  
  3. (select explode(split(mip,' ')) as word from data) w      
  4. group by word  
  5. order by cnt desc;  

 

 

 

 

·按照流量统计最受欢迎的Top10课程 (traffic

  1. create table traffic_count as select mid,  
  2. sum(mtraffic) as cnt from data   
  3. group by mid order by cnt desc;  

 

 

 

 

2、 数据可视化:将统计结果倒入MySql数据库中,通过图形化展示的方式展现出来。

Id_count

 

 

 

Ip_count

 

 

 

Traffic_count

 

 

 

柱状图:

 

 

 

 

折线图:

 

 

 

堆叠柱状图:

 

 

 

数据视图:

 

 

 

平铺图:

 

 

posted @ 2021-11-05 21:19  马梦佳  阅读(45)  评论(0编辑  收藏  举报