本文实例讲述了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相关内容感兴趣的读者可查看本站专题:《Python日志操作技巧总结》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》、《Python入门与进阶经典教程》及《Python文件与目录操作技巧汇总》
希望本文所述对大家Python程序设计有所帮助。
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
白云城资源网 Copyright www.dyhadc.com
暂无“Python实现的大数据分析操作系统日志功能示例”评论...
RTX 5090要首发 性能要翻倍!三星展示GDDR7显存
三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。
首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。
据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。
更新日志
2025年04月26日
2025年04月26日
- 小骆驼-《草原狼2(蓝光CD)》[原抓WAV+CUE]
- 群星《欢迎来到我身边 电影原声专辑》[320K/MP3][105.02MB]
- 群星《欢迎来到我身边 电影原声专辑》[FLAC/分轨][480.9MB]
- 雷婷《梦里蓝天HQⅡ》 2023头版限量编号低速原抓[WAV+CUE][463M]
- 群星《2024好听新歌42》AI调整音效【WAV分轨】
- 王思雨-《思念陪着鸿雁飞》WAV
- 王思雨《喜马拉雅HQ》头版限量编号[WAV+CUE]
- 李健《无时无刻》[WAV+CUE][590M]
- 陈奕迅《酝酿》[WAV分轨][502M]
- 卓依婷《化蝶》2CD[WAV+CUE][1.1G]
- 群星《吉他王(黑胶CD)》[WAV+CUE]
- 齐秦《穿乐(穿越)》[WAV+CUE]
- 发烧珍品《数位CD音响测试-动向效果(九)》【WAV+CUE】
- 邝美云《邝美云精装歌集》[DSF][1.6G]
- 吕方《爱一回伤一回》[WAV+CUE][454M]