「已注销」 2024-11-24 16:35 采纳率: 0%
浏览 5

MapReduce代码集群上跑报错,如何解决?

MapReduce代码本地可以跑通:

img


但是上Hadoop集群就报错:

img

这是我的map.py、reduce.py和run.sh

import os
import sys
import re


def get_white_list_word(white_list_dir):
    white_list_word = set()

    if os.path.isdir(white_list_dir):
        for cachefile in os.listdir(white_list_dir):
            with open(white_list_dir + '/' + cachefile) as cachefile:
                for word in cachefile:
                    word = word.strip()

                    white_list_word.add(word)

    return white_list_word


def mapper_func(white_list_dir):
    white_list_word = get_white_list_word(white_list_dir)

    for line in sys.stdin:
        word_list = line.strip().split(' ')

        for word in word_list:
            if len(re.findall(r'\w+', word)) < 1:
                continue

            word = re.findall(r'\w+', word)[0].lower()

            if word in white_list_word:
                print('\t'.join([word, '1']))


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)

import sys


def reducer_func():
    cur_word = None
    cur_cnt = 0

    for line in sys.stdin:
        word, cnt = line.strip().split('\t')

        if cur_word == None:
            cur_word = word

        if cur_word != word:
            print('\t'.join([cur_word, str(cur_cnt)]))

            cur_word = word
            cur_cnt = 0

        cur_cnt += int(cnt)

    print('\t'.join([cur_word, str(cur_cnt)]))


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)

HADOOP_CMD="/usr/local/src/hadoop_2.6.1/bin/hadoop"
STREAM_JAR_PATH="/usr/local/src/hadoop_2.6.1/share/hadoop/tools/lib/hadoop-streaming-2.6.1.jar"

INPUT_PATH="/input_mr_cachearchive_broadcast/the_man_of_property.txt"
OUTPUT_PATH="/output_mr_cachearchive_broadcast"

$HADOOP_CMD fs -rmr -skipTrash $OUTPUT_PATH

$HADOOP_CMD jar $STREAM_JAR_PATH \
  -input $INPUT_PATH \
  -output $OUTPUT_PATH \
  -mapper "python map.py mapper_func WLD" \
  -reducer "python reduce.py reducer_func" \
  -jobconf "mapred.reduce.tasks=2" \
  -jobconf "mapred.job.name=mr_cachearchive_broadcast" \
  -jobconf "stream.non.zero.exit.is.failure=false" \
  -cacheArchive "hdfs://master:9000/input_mr_cachearchive_broadcast/white_list_dir.tgz#WLD" \
  -file "./map.py" \
  -file "./reduce.py"

  • 写回答

1条回答 默认 最新

  • 关注

    以下回复参考:皆我百晓生券券喵儿等免费微信小程序作答:

    根据你提供的代码和错误信息,看起来你的Hadoop MapReduce任务在运行时遇到了问题。这里有几个可能的原因和解决方案:

    1. Hadoop版本和Python版本兼容性问题:确保你的Hadoop版本与Python版本兼容。有时候,某些Hadoop版本可能与Python 3不完全兼容。你可能需要尝试使用Python 2来运行你的MapReduce任务。
    2. Hadoop配置问题:检查你的Hadoop集群配置是否正确。确保所有的节点都可以互相通信,并且资源管理器(ResourceManager)正在运行。
    3. 数据流问题:你的mapper和reducer函数看起来没有问题,但是你需要确保输入数据(the_man_of_property.txt)的格式正确,并且你的white list目录包含正确的单词列表。另外,-cacheArchive选项的使用可能存在问题,确保你的归档文件路径正确,并且归档文件已成功上传到HDFS。
    4. Job配置问题:检查你的run.sh脚本中的Hadoop命令配置是否正确。尤其是-jobconf选项,确认你设置的mapred.reduce.tasksmapred.job.name等参数是否正确。如果这些参数有误,可能会导致任务失败。
    5. 资源问题:确认你的集群有足够的资源(CPU、内存等)来运行你的MapReduce任务。如果资源不足,可能会导致任务失败。
    6. 警告信息中的潜在问题:在日志中,你看到了许多关于废弃选项的警告信息。虽然这些可能不会直接导致任务失败,但是最好更新你的Hadoop命令以使用最新的、非废弃的选项。例如,使用-D替代-jobconf

    为了更准确地找出问题所在,你可以查看Hadoop的日志输出,找到更详细的错误信息。你可以在Hadoop的JobTracker或ResourceManager的Web UI上查看日志,或者在运行MapReduce任务的机器上的本地日志目录中查找。

    最后,如果你已经检查了以上所有可能的问题并且仍然无法解决问题,建议你向具有Hadoop和Python经验的开发者寻求帮助,或者查阅相关的技术社区和文档。

    评论

报告相同问题?

问题事件

  • 创建了问题 11月24日