国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

利用Python中的pandas庫對cdn日志進行分析詳解

2020-02-23 04:24:24
字體:
來源:轉載
供稿:網友

前言

最近工作工作中遇到一個需求,是要根據CDN日志過濾一些數據,例如流量、狀態碼統計,TOP IP、URL、UA、Referer等。以前都是用 bash shell 實現的,但是當日志量較大,日志文件數G、行數達數千萬億級時,通過 shell 處理有些力不從心,處理時間過長。于是研究了下Python pandas這個數據處理庫的使用。一千萬行日志,處理完成在40s左右。

代碼

#!/usr/bin/python# -*- coding: utf-8 -*-# sudo pip install pandas__author__ = 'Loya Chen'import sysimport pandas as pdfrom collections import OrderedDict"""Description: This script is used to analyse qiniu cdn log.================================================================================日志格式IP - ResponseTime [time +0800] "Method URL HTTP/1.1" code size "referer" "UA"================================================================================日志示例 [0] [1][2]  [3]  [4]   [5]101.226.66.179 - 68 [16/Nov/2016:04:36:40 +0800] "GET http://www.qn.com/1.jpg -" [6] [7] [8]    [9]200 502 "-" "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)"================================================================================"""if len(sys.argv) != 2: print('Usage:', sys.argv[0], 'file_of_log') exit() else: log_file = sys.argv[1] # 需統計字段對應的日志位置 ip  = 0url  = 5status_code = 6size = 7referer = 8ua  = 9# 將日志讀入DataFramereader = pd.read_table(log_file, sep=' ', names=[i for i in range(10)], iterator=True)loop = TruechunkSize = 10000000chunks = []while loop: try: chunk = reader.get_chunk(chunkSize) chunks.append(chunk) except StopIteration: #Iteration is stopped. loop = Falsedf = pd.concat(chunks, ignore_index=True)byte_sum = df[size].sum()        #流量統計top_status_code = pd.DataFrame(df[6].value_counts())      #狀態碼統計top_ip  = df[ip].value_counts().head(10)      #TOP IPtop_referer = df[referer].value_counts().head(10)      #TOP Referertop_ua  = df[ua].value_counts().head(10)      #TOP User-Agenttop_status_code['persent'] = pd.DataFrame(top_status_code/top_status_code.sum()*100)top_url  = df[url].value_counts().head(10)      #TOP URLtop_url_byte = df[[url,size]].groupby(url).sum().apply(lambda x:x.astype(float)/1024/1024) /   .round(decimals = 3).sort_values(by=[size], ascending=False)[size].head(10) #請求流量最大的URLtop_ip_byte = df[[ip,size]].groupby(ip).sum().apply(lambda x:x.astype(float)/1024/1024) /   .round(decimals = 3).sort_values(by=[size], ascending=False)[size].head(10) #請求流量最多的IP# 將結果有序存入字典result = OrderedDict([("流量總計[單位:GB]:"   , byte_sum/1024/1024/1024),   ("狀態碼統計[次數|百分比]:"  , top_status_code),   ("IP TOP 10:"    , top_ip),   ("Referer TOP 10:"   , top_referer),   ("UA TOP 10:"    , top_ua),   ("URL TOP 10:"   , top_url),   ("請求流量最大的URL TOP 10[單位:MB]:" , top_url_byte),    ("請求流量最大的IP TOP 10[單位:MB]:" , top_ip_byte)])# 輸出結果for k,v in result.items(): print(k) print(v) print('='*80)            
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 江孜县| 迭部县| 山阳县| 永和县| 象州县| 新宾| 隆化县| 沧州市| 天全县| 宁国市| 新邵县| 彰武县| 黄平县| 镇安县| 罗城| 新泰市| 寻甸| 古田县| 安新县| 涞源县| 红桥区| 文安县| 清丰县| 隆德县| 富锦市| 庆元县| 新化县| 泊头市| 奉节县| 大同县| 临邑县| 鄂托克旗| 承德县| 射洪县| 司法| 郎溪县| 天等县| 靖边县| 蓬安县| 洛隆县| 闽侯县|