【问题标题】:How can I include a python package with Hadoop streaming job?如何在 Hadoop 流式作业中包含 python 包?
【发布时间】:2011-07-25 03:33:58
【问题描述】:

我正在尝试在 Hadoop 流式作业中包含一个 python 包 (NLTK),但我不确定如何在不通过 CLI 参数“-file”手动包含每个文件的情况下执行此操作。

编辑:一种解决方案是在所有从属设备上安装此软件包,但我目前没有该选项。

【问题讨论】:

    标签: python hadoop


    【解决方案1】:

    刚刚发现这个解决方案的瑰宝:http://blog.cloudera.com/blog/2008/11/sending-files-to-remote-task-nodes-with-hadoop-mapreduce/

    首先创建带有所需库的 zip

    zip -r nltkandyaml.zip nltk yaml
    mv ntlkandyaml.zip /path/to/where/your/mapper/will/be/nltkandyaml.mod
    

    接下来,通过 Hadoop 流“-file”参数包含:

    hadoop -file nltkandyaml.zip
    

    最后,通过 python 加载库:

    import zipimport
    importer = zipimport.zipimporter('nltkandyaml.mod')
    yaml = importer.load_module('yaml')
    nltk = importer.load_module('nltk') 
    

    此外,此页面总结了如何包含语料库:http://www.xcombinator.com/2009/11/18/how-to-use-cascading-with-hadoop-streaming/

    下载并解压wordnet语料库

    cd wordnet
    zip -r ../wordnet-flat.zip *
    

    在python中:

    wn = WordNetCorpusReader(nltk.data.find('lib/wordnet-flat.zip'))
    

    【讨论】:

    • 领先我 15 秒。很好的发现。
    • 我用 hadoop 流尝试了这个解决方案,并为 nltk zip 提供了 -file 选项,zipimporter 一直抱怨它不是一个 zip 文件,对此有什么想法吗?我的映射器似乎在我的本地机器上工作,它只是在导致问题的 hadoop 流上
    • @viper,也许尝试以下修复:1)重新压缩文件,但可能使用不同的压缩程序或在不同的操作系统上; 2)确保你的路径是正确的——即 zipimporter 只是没有找到文件吗? 3) 检查集群和本地机器上 zipimporter 的版本?我模糊地记得曾经遇到过类似的错误,但不记得我做了什么来修复。
    • @dolan 我确实尝试在本地机器上重新压缩文件。我会尝试在集群上做,然后再试一次。
    • @Reihan_amn -- 我不太确定你在哪里遇到问题。看起来有问题的一件事是您正在加载“nltk/__init__”,而 init 文件/函数会自动加载到模块上。您可能最好发布一个新的 Stack Overflow 问题,只需参考这个原始问题,并详细描述您的代码等。
    【解决方案2】:

    我会将包压缩到.tar.gz.zip 中,然后将整个压缩包或存档在-file 选项中传递给您的hadoop 命令。我过去用 Perl 做过这个,但没有用 Python。

    也就是说,如果您使用 Python 的 zipimport http://docs.python.org/library/zipimport.html,我认为这仍然适用于您,它允许您直接从 zip 导入模块。

    【讨论】:

      【解决方案3】:

      你可以像这样使用 zip lib:

      import sys
      sys.path.insert(0, 'nltkandyaml.mod')
      import ntlk
      import yaml
      

      【讨论】:

        【解决方案4】:

        加载外部python包nltk的示例
        参考答案
        Running extrnal python lib like (NLTK) with hadoop streaming
        我按照以下方法成功运行了 nltk 包和 hadoop 流。

        假设,您的系统中已经有您的包或(在我的情况下为 nltk)

        第一:

        zip -r nltk.zip nltk
        mv ntlk.zip /place/it/anywhere/you/like/nltk.mod
        

        为什么任何地方都可以工作?
        Ans :- 因为我们会通过命令行提供这个 .mod 压缩文件的路径,所以我们不需要太担心。

        秒:
        映射器或 .py 文件中的更改

        #Hadoop cannot unzip files by default thus you need to unzip it   
        import zipimport
        importer = zipimport.zipimporter('nltk.mod')
        nltk = importer.load_module('nltk')
        
        #now import what ever you like from nltk
        from nltk import tree
        from nltk import load_parser
        from nltk.corpus import stopwords
        nltk.data.path += ["."]
        

        第三: 运行 map-reduce 的命令行参数

        hadoop jar /usr/lib/hadoop-mapreduce/hadoop-streaming.jar \
        -file /your/path/to/mapper/mapper.py \
        -mapper '/usr/local/bin/python3.4 mapper.py' \
        -file /your/path/to/reducer/reducer.py \
        -reducer '/usr/local/bin/python3.4 reducer.py' \
        -file /your/path/to/nltkzippedmodfile/nltk.mod \
        -input /your/path/to/HDFS/input/check.txt -output /your/path/to/HDFS/output/
        

        因此,上述步骤解决了我的问题,我认为它也应该解决其他问题。
        干杯,

        【讨论】:

        • 为什么把它移到 nltk.mod?为什么不直接从 nltk.zip 导入?
        【解决方案5】:

        如果您使用更复杂的库,例如 numpy、pandas,virtualenv 是更好的方法。 您可以添加 -archives 以将环境发送到集群。

        参考文字: https://henning.kropponline.de/2014/07/18/virtualenv-hadoop-streaming/

        更新

        我在我们的在线环境中尝试了上面的virtualenv,发现一些问题。在集群中,出现“找不到平台无关的库”之类的错误。然后我尝试了conda创建python env,效果很好。

        如果你是中国人,可以看这个:https://blog.csdn.net/Jsin31/article/details/53495423

        如果没有,我可以简单翻译一下:

        1. 通过conda创建一个环境:

          conda create -n test python=2.7.12 numpy pandas

        2. 进入conda env路径,可以通过cmd找到:

          conda env list

          然后,你就可以打包了:

          tar cf test.tar test

        3. 通过hadoop流提交作业:
        hadoop jar /usr/lib/hadoop/hadoop-streaming.jar \
        -archives test.tar \
        -input /user/testfiles \
        -output /user/result \ 
        -mapper "test.tar/test/bin/python mapper.py" \
        -file mapper.py \
        -reducer"test.tar/test/bin/python reducer.py" \
        -file reducer.py
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2012-10-01
          • 2021-02-26
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-06-30
          • 2010-12-23
          相关资源
          最近更新 更多