22 优化日志文件统计程序-按月份统计每个用户每天的访问次数

       读取任务一中序列文件,统计每个用户每天的访问次数,最终将2021/1和2021/2的数据分别输出在两个文件中。

一、创建项目步骤:

1.创建项目

2.修改pom.xml文件

<packaging>jar</packaging>

<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>

<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>

3.在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:\\countLog.log
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n

二、编写程序:

1.自定义键的类型 MemberLogTime  包含两个属性(memberId,memberLogTime)  实现WritableComparable接口

2.编写Mapper模块:(在Mapper中计数器,使用枚举)

package com.maidu.countlog;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import java.io.IOException;
/**@author:yt* @since:2024-05-15* 定义Mapper模块*/
public class LogCountMapper  extends Mapper<Text,Text,MemberLogTime, IntWritable> {private MemberLogTime mt =new MemberLogTime();private IntWritable one =new IntWritable(1);//使用枚举enum LogCounter{January,February}@Overrideprotected void map(Text key, Text value, Mapper<Text, Text, MemberLogTime, IntWritable>.Context context)throws IOException, InterruptedException {String memberId =key.toString();String logTime =value.toString();//计数器if(logTime.contains("2021/1")){//一月计数器值+1context.getCounter(LogCounter.January).increment(1);}else{context.getCounter(LogCounter.February).increment(1);}//将用户ID和访问时间存到MemberLogTime对象中mt.setMemberId(memberId);mt.setLogTime(logTime);context.write(mt,one);}
}

3.编写Combiner:

package com.maidu.countlog;import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.mapreduce.Reducer;import java.io.IOException;/*** @author:yt* @since:2024-05-15*   合并的类*/
public class LogCoutCombiner  extends Reducer<MemberLogTime, IntWritable,MemberLogTime,IntWritable> {@Overrideprotected void reduce(MemberLogTime key, Iterable<IntWritable> values, Reducer<MemberLogTime, IntWritable, MemberLogTime, IntWritable>.Context context)throws IOException, InterruptedException {int sum =0;for(IntWritable val:values){sum+=val.get();}context.write(key,new IntWritable(sum));}
}

4.编写Patitioner:

package com.maidu.countlog;import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.mapreduce.Partitioner;/*** @author:yt* @since:2024-05-15*/
public class LogCountPartitioner extends Partitioner<MemberLogTime, IntWritable> {@Overridepublic int getPartition(MemberLogTime memberLogTime, IntWritable intWritable, int numPartitions) {String date = memberLogTime.getLogTime();if(  date.contains( "2021/1")  ){return 0%numPartitions;}else{return 1%numPartitions;}}
}

5.编写Reduce模块:

