pycharm做mapreduce任务时,单独map和reduce调试技巧

上一节我们在使用pycharm出现不合法字符找到了解决的方法,就是配置编写环境为unix就解决了,但是日常开发中,少不了调试,在vim中开发mapreduce任务暂时没找到更好的调试方法,那么我们接下来学习下pycharm中是怎么调试的。

1.先大体了解下目录情况

这里写图片描述

2.map.py调试
对于白名单的代码应该很熟悉了,现在我们将主方法注释,用mapper_func方法调用map的函数,修改部分mapper_func的方法

import sys

def read_local_file_func(f):
    word_set = set()
    file_in = open(f, 'r')
    for line in file_in:
        word = line.strip()
        word_set.add(word)
    return word_set


def mapper_func(white_list_fd):
    word_set = read_local_file_func(white_list_fd)
    file_in = open('The_Man_of_Property.txt', 'r') #这里模拟读取hdfs上的数据
    for line in file_in:  #对元数据进行遍历
        ss = line.strip().split(' ')
        for s in ss:
            word = s.strip()
            if word != "" and (word in word_set):
                print "%s\t%s" % (s, 1)

mapper_func('white_list')   #调用方法执行,传入白名单进行遍历执行

# if __name__ == "__main__":
#     module = sys.modules[__name__]
#     func = getattr(module, sys.argv[1])
#     args = None
#     if len(sys.argv) > 1:
#         args = sys.argv[2:]
#     func(*args)

接下来我们可以选择执行run,也可以debug观察代码运行步骤,我们debug查看下,按下F6进行逐步查看,这里就不查看了。
这里写图片描述
下来我们执行完查看,下map最终执行结果,我们看到that等代词,在文章中出现的次数,列举出每一次出现情况,当然这里需要根据key进行sort排序后,在reduce进行分桶统计
这里写图片描述

2.red.py的调试
将控制台执行的结果,复制粘贴到mapper_result文件中,作为red.py的输入源,我们查看下,并且简单修改下red.py的代码
模拟数据源mapper_result进行排序后的数据文件
这里写图片描述
red.py的代码:

#!/usr/bin/python

import sys

def reduer_func():
    current_word = None
    count_pool = []
    sum = 0

    # for line in sys.stdin:
    file_in = open('mapper_result', 'r')#添加输入源
    for line in file_in:
        word, val = line.strip().split('\t')

        if current_word == None:
            current_word = word

        if current_word != word:
            for count in count_pool:
                sum += count
            print "%s\t%s" % (current_word, sum)
            current_word = word
            count_pool = []
            sum = 0

        count_pool.append(int(val))

    for count in count_pool:
        sum += count
    print "%s\t%s" % (current_word, str(sum))

reduer_func()#调用执行


# if __name__ == "__main__":
#     module = sys.modules[__name__]
#     func = getattr(module, sys.argv[1])
#     args = None
#     if len(sys.argv) > 1:
#         args = sys.argv[2:]
#         func(*args)

我们执行red.py,进行调试,查看输入结果
这里写图片描述

致此,mapreduce的白名单的实例过程应该明白了,需要debug查看每行执行过程,了解代码执行流程是最重要的,不能去猜想。
有问题的请留言,谢谢。(老班让写,没办法,估计大家都会调试,。。。。尴尬)

猜你喜欢

转载自blog.csdn.net/qq_17336559/article/details/80947200
今日推荐