標籤:common valueof tmp nbsp iterable 訪問 拷貝 div sort
一、網站基本指標(即針對於網站使用者行為而產生的日誌中進行統計分析)
1. PV:網頁瀏覽量(Page View頁面瀏覽次數,只要進入該網頁就產生一條記錄,不限IP,統計點每天(較多)/每周/每月/..)2. UV:獨立訪客數(Unique Vistor,以Cookie為依據,同一天內一個使用者多次訪問,只記為一個)3. VV:訪客的訪問次數(Visit View,以Session為依據,訪客訪問網站到關掉該網站所有頁面即記為一次訪問)4. IP:獨立IP數(即記錄不同IP,同一IP訪問多次算作一次)5. 通常網站流量(traffic)是指網站的訪問量,是用來描述訪問一個網站的使用者數量以及使用者所瀏覽的網頁數量等指標 對於虛擬空間商來說流量是指:使用者在訪問網站過程中,產生的資料量大小
二、PV統計樣本(統計各省的PV)
1. 分析資料中欄位(provinceId)2. 資料類型 <11,11,13>Map()--><11,list<1,1>><12,list<1>>-->reduce()---><11 /t 2><13 /t 1>3. 條件過濾(或稱之為資料清洗) values.length < 30; StringUtils.isBlank(url) StringUtils.isBlank(proviceIdValue) proviceId = Integer.valueOf(proviceIdValue); 注意:條件的前後放置一定程度上會影響MR程式的運行效率 這是一處最佳化,還可以使用combine,壓縮提高效率 PS:Configuration configuration = new Configuration(); 這行代碼會先讀取預設設定檔後從資源檔中擷取自訂設定檔4. 自訂計數器(用於記錄被過濾掉那些資料) //Counter LEGTH_LT_30_COUNTER context.getCounter("WEBPVMAP_COUNTERS", "LEGTH_LT_30_COUNTER").increment(1L); //Counter URL_ISBLANK context.getCounter("WEBPVMAP_COUNTERS", "URL_ISBLANK").increment(1L); //Counter PROVICEIDVALUE_ISBLANK context.getCounter("WEBPVMAP_COUNTERS", "PROVICEIDVALUE_ISBLANK").increment(1L); //Counter STRING_CASE_TO_INTEGER_EXCEPTION context.getCounter("WEBPVMAP_COUNTERS", "STRING_CASE_TO_INTEGER_EXCEPTION").increment(1L); 運行MR輸出: WEBPVMAP_COUNTERS PROVICEIDVALUE_ISBLANK=21742 STRING_CASE_TO_INTEGER_EXCEPTION=1 URL_ISBLANK=29092
三、具體代碼實現
1. 放置好資源檔
(即:將自訂設定檔拷貝到MR程式的資源檔夾,當然使用javaAPI將屬性set進configuration也行)
[[email protected] hadoop-2.5.0]$ cp etc/hadoop/core-site.xml etc/hadoop/hdfs-site.xml etc/hadoop/log4j.properties /home/liuwl/local/workspace/bigdata-test/src/resouce[[email protected] hadoop-2.5.0]$
2. 具體代碼
package com.eRrsr.bigdata_test;import java.io.IOException;import org.apache.commons.lang.StringUtils;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.fs.Path;import org.apache.hadoop.io.IntWritable;import org.apache.hadoop.io.LongWritable;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 WebPVMapReduce { //Mapper Class private static class WebPVMapper extends Mapper<LongWritable, Text, IntWritable, IntWritable>{ private IntWritable mapOutKey = new IntWritable(); private final static IntWritable mapOutValue = new IntWritable(1); @Override public void map(LongWritable key, Text value, Context context)throws IOException, InterruptedException { String lineValue = value.toString(); //使用\t分隔 String [] values = lineValue.split("\t"); //過濾掉分隔長度小於30的記錄 if(values.length < 30){ //計數器:參數1->計數器分組名稱;參數2->計數器名稱 context.getCounter("WEBPVMAP_COUNTERS", "LEGTH_LT_30_COUNTER").increment(1L); return; } //過濾掉分隔後的欄位中url為空白的記錄 String url = values[1]; if(StringUtils.isBlank(url)){ context.getCounter("WEBPVMAP_COUNTERS", "URL_ISBLANK").increment(1L); return; } //過濾掉省份id為空白的記錄 String proviceIdValue = values[23]; if(StringUtils.isBlank(proviceIdValue)){ context.getCounter("WEBPVMAP_COUNTERS", "PROVICEIDVALUE_ISBLANK").increment(1L); return; } //過濾掉省份id轉int異常的資料 Integer proviceId = Integer.MAX_VALUE; try{ proviceId = Integer.valueOf(proviceIdValue); }catch(Exception e){ context.getCounter("WEBPVMAP_COUNTERS", "STRING_CASE_TO_INTEGER_EXCEPTION").increment(1L); return; } mapOutKey.set(proviceId); context.write(mapOutKey, mapOutValue); } } //Reduce Class private static class WebPVReduce extends Reducer<IntWritable, IntWritable, IntWritable, IntWritable>{ private IntWritable reduceOutValue = new IntWritable(); @Override public void reduce(IntWritable key, Iterable<IntWritable> values,Context context)throws IOException, InterruptedException { int sum = 0; for(IntWritable value : values){ sum += value.get(); } reduceOutValue.set(sum); context.write(key, reduceOutValue); } } //Driver Method public int run(String[] args) throws Exception { //會先讀取預設配置,後讀取資源檔中自訂配置 Configuration configuration = new Configuration(); Job job = Job.getInstance(configuration, this.getClass().getSimpleName()); job.setJarByClass(this.getClass()); //input Path inPath = new Path(args[0]); FileInputFormat.addInputPath(job,inPath); //output Path outPath = new Path(args[1]); FileOutputFormat.setOutputPath(job, outPath); //mapper job.setMapperClass(WebPVMapper.class); job.setMapOutputKeyClass(IntWritable.class); job.setMapOutputValueClass(IntWritable.class); // ======================shuffle========================== // 1.partitioner // job.setPartitionerClass(cls); // 2.sort // job.setSortComparatorClass(cls); // 3.combiner // 在shullfe過程中預先執行類似reduce的累加操作,使得reduce從本地檔案擷取map()後的資料更快,效率也就更高 job.setCombinerClass(WebPVReduce.class); // 5.group // job.setGroupingComparatorClass(cls); // ======================shuffle========================== //Reduce job.setReducerClass(WebPVReduce.class); job.setOutputKeyClass(IntWritable.class); job.setOutputValueClass(IntWritable.class); //submit job boolean 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/mapreduce/PV/input", "hdfs://hadoop09-linux-01.ibeifeng.com:8020/user/liuwl/tmp/mapreduce/PV/output5" }; //run job int status = new WebPVMapReduce().run(args); System.exit(status); }}
Hadoop.2.x_網站PV樣本