From beb743f10d61b57741cb07a6adc88fe8dc391ccd Mon Sep 17 00:00:00 2001 From: fly6516 Date: Mon, 14 Apr 2025 03:29:09 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E6=97=A5=E5=BF=97?= =?UTF-8?q?=E8=A7=A3=E6=9E=90=E5=92=8C404=20=E9=94=99=E8=AF=AF=E7=BB=9F?= =?UTF-8?q?=E8=AE=A1=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 新增日志解析函数 parse_log_line,用于解析 Apache 日志 - 添加过滤 404 错误的函数 filter_404 - 实现从 HDFS 读取日志、解析、过滤和统计 404 错误的完整流程- 打印 404 错误记录的数量 --- 2-1.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/2-1.py b/2-1.py index bd3e089..dd0dc31 100644 --- a/2-1.py +++ b/2-1.py @@ -35,13 +35,13 @@ def filter_404(log): # 解析并过滤日志 parsed_logs = raw_logs.map(parse_log_line).filter(lambda x: x is not None) -# 过滤出 404 错误记录 +# 过滤出 404 错误记录并缓存 error_404_logs = parsed_logs.filter(filter_404).cache() -# 统计 404 错误的数量 +# 统计 404 错误数量 count_404 = error_404_logs.count() -# 打印 404 错误记录数量 -print(f"日志中共有 {count_404} 条 404 响应代码记录。") +# 打印结果(使用 .format 替代 f-string) +print("日志中共有 {} 条 404 响应代码记录。".format(count_404)) sc.stop()