Python实现的大数据分析操作系统日志功能示例
作者:chengqiuming 时间:2022-07-31 00:40:34
本文实例讲述了Python实现的大数据分析操作系统日志功能。分享给大家供大家参考,具体如下:
一 代码
1、大文件切分
import os
import os.path
import time
def FileSplit(sourceFile, targetFolder):
if not os.path.isfile(sourceFile):
print(sourceFile, ' does not exist.')
return
if not os.path.isdir(targetFolder):
os.mkdir(targetFolder)
tempData = []
number = 1000
fileNum = 1
linesRead = 0
with open(sourceFile, 'r') as srcFile:
dataLine = srcFile.readline().strip()
while dataLine:
for i in range(number):
tempData.append(dataLine)
dataLine = srcFile.readline()
if not dataLine:
break
desFile = os.path.join(targetFolder, sourceFile[0:-4] + str(fileNum) + '.txt')
with open(desFile, 'a+') as f:
f.writelines(tempData)
tempData = []
fileNum = fileNum + 1
if __name__ == '__main__':
#sourceFile = input('Input the source file to split:')
#targetFolder = input('Input the target folder you want to place the split files:')
sourceFile = 'test.txt'
targetFolder = 'test'
FileSplit(sourceFile, targetFolder)
2、Mapper代码
import os
import re
import threading
import time
def Map(sourceFile):
if not os.path.exists(sourceFile):
print(sourceFile, ' does not exist.')
return
pattern = re.compile(r'[0-9]{1,2}/[0-9]{1,2}/[0-9]{4}')
result = {}
with open(sourceFile, 'r') as srcFile:
for dataLine in srcFile:
r = pattern.findall(dataLine)
if r:
t = result.get(r[0], 0)
t += 1
result[r[0]] = t
desFile = sourceFile[0:-4] + '_map.txt'
with open(desFile, 'a+') as fp:
for k, v in result.items():
fp.write(k + ':' + str(v) + '\n')
if __name__ == '__main__':
desFolder = 'test'
files = os.listdir(desFolder)
#如果不使用多线程,可以直接这样写
'''for f in files:
Map(desFolder + '\\' + f)'''
#使用多线程
def Main(i):
Map(desFolder + '\\' + files[i])
fileNumber = len(files)
for i in range(fileNumber):
t = threading.Thread(target = Main, args =(i,))
t.start()
3.Reducer代码
import os
def Reduce(sourceFolder, targetFile):
if not os.path.isdir(sourceFolder):
print(sourceFolder, ' does not exist.')
return
result = {}
#Deal only with the mapped files
allFiles = [sourceFolder+'\\'+f for f in os.listdir(sourceFolder) if f.endswith('_map.txt')]
for f in allFiles:
with open(f, 'r') as fp:
for line in fp:
line = line.strip()
if not line:
continue
position = line.index(':')
key = line[0:position]
value = int(line[position + 1:])
result[key] = result.get(key,0) + value
with open(targetFile, 'w') as fp:
for k,v in result.items():
fp.write(k + ':' + str(v) + '\n')
if __name__ == '__main__':
Reduce('test', 'test\\result.txt')
二 运行结果
依次运行上面3个程序,得到最终结果:
07/10/2013:4634
07/16/2013:51
08/15/2013:3958
07/11/2013:1
10/09/2013:733
12/11/2013:564
02/12/2014:4102
05/14/2014:737
希望本文所述对大家Python程序设计有所帮助。
来源:https://blog.csdn.net/chengqiuming/article/details/78601136
标签:Python,大数据,操作系统日志
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
自适应css布局——流动布局新时代[译]
2009-08-13 12:28:00
![](https://img.aspxhome.com/file/UploadPic/20098/13/01-20s.jpg)
干涉MySQL优化器使用hash join的方法
2024-01-21 17:29:52
![](https://img.aspxhome.com/file/2023/6/103576_0s.png)
PyCharm创建Django项目的简单步骤记录
2023-08-28 11:03:37
![](https://img.aspxhome.com/file/2023/4/121024_0s.png)
python中单下划线(_)和双下划线(__)的特殊用法
2022-08-09 22:23:22
Python用requests-html爬取网页的实现
2023-03-21 13:24:14
浅析Python 中的 WSGI 接口和 WSGI 服务的运行
2023-02-18 14:45:40
在Asp程序中取得表单所有内容的方法
2010-04-24 16:07:00
数据库查询优化(主从表的设计)
2024-01-18 16:25:52
mysql installer web community 5.7.21.0.msi安装图文教程
2024-01-25 02:35:23
![](https://img.aspxhome.com/file/2023/2/115572_0s.png)
phpMyAdmin安装配置方法
2009-09-09 20:15:00
如何有效防止sql注入的方法
2024-01-18 21:18:13
![](https://img.aspxhome.com/file/2023/4/116324_0s.png)
PHP实现无限极分类的两种方式示例【递归和引用方式】
2023-11-15 18:26:33
php面向对象全攻略 (十二) 抽象方法和抽象类
2023-11-18 20:44:18
Python答题卡识别并给出分数的实现代码
2022-04-10 03:52:46
![](https://img.aspxhome.com/file/2023/3/124753_0s.jpg)
经验总结:ASP与存储过程解析
2008-11-07 13:33:00
Python中使用copy模块实现列表(list)拷贝
2022-05-13 02:46:48
精致的web设计
2009-12-04 19:07:00
python 嵌套型partials的使用
2021-12-24 09:01:46
Python中字符串的基础介绍及常用操作总结
2022-04-12 18:01:03
![](https://img.aspxhome.com/file/2023/2/80382_0s.png)
Python tkinter事件高级用法实例
2023-12-09 10:28:17
![](https://img.aspxhome.com/file/2023/5/93685_0s.jpg)