package com.maidu.countlog;import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.mapreduce.Reducer;import java.io.IOException;/*** @author:yt* @since:2024-05-15*/
public class LogCountReducer  extends Reducer<MemberLogTime, IntWritable,MemberLogTime,IntWritable> {@Overrideprotected void reduce(MemberLogTime key, Iterable<IntWritable> values, Reducer<MemberLogTime, IntWritable, MemberLogTime, IntWritable>.Context context)throws IOException, InterruptedException {//计数器(动态计数器)if(key.getLogTime().contains("2021/1")){context.getCounter("OuputCounter","JanuaryResult").increment(1);} else{context.getCounter("OuputCounter","FabruaryResult").increment(1);}int sum =0;for(IntWritable val:values){sum+=val.get();}context.write(key,new IntWritable(sum));}
}

6.编写Driver模块:

package com.maidu.countlog;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.input.SequenceFileAsTextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
/*** @author:yt* @since:2024-05-15*/
public class LogCount {public static void main(String[] args) throws  Exception {Configuration  conf =new Configuration();Job job =Job.getInstance(conf,"Log count");job.setJarByClass(LogCount.class);job.setMapperClass(LogCountMapper.class);job.setReducerClass(LogCountReducer.class);job.setCombinerClass(LogCoutCombiner.class);job.setPartitionerClass(LogCountPartitioner.class);//设置reduce任务数2job.setNumReduceTasks(2);job.setOutputKeyClass(MemberLogTime.class);job.setOutputValueClass(IntWritable.class);job.setInputFormatClass(SequenceFileAsTextInputFormat.class);job.setOutputFormatClass(TextOutputFormat.class);//添加输入路径FileInputFormat.addInputPath(job,new Path(args[0]));FileOutputFormat.setOutputPath(job,new Path(args[1]));System.exit(job.waitForCompletion(true)?0:1);}
}

7.使用Maven打包为Jar文件,上传到master节点上执行

[yt@master ~]$ hadoop jar countLog-1.0-SNAPSHOT.jar  com.maidu.countlog.LogCount  /ouput-2301-select/part-m-00000  /logcount/2301/
 

8.查看运行结果

(1)计数器

(2)结果

(3)查看其中一个文件内容

[yt@master ~]$ hdfs dfs -cat /logcount/2301/part-r-00001

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/pingmian/13142.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

HNU-算法设计与分析-作业1

算法设计与分析 计科210X 甘晴void 202108010XXX 前言 这个系列本来想只用一个博客搞定的&#xff0c;谁曾想CSDN对于大批量文字的在线编辑一塌糊涂&#xff0c;感觉走倒车了。只能分成几个博客分别来讲了。后续会有作业-23456。作业重要的是搞懂原因。 文章目录 算法设计与…

python manage.py 命令大全

以下是常见的 Django 管理命令列表及其用途&#xff1a; startapp&#xff1a;创建一个新的 Django 应用程序。 startproject&#xff1a;创建一个新的 Django 项目。 runserver&#xff1a;启动开发服务器。 makemigrations&#xff1a;根据你对模型的更改创建迁移文件。 …

【数据结构】时间、空间复杂度实例分析

跌倒了&#xff0c;就重新站起来&#xff0c;继续向前走&#xff1b;傻坐在地上是没用的。&#x1f493;&#x1f493;&#x1f493; 目录 •✨说在前面 &#x1f34b;知识点一&#xff1a;算法的效率 • &#x1f330;1.斐波那契数列的第n项 • &#x1f330;2.算法的复杂度…

破解OKR落地难题:撰写阶段的陷阱与策略

OKR&#xff08;Objectives and Key Results&#xff0c;目标与关键成果&#xff09;作为一种目标管理工具&#xff0c;已经被越来越多的企业所采纳。然而&#xff0c;OKR的落地实施并非一帆风顺&#xff0c;尤其在撰写阶段&#xff0c;往往会遇到各种陷阱和挑战。本文将详细分…

001_PyQt简介

本系列面向零基础小白&#xff0c;从零开始到Pyqt 进行项目实战。 什么叫从零开始&#xff1f;从软件安装、环境配置开始。 不跳过一个细节&#xff0c;不漏掉一行代码&#xff0c;不省略一个例图。 PyQt作为一个强大的工具包&#xff0c;成功地将脚本语言python和QT库融合到…

nginx反向代理kafka集群实现内外网隔离访问 —— 筑梦之路

背景说明 我们在使用Kafka客户端连接到Kafka集群时&#xff0c;即使连接的节点只配置了一个集群的Broker地址&#xff0c;该Broker将返回给客户端集群所有节点的信息列表。然后客户端使用该列表信息&#xff08;Topic的分区信息&#xff09;再与集群进行数据交互。这里Kafka列表…

代码随想录算法训练营第36期DAY29

DAY29 39组合总和 class Solution {private: vector<vector<int>> result; vector<int>path; void backtracking(vector<int> candidates,int target,int sum,int startindex){ if(sum>target) return; if(sumtarget){ …

WordPress外贸建站程序对比

在选择建站引擎时&#xff0c;WordPress是许多企业和个体创业者的首选。然而&#xff0c;WordPress本身有各种不同的版本和扩展&#xff0c;因此在选择最适合你业务的引擎时需要仔细权衡。本文将对比一些流行的WordPress建站程序&#xff0c;帮助你找到最符合你需求的引擎。 1.…

pytorch-10 神经网络的损失函数

1. 回归&#xff1a;SSE和MSE # MSE损失函数 import torch from torch.nn import MSELossyhat torch.randn(size(50,), dtypetorch.float32) y torch.randn(size(50,), dtypetorch.float32)criterion MSELoss() loss1 criterion(yhat, y)# 计算mse 误差平方 criterion MS…

投影与降维

摘要&#xff1a; 投影是将数据从原始的高维空间映射到一个低维空间的过程&#xff0c;通常这个低维空间的维度小于原始空间。降维是减少数据集中变量数量的技术&#xff0c;旨在提取数据的代表性特征&#xff0c;同时去除无关或冗余的信息。两者都旨在处理高维数据&#xff0c…

Python操作Redis(连接方式、通用操作、字符串操作、Hash操作、List操作)

Python操作Redis 目录 Python操作Redis普通连接连接池连接通用操作字符串操作Hash操作List操作 安装&#xff1a;pip install redis 普通连接 每次连接都会创建新的连接 import redisconn redis.Redis(host127.0.0.1,port6379,db0, # 数据库编号(Redis支持多数据库)passwor…

SQL进阶(六):通关题:制作一个活动日历

目录 通关题&#xff1a;用 SQL 制作一个活动日历任务 1&#xff1a; 制作一个日历Q1: 在 2023 年当中&#xff0c;星期 2 出现的次数和星期 5 出现的次数的关系是&#xff1f;&#xff08;选择 > 或 < 或 &#xff09;Q2: 在 2023 年每个月的5号&#xff0c;10号&#x…

当CV遇上transformer(三)Clip模型及源码分析

当CV遇上transformer(三)Clip模型及源码分析 2020年10月&#xff0c;Dosovitskiy首次将纯Transformer的网络结构应用于图像分类任务中(ViT)&#xff0c;并取得了当时最优的分类效果&#xff0c;其研究成果是Transformer完全替代标准卷积的首次尝试。随着谷歌提出ViT之后&#…

Python 全栈体系【四阶】(四十五)

第五章 深度学习 十、生成对抗网络&#xff08;GAN&#xff09; 1. 图像生成技术概述 1.1 什么是图像生成技术 图像生成技术是指利用机器学习或深度学习等人工智能技术&#xff0c;通过训练模型来生成逼真的图像。这些技术可以根据给定的输入&#xff0c;生成与真实图像相似…

反序列化漏洞【1】

1.不安全的反序列化漏洞介绍 序列化&#xff1a;将对象转换成字符串&#xff0c;目的是方便传输&#xff0c;关键词serialize a代表数组&#xff0c;数组里有三个元素&#xff0c;第一个元素下标为0&#xff0c;长度为7&#xff0c;内容为porsche&#xff1b;第二个元素下标为1…

GPT-4o API 全新版本发布:提升性能,增加性价比

5月13日&#xff0c;OpenAI 发布了全新ChatGPT模型 GPT-4o&#xff0c;它在响应速度和多媒体理解上都有显著提升。在这篇文章中&#xff0c;我们将介绍 GPT-4o 的主要特点及其 API 集成方式。 什么是 GPT-4o&#xff1f; GPT-4o 是 OpenAI 于5月13日发布的最新多模态 AI 模型…

【简单介绍下在Ubuntu中如何设置中文输入法】

&#x1f308;个人主页: 程序员不想敲代码啊 &#x1f3c6;CSDN优质创作者&#xff0c;CSDN实力新星&#xff0c;CSDN博客专家 &#x1f44d;点赞⭐评论⭐收藏 &#x1f91d;希望本文对您有所裨益&#xff0c;如有不足之处&#xff0c;欢迎在评论区提出指正&#xff0c;让我们共…

PgMP考试难度大吗?社会认可怎么样?

对于已经在职场摸爬滚打多年&#xff0c;或者对项目和项目集管理具有极高兴趣和追求的你来说&#xff0c;一定听说过PgMP&#xff08;项目集管理专业人士&#xff09;这个国际认证。那么PgMP很难考吗&#xff1f; 免费送备考资料。联系我们&#xff1a;18938656370 一、PgMP考…

国产化数据库_金仓_Linux版Docker版部署过程及简单使用

国产化数据库金仓Linux版部署过程 文档参考&#xff1a;https://help.kingbase.com.cn/v8/install-updata/install-linux/install-linux-3.html#id12 以下安装是在Centos7系统下进行 0.安装包准备 找到你的操作系统对应的平台所支持的软件包下载&#xff0c;我这里下载的是x…

react的多级路由定义

在写实验室项目的时候&#xff0c;有一个需求&#xff0c;在二级路由页面点击按钮&#xff0c;跳转到详情列表页面&#xff0c;同时三级路由不用在导航栏显示&#xff0c;效果图如下&#xff1a; 前期的尝试&#xff1a; 在route,js文件这样定义的&#xff1a; {path: music,…