python调用mrjob实现hadoop的mapreduce日志解析

简介:

咱们一般写mapreduce是通过java和streaming来写的,身为pythoner的我,

java不会,没办法就用streaming来写mapreduce日志分析。 这里要介绍一个

模块,是基于streaming搞的东西。


mrjob 可以让用 Python 来编写 MapReduce 运算,并在多个不同平台上运行,你可以:

  • 使用纯 Python 编写多步的 MapReduce 作业

  • 在本机上进行测试

  • 在 Hadoop 集群上运行


pip 的安装方法:

1
pip install mrjob


我测试的脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#coding:utf- 8
from mrjob.job  import  MRJob
import  re
#xiaorui.cc
#WORD_RE = re.compile(r "[\w']+" )
WORD_RE = re.compile(r "\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}" )
class  MRWordFreqCount(MRJob):
     def mapper(self, word, line):
         for  word  in  WORD_RE.findall(line):
             yield word.lower(),  1
     def combiner(self, word, counts):
         yield word, sum(counts)
     def reducer(self, word, counts):
         yield word, sum(counts)
if  __name__ ==  '__main__' :
     MRWordFreqCount.run()


用法算简单:

python i.py -r inline input1 input2 input3 > out 命令可以将处理多个文件的结果输出到out文件里面。

本地模拟hadoop运行:python 1.py -r local <input> output

这个会把结果输出到output里面,这个output必须写。

hadoop集群上运行:python 1.py -r hadoop <input> output


执行脚本 ~

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
[root@kspc ~]# python mo.py -r local  < 10.7 . 17.7 -dnsquery.log. 1 > output
no configs found; falling back on auto-configuration
no configs found; falling back on auto-configuration
creating tmp directory /tmp/mo.root. 20131224.040935 . 241241
reading from STDIN
writing to /tmp/mo.root. 20131224.040935 . 241241 /step- 0 -mapper_part- 00000
> /usr/bin/python mo.py --step-num= 0  --mapper /tmp/mo.root. 20131224.040935 . 241241 /input_part- 00000  | sort | /usr/bin/python mo.py --step-num= 0  --combiner > /tmp/mo.root. 20131224.040935 . 241241 /step- 0 -mapper_part- 00000
writing to /tmp/mo.root. 20131224.040935 . 241241 /step- 0 -mapper_part- 00001
> /usr/bin/python mo.py --step-num= 0  --mapper /tmp/mo.root. 20131224.040935 . 241241 /input_part- 00001  | sort | /usr/bin/python mo.py --step-num= 0  --combiner > /tmp/mo.root. 20131224.040935 . 241241 /step- 0 -mapper_part- 00001
Counters from step  1 :
   (no counters found)
writing to /tmp/mo.root. 20131224.040935 . 241241 /step- 0 -mapper-sorted
> sort /tmp/mo.root. 20131224.040935 . 241241 /step- 0 -mapper_part- 00000  /tmp/mo.root. 20131224.040935 . 241241 /step- 0 -mapper_part- 00001
writing to /tmp/mo.root. 20131224.040935 . 241241 /step- 0 -reducer_part- 00000
> /usr/bin/python mo.py --step-num= 0  --reducer /tmp/mo.root. 20131224.040935 . 241241 /input_part- 00000  > /tmp/mo.root. 20131224.040935 . 241241 /step- 0 -reducer_part- 00000
writing to /tmp/mo.root. 20131224.040935 . 241241 /step- 0 -reducer_part- 00001
> /usr/bin/python mo.py --step-num= 0  --reducer /tmp/mo.root. 20131224.040935 . 241241 /input_part- 00001  > /tmp/mo.root. 20131224.040935 . 241241 /step- 0 -reducer_part- 00001
Counters from step  1 :
   (no counters found)
Moving /tmp/mo.root. 20131224.040935 . 241241 /step- 0 -reducer_part- 00000  -> /tmp/mo.root. 20131224.040935 . 241241 /output/part- 00000
Moving /tmp/mo.root. 20131224.040935 . 241241 /step- 0 -reducer_part- 00001  -> /tmp/mo.root. 20131224.040935 . 241241 /output/part- 00001
Streaming  final  output from /tmp/mo.root. 20131224.040935 . 241241 /output
removing tmp directory /tmp/mo.root. 20131224.040935 . 241241

