使用Python构建基于Hadoop的MapReduce日志分析平台

【广告位】文章头部(标题下方、正文上方)

流量比较大的日志要是直接写入Hadoop对Namenode负载过大,所以入库前合并,可以把各个节点的日志凑并成一个文件写入HDFS。 根据情况定期合成,写入到hdfs里面。

使用Python构建基于Hadoop的MapReduce日志分析平台

咱们看看日志的大小,200G的dns日志文件,我压缩到了18G,要是用awk perl当然也可以,但是处理速度肯定没有分布式那样的给力。

使用Python构建基于Hadoop的MapReduce日志分析平台

Hadoop Streaming原理

mapper和reducer会从标准输入中读取用户数据,一行一行处理后发送给标准输出。Streaming工具会创建MapReduce作业,发送给各个tasktracker,同时监控整个作业的执行过程。

任何语言,只要是方便接收标准输入输出就可以做mapreduce~

再搞之前我们先简单测试下shell模拟mapreduce的性能速度~

看下他的结果,350M的文件用时35秒左右。

这是2G的日志文件,居然用了3分钟。 当然和我写的脚本也有问题,我们是模拟mapreduce的方式,而不是调用shell下牛逼的awk,gawk处理。

awk的速度 !果然很霸道,处理日志的时候,我也很喜欢用awk,只是学习的难度有点大,不像别的shell组件那么灵活简单。

推荐阅读:

《Python开发技术详解》.( 周伟,宗杰).[高清PDF扫描版+随书视频+代码] http://www.linuxidc.com/Linux/2013-11/92693.htm

Python脚本获取Linux系统信息 http://www.linuxidc.com/Linux/2013-08/88531.htm

Python 网站文件及数据库备份脚本 http://www.linuxidc.com/Linux/2012-06/62346.htm

Python文件处理:读取文件 http://www.linuxidc.com/Linux/2013-08/88496.htm

如何发布自定义的Python模块 http://www.linuxidc.com/Linux/2013-08/88495.htm

Python爬虫多线程抓取代理服务器 http://www.linuxidc.com/Linux/2013-07/87289.htm

Python中re(正则表达式)模块详解 http://www.linuxidc.com/Linux/2013-08/88588.htm

Python 的详细介绍:请点这里
Python 的下载地址:请点这里

这是官方的提供的两个demo ~
map.py

#!/usr/bin/env python
"""A more advanced Mapper, using Python iterators and generators."""
import sys
def read_input(file):
    for line in file:
        # split the line into words
        yield line.split()
def main(separator='\t'):
    # input comes from STDIN (standard input)
    data = read_input(sys.stdin)
    for words in data:
        # write the results to STDOUT (standard output);
        # what we output here will be the input for the
        # Reduce step, i.e. the input for reducer.py
        #
        # tab-delimited; the trivial word count is 1
        for word in words:
            print '%s%s%d' % (word, separator, 1)
if __name__ == "__main__":
    main()

 

reduce.py的修改方式

#!/usr/bin/env python
"""A more advanced Reducer, using Python iterators and generators."""
from itertools import groupby
from operator import itemgetter
import sys
def read_mapper_output(file, separator='\t'):
    for line in file:
        yield line.rstrip().split(separator, 1)
def main(separator='\t'):
    # input comes from STDIN (standard input)
    data = read_mapper_output(sys.stdin, separator=separator)
    # groupby groups multiple word-count pairs by word,
    # and creates an iterator that returns consecutive keys and their group:
    #  current_word - string containing a word (the key)
    #  group - iterator yielding all ["<current_word>", "<count>"] items
    for current_word, group in groupby(data, itemgetter(0)):
        try:
            total_count = sum(int(count) for current_word, count in group)
            print "%s%s%d" % (current_word, separator, total_count)
        except ValueError:
            # count was not a number, so silently discard this item
            pass
if __name__ == "__main__":
    main()

咱们再简单点:

#!/usr/bin/env python
import sys
for line in sys.stdin:
    line = line.strip()
    words = line.split()
    for word in words:
        print '%s\t%s' % (word, 1)

 

#!/usr/bin/env python
                                                                                                                                                                                                           
from operator import itemgetter
import sys
                                                                                                                                                                                                           
current_word = None
current_count = 0
word = None
                                                                                                                                                                                                           
for line in sys.stdin:
    line = line.strip()
    word, count = line.split('\t', 1)
    try:
        count = int(count)
    except ValueError:
        continue
    if current_word == word:
        current_count += count
    else:
        if current_word:
            print '%s\t%s' % (current_word, current_count)
        current_count = count
        current_word = word
                                                                                                                                                                                                         
if current_word == word:
    print '%s\t%s' % (current_word, current_count)

【广告位】文章正文中间
本文已展示 50% · 完整内容需验证码解锁

关注公众号,解锁完整文章

微信扫码关注 星哥玩云, 在公众号对话框回复关键词 星哥玩云博客 获取验证码。输入后即可阅读全文, 一次解锁全站文章通用。

星哥玩云 微信公众号二维码
微信扫一扫 / 长按识别,关注「星哥玩云」
【广告位】文章底部(正文结束、评论区上方)
评论功能已在静态化后停用 · 返回顶部 ↑