【发布时间】:2011-07-25 03:33:58
【问题描述】:
我正在尝试在 Hadoop 流式作业中包含一个 python 包 (NLTK),但我不确定如何在不通过 CLI 参数“-file”手动包含每个文件的情况下执行此操作。
编辑:一种解决方案是在所有从属设备上安装此软件包,但我目前没有该选项。
【问题讨论】:
我正在尝试在 Hadoop 流式作业中包含一个 python 包 (NLTK),但我不确定如何在不通过 CLI 参数“-file”手动包含每个文件的情况下执行此操作。
编辑:一种解决方案是在所有从属设备上安装此软件包,但我目前没有该选项。
【问题讨论】:
刚刚发现这个解决方案的瑰宝: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'))
【讨论】:
我会将包压缩到.tar.gz 或.zip 中,然后将整个压缩包或存档在-file 选项中传递给您的hadoop 命令。我过去用 Perl 做过这个,但没有用 Python。
也就是说,如果您使用 Python 的 zipimport http://docs.python.org/library/zipimport.html,我认为这仍然适用于您,它允许您直接从 zip 导入模块。
【讨论】:
你可以像这样使用 zip lib:
import sys
sys.path.insert(0, 'nltkandyaml.mod')
import ntlk
import yaml
【讨论】:
加载外部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/
因此,上述步骤解决了我的问题,我认为它也应该解决其他问题。
干杯,
【讨论】:
如果您使用更复杂的库,例如 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
如果没有,我可以简单翻译一下:
通过conda创建一个环境:
conda create -n test python=2.7.12 numpy pandas
进入conda env路径,可以通过cmd找到:
conda env list
然后,你就可以打包了:
tar cf test.tar test
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
【讨论】: