一個典型的MapRuduce實例------webcount(網站統計訪客信息)

来源:http://www.cnblogs.com/learn21cn/archive/2016/12/05/6132528.html
-Advertisement-
Play Games

統計某一特定網站的某個時辰訪客人數 所用版本:hadoop2.6.5 數據樣式如下: 輔助類 mapper 映射特定年份中每月每天每個時辰的訪客數 reducer 彙總一個時辰內訪客人數 driver 配置信息,程式入口 command result ...


統計某一特定網站的某個時辰訪客人數

所用版本:hadoop2.6.5

數據樣式如下:

111.111.111.111 - - [16/Dec/2012:05:32:50 -0500] "GET / HTTP/1.1" 200 14791 "-" "Mozilla/5.0 (compatible; Baiduspider/2.0; +http://www.baidu.com/search/spider.html)"
111.111.111.111 - - [16/Dec/2012:05:33:50 -0500] "GET / HTTP/1.1" 200 14791 "-" "Mozilla/5.0 (compatible; Baiduspider/2.0; +http://www.baidu.com/search/spider.html)"
111.111.111.111 - - [16/Dec/2012:05:34:45 -0500] "GET / HTTP/1.1" 200 14791 "-" "Mozilla/5.0 (compatible; Baiduspider/2.0; +http://www.baidu.com/search/spider.html)"
111.111.111.111 - - [16/Dec/2012:05:34:50 -0500] "GET / HTTP/1.1" 200 14791 "-" "Mozilla/5.0 (compatible; Baiduspider/2.0; +http://www.baidu.com/search/spider.html)"
111.111.111.111 - - [16/Dec/2012:09:34:55 -0500] "GET / HTTP/1.1" 200 14791 "-" "Mozilla/5.0 (compatible; Baiduspider/2.0; +http://www.baidu.com/search/spider.html)"
111.111.111.111 - - [16/Dec/2012:10:23:30 -0500] "GET / HTTP/1.1" 200 14791 "-" "Mozilla/5.0 (compatible; Baiduspider/2.0; +http://www.baidu.com/search/spider.html)"
111.111.111.111 - - [16/Dec/2012:10:32:50 -0500] "GET / HTTP/1.1" 200 14791 "-" "Mozilla/5.0 (compatible; Baiduspider/2.0; +http://www.baidu.com/search/spider.html)"

輔助類

 1 package com.trendwise.software;
 2 
 3 import java.text.SimpleDateFormat; 
 4 import java.util.Date; 
 5 import java.io.DataInput; import java.io.DataOutput; 
 6 import java.io.IOException; 
 7 import org.apache.hadoop.io.WritableComparable; 
 8 
 9 public class DateWritable implements WritableComparable<DateWritable>{
10     private final static SimpleDateFormat formatter = new SimpleDateFormat( "yyyy-MM-dd' T 'HH:mm:ss.SSS" ); 
11     private Date date; 
12     public Date getDate() { 
13         return date; 
14     } 
15     public void setDate( Date date ) { 
16         this.date = date; 
17     } 
18 
19     @Override
20     public void readFields(DataInput in) throws IOException {
21         date = new Date( in.readLong() );         
22     }
23 
24     @Override
25     public void write(DataOutput out) throws IOException {
26         out.writeLong( date.getTime() );         
27     }
28 
29     @Override
30     public int compareTo(DateWritable o) {
31         return date.compareTo( o.getDate() ); 
32     }
33     
34     public String toString() { 
35         return formatter.format( date); 
36     }     
37 }

mapper 映射特定年份中每月每天每個時辰的訪客數

 1 package com.trendwise.software;
 2 
 3 import java.io.IOException;
 4 import java.util.Calendar;
 5 import org.apache.hadoop.io.IntWritable;
 6 import org.apache.hadoop.io.LongWritable;
 7 import org.apache.hadoop.io.Text;
 8 import org.apache.hadoop.mapreduce.Mapper;
 9 
