python - 如何从python中的日志中提取 key

标签 python hadoop hadoop-streaming

我编写了python代码,以便从日志中提取 key 。使用相同的日志,它在一台机器上运行良好。但是当我在hadoop中运行它时,它失败了。我猜使用regex时有一些错误.谁能给我点意见?是regex不支持hadoop?

此 python 代码旨在提取 qryrc ,并计算 rc 的值,然后打印为qry query_count rc_count .当在hadoop中运行它时,它会报告
java.lang.RuntimeException: PipeMapRed.waitOutputThreads(): subprocess failed with code 1 .

我搜索谷歌,您的映射器代码中可能存在一些错误。那么我该如何解决呢?

像这样的日志格式,

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=



我的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)

最佳答案

最有可能的是您的正则表达式捕获的内容超出了您的预期。我建议将其拆分为一些更简单的部分,例如:

(?<= qry=).*(?= quid0)


(?<= rc=).*(?= discount)

关于python - 如何从python中的日志中提取 key ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18394844/

相关文章:

java - 如何让 Hadoop 服务器监听所有 IP

hadoop - MRjob文档中的“The location specified by MRJOB_CONF”

hadoop - 在hadoop中找不到shell脚本

python,dijkstra的最短路径,类型错误 - 生成器不支持项目分配

python - numpy 的内存分析器

python - 如果日期字符串中有字符或负数,我该如何编写语句?

python - 将类似范围的序列连接到元组列表

hadoop - 将一个 Hive 表拆分成测试集和训练集?

hadoop - hive ,直线 : Peer indicated failure: GSS initiate failed

hadoop - 允许多个 hadoop/EMR 任务在关闭前失败