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

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

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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/
 

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

查看统计的结果:

统计成功。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值