2025-04-13 18:39:43 +00:00
|
|
|
|
import re
|
|
|
|
|
from pyspark import SparkContext
|
|
|
|
|
|
|
|
|
|
sc = SparkContext.getOrCreate()
|
|
|
|
|
|
|
|
|
|
logFile = "hdfs://master:9000/user/root/apache.access.log.PROJECT"
|
|
|
|
|
raw_logs = sc.textFile(logFile)
|
|
|
|
|
|
|
|
|
|
# 日志模式
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# 过滤出包含 404 响应代码的日志
|
|
|
|
|
def filter_404(log):
|
|
|
|
|
return log['status_code'] == 404
|
|
|
|
|
|
|
|
|
|
# 解析并过滤日志
|
|
|
|
|
parsed_logs = raw_logs.map(parse_log_line).filter(lambda x: x is not None)
|
|
|
|
|
|
2025-04-13 19:29:09 +00:00
|
|
|
|
# 过滤出 404 错误记录并缓存
|
2025-04-13 18:39:43 +00:00
|
|
|
|
error_404_logs = parsed_logs.filter(filter_404).cache()
|
|
|
|
|
|
2025-04-13 19:29:09 +00:00
|
|
|
|
# 统计 404 错误数量
|
2025-04-13 18:39:43 +00:00
|
|
|
|
count_404 = error_404_logs.count()
|
|
|
|
|
|
2025-04-13 19:29:09 +00:00
|
|
|
|
# 打印结果(使用 .format 替代 f-string)
|
|
|
|
|
print("日志中共有 {} 条 404 响应代码记录。".format(count_404))
|
2025-04-13 18:39:43 +00:00
|
|
|
|
|
|
|
|
|
sc.stop()
|