执行的时候,资源的占用情况。

133630767.jpg


发现一个很奇妙的东西,mrjob居然调用shell下的sort来排序。。。。

133937941.jpg


为了更好的理解mrjob的用法,再来个例子。


1
2
3
4
5
6
7
8
9
10
11
12
13
from mrjob.job  import  MRJob
#from xiaorui.cc
class  MRWordFrequencyCount(MRJob):
#把东西拼凑起来
     def mapper(self, _, line):
         yield  "chars" , len(line)
         yield  "words" , len(line.split())
         yield  "lines" ,  1
#总结kv
     def reducer(self, key, values):
         yield key, sum(values)
if  __name__ ==  '__main__' :
     MRWordFrequencyCount.run()


看下结果:

135509171.jpg

下面是官网给的一些个用法:


我们可以看到他是支持hdfs和s3存储的 !

Running your job different ways

The most basic way to run your job is on the command line:

$ python my_job.py input.txt

By default, output will be written to stdout.

You can pass input via stdin, but be aware that mrjob will just dump it to a file first:

$ python my_job.py < input.txt

You can pass multiple input files, mixed with stdin (using the - character):

$ python my_job.py input1.txt input2.txt - < input3.txt

By default, mrjob will run your job in a single Python process. This provides the friendliest debugging experience, but it’s not exactly distributed computing!

You change the way the job is run with the -r/--runner option. You can use -rinline (the default), -rlocal, -rhadoop, or -remr.

To run your job in multiple subprocesses with a few Hadoop features simulated, use -rlocal.

To run it on your Hadoop cluster, use -rhadoop.

If you have Elastic MapReduce configured (see Elastic MapReduce Quickstart), you can run it there with -remr.

Your input files can come from HDFS if you’re using Hadoop, or S3 if you’re using EMR:

$ python my_job.py -r emr s3://my-inputs/input.txt
$ python my_job.py -r hadoop hdfs://my_home/input.txt


 本文转自 rfyiamcool 51CTO博客,原文链接:http://blog.51cto.com/rfyiamcool/1344368 ,如需转载请自行联系原作者



