使用Hadoop Streaming运行外部Python库(如NLTK)
我尝试使用这个链接中的内容:http://blog.cloudera.com/blog/2008/11/sending-files-to-remote-task-nodes-with-hadoop-mapreduce/
zip -r nltkandyaml.zip nltk yaml
mv ntlkandyaml.zip /path/to/where/your/mapper/will/be/nltkandyaml.mod
import zipimport
importer = zipimport.zipimporter('nltkandyaml.mod')
yaml = importer.load_module('yaml')
nltk = importer.load_module('nltk')
但是我遇到的错误是:
job_201406080403_3863/attempt_201406080403_3863_m_000000_0/work/./app/mapper.py", 第12行,出错位置: import nltk 导入错误:没有名为nltk的模块
有没有人遇到过类似的问题,能不能给出一个详细的解决方案?
谢谢
1 个回答
0
我按照以下方法成功地在Hadoop流处理上运行了nltk包。
注意:我只使用了nltk包,没有使用yaml,所以我的回答只会集中在如何加载nltk包上,而不是yaml。不过我相信这对你的问题也应该有帮助。
假设你已经在系统中安装了nltk包。
首先:
zip -r nltk.zip nltk
mv ntlk.zip /place/it/anywhere/you/like/nltk.mod
为什么在任何地方都可以工作?
答案:因为我们会通过命令行提供这个.mod压缩文件的路径,所以我们不需要太担心这个问题。
第二:
在你的mapper或.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/
因此,上述步骤解决了我的问题,我认为也应该能解决其他人的问题。
祝好,