最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python實(shí)現(xiàn)的大數(shù)據(jù)分析操作系統(tǒng)日志功能示例

 更新時(shí)間:2019年02月11日 11:14:14   作者:chengqiuming  
這篇文章主要介紹了Python實(shí)現(xiàn)的大數(shù)據(jù)分析操作系統(tǒng)日志功能,涉及Python大文件切分、讀取、多線程操作等相關(guān)使用技巧,需要的朋友可以參考下

本文實(shí)例講述了Python實(shí)現(xiàn)的大數(shù)據(jù)分析操作系統(tǒng)日志功能。分享給大家供大家參考,具體如下:

一 代碼

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)
  #如果不使用多線程,可以直接這樣寫(xiě)
  '''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')

二 運(yùn)行結(jié)果

依次運(yùn)行上面3個(gè)程序,得到最終結(jié)果:

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

更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python日志操作技巧總結(jié)》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門(mén)與進(jìn)階經(jīng)典教程》及《Python文件與目錄操作技巧匯總

希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • Matplotlib繪制條形圖的方法你知道嗎

    Matplotlib繪制條形圖的方法你知道嗎

    這篇文章主要為大家詳細(xì)介紹了Matplotlib繪制條形圖的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助<BR>
    2022-03-03
  • python3?最常用的三種裝飾器語(yǔ)法匯總

    python3?最常用的三種裝飾器語(yǔ)法匯總

    這篇文章主要介紹了python3?最常用的三種裝飾器語(yǔ)法總結(jié),本文通過(guò)示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-06-06
  • python版本坑:md5例子(python2與python3中md5區(qū)別)

    python版本坑:md5例子(python2與python3中md5區(qū)別)

    這篇文章主要介紹了python版本坑:md5例子(python2與python3中md5區(qū)別),需要的朋友可以參考下
    2017-06-06
  • 細(xì)說(shuō)NumPy數(shù)組的四種乘法的使用

    細(xì)說(shuō)NumPy數(shù)組的四種乘法的使用

    這篇文章主要介紹了細(xì)說(shuō)NumPy數(shù)組的四種乘法的使用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-12-12
  • 酷! 程序員用Python帶你玩轉(zhuǎn)沖頂大會(huì)

    酷! 程序員用Python帶你玩轉(zhuǎn)沖頂大會(huì)

    程序員用Python玩轉(zhuǎn)王思聰?shù)摹稕_頂大會(huì)》,感興趣的小伙伴們可以參考一下
    2018-01-01
  • Python 安裝第三方庫(kù) pip install 安裝慢安裝不上的解決辦法

    Python 安裝第三方庫(kù) pip install 安裝慢安裝不上的解決辦法

    很多朋友反映在使用pip install安裝python 第三方庫(kù)的過(guò)程中會(huì)出現(xiàn)網(wǎng)速很慢,或者是安裝下載到中途,停止,卡主,或者是下載報(bào)錯(cuò)等問(wèn)題,下面小編給大家?guī)?lái)了解決方法,一起看看吧
    2019-06-06
  • scikit-learn線性回歸,多元回歸,多項(xiàng)式回歸的實(shí)現(xiàn)

    scikit-learn線性回歸,多元回歸,多項(xiàng)式回歸的實(shí)現(xiàn)

    這篇文章主要介紹了scikit-learn線性回歸,多元回歸,多項(xiàng)式回歸的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-08-08
  • Python爬取破解無(wú)線網(wǎng)絡(luò)wifi密碼過(guò)程解析

    Python爬取破解無(wú)線網(wǎng)絡(luò)wifi密碼過(guò)程解析

    這篇文章主要介紹了Python爬取破解無(wú)線網(wǎng)絡(luò)密碼過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-09-09
  • python循環(huán)監(jiān)控遠(yuǎn)程端口的方法

    python循環(huán)監(jiān)控遠(yuǎn)程端口的方法

    這篇文章主要介紹了python循環(huán)監(jiān)控遠(yuǎn)程端口的方法,涉及Python實(shí)現(xiàn)端口監(jiān)控的技巧,需要的朋友可以參考下
    2015-03-03
  • 原來(lái)我一直安裝 Python 庫(kù)的姿勢(shì)都不對(duì)呀

    原來(lái)我一直安裝 Python 庫(kù)的姿勢(shì)都不對(duì)呀

    平常我都是直接執(zhí)行 pip install 安裝的第三方庫(kù),很多教程也是這么介紹的,一直以來(lái)我都認(rèn)為這是標(biāo)準(zhǔn)的、正確的安裝 Python 第三方庫(kù)的姿勢(shì)。下面小編給大家分享一篇教程,一起看看吧
    2019-11-11

最新評(píng)論

木兰县| 前郭尔| 金堂县| 北京市| 韩城市| 驻马店市| 杭锦后旗| 临泽县| 井研县| 九龙县| 文水县| 阿合奇县| 阿合奇县| 谷城县| 仙居县| 光山县| 台前县| 大厂| 垦利县| 普兰县| 大邑县| 大英县| 星子县| 务川| 翁牛特旗| 榆林市| 贡山| 尉犁县| 延庆县| 泽库县| 商丘市| 墨竹工卡县| 赣州市| 来安县| 宿州市| 吴堡县| 东兰县| 镇赉县| 临武县| 枣强县| 安乡县|