相关实践学习
【涂鸦即艺术】基于云应用开发平台CAP部署AI实时生图绘板
【涂鸦即艺术】基于云应用开发平台CAP部署AI实时生图绘板
相关文章
|
存储 Java 文件存储
微服务——SpringBoot使用归纳——Spring Boot使用slf4j进行日志记录—— logback.xml 配置文件解析
本文解析了 `logback.xml` 配置文件的详细内容,包括日志输出格式、存储路径、控制台输出及日志级别等关键配置。通过定义 `LOG_PATTERN` 和 `FILE_PATH`,设置日志格式与存储路径;利用 `&lt;appender&gt;` 节点配置控制台和文件输出,支持日志滚动策略(如文件大小限制和保存时长);最后通过 `&lt;logger&gt;` 和 `&lt;root&gt;` 定义日志级别与输出方式。此配置适用于精细化管理日志输出,满足不同场景需求。
3484 1
|
监控 Java 应用服务中间件
Tomcat log日志解析
理解和解析Tomcat日志文件对于诊断和解决Web应用中的问题至关重要。通过分析 `catalina.out`、`localhost.log`、`localhost_access_log.*.txt`、`manager.log`和 `host-manager.log`等日志文件,可以快速定位和解决问题,确保Tomcat服务器的稳定运行。掌握这些日志解析技巧,可以显著提高运维和开发效率。
2157 13
|
监控 Shell Linux
Android调试终极指南:ADB安装+多设备连接+ANR日志抓取全流程解析,覆盖环境变量配置/多设备调试/ANR日志分析全流程,附Win/Mac/Linux三平台解决方案
ADB(Android Debug Bridge)是安卓开发中的重要工具,用于连接电脑与安卓设备,实现文件传输、应用管理、日志抓取等功能。本文介绍了 ADB 的基本概念、安装配置及常用命令。包括:1) 基本命令如 `adb version` 和 `adb devices`;2) 权限操作如 `adb root` 和 `adb shell`;3) APK 操作如安装、卸载应用;4) 文件传输如 `adb push` 和 `adb pull`;5) 日志记录如 `adb logcat`;6) 系统信息获取如屏幕截图和录屏。通过这些功能,用户可高效调试和管理安卓设备。
11933 2
|
存储 分布式计算 Hadoop
基于Java的Hadoop文件处理系统:高效分布式数据解析与存储
本文介绍了如何借鉴Hadoop的设计思想,使用Java实现其核心功能MapReduce,解决海量数据处理问题。通过类比图书馆管理系统,详细解释了Hadoop的两大组件:HDFS(分布式文件系统)和MapReduce(分布式计算模型)。具体实现了单词统计任务,并扩展支持CSV和JSON格式的数据解析。为了提升性能,引入了Combiner减少中间数据传输,以及自定义Partitioner解决数据倾斜问题。最后总结了Hadoop在大数据处理中的重要性,鼓励Java开发者学习Hadoop以拓展技术边界。
858 7
|
存储 监控 安全
深入解析Sysmon日志:增强网络安全与威胁应对的关键一环
在不断演进的网络安全领域中,保持对威胁的及时了解至关重要。Sysmon日志在这方面发挥了至关重要的作用,通过提供有价值的见解,使组织能够加强其安全姿态。Windows在企业环境中是主导的操作系统,因此深入了解Windows事件日志、它们的独特特性和局限性,并通过Sysmon进行增强,变得至关重要。
687 1
|
存储 运维 监控
深入Linux核心:文件系统与日志解析
【8月更文挑战第20天】
494 2
|
Java Shell Linux
【Linux入门技巧】新员工必看:用Shell脚本轻松解析应用服务日志
关于如何使用Shell脚本来解析Linux系统中的应用服务日志,提供了脚本实现的详细步骤和技巧,以及一些Shell编程的技能扩展。
935 0
【Linux入门技巧】新员工必看:用Shell脚本轻松解析应用服务日志
|
存储 缓存 网络协议
搭建dns服务常见报错--查看/etc/named.conf没有错误日志信息却显示出错(/etc/named.conf:49: missing ‘;‘ before ‘include‘)及dns介绍
搭建dns服务常见报错--查看/etc/named.conf没有错误日志信息却显示出错(/etc/named.conf:49: missing ‘;‘ before ‘include‘)及dns介绍
1301 0
|
数据库 Java 监控
Struts 2 日志管理化身神秘魔法师,洞察应用运行乾坤,演绎奇幻篇章!
【8月更文挑战第31天】在软件开发中,了解应用运行状况至关重要。日志管理作为 Struts 2 应用的关键组件,记录着每个动作和决策,如同监控摄像头,帮助我们迅速定位问题、分析性能和使用情况,为优化提供依据。Struts 2 支持多种日志框架(如 Log4j、Logback),便于配置日志级别、格式和输出位置。通过在 Action 类中添加日志记录,我们能在开发过程中获取详细信息,及时发现并解决问题。合理配置日志不仅有助于调试,还能分析用户行为,提升应用性能和稳定性。
434 0
|
SQL 安全 测试技术
【数据守护者必备】SQL数据备份与恢复策略全解析:从全量到日志备份,手把手教你确保企业信息万无一失的实战技巧!
【8月更文挑战第31天】数据库是企业核心业务数据的基石,为防止硬件故障、软件错误或人为失误导致的数据丢失,制定可靠的备份与恢复策略至关重要。本文通过一个在线购物平台的案例,详细介绍了使用 SQL Server 进行全量备份、差异备份及事务日志备份的方法,并演示了如何利用 SQL Server Agent 实现自动化备份任务。此外,还提供了数据恢复的具体步骤和测试建议,确保数据安全与业务连续性。
875 0

推荐镜像

更多