18 统计网站每日的访问次数

本文介绍了如何将竞赛数据上传至HDFS,通过Idea创建MapReduce项目,配置依赖,编写Mapper、Reducer和Driver模块的代码,使用Maven打包为Jar文件,然后执行jar进行数据统计。最后展示了统计结果的查看过程。
摘要由CSDN通过智能技术生成

1.将竞赛的数据上传HDFS,查看数据的格式

  

通过浏览器访问hdfs,查看该文档前面的部分数据

每条数据的字段值之间使用逗号隔开的 ,最终时间是第五个自动,获取第五个字段值的中的年月日。

  2.通过Idea创建项目mr-raceData ,基础的配置

修改pom.xml,添加依赖

<dependencies>
    <dependency>
        <groupId>org.apache.hadoop</groupId>
        <artifactId>hadoop-client</artifactId>
        <version>3.1.4</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>1.7.30</version>
    </dependency>
</dependencies>

在resources目录下,新建log4j.properties

log4j.rootLogger=INFO, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n
log4j.appender.logfile=org.apache.log4j.FileAppender
log4j.appender.logfile.File=D:\\visitcount.log
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n

编写代码后,需要将其打成Jar包,需要修改pom.xml 

<build>
    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <configuration>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
            </configuration>
            <executions>
                <execution>
                    <id>make-assembly</id>
                    <phase>package</phase>
             
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

打包使用的插件:

指定打包的方式为jar 

编写源代码:

Mapper模块:

Reducer模块:

Driver模块:

最后使用maven打包为Jar,按以下四步参考,clean-->validate-->compile-->package

在当前项目下的target目录下找到打包后的jar文件

将jar文件拷贝到桌面,并上传的master的当前用户目录下

将竞赛日志数据取部分上传到hdfs上

[yt@master ~]$ hdfs dfs -put access_log.txt  /bigdata/
 

执行jar文件,实现访问每条访问次数的统计

[yt@master ~]$ hadoop jar visitcount-1.0-SNAPSHOT.jar  com.maidu.visitcount.DailyAccessCount  /bigdata/access_log.txt  /output11/
 

执行完成后,可以查看输出文件,看到最终的统计结果。

查看统计的结果:

统计成功。

  • 21
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
真正没有任何时间限制 真正的访客页面停留时间统计 系统支持所有不同类型及版本的浏览器 具有成熟的稳定性和可靠性 系统支持海量访问数据的统计分析 并拥有丰富的特色优势功能 真正实时的访问统计系统 全球唯一可以统计当前实时在线访客的系统 并且可以与任意访客发起沟通对话 唯一可以统计并发在线访客的系统 告诉您真实的网站负载 鼠标点击热点功能实时查看 支持任意时间段 多种查询条件的统计分析展现 准确告诉您页面热点情况 为您提供广告战略决策支持 准确把握每一个访客的访问行为轨迹 了解客户心理 更好的为其提供个性化服务 未知地区信息按需更新功能(不存在未知地区) 让您准确把握营销区域 提升销售机会 多指标 浏览量 访问量 停留时间 独立访客 二跳率 跳出率 综合评价功能 让您准确把握各网站 各栏目专题的排名评比效果 给你最准确的业绩考核参考 自动统计简报邮件 让您一上班就可以准确把握网站访问情况 做到运筹帷幄 心中有数 废话不多说 有用得上的赶紧下载吧 http: www adjyc com free html 软件程序免费下载安装 无任何使用时间限制 一次安装 永久使用 功能强不强 一试便知 支持的操作系统 Microsoft Windows Server 2003 x86 and x64 Editions Microsoft Windows Server 2008 x86 and x64 Editions Microsoft Windows Server 2008 R2 Editions Microsoft Windows Server 2012 Editions 支持的数据库 Microsoft SQL Server 2008 x86 and x64 Editions Microsoft SQL Server 2008 R2 x86 and x64 Editions Microsoft SQL Server 2012 x86 and x64 Editions 微软即将发布sqlserver2014也是支持的 ">真正没有任何时间限制 真正的访客页面停留时间统计 系统支持所有不同类型及版本的浏览器 具有成熟的稳定性和可靠性 系统支持海量访问数据的统计分析 并拥有丰富的特色优势功能 真正实时的访问统计系统 全球唯 [更多]
要实现统计网站2023每日访问次数,可以使用Hadoop中的MapReduce框架。具体步骤如下: 1. 将网站2023的访问日志文件上传至Hadoop分布式文件系统中。 2. 编写MapReduce程序,其中Mapper阶段将每条访问记录解析成日期和访问次数键值对,Reducer阶段将相同日期的访问次数累加起来。 3. 运行MapReduce程序,得到每日访问次数统计结果。 以下是示例代码: Mapper部分: ```java public class AccessLogMapper extends Mapper<LongWritable, Text, Text, IntWritable> { private final static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); private final static IntWritable ONE = new IntWritable(1); private Text date = new Text(); public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException { String line = value.toString(); String[] fields = line.split("\t"); if (fields.length >= 3) { try { Date timestamp = sdf.parse(fields[0]); date.set(sdf.format(timestamp)); context.write(date, ONE); } catch (ParseException e) { // ignore bad records } } } } ``` Reducer部分: ```java public class AccessLogReducer extends Reducer<Text, IntWritable, Text, IntWritable> { private IntWritable totalCount = new IntWritable(); public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException { int count = 0; for (IntWritable value : values) { count += value.get(); } totalCount.set(count); context.write(key, totalCount); } } ``` 运行命令: ```bash hadoop jar access-log.jar AccessLog /input/access.log /output ``` 其中,`access-log.jar`是打包好的MapReduce程序,`AccessLog`是程序的入口类,`/input/access.log`是输入文件路径,`/output`是输出文件路径。最终的统计结果将保存在`/output`目录下的结果文件中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值