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

本文涉及的产品
全局流量管理 GTM,标准版 1个月
云解析 DNS,旗舰版 1个月
日志服务 SLS,月写入数据量 50GB 1个月
简介:

咱们一般写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 ,如需转载请自行联系原作者



相关实践学习
日志服务之使用Nginx模式采集日志
本文介绍如何通过日志服务控制台创建Nginx模式的Logtail配置快速采集Nginx日志并进行多维度分析。
相关文章
|
10天前
|
数据采集 JSON API
深入解析:使用 Python 爬虫获取淘宝店铺所有商品接口
本文介绍如何使用Python结合淘宝开放平台API获取指定店铺所有商品数据。首先需注册淘宝开放平台账号、创建应用并获取API密钥,申请接口权限。接着,通过构建请求、生成签名、调用接口(如`taobao.items.search`和`taobao.item.get`)及处理响应,实现数据抓取。代码示例展示了分页处理和错误处理方法,并强调了调用频率限制、数据安全等注意事项。此技能对开发者和数据分析师极具价值。
|
3天前
|
API 开发工具 Python
|
27天前
|
存储 索引 Python
Python入门:6.深入解析Python中的序列
在 Python 中,**序列**是一种有序的数据结构,广泛应用于数据存储、操作和处理。序列的一个显著特点是支持通过**索引**访问数据。常见的序列类型包括字符串(`str`)、列表(`list`)和元组(`tuple`)。这些序列各有特点,既可以存储简单的字符,也可以存储复杂的对象。 为了帮助初学者掌握 Python 中的序列操作,本文将围绕**字符串**、**列表**和**元组**这三种序列类型,详细介绍其定义、常用方法和具体示例。
Python入门:6.深入解析Python中的序列
|
27天前
|
存储 Linux iOS开发
Python入门:2.注释与变量的全面解析
在学习Python编程的过程中,注释和变量是必须掌握的两个基础概念。注释帮助我们理解代码的意图,而变量则是用于存储和操作数据的核心工具。熟练掌握这两者,不仅能提高代码的可读性和维护性,还能为后续学习复杂编程概念打下坚实的基础。
Python入门:2.注释与变量的全面解析
|
26天前
|
存储 人工智能 程序员
通义灵码AI程序员实战:从零构建Python记账本应用的开发全解析
本文通过开发Python记账本应用的真实案例,展示通义灵码AI程序员2.0的代码生成能力。从需求分析到功能实现、界面升级及测试覆盖,AI程序员展现了需求转化、技术选型、测试驱动和代码可维护性等核心价值。文中详细解析了如何使用Python标准库和tkinter库实现命令行及图形化界面,并生成单元测试用例,确保应用的稳定性和可维护性。尽管AI工具显著提升开发效率,但用户仍需具备编程基础以进行调试和优化。
227 9
|
3天前
|
机器学习/深度学习 数据采集 自然语言处理
基于Python的情感分析与情绪识别技术深度解析
本文探讨了基于Python的情感分析与情绪识别技术,涵盖基础概念、实现方法及工业应用。文中区分了情感分析与情绪识别的核心差异,阐述了从词典法到深度学习的技术演进,并通过具体代码展示了Transformers架构在细粒度情感分析中的应用,以及多模态情绪识别框架的设计。此外,还介绍了电商评论分析系统的构建与优化策略,包括领域自适应训练和集成学习等方法。未来,随着深度学习和多模态数据的发展,该技术将更加智能与精准。
22 0
|
1月前
|
监控 算法 安全
内网桌面监控软件深度解析:基于 Python 实现的 K-Means 算法研究
内网桌面监控软件通过实时监测员工操作,保障企业信息安全并提升效率。本文深入探讨K-Means聚类算法在该软件中的应用,解析其原理与实现。K-Means通过迭代更新簇中心,将数据划分为K个簇类,适用于行为分析、异常检测、资源优化及安全威胁识别等场景。文中提供了Python代码示例,展示如何实现K-Means算法,并模拟内网监控数据进行聚类分析。
43 10
|
2月前
|
存储 算法 安全
控制局域网上网软件之 Python 字典树算法解析
控制局域网上网软件在现代网络管理中至关重要,用于控制设备的上网行为和访问权限。本文聚焦于字典树(Trie Tree)算法的应用,详细阐述其原理、优势及实现。通过字典树,软件能高效进行关键词匹配和过滤,提升系统性能。文中还提供了Python代码示例,展示了字典树在网址过滤和关键词屏蔽中的具体应用,为局域网的安全和管理提供有力支持。
62 17
|
2月前
|
运维 Shell 数据库
Python执行Shell命令并获取结果:深入解析与实战
通过以上内容,开发者可以在实际项目中灵活应用Python执行Shell命令,实现各种自动化任务,提高开发和运维效率。
80 20
|
18天前
|
存储 数据采集 JSON
Python爬取某云热歌榜:解析动态加载的歌曲数据
Python爬取某云热歌榜:解析动态加载的歌曲数据

热门文章

最新文章