Python如何解决错误:java.lang.RuntimeException:PipeMapRed.waitOutputThreads():子进程失败,代码为2

时间:2017-02-07 07:49:50

标签: python-2.7 hadoop

我遇到了在hadoop流中运行简单python代码的问题。 我已尝试过以前帖子中的所有建议,但出现类似错误并仍然存在问题。

  1. 添加了usr / bin / env python
  2. chmod a + x mapper和reducer python code
  3. put"" -mapper" python mapper.py -n 1 -r 0.4"
  4. 我已在外面运行代码并且运行良好。

    更新:我使用以下代码运行hadoop流之外的代码:

    cat file |python mapper.py -n 5 -r 0.4 |sort|python reducer.py -f 3618 
    

    这很好..但现在我需要将它运行到HADOOP STREAMING

    hadoop jar /usr/lib/hadoop-mapreduce/hadoop-streaming.jar \
    -D mapreduce.job.reduces=5  \
    -files lr \
    -mapper "python lr/mapper.py -n 5 -r 0.4"  \
    -reducer "python lr/reducer.py -f 3618"  \
    -input training \
    -output models 
    

    hadoop流媒体是失败的。我查看了日志,但是我没有看到任何告诉我它为什么会发生的事情?

    我有以下 mapper.py

    #!/usr/bin/env python
    
    import sys
    import random
    
    from optparse import OptionParser
    
    parser = OptionParser()
    parser.add_option("-n", "--model-num", action="store", dest="n_model",
                      help="number of models to train", type="int")
    parser.add_option("-r", "--sample-ratio", action="store", dest="ratio",
                      help="ratio to sample for each ensemble", type="float")
    
    options, args = parser.parse_args(sys.argv)
    
    random.seed(8803)
    r = options.ratio
    for line in sys.stdin:
        # TODO
        # Note: The following lines are only there to help 
        #       you get started (and to have a 'runnable' program). 
        #       You may need to change some or all of the lines below.
        #       Follow the pseudocode given in the PDF.
        key = random.randint(0, options.n_model-1)
        value = line.strip()
        for i in range(1, options.n_model+1):
            m = random.random()
            if m < r:
                print "%d\t%s" % (i, value)
    

    和我的 reducer.py

    #!/usr/bin/env python
    import sys
    import pickle
    from optparse import OptionParser
    from lrsgd import LogisticRegressionSGD
    from utils import parse_svm_light_line
    
    parser = OptionParser()
    parser.add_option("-e", "--eta", action="store", dest="eta",
                      default=0.01, help="step size", type="float")
    parser.add_option("-c", "--Regularization-Constant", action="store", dest="C",
                      default=0.0, help="regularization strength", type="float")
    parser.add_option("-f", "--feature-num", action="store", dest="n_feature",
                      help="number of features", type="int")
    options, args = parser.parse_args(sys.argv)
    
    classifier = LogisticRegressionSGD(options.eta, options.C, options.n_feature)
    
    for line in sys.stdin:
        key, value = line.split("\t", 1)
        value = value.strip()
        X, y = parse_svm_light_line(value)
        classifier.fit(X, y)
    
    pickle.dump(classifier, sys.stdout)
    

    当我在代码之外运行它时,它运行正常,但是当我在hadoop-streaming中运行它时,它给了我错误:

    17/02/07 07:44:34 INFO mapreduce.Job: Task Id : attempt_1486438814591_0038_m_000001_2, Status : FAILED
    Error: java.lang.RuntimeException: PipeMapRed.waitOutputThreads(): subprocess failed with code 2
        at org.apache.hadoop.streaming.PipeMapRed.waitOutputThreads(PipeMapRed.java:322)
        at org.apache.hadoop.streaming.PipeMapRed.mapRedFinished(PipeMapRed.java:535)
        at org.apache.hadoop.streaming.PipeMapper.close(PipeMapper.java:130)
    

1 个答案:

答案 0 :(得分:0)

在帖子-How to resolve java.lang.RuntimeException: PipeMapRed.waitOutputThreads(): subprocess failed with code 2?

中使用Harishanker的答案

确保使用chmod可执行映射器文件和化简器文件。 (例如:“ chmod 744 mapper.py”)

然后像这样执行流式传输命令:

hadoop jar /usr/lib/hadoop-mapreduce/hadoop-streaming.jar \
-D mapreduce.job.reduces=5  \
-files lr \
-mapper lr/mapper.py -n 5 -r 0.4  \
-reducer lr/reducer.py -f 3618  \
-input training \
-output models 

现在它应该可以工作了。如果没有,请发表评论。