10 public class LogMapper extends Mapper<LongWritable, Text, DateWritable, IntWritable> { 
11     public static DateWritable dates = new DateWritable(); 
12     public final static IntWritable two = new IntWritable(1); 
13     public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException { 
14         String text = value.toString(); 
15         // Get the date and time 
16         int openBracket = text.indexOf( '[' ); 
17         int closeBracket = text.indexOf( ']' ); 
18         if( openBracket != -1 && closeBracket != -1 ) { 
19             // Read the date 
20             String dateString = text.substring( text.indexOf( '[' ) + 1, text. indexOf( ']' ) ); 
21             // Build a date object from a string of the form: 16/Dec/2012:05:32:50 -0500 
22             int index = 0; 
23             int nextIndex = dateString.indexOf( '/' ); 
24             int day = Integer.parseInt( dateString.substring(index, nextIndex) );
25             
26             index = nextIndex; nextIndex = dateString.indexOf( '/', index+1 ); 
27             String month = dateString.substring( index+1, nextIndex ); 
28             index = nextIndex; 
29             nextIndex = dateString.indexOf( ':', index ); 
30             int year = Integer.parseInt(dateString.substring(index + 1, nextIndex)); 
31             index = nextIndex; nextIndex = dateString.indexOf( ':', index+1 ); 
32             int hour = Integer.parseInt(dateString.substring(index + 1, nextIndex)); 
33             // Build a calendar object for this date 
34             Calendar calendar = Calendar.getInstance(); 
35             calendar.set( Calendar.DATE, day );
36             calendar.set( Calendar.YEAR, year ); 
37             calendar.set( Calendar.HOUR, hour ); 
38             calendar.set( Calendar.MINUTE, 0 ); 
39             calendar.set( Calendar.SECOND, 0 ); 
40             calendar.set( Calendar.MILLISECOND, 0 ); 
41             if( month.equalsIgnoreCase( "dec" ) ) { 
42                 calendar.set( Calendar.MONTH, Calendar.DECEMBER ); 
43             } 
44             else if( month.equalsIgnoreCase( "nov" ) ) { 
45                 calendar.set( Calendar.MONTH, Calendar.NOVEMBER ); 
46             } 
47             else if( month.equalsIgnoreCase( "oct" ) ) { 
48                 calendar.set( Calendar.MONTH, Calendar.OCTOBER ); 
49             }
50             else if( month.equalsIgnoreCase( "sep" ) ) { 
51                 calendar.set( Calendar.MONTH, Calendar.SEPTEMBER ); 
52             } 
53             else if( month.equalsIgnoreCase( "aug" ) ) { 
54                 calendar.set( Calendar.MONTH, Calendar.AUGUST ); 
55             } 
56             else if( month.equalsIgnoreCase( "jul" ) ) { 
57                 calendar.set( Calendar.MONTH, Calendar.JULY ); 
58             } 
59             else if( month.equalsIgnoreCase( "jun" ) ) {
60                 calendar.set( Calendar.MONTH, Calendar.JUNE ); 
61             } 
62             else if( month.equalsIgnoreCase( "may" ) ) {
63                 calendar.set( Calendar.MONTH, Calendar.MAY ); 
64             } 
65             else if( month.equalsIgnoreCase( "apr" ) ) { 
66                 calendar.set( Calendar.MONTH, Calendar.APRIL ); 
67             } 
68             else if( month.equalsIgnoreCase( "mar" ) ) { 
69                 calendar.set( Calendar.MONTH, Calendar.MARCH ); 
70             } 
71             else if( month.equalsIgnoreCase( "feb" ) ) { 
72                 calendar.set( Calendar.MONTH, Calendar.FEBRUARY ); 
73             } 
74             else if( month.equalsIgnoreCase( "jan" ) ) { 
75                 calendar.set( Calendar.MONTH, Calendar.JANUARY ); 
76             } 
77             
78             dates.setDate( calendar.getTime() ); 
79             context.write(dates, two); 
80             
81         }
82     }
83 }

