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脚本来获取Google搜索结果的示例
May 04 Python
python获取指定路径下所有指定后缀文件的方法
May 26 Python
Python实现好友全头像的拼接实例(推荐)
Jun 24 Python
mac 安装python网络请求包requests方法
Jun 13 Python
python中join()方法介绍
Oct 11 Python
用Python逐行分析文件方法
Jan 28 Python
Python爬取爱奇艺电影信息代码实例
Nov 26 Python
python定间隔取点(np.linspace)的实现
Nov 27 Python
python numpy 矩阵堆叠实例
Jan 17 Python
Win 10下Anaconda虚拟环境的教程
May 18 Python
Python操作MySQL数据库的示例代码
Jul 13 Python
套娃式文件夹如何通过Python批量处理
Aug 23 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多进程编程总结(推荐)
2016/07/18 PHP
在Laravel的Model层做数据缓存的实现
2019/09/26 PHP
PHP7变量处理机制修改
2021/03/09 PHP
Gird组件 Part-3:范例RSSFeed Viewer
2007/03/10 Javascript
了解jQuery技巧来提高你的代码
2010/01/08 Javascript
js字符串日期yyyy-MM-dd转化为date示例代码
2014/03/06 Javascript
Javascript中的五种数据类型详解
2014/12/26 Javascript
Nodejs关于gzip/deflate压缩详解
2015/03/04 NodeJs
理解javascript正则表达式
2016/03/08 Javascript
jQuery页面加载初始化的3种方法(推荐)
2016/06/02 Javascript
用jquery快速解决IE输入框不能输入的问题
2016/10/04 Javascript
基于JavaScript实现Tab选项卡切换效果
2016/11/24 Javascript
JS定时器实现数值从0到10来回变化
2016/12/09 Javascript
使用contextMenu插件实现Bootstrap table弹出右键菜单
2017/02/20 Javascript
JavaScript实现修改伪类样式
2017/11/27 Javascript
node 命令方式启动修改端口的方法
2018/05/12 Javascript
jquery 动态遍历select 赋值的实例
2018/09/12 jQuery
小程序两种滚动公告栏的实现方法
2019/09/17 Javascript
python实现五子棋小游戏
2020/03/25 Python
Python判断变量名是否合法的方法示例
2019/01/28 Python
Python 中Django验证码功能的实现代码
2019/06/20 Python
Python实用工具FuckIt.py介绍
2019/07/02 Python
利用python实现汉字转拼音的2种方法
2019/08/12 Python
Python 3.6 中使用pdfminer解析pdf文件的实现
2019/09/25 Python
pytorch数据预处理错误的解决
2020/02/20 Python
哈工大自然语言处理工具箱之ltp在windows10下的安装使用教程
2020/05/07 Python
python 求两个向量的顺时针夹角操作
2021/03/04 Python
伦敦剧院门票:From The Box Office
2018/06/30 全球购物
英国复古服装购物网站:Collectif
2019/10/30 全球购物
Java如何调用外部Exe程序
2015/07/04 面试题
机关单位动员会主持词
2014/03/20 职场文书
五年级学生评语
2014/04/22 职场文书
生物科学专业自荐书
2014/06/20 职场文书
班主任开场白
2015/06/01 职场文书
详细聊一聊mysql的树形结构存储以及查询
2022/04/05 MySQL
MySQL常用慢查询分析工具详解
2022/08/14 MySQL