分享一个解析percona MySQL slowlog的python正则

栏目: 数据库 · Mysql · 发布时间: 8年前

直接上脚本把,有需要自取

import re
log="""# User@Host: root[root] @  [127.0.0.1]  Id:    83
# Schema:   Last_errno: 0  Killed: 0
# Query_time: 0.000086  Lock_time: 0.000000  Rows_sent: 1  Rows_examined: 0  Rows_affected: 0
# Bytes_sent: 57
SET timestamp=1536162668;
SELECT DATABASE();"""
slow_log_patern = '^#\s+User@Host:\s+(?P<user>\w+)\[\w+\]\s+@\s+' \
                  '(?P<proxyHost>(\w|\[|\]|\.|\s)+)\s+Id:\s+(?P<threadID>\d+)\n' \
                  '#\s+Schema:\s(?P<Schema>(\w+|\s))\s+' \
                  'Last_errno:\s(?P<lastErrNo>\d+)\s+Killed:\s(?P<KilledErrNo>\d+)\n' \
                  '#\s+Query_time:\s+(?P<queryTime>\w+\.?\w+)\s+' \
                  'Lock_time:\s+(?P<lockTime>\w+\.?\w+)\s+Rows_sent:\s+(?P<rowsSent>\d+)\s+' \
                  'Rows_examined:\s+(?P<rowsExamined>\d+)\s+Rows_affected:\s+(?P<rowsAffected>\d+)\n' \
                  '#\s+Bytes_sent:\s(?P<bytesSent>\d+)\n' \
                  'SET\s+timestamp=(?P<sqlTimestamp>\d+);\n' \
                  '(?P<sqlText>(.|\n)*)'

slow_log_regex = re.compile(slow_log_patern)
match = slow_log_regex.search(log)
if match:
    sql_dict = match.groupdict()
print sql_dict

输出结果

{'rowsExamined': '0', 'lastErrNo': '0', 'sqlTimestamp': '1536162668', 'queryTime': '0.000086', 'proxyHost': '[127.0.0.1] ', 'user': 'root', 'bytesSent': '57', 'threadID': '83', 'KilledErrNo': '0', 'lockTime': '0.000000', 'Schema': ' ', 'rowsSent': '1', 'sqlText': 'SELECT DATABASE();', 'rowsAffected': '0'}

以上所述就是小编给大家介绍的《分享一个解析percona MySQL slowlog的python正则》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。

计算理论导论

计算理论导论

塞普斯 / 机械工业出版社 / 2002-8 / 39.0

This book——by a noted authority and educator in the field——presents computer science theory from a uniquely intuitive,“big picture”perspective.The author grounds his clear and interesting study on ......一起来看看 《计算理论导论》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

URL 编码/解码
URL 编码/解码

URL 编码/解码

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具