如何在python中从日志中提取密钥
为了从日志中提取密钥,我编写了python代码。使用同一个日志,它在一台机器上运行得很好。但是当我在hadoop中运行它时,它失败了。我猜在使用如何在python中从日志中提取密钥,python,hadoop,hadoop-streaming,Python,Hadoop,Hadoop Streaming,为了从日志中提取密钥,我编写了python代码。使用同一个日志,它在一台机器上运行得很好。但是当我在hadoop中运行它时,它失败了。我猜在使用regex时会出现一些错误。谁能给我一些意见呢?regex不能支持hadoop吗 此python代码旨在提取qry和rc,并计算rc的值,然后将其打印为qry query\u count rc\u count。在hadoop中运行时,它会报告 java.lang.RuntimeException:PipeMapRed.waitOutputThreads(
regex
时会出现一些错误。谁能给我一些意见呢?regex
不能支持hadoop吗
此python代码旨在提取qry
和rc
,并计算rc
的值,然后将其打印为qry query\u count rc\u count
。在hadoop中运行时,它会报告
java.lang.RuntimeException:PipeMapRed.waitOutputThreads():子进程失败,代码为1
我搜索谷歌,可能有一些错误在您的映射程序代码。那么我如何才能修复它
像这样的日志格式
注意:01-03 23:57:23[a.cpp][b][222]show_ver=11 sid=ae1d esid=6WVj uid=D1 a=20 qry=cars qid0=293 loc_src=4 phn=0 mid=0 wvar=c op=0 qry_src=0 op|u type=1 src=110 | 120 | 111 at=60942 rc=3 | 1 | 1折扣=20 indv_type=0 rep
我的python代码是这样的
import sys
import re
for line in sys.stdin:
count_result = 0
line = line.strip()
match=re.search('.*qry=(.*?)qid0.*rc=(.*?)discount',line).groups()
if (len(match)<2):
continue
counts_tmp = match[1].strip()
counts=counts_tmp.split('|')
for count in counts:
if count.isdigit():
count_result += int(count)
key_tmp = match[0].strip()
if key_tmp.strip():
key = key_tmp.split('\t')
key = ' '.join(key)
print '%s\t%s\t%s' %(key,1,count_result)
导入系统
进口稀土
对于sys.stdin中的行:
计数结果=0
line=line.strip()
match=re.search('.*qry=(.*)qid0.*rc=(.*)折扣',行).groups()
如果(len(match)做了大量假设并冒昧地做出了有根据的猜测,那么您可能可以像这样解析日志:
from collections import defaultdict
input = """NOTICE: 01-03 23:57:23: [a.cpp][b][222] show_ver=11 sid=ae1d esid=6WVj uid=D1 a=20 qry=cars qid0=293 loc_src=4 phn=0 mid=0 wvar=c op=0 qry_src=0 op_type=1 src=110|120|111 at=60942 rc=3|1|1 discount=20 indv_type=0 rep_query=
NOTICE: 01-03 23:57:23: [a.cpp][b][222] show_ver=11 sid=ae1d esid=6WVj uid=D1 a=20 qry=boats qid0=293 loc_src=4 phn=0 mid=0 wvar=c op=0 qry_src=0 op_type=1 src=110|120|111 at=60942 rc=3|5|2 discount=20 indv_type=0 rep_query=
NOTICE: 01-03 23:57:23: [a.cpp][b][222] show_ver=11 sid=ae1d esid=6WVj uid=D1 a=20 qry=cars qid0=293 loc_src=4 phn=0 mid=0 wvar=c op=0 qry_src=0 op_type=1 src=110|120|111 at=60942 rc=3|somestring|12 discount=20 indv_type=0 rep_query="""
d = defaultdict (lambda: 0)
for line in input.split ("\n"):
tokens = line.split (" ")
count = 0
qry = None
for token in tokens:
pair = token.split ("=")
if len (pair) != 2: continue
key, value = pair
if key == "qry":
qry = value
if key == "rc":
values = value.split ("|")
for value in values:
try: count += int (value)
except: pass
if qry: d [qry] += count
print (d)
假设(a)键-值对由空格分隔,(b)键和值中都没有空格。最有可能的是正则表达式捕获的内容超出了预期。我建议将其拆分为一些更简单的部分,如:
(?<= qry=).*(?= quid0)
(?请记住“qry_src=”和“src=”match*rc=
。我强烈怀疑regex
是否存在漏洞。很高兴听到这一消息并提供帮助。
(?<= rc=).*(?= discount)