reducer 彙總一個時辰內訪客人數

 1 package com.trendwise.software;
 2 
 3 import java.io.IOException;
 4 import org.apache.hadoop.io.IntWritable;
 5 import org.apache.hadoop.mapreduce.Reducer;
 6  
 7 public class  LogReducer extends Reducer<DateWritable, IntWritable, DateWritable, IntWritable> {
 8     @Override
 9     public void reduce( DateWritable key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException { 
10     
11         int countn = 0; 
12         for(IntWritable v :values){ 
13             countn += v.get(); 
14         }     
15         context.write(key, new IntWritable( countn) ); 
16     } 
17 }

driver 配置信息,程式入口

 1 package com.trendwise.software;
 2 
 3 import java.io.IOException;
 4 import org.apache.hadoop.conf.Configuration;
 5 import org.apache.hadoop.fs.Path;
 6 import org.apache.hadoop.io.IntWritable;
 7 import org.apache.hadoop.mapreduce.Job;
 8 import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
 9 import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
10 
11 public class Driver {
12     
13     public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException { 
14                 
15         String in = args[0];
16         String out = args[1];
17         int unitmb =Integer.valueOf(args[2]);                
18         int nreducer = Integer.valueOf(args[3]);
19         
20         Configuration conf = new Configuration();                
21         conf.set("mapreduce.input.fileinputformat.split.maxsize", String.valueOf(unitmb * 1024 * 1024));
22         conf.set("mapred.min.split.size", String.valueOf(unitmb * 1024 * 1024));
23         conf.set("mapreduce.input.fileinputformat.split.minsize.per.node", String.valueOf(unitmb * 1024 * 1024));
24         conf.set("mapreduce.input.fileinputformat.split.minsize.per.rack", String.valueOf(unitmb * 1024 * 1024));
25                 
26         Job job = new Job(conf);        
27         FileInputFormat.addInputPath(job, new Path(in));
28         FileOutputFormat.setOutputPath(job, new Path(out));            
29         job.setMapperClass(LogMapper.class); 
30         job.setReducerClass(LogReducer.class); 
31         job.setCombinerClass(LogReducer.class); 
32         job.setNumReduceTasks(nreducer);
33         job.setMapOutputKeyClass(DateWritable.class);
34         job.setMapOutputValueClass(IntWritable.class);    
35         job.setOutputKeyClass(DateWritable.class); 
36         job.setOutputValueClass(IntWritable.class);
37         job.setJarByClass(Driver.class);
38         job.waitForCompletion(true);    
39                     
40     }     
41 }

command

result

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 當運行下一個活動時,上一個活動被K掉了,當我們返回上一個活動時,系統會重啟create一個活動,問題來了我們之前在保存的數據怎麼辦?onSaveInstanceState可以用這個方法來進行保存,鍵值對[ke,"value"],其實和Intent一樣,也是通過這樣保存。 ...
  • 今天碰到了在XML中應用以內部類形式定義的自定義view,結果遇到了一些坑。雖然通過看了一些前輩寫的文章解決了這個問題,但是我看到的幾篇都沒有完整說清楚why,於是決定做這個總結。 使用自定義內部類view的規則 本文主要是總結why,所以先把XML佈局文件中引用內部類的自定義view的做法擺出來, ...
  • 三級緩存的提出就是為了提升用戶體驗。當我們第一次打開應用獲取圖片時,先到網路去下載圖片,然後依次存入記憶體緩存,磁碟緩存,當我們再一次需要用到剛纔下載的這張圖片時,就不需要再重覆的到網路上去下載,直接可以從記憶體緩存和磁碟緩存中找,由於記憶體緩存速度較快,我們優先到記憶體緩存中尋找該圖片,如果找到則運用,如 ...
  • 最近解決了一個Android APP的bug,發現APP在被後臺清理後,會自動重啟。現象很奇怪,有的手機(HTC)後臺清理後,程式會再次重啟,而有的手機(小米)則不會。猜想可能是小米手機內部做了處理,當執行後臺清理後,對應的APP不會再重啟了。 Debug後發現,APP內部有一個Service,在S ...
  • 散列表的具體實現就不多做介紹了,就是一個數組,每個下標存儲的是碰撞的元素的鏈表頭指針,如下圖所示: 下麵直接研究對用鏈接法散列的分析: 給定一個能存放n個元素的、具有m個槽位的散列表T,定義T的裝載因數α為n/m,即一個鏈中平均存儲的元素數。 用鏈接法散列的最壞情況性能很差:所有的n個關鍵字都散列到 ...
  • 之前關於如何實現屏幕頁面切換,寫過一篇博文《Android中使用ViewFlipper實現屏幕切換》,相比ViewFlipper,ViewPager更適用複雜的視圖切換,而且Viewpager有自己的adapter,這也讓其適應複雜對象,實現數據的動態載入。 ViewPager是谷歌官方給我們提供的 ...
  • 一、分析目的 為企業戰略決策、投資決策、營銷決策提供依據 二、分析思路 三、分析方法——廚藝大比拼 數據分析的過程 對比 分類 相關:相關分析研究的是事物間的某種聯繫,最常見的聯繫就是因果分析。 分佈:分佈分析就是集中和離散趨勢。 四、分析應用 巨集觀環境:PEST分析——政治(Political)環 ...
  • 我們在數據分析之前需要進行數據處理、數據錄入並把數據清洗乾凈,做好數據的加工和描述工作。 【參考文獻】《數據分析:企業的賢內助》 陳哲 著 ...
一周排行
    -Advertisement-
    Play Games
  • 示例項目結構 在 Visual Studio 中創建一個 WinForms 應用程式後,項目結構如下所示: MyWinFormsApp/ │ ├───Properties/ │ └───Settings.settings │ ├───bin/ │ ├───Debug/ │ └───Release/ ...
  • [STAThread] 特性用於需要與 COM 組件交互的應用程式,尤其是依賴單線程模型(如 Windows Forms 應用程式)的組件。在 STA 模式下,線程擁有自己的消息迴圈,這對於處理用戶界面和某些 COM 組件是必要的。 [STAThread] static void Main(stri ...
  • 在WinForm中使用全局異常捕獲處理 在WinForm應用程式中,全局異常捕獲是確保程式穩定性的關鍵。通過在Program類的Main方法中設置全局異常處理,可以有效地捕獲並處理未預見的異常,從而避免程式崩潰。 註冊全局異常事件 [STAThread] static void Main() { / ...
  • 前言 給大家推薦一款開源的 Winform 控制項庫,可以幫助我們開發更加美觀、漂亮的 WinForm 界面。 項目介紹 SunnyUI.NET 是一個基於 .NET Framework 4.0+、.NET 6、.NET 7 和 .NET 8 的 WinForm 開源控制項庫,同時也提供了工具類庫、擴展 ...
  • 說明 該文章是屬於OverallAuth2.0系列文章,每周更新一篇該系列文章(從0到1完成系統開發)。 該系統文章,我會儘量說的非常詳細,做到不管新手、老手都能看懂。 說明:OverallAuth2.0 是一個簡單、易懂、功能強大的許可權+可視化流程管理系統。 有興趣的朋友,請關註我吧(*^▽^*) ...
  • 一、下載安裝 1.下載git 必須先下載並安裝git,再TortoiseGit下載安裝 git安裝參考教程:https://blog.csdn.net/mukes/article/details/115693833 2.TortoiseGit下載與安裝 TortoiseGit,Git客戶端,32/6 ...
  • 前言 在項目開發過程中,理解數據結構和演算法如同掌握蓋房子的秘訣。演算法不僅能幫助我們編寫高效、優質的代碼,還能解決項目中遇到的各種難題。 給大家推薦一個支持C#的開源免費、新手友好的數據結構與演算法入門教程:Hello演算法。 項目介紹 《Hello Algo》是一本開源免費、新手友好的數據結構與演算法入門 ...
  • 1.生成單個Proto.bat內容 @rem Copyright 2016, Google Inc. @rem All rights reserved. @rem @rem Redistribution and use in source and binary forms, with or with ...
  • 一:背景 1. 講故事 前段時間有位朋友找到我,說他的窗體程式在客戶這邊出現了卡死,讓我幫忙看下怎麼回事?dump也生成了,既然有dump了那就上 windbg 分析吧。 二:WinDbg 分析 1. 為什麼會卡死 窗體程式的卡死,入口門檻很低,後續往下分析就不一定了,不管怎麼說先用 !clrsta ...
  • 前言 人工智慧時代,人臉識別技術已成為安全驗證、身份識別和用戶交互的關鍵工具。 給大家推薦一款.NET 開源提供了強大的人臉識別 API,工具不僅易於集成,還具備高效處理能力。 本文將介紹一款如何利用這些API,為我們的項目添加智能識別的亮點。 項目介紹 GitHub 上擁有 1.2k 星標的 C# ...