您好,登錄后才能下訂單哦!
本文實例講述了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程序設計有所幫助。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。