簡體   English   中英

在Hadoop Streaming中使用Python使用文件

[英]Using files in Hadoop Streaming with Python

我是Hadoop和MapReduce的新手,正在嘗試自己的工作方式。 我正在嘗試在python中開發一個mapreduce應用程序,其中使用了2個.CSV文件中的數據。 我只是在mapper中讀取兩個文件,然后將文件中的鍵值對打印到sys.stdout

當我在單台機器上使用該程序時,它運行良好,但是使用Hadoop Streaming時,出現錯誤。 我認為在Hadoop上的映射器中讀取文件時犯了一些錯誤。 請幫助我提供代碼,並告訴我如何在Hadoop流中使用文件處理。 mapper.py代碼如下。 (您可以從注釋中了解代碼):

#!/usr/bin/env python
import sys
from numpy import genfromtxt

def read_input(inVal):
    for line in inVal:
        # split the line into words
        yield line.strip()

def main(separator='\t'):
    # input comes from STDIN (standard input)
    labels=[]
    data=[]    
    incoming = read_input(sys.stdin)
    for vals in incoming:
        # 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;
        if len(vals) > 10:
            data.append(vals)
        else:
            labels.append(vals)

    for i in range(0,len(labels)):
        print "%s%s%s\n" % (labels[i], separator, data[i])


if __name__ == "__main__":
    main()

如下所示,從兩個.csv文件向此映射器輸入了60000條記錄(在單台計算機上,而不是hadoop集群上):

cat mnist_train_labels.csv mnist_train_data.csv | ./mapper.py

搜索解決方案大約3天后,我能夠解決該問題。

問題在於較新版本的Hadoop(在我的案例中為2.2.0)。 當從文件中讀取值時,映射器代碼在某個時候給出的退出代碼為非零(可能是因為它一次讀取了一大堆值(784))。 Hadoop 2.2.0中有一個設置,該設置告訴Hadoop系統給出一般錯誤(子流程失敗,代碼1)。 默認情況下,此設置設置為True。 我只需要將此屬性的值設置為False,就可以使我的代碼運行無任何錯誤。

設置為: stream.non.zero.exit.is.failure 只需在流式傳輸時將其設置為false。 因此,流式傳輸命令將類似於:

**hadoop jar ... -D stream.non.zero.exit.is.failure=false ...**

希望它可以幫助某人,並節省3天...;)

您沒有發布錯誤。 在流式傳輸中,您需要傳遞-file參數或-input,以便文件隨流式傳輸作業一起上傳,或者知道在hdfs上的哪里可以找到它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM