From 4747d647c766834151d51f7df1d4a88d05c42be8 Mon Sep 17 00:00:00 2001 From: fly6516 Date: Mon, 14 Apr 2025 03:46:02 +0800 Subject: [PATCH] =?UTF-8?q?refactor(2-3.py):=20=E9=87=8D=E6=9E=84=E6=97=A5?= =?UTF-8?q?=E5=BF=97=E5=88=86=E6=9E=90=E4=BB=A3=E7=A0=81=E5=B9=B6=E6=8F=90?= =?UTF-8?q?=E5=8F=96=20404=20=E9=94=99=E8=AF=AF=E6=9C=80=E5=A4=9A=E7=9A=84?= =?UTF-8?q?=E7=AB=AF=E7=82=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 重命名变量和函数以提高代码可读性 - 修改日志解析逻辑,优化错误处理 - 提取状态码为 404 的日志并计算触发错误最多的端点 - 输出前 20个触发 404 错误最多的端点 --- 2-3.py | 24 ++++++++++++------------ 2-4.py | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 12 deletions(-) create mode 100644 2-4.py diff --git a/2-3.py b/2-3.py index 686ade0..b2d61a1 100644 --- a/2-3.py +++ b/2-3.py @@ -4,12 +4,12 @@ from pyspark import SparkContext # 初始化 SparkContext sc = SparkContext.getOrCreate() -# 日志正则表达式 +# 日志匹配的正则表达式 LOG_PATTERN = re.compile( r'^(\S+) (\S+) (\S+) \[([\w:/]+\s[+-]\d{4})\] "(\S+) (\S+)\s*(\S*)\s?" (\d{3}) (\S+)' ) -# 日志解析函数 +# 解析日志的函数 def parse_log_line(line): match = LOG_PATTERN.match(line) if not match: @@ -35,24 +35,24 @@ if __name__ == "__main__": logFile = "hdfs://master:9000/user/root/apache.access.log.PROJECT" raw_logs = sc.textFile(logFile) - # 解析日志 + # 解析并过滤有效日志 access_logs = raw_logs.map(parse_log_line).filter(lambda x: x is not None).cache() - # 获取 404 错误日志 + # 提取状态码为 404 的日志 error_404_logs = access_logs.filter(lambda log: log['status_code'] == 404).cache() - # 获取前 25 个产生 404 错误最多的主机 - top_25_404_hosts = ( + # 计算触发 404 错误最多的端点 + top_20_404_endpoints = ( error_404_logs - .map(lambda log: (log['ip'], 1)) + .map(lambda log: (log['endpoint'], 1)) .reduceByKey(lambda a, b: a + b) - .takeOrdered(25, key=lambda x: -x[1]) + .takeOrdered(20, key=lambda x: -x[1]) ) - # 打印结果 - print("前 25 个产生 404 错误最多的主机:") - for i, (host, count) in enumerate(top_25_404_hosts): - print("{}: {} 次 404 错误".format(i + 1, host, count)) + # 输出结果 + print("前 20 个触发 404 错误最多的端点:") + for i, (endpoint, count) in enumerate(top_20_404_endpoints): + print("{}: {} => {} 次 404 错误".format(i + 1, endpoint, count)) # 停止 Spark sc.stop() diff --git a/2-4.py b/2-4.py new file mode 100644 index 0000000..686ade0 --- /dev/null +++ b/2-4.py @@ -0,0 +1,58 @@ +import re +from pyspark import SparkContext + +# 初始化 SparkContext +sc = SparkContext.getOrCreate() + +# 日志正则表达式 +LOG_PATTERN = re.compile( + r'^(\S+) (\S+) (\S+) \[([\w:/]+\s[+-]\d{4})\] "(\S+) (\S+)\s*(\S*)\s?" (\d{3}) (\S+)' +) + +# 日志解析函数 +def parse_log_line(line): + match = LOG_PATTERN.match(line) + if not match: + return None + + content_size_str = match.group(9) + content_size = int(content_size_str) if content_size_str.isdigit() else 0 + + return { + 'ip': match.group(1), + 'user_identity': match.group(2), + 'user_id': match.group(3), + 'timestamp': match.group(4), + 'method': match.group(5), + 'endpoint': match.group(6), + 'protocol': match.group(7), + 'status_code': int(match.group(8)), + 'content_size': content_size + } + +if __name__ == "__main__": + # 加载日志文件 + logFile = "hdfs://master:9000/user/root/apache.access.log.PROJECT" + raw_logs = sc.textFile(logFile) + + # 解析日志 + access_logs = raw_logs.map(parse_log_line).filter(lambda x: x is not None).cache() + + # 获取 404 错误日志 + error_404_logs = access_logs.filter(lambda log: log['status_code'] == 404).cache() + + # 获取前 25 个产生 404 错误最多的主机 + top_25_404_hosts = ( + error_404_logs + .map(lambda log: (log['ip'], 1)) + .reduceByKey(lambda a, b: a + b) + .takeOrdered(25, key=lambda x: -x[1]) + ) + + # 打印结果 + print("前 25 个产生 404 错误最多的主机:") + for i, (host, count) in enumerate(top_25_404_hosts): + print("{}: {} 次 404 错误".format(i + 1, host, count)) + + # 停止 Spark + sc.stop()