统计某一特定网站的某个时辰访客人数

所用版本: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)"

辅助类

 package com.trendwise.software;

 import java.text.SimpleDateFormat;
import java.util.Date;
import java.io.DataInput; import java.io.DataOutput;
import java.io.IOException;
import org.apache.hadoop.io.WritableComparable; public class DateWritable implements WritableComparable<DateWritable>{
private final static SimpleDateFormat formatter = new SimpleDateFormat( "yyyy-MM-dd' T 'HH:mm:ss.SSS" );
private Date date;
public Date getDate() {
return date;
}
public void setDate( Date date ) {
this.date = date;
} @Override
public void readFields(DataInput in) throws IOException {
date = new Date( in.readLong() );
} @Override
public void write(DataOutput out) throws IOException {
out.writeLong( date.getTime() );
} @Override
public int compareTo(DateWritable o) {
return date.compareTo( o.getDate() );
} public String toString() {
return formatter.format( date);
}
}

mapper 映射特定年份中每月每天每个时辰的访客数

 package com.trendwise.software;

 import java.io.IOException;
import java.util.Calendar;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper; public class LogMapper extends Mapper<LongWritable, Text, DateWritable, IntWritable> {
public static DateWritable dates = new DateWritable();
public final static IntWritable two = new IntWritable(1);
public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
String text = value.toString();
// Get the date and time
int openBracket = text.indexOf( '[' );
int closeBracket = text.indexOf( ']' );
if( openBracket != -1 && closeBracket != -1 ) {
// Read the date
String dateString = text.substring( text.indexOf( '[' ) + 1, text. indexOf( ']' ) );
// Build a date object from a string of the form: 16/Dec/2012:05:32:50 -0500
int index = 0;
int nextIndex = dateString.indexOf( '/' );
int day = Integer.parseInt( dateString.substring(index, nextIndex) ); index = nextIndex; nextIndex = dateString.indexOf( '/', index+1 );
String month = dateString.substring( index+1, nextIndex );
index = nextIndex;
nextIndex = dateString.indexOf( ':', index );
int year = Integer.parseInt(dateString.substring(index + 1, nextIndex));
index = nextIndex; nextIndex = dateString.indexOf( ':', index+1 );
int hour = Integer.parseInt(dateString.substring(index + 1, nextIndex));
// Build a calendar object for this date
Calendar calendar = Calendar.getInstance();
calendar.set( Calendar.DATE, day );
calendar.set( Calendar.YEAR, year );
calendar.set( Calendar.HOUR, hour );
calendar.set( Calendar.MINUTE, 0 );
calendar.set( Calendar.SECOND, 0 );
calendar.set( Calendar.MILLISECOND, 0 );
if( month.equalsIgnoreCase( "dec" ) ) {
calendar.set( Calendar.MONTH, Calendar.DECEMBER );
}
else if( month.equalsIgnoreCase( "nov" ) ) {
calendar.set( Calendar.MONTH, Calendar.NOVEMBER );
}
else if( month.equalsIgnoreCase( "oct" ) ) {
calendar.set( Calendar.MONTH, Calendar.OCTOBER );
}
else if( month.equalsIgnoreCase( "sep" ) ) {
calendar.set( Calendar.MONTH, Calendar.SEPTEMBER );
}
else if( month.equalsIgnoreCase( "aug" ) ) {
calendar.set( Calendar.MONTH, Calendar.AUGUST );
}
else if( month.equalsIgnoreCase( "jul" ) ) {
calendar.set( Calendar.MONTH, Calendar.JULY );
}
else if( month.equalsIgnoreCase( "jun" ) ) {
calendar.set( Calendar.MONTH, Calendar.JUNE );
}
else if( month.equalsIgnoreCase( "may" ) ) {
calendar.set( Calendar.MONTH, Calendar.MAY );
}
else if( month.equalsIgnoreCase( "apr" ) ) {
calendar.set( Calendar.MONTH, Calendar.APRIL );
}
else if( month.equalsIgnoreCase( "mar" ) ) {
calendar.set( Calendar.MONTH, Calendar.MARCH );
}
else if( month.equalsIgnoreCase( "feb" ) ) {
calendar.set( Calendar.MONTH, Calendar.FEBRUARY );
}
else if( month.equalsIgnoreCase( "jan" ) ) {
calendar.set( Calendar.MONTH, Calendar.JANUARY );
} dates.setDate( calendar.getTime() );
context.write(dates, two); }
}
}

