標籤:樣本 get tput override out status gets mapper uniq
一、網站基本指標(即針對於網站使用者行為而產生的日誌中進行統計分析)
1. PV:網頁瀏覽量(Page View頁面瀏覽次數,只要進入該網頁就產生一條記錄,不限IP,統計點每天(較多)/每周/每月/..)2. UV:獨立訪客數(Unique Vistor,以Cookie為依據,同一天內一個使用者多次訪問,只記為一個)3. VV:訪客的訪問次數(Visit View,以Session為依據,訪客訪問網站到關掉該網站所有頁面即記為一次訪問)4. IP:獨立IP數(即記錄不同IP,同一IP訪問多次算作一次)5. 通常網站流量(traffic)是指網站的訪問量,是用來描述訪問一個網站的使用者數量以及使用者所瀏覽的網頁數量等指標 對於虛擬空間商來說流量是指:使用者在訪問網站過程中,產生的資料量大小
二、UV統計樣本(也就是每天每個省份有多少人訪問了該網站)
1. 分析需求 1> 我們得到的是怎樣的資料,找出共同點,會議map,shuffle,reduce都做了什麼事 2> 我們想要怎樣的資料,列舉出來2. 實施計劃注意的地方 1> 資料以什麼分隔,是否我們需要自訂資料類型 2> 大致上我們需要過濾掉無效的記錄 使用自訂資料類型將我們需要的欄位組合起來 然後根據省份對記錄累加(去重階段) 3> 資料類型可以不定義,使用Text將欄位值組合即可 然後轉到reduce方法,建立hashMap對於資料根據時間和省份遞增儲存 在cleanup中組合成想要的輸出資料即可
三、UV統計程式碼範例
WebUvMr.java============package com.bigdata_senior.WebUvMr;import java.io.IOException;import java.util.HashMap;import java.util.Map;import java.util.Set;import org.apache.commons.lang.StringUtils;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.fs.Path;import org.apache.hadoop.io.LongWritable;import org.apache.hadoop.io.NullWritable;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;public class WebUvMr {//Mapper Classprivate static class WordCountMapper extends Mapper<LongWritable, Text, Text, NullWritable>{private Text mapOutKey = new Text();@Overridepublic void map(LongWritable key, Text value, Context context)throws IOException, InterruptedException {String lineValue = value.toString();String [] strValue = lineValue.split("\t");if(30 > strValue.length){return;}String guidIdValue = strValue[5];if(StringUtils.isBlank(guidIdValue)){return;}String trackTimeValue = strValue[17];if(StringUtils.isBlank(trackTimeValue)){return;}String dateValue = trackTimeValue.substring(0,10);Integer proviceIdValue = Integer.MAX_VALUE;try{if(StringUtils.isBlank(strValue[23])){return;}proviceIdValue = Integer.valueOf(strValue[23]);}catch(Exception e){return;}mapOutKey.set(dateValue+"\t"+proviceIdValue+"_"+guidIdValue);//System.out.println("key--> "+mapOutKey+" value--> "+NullWritable.get());context.write(mapOutKey, NullWritable.get());}}//Reduce Classprivate static class WordCountReduce extends Reducer<Text, NullWritable, Text, LongWritable>{private Map<String,Integer> dateMap;private Text outputKey = new Text();private LongWritable outputvalue = new LongWritable();@Overrideprotected void setup(Context context) throws IOException,InterruptedException {dateMap = new HashMap<String,Integer>();}@Overridepublic void reduce(Text key, Iterable<NullWritable> values,Context context)throws IOException, InterruptedException {String date = key.toString().split("_")[0];if(dateMap.containsKey(date)){Integer preUv = dateMap.get(date);//System.out.println("====-> "+preUv);Integer uv = preUv + 1;dateMap.put(date, uv);}else{dateMap.put(date, 1);}//System.out.println(dateMap.toString());}@Overrideprotected void cleanup(Context context) throws IOException,InterruptedException {Set<String> dateSet = dateMap.keySet();//System.out.println(dateSet.toString());for(String date : dateSet){Integer uv = dateMap.get(date);outputKey.set(date);outputvalue.set(uv);System.out.println("result:-->key "+outputKey+" value-->"+outputvalue);context.write(outputKey, outputvalue);}}}//Driverpublic int run(String[] args) throws Exception {Configuration configuration = new Configuration();Job job = Job.getInstance(configuration, this.getClass().getSimpleName());job.setJarByClass(this.getClass());//inputPath inPath = new Path(args[0]);FileInputFormat.addInputPath(job,inPath);//outputPath outPath = new Path(args[1]);FileOutputFormat.setOutputPath(job, outPath);//mapperjob.setMapperClass(WordCountMapper.class);job.setMapOutputKeyClass(Text.class);job.setMapOutputValueClass(NullWritable.class);//Reducejob.setReducerClass(WordCountReduce.class);job.setOutputKeyClass(Text.class);job.setOutputValueClass(LongWritable.class);//submit jobboolean isSuccess = job.waitForCompletion(true);return isSuccess ? 0 : 1;}public static void main(String[] args) throws Exception {args = new String[]{"hdfs://hadoop09-linux-01.ibeifeng.com:8020/user/liuwl/tmp/webuv/input","hdfs://hadoop09-linux-01.ibeifeng.com:8020/user/liuwl/tmp/webuv/output4"};//run jobint status = new WebUvMr().run(args);System.exit(status);}}
Hadoop.2.x_WebUV樣本