Python实现的大数据分析操作系统日志功能示例

 更新时间:2019年02月11日 11:14:14   作者:chengqiuming  
这篇文章主要介绍了Python实现的大数据分析操作系统日志功能,涉及Python大文件切分、读取、多线程操作等相关使用技巧,需要的朋友可以参考下

本文实例讲述了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程序设计有所帮助。

相关文章

  • Matplotlib绘制条形图的方法你知道吗

    Matplotlib绘制条形图的方法你知道吗

    这篇文章主要为大家详细介绍了Matplotlib绘制条形图的方法,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助<BR>
    2022-03-03
  • python3 最常用的三种装饰器语法汇总

    python3 最常用的三种装饰器语法汇总

    这篇文章主要介绍了python3 最常用的三种装饰器语法总结,本文通过示例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-06-06
  • python版本坑:md5例子(python2与python3中md5区别)

    python版本坑:md5例子(python2与python3中md5区别)

    这篇文章主要介绍了python版本坑:md5例子(python2与python3中md5区别),需要的朋友可以参考下
    2017-06-06
  • 细说NumPy数组的四种乘法的使用

    细说NumPy数组的四种乘法的使用

    这篇文章主要介绍了细说NumPy数组的四种乘法的使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-12-12
  • 酷! 程序员用Python带你玩转冲顶大会

    酷! 程序员用Python带你玩转冲顶大会

    程序员用Python玩转王思聪的《冲顶大会》,感兴趣的小伙伴们可以参考一下
    2018-01-01
  • Python 安装第三方库 pip install 安装慢安装不上的解决办法

    Python 安装第三方库 pip install 安装慢安装不上的解决办法

    很多朋友反映在使用pip install安装python 第三方库的过程中会出现网速很慢,或者是安装下载到中途,停止,卡主,或者是下载报错等问题,下面小编给大家带来了解决方法,一起看看吧
    2019-06-06
  • scikit-learn线性回归,多元回归,多项式回归的实现

    scikit-learn线性回归,多元回归,多项式回归的实现

    这篇文章主要介绍了scikit-learn线性回归,多元回归,多项式回归的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-08-08
  • Python爬取破解无线网络wifi密码过程解析

    Python爬取破解无线网络wifi密码过程解析

    这篇文章主要介绍了Python爬取破解无线网络密码过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-09-09
  • python循环监控远程端口的方法

    python循环监控远程端口的方法

    这篇文章主要介绍了python循环监控远程端口的方法,涉及Python实现端口监控的技巧,需要的朋友可以参考下
    2015-03-03
  • 原来我一直安装 Python 库的姿势都不对呀

    原来我一直安装 Python 库的姿势都不对呀

    平常我都是直接执行 pip install 安装的第三方库,很多教程也是这么介绍的,一直以来我都认为这是标准的、正确的安装 Python 第三方库的姿势。下面小编给大家分享一篇教程,一起看看吧
    2019-11-11

最新评论