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


Posted in Python onFebruary 11, 2019

本文实例讲述了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错误:AttributeError: 'module' object has no attribute 'setdefaultencoding'问题的解决方法
Aug 22 Python
深入理解Python中的内置常量
May 20 Python
python实现微信自动回复功能
Apr 11 Python
Python闭包函数定义与用法分析
Jul 20 Python
Windows 64位下python3安装nltk模块
Sep 19 Python
浅谈Python3中strip()、lstrip()、rstrip()用法详解
Apr 29 Python
python3 自动识别usb连接状态,即对usb重连的判断方法
Jul 03 Python
pyenv与virtualenv安装实现python多版本多项目管理
Aug 17 Python
python flask搭建web应用教程
Nov 19 Python
基于Pycharm加载多个项目过程图解
Jan 19 Python
Python + selenium + crontab实现每日定时自动打卡功能
Mar 31 Python
scrapy在python爬虫中搭建出错的解决方法
Nov 22 Python
Python实现对特定列表进行从小到大排序操作示例
Feb 11 #Python
实例讲解Python中浮点型的基本内容
Feb 11 #Python
实例介绍Python中整型
Feb 11 #Python
python开发准备工作之配置虚拟环境(非常重要)
Feb 11 #Python
pycharm配置pyqt5-tools开发环境的方法步骤
Feb 11 #Python
pycharm+PyQt5+python最新开发环境配置(踩坑)
Feb 11 #Python
Python requests模块实例用法
Feb 11 #Python
You might like
学习php设计模式 php实现工厂模式(factory)
2015/12/07 PHP
PHP使用curl模拟post上传及接收文件的方法
2016/03/04 PHP
php判断用户是否关注微信公众号
2016/07/22 PHP
Fleaphp常见函数功能与用法示例
2016/11/15 PHP
纯JavaScript实现的兼容各浏览器的添加和移除事件封装
2015/03/28 Javascript
JS替换字符串中空格方法
2015/04/17 Javascript
《JavaScript高级编程》学习笔记之object和array引用类型
2015/11/01 Javascript
JS实现iframe编辑器光标位置插入内容的方法(兼容IE和Firefox)
2016/06/24 Javascript
从零开始学习Node.js系列教程五:服务器监听方法示例
2017/04/13 Javascript
微信小程序中多个页面传参通信的学习与实践
2017/05/05 Javascript
nodejs 子进程正确的打开方式
2017/07/03 NodeJs
gulp教程_从入门到项目中快速上手使用方法
2017/09/14 Javascript
在vue项目中使用sass的配置方法
2018/03/20 Javascript
如何使用CSS3+JQuery实现悬浮墙式菜单
2019/06/18 jQuery
Vue使用鼠标在Canvas上绘制矩形
2020/12/24 Vue.js
[02:30]DOTA2英雄基础教程 暗影恶魔
2013/12/17 DOTA
python数据结构之图的实现方法
2015/07/08 Python
Django自定义分页与bootstrap分页结合
2021/02/22 Python
Django自定义认证方式用法示例
2017/06/23 Python
python交互界面的退出方法
2019/02/16 Python
Python小白必备的8个最常用的内置函数(推荐)
2019/04/03 Python
NumPy 基本切片和索引的具体使用方法
2019/04/24 Python
pyqt5 实现多窗口跳转的方法
2019/06/19 Python
python实现集中式的病毒扫描功能详解
2019/07/09 Python
Python实现图片添加文字
2019/11/26 Python
Python多线程:主线程等待所有子线程结束代码
2020/04/25 Python
快速了解Python开发环境Spyder
2020/06/29 Python
python 利用百度API识别图片文字(多线程版)
2020/12/14 Python
英国二手iPhone、音乐、电影和游戏商店:musicMagpie
2018/10/26 全球购物
J2EE面试题
2016/03/14 面试题
访谈节目策划方案
2014/05/15 职场文书
保护环境的标语
2014/06/09 职场文书
简易离婚协议书范本2014
2014/10/15 职场文书
民主生活会发言材料
2014/10/20 职场文书
中学生国庆节演讲稿2015
2015/07/30 职场文书
婚礼迎宾词大全
2015/08/10 职场文书