直接上脚本把,有需要自取
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正则》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:- python中的正则表达式模式,用于解析HTML标题标记
- 手摸手教你用go写爬虫之二(准备知识:正则解析有效信息)
- 从一则案例解析js正则的String对象的replace方法使用技巧
- 带答案面经分享-L1正则&L2正则
- 神经网络中的网络优化和正则化(四):正则化
- 正则表达式 – 如何使用正则表达式进行Erlang模式匹配?
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Machine Learning
Kevin Murphy / The MIT Press / 2012-9-18 / USD 90.00
Today's Web-enabled deluge of electronic data calls for automated methods of data analysis. Machine learning provides these, developing methods that can automatically detect patterns in data and then ......一起来看看 《Machine Learning》 这本书的介绍吧!