MapReduce编程入门-日志访问次数统计任务
使用到的资源
hadoop-eclipse-plugin-2.6.4.jar
hadoop-2.7.7
日志文件 user_login.txt
上述资源以及实验源码均已上传到百度云盘,可自行获取
链接:https://pan.baidu.com/s/1sprb5Ytue3rxMEAMTXZoVQ
提取码:fec7
复制这段内容后打开百度网盘手机App,操作更方便哦
搭建IDE开发环境,创建MapReduce工程MemberCount
1.将插件hadoop-eclipse-plugin-2.6.4.jar复制到eclipse的dropins目录下
2.菜单选择Windowns – Perspective – Open Perspective – Other,在对话框中选中 Map/Reduce,点击OK
3.点击小象图标增加 Hadoop 集群的连接
4.集群信息按自己的 Hadoop 集群实际信息填写,点击Finish
5.创建连接后,刷新 HDFS 列表即可获得文件目录最新状态
6.在主菜单上选择Windowns – Preference,选中Hadoop Map/Reduce,添加 hadoop-2.7.7 文件所在路径,导入MapReduce运行依赖的jar包
7.新建工程 Map/Reduce Project
8.工程命名为 MemberCount ,点击 Finish
9.建好后的工程目录,可见jar包已经导入
通过MapReduce编程解决统计访问次数
10.编写完业务代码后,点击主菜单File – Export,选择 Runnable JAR file ,点击 Next
任务一逻辑代码如下:
package mapreduce.util;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.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;
import org.apache.hadoop.util.GenericOptionsParser;public class dailyAccessCount {public static class MyMapperextends Mapper<Object,Text,Text,IntWritable>{private final static IntWritable one = new IntWritable(1);public void map(Object key,Text value,Context context)throws IOException,InterruptedException{String line = value.toString();//指定逗号为分隔符,组成数组String array[] = line.split(",");//提取数组中的访问日期作为KeyString keyOutput = array[1];//组成键值对context.write(new Text(keyOutput),one);}}public static class MyReducerextends Reducer<Text,IntWritable,Text,IntWritable>{private IntWritable result = new IntWritable();public void reduce(Text key,Iterable<IntWritable> values,Context context)throws IOException,InterruptedException {//定义累加器,初始值为0int sum = 0;for (IntWritable val : values) {//将相同键的所有值进行累加sum += val.get();}result.set(sum);context.write(key,result);}}public static void main(String[] args) throws Exception {Configuration conf = new Configuration();Job job = Job.getInstance(conf,"Daily Access Count");job.setJarByClass(dailyAccessCount.class);job.setMapperClass(MyMapper.class);job.setReducerClass(MyReducer.class);job.setMapOutputKeyClass(Text.class);job.setMapOutputValueClass(IntWritable.class);job.setOutputKeyClass(Text.class);job.setOutputValueClass(IntWritable.class);for (int i = 0; i < args.length - 1; ++ i) {FileInputFormat.addInputPath(job, new Path(args[i]));}FileOutputFormat.setOutputPath(job, new Path(args[args.length - 1]));System.exit(job.waitForCompletion(true) ? 0 : 1);}
}
11.选择相应的类,点击Finish
12.使用Xftp工具将日志文件和业务jar包传输到虚拟机的opt文件夹中
13.上传需要统计的用户登录日志文件到HDFS文件系统中
创建/user/root目录
/opt/hadoop/bin/hdfs dfs -mkdir -p /user/root/
上传日志文件到hdfs文件系统中
/opt/hadoop/bin/hdfs dfs -copyFromLocal /opt/user_login.txt /user/root/
14.执行用户日志统计程序
执行命令:
/opt/hadoop/bin/hadoop jar /opt/dailyAccessCount.jar /user/root/user_login.txt /user/root/AccessCount
运行结果:
15.统计结果生成的文件
16.查看part-r-00000文件内容可见统计结果
通过MapReduce编程解决按访问次数进行排序
17.编写任务二业务代码生成jar包上传到虚拟机中
任务二逻辑代码:
package mapreduce.util;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.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;
import org.apache.hadoop.util.GenericOptionsParser;public class accessTimesSort {public static class MyMapper extends Mapper<Object, Text,IntWritable,Text>{public void map(Object key, Text value, Context context) throws IOException, InterruptedException {String lines = value.toString(); //指定tab为分隔符,组成数组String array[] = lines.split("\t"); //提取访问次数做为Keyint keyOutput = Integer.parseInt(array[1]); //提取访问日期做为ValuesString valueOutput = array[0]; context.write(new IntWritable(keyOutput), new Text(valueOutput)); }}public static class MyReducer extends Reducer<IntWritable,Text,Text,IntWritable> {public void reduce(IntWritable key, Iterable<Text> values, Context context)throws IOException, InterruptedException {for(Text value : values){context.write(value, key);}}}public static void main(String[] args) throws Exception{Configuration conf = new Configuration();String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();if (otherArgs.length < 2) {System.err.println("Usage: wordcount <in> [<in>...] <out>");System.exit(2);}Job job = new Job(conf, "Access Time Sort");job.setJarByClass(accessTimesSort.class);job.setMapperClass(MyMapper.class);job.setReducerClass(MyReducer.class);job.setMapOutputKeyClass(IntWritable.class);job.setMapOutputValueClass(Text.class);job.setOutputKeyClass(Text.class);job.setOutputValueClass(IntWritable.class);for (int i = 0; i < otherArgs.length - 1; ++i) {FileInputFormat.addInputPath(job, new Path(otherArgs[i]));}FileOutputFormat.setOutputPath(job,new Path(otherArgs[otherArgs.length - 1]));System.exit(job.waitForCompletion(true) ? 0 : 1);}
}
18.执行任务二统计程序
执行命令
/opt/hadoop/bin/hadoop jar /opt/accessTimesSort.jar /user/root/AccessCount /user/root/TimesSort
19.统计生成的结果文件
20.打开查看统计结果,可见内容以及按照访问次数升序排序
至此,统计实验完毕。
MapReduce编程入门-日志访问次数统计任务相关推荐
- 大数据之hadoop伪集群搭建与MapReduce编程入门
一.理论知识预热 一句话介绍hadoop: Hadoop的核心由分布式文件系统HDFS与Map/Reduce计算模型组成. (1)HDFS分布式文件系统 HDFS由三个角色构成: 1)NameNode ...
- shell最大出现和连续出现次数_shell脚本分析 nginx日志访问次数最多及最耗时的页面(慢查询)...
当服务器压力比较大,跑起来很费力时候.我们经常做站点页面优化,会去查找那些页面访问次数比较多,而且比较费时. 找到那些访问次数高,并且比较耗时的地址,就行相关优化,会取得立竿见影的效果的. 下面是我在 ...
- shell脚本分析 nginx日志访问次数最多及最耗时的页面(慢查询)
当服务器压力比较大,跑起来很费力时候.我们经常做站点页面优化,会去查找那些页面访问次数比较多,而且比较费时. 找到那些访问次数高,并且比较耗时的地址,就行相关优化,会取得立竿见影的效果的. 下面是我在 ...
- 用户IP访问次数统计
技术源于生活,服务生活 线上地址:https://api.imibi.cn Redis存储 1.使用Redis的0号数据库,数据格式是hash. import redis con = redis.Re ...
- ip访问次数统计 nginx_Linux分析Nginx日志统计IP访问次数的shell脚本
平时运维遇到最多的就是nginx的日志分析了,要时常做系统监控,检查IP的访问次数是否有异常,防止恶意访问. 假设我的nginx日志如下: ....... 211.253.43.23 - - [03/ ...
- jsp网站访问次数统计
JSP 点击量统计 有时候我们需要知道某个页面被访问的次数,这时我们就需要在页面上添加页面统计器,页面访问的统计一般在用户第一次载入时累加该页面的访问数上. 要实现一个计数器,您可以利用应用程序隐式对 ...
- 博客园添加访问次数统计
1. 先到http://www.amazingcounters.com/sign-up.php 这个地址去申请一个帐户,申请时填写好自己的资料,比如 你的统计风格样式.要统计的地址.要统计的类型, ...
- Hexo Next主题添加访客统计、访问次数统计、文章阅读次数统计
Hexo的版本:v3.9.0 Next的版本:v7.4.0 1.打开next主题配置文件\themes\next_config.yml,搜索找到busuanzi_count,把enable设置为tru ...
- (2)文章页面浏览次数+网站访问次数统计显示
文章目录 一.插件安装与配置 二.页面显示 三.样式修改 我的个人网站IP地址:139.9.58.252(网站还在备案,域名不可用),现想实现页面浏览次数和网站访问次数的统计显示 一.插件安装与配置 ...
最新文章
- 马化腾六年后知乎再提问:未来十年哪些基础科学突破会影响互联网科技产业?...
- oracle 数据库信息查询
- mysql的left join和inner join的效率对比,以及如何优化
- Redis配置主从数据,实现主从库之间数据同步
- nrf51822-配对绑定实现过程
- 设计模式--责任链模式(COR)
- 从C# 到 Java 点滴
- Qt - 一文理解QThread多线程(万字剖析整理)
- 微信分享 无法获取到分享状态的问题-微信分享功能调整
- Mapreduce统计
- erp软件的主要功能
- 【Python画樱花源码】
- WXS脚本之WXS语法
- 怎样解决移动硬盘提示格式化的问题
- 组建无线网络的六条思路
- CTF CRYPTO 从零开始的RSA1
- C Primer Plus (第六版)编程练习参考答案
- SpringBootMybatisPlus 种子项目
- 视频直播源码开发时关于MySQL数据库的恢复方案
- 工业质检如何以“智”取胜?15分钟上手工业零部件检测全流程方案