reducer 汇总一个时辰内访客人数

 package com.trendwise.software;

 import java.io.IOException;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.mapreduce.Reducer; public class LogReducer extends Reducer<DateWritable, IntWritable, DateWritable, IntWritable> {
@Override
public void reduce( DateWritable key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException { int countn = 0;
for(IntWritable v :values){
countn += v.get();
}
context.write(key, new IntWritable( countn) );
}
}

driver 配置信息,程序入口

 package com.trendwise.software;

 import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; public class Driver { public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException { String in = args[0];
String out = args[1];
int unitmb =Integer.valueOf(args[2]);
int nreducer = Integer.valueOf(args[3]); Configuration conf = new Configuration();
conf.set("mapreduce.input.fileinputformat.split.maxsize", String.valueOf(unitmb * 1024 * 1024));
conf.set("mapred.min.split.size", String.valueOf(unitmb * 1024 * 1024));
conf.set("mapreduce.input.fileinputformat.split.minsize.per.node", String.valueOf(unitmb * 1024 * 1024));
conf.set("mapreduce.input.fileinputformat.split.minsize.per.rack", String.valueOf(unitmb * 1024 * 1024)); Job job = new Job(conf);
FileInputFormat.addInputPath(job, new Path(in));
FileOutputFormat.setOutputPath(job, new Path(out));
job.setMapperClass(LogMapper.class);
job.setReducerClass(LogReducer.class);
job.setCombinerClass(LogReducer.class);
job.setNumReduceTasks(nreducer);
job.setMapOutputKeyClass(DateWritable.class);
job.setMapOutputValueClass(IntWritable.class);
job.setOutputKeyClass(DateWritable.class);
job.setOutputValueClass(IntWritable.class);
job.setJarByClass(Driver.class);
job.waitForCompletion(true); }
}

command

result

一个典型的MapRuduce实例------webcount(网站统计访客信息)的更多相关文章

  1. PHP一个典型的闭包实例

    <?php // 一个基本的购物车,包括一些已经添加的商品和每种商品的数量. // 其中有一个方法用来计算购物车中所有商品的总价格,该方法使 // 用了一个 closure 作为回调函数. cl ...

  2. Tawk.to一键给自己的网站增加在线客服功能

    Tawk.to一键给自己的网站增加在线客服功能 很多外贸网站只有contact页面,留下邮箱.电话等联系方式,而在国际贸易当中能够及时在线交流沟通,能给客户留下更好的印象.接下来,就让我们一起来了解一 ...

  3. Blazor技术开发了一个访客管理系统

    简单介绍一下系统功能 该系统为了在疫情期间能很好管理访客登记做好风险管控,同时可以整合智能设备做到自动确认并跟踪访客的行动轨迹,该项目完全开源. 系统流程 访客可以同通过手机进行预注册,同时上传照片, ...

  4. Asp.Net MVC3.0网站统计登录认证的在线人数

    Asp.Net MVC3.0网站统计登录认证的在线人数 前言 对于一个网站来说,统计在线人数是一个很重要的工作.平时也发现很多的网站论坛等都有在线人数的显示.对于一个网站如果在线人数很多,用户看到了这 ...

  5. [.NET] 一步步打造一个简单的 MVC 电商网站 - BooksStore(二)

    一步步打造一个简单的 MVC 电商网站 - BooksStore(二) 本系列的 GitHub地址:https://github.com/liqingwen2015/Wen.BooksStore 前: ...

  6. 网站统计中的数据收集原理及实现(share)

    转载自:http://blog.codinglabs.org/articles/how-web-analytics-data-collection-system-work.html 网站数据统计分析工 ...

  7. 使用nginx lua实现网站统计中的数据收集

    导读网站数据统计分析工具是各网站站长和运营人员经常使用的一种工具,常用的有 谷歌分析.百度统计和腾讯分析等等.所有这些统计分析工具的第一步都是网站访问数据的收集.目前主流的数据收集方式基本都是基于ja ...

  8. [.NET] 一步步打造一个简单的 MVC 电商网站 - BooksStore(一)

    一步步打造一个简单的 MVC 电商网站 - BooksStore(一) 本系列的 GitHub地址:https://github.com/liqingwen2015/Wen.BooksStore &l ...

  9. 通过网站统计或系统监视器查看IIS并发连接数

    如果要查看IIS连接数,最简单方便的方法是通过“网站统计”来查看,“网站统计”的当前在线人数可以认为是当前IIS连接数;如果要想知道确切的当前网站IIS连接数的话,最有效的方法是通过windows自带 ...

随机推荐

  1. idea打包jar的多种方式

    这里总结出用IDEA打包jar包的多种方式,以后的项目打包Jar包可以参考如下形式: 用IDEA自带的打包形式 用Maven插件maven-shade-plugin打包 用Maven插件maven-a ...

  2. 根据xml文件名获取xml数据并转化为实体。

    1.定义一个xml文件. <?xml version="1.0" encoding="utf-8" ?> <UserManager xmlns ...

  3. [CentOs7]搭建ftp服务器(2)——添加用户

    摘要 上篇文章完成了ftp服务器的安装与匿名访问的内容,当然出于安全的考虑是不允许匿名访问服务器的,所以就有了本篇的内容 ,为ftp服务器添加用户,用改用户进行访问. vsftpd添加用户 FTP用户 ...

  4. SQL Server 多实例下的复制

    一.本文所涉及的内容(Contents) 本文所涉及的内容(Contents) 背景(Contexts) 搭建步骤(Procedure) 注意事项(Attention) 二.背景(Contexts) ...

  5. 2、摘要函数——MD2/MD4/MD5数字签名

    摘要是用来防止数据被私自改动的方法,其中用到的函数叫做摘要函数.这些函数的输入可以是任意大小的信息,但是输出是大小固定的摘要.摘要有个重要的特性:如果改变了输入信息的任何内容,即使改变一位,输出也将发 ...

  6. VS2013:Unit Test 单元测试入门

    一.打开VS ,找到工具栏的“工具->扩展和更新”,添加图片内容 二.新建控制台应用程序: CalculatorDemo 三.新建类:Operation.cs,并添加方法 namespace C ...

  7. 使用h5的history改善ajax列表请求体验

    信息比较丰富的网站通常会以分页显示,在点“下一页”时,很多网站都采用了动态请求的方式,避免页面刷新.虽然大家都是ajax,但是从一些小的细节还是 可以区分优劣.一个小的细节是能否支持浏览器“后退”和“ ...

  8. 2.ASP.NET MVC 中使用Crystal Report水晶报表

    上一篇,介绍了怎么导出Excel文件,这篇文章介绍在ASP.NET MVC中使用水晶报表. 项目源码下载:https://github.com/caofangsheng93/CrystalReport ...

  9. 基于STM32Cube的IIC主从通信

    1.建立STM32Cube工程,我使用的芯片是STM32F429,为了更简单的建立单独的IIC通信任务,我使用了FREERTOS,另外选择了RCC(系统时钟初始化),I2C1,I2C2(为了验证代码的 ...

  10. c#使用Split分割换行符 \r\n

    c# 使用Split分割 换行符,方法如下(其余方法有空再添加):   string str = "aa" + "\r\n" + "bb"; ...