繁体   English   中英

从Hadoop Mapreduce作业在HDFS上打开文件

[英]Opening files on HDFS from Hadoop mapreduce job

通常,我可以使用以下内容打开一个新文件:

aDict = {}
with open('WordLists/positive_words.txt', 'r') as f:
    aDict['positive'] = {line.strip() for line in f}

with open('WordLists/negative_words.txt', 'r') as f:
    aDict['negative'] = {line.strip() for line in f}

这将在WordLists文件夹中打开两个相关的文本文件,并将每一行作为肯定或否定附加到字典中。

但是,当我想在Hadoop中运行mapreduce作业时,我认为这行不通。 我正在运行我的程序,如下所示:

./hadoop/bin/hadoop jar contrib/streaming/hadoop-streaming-1.1.2.jar -D mapred.reduce.tasks=0 -file hadoop_map.py -mapper hadoop_reduce.py -input /toBeProcessed -output /Completed

我试图将代码更改为此:

with open('/mapreduce/WordLists/negative_words.txt', 'r')

其中mapreduce是HDFS上的文件夹,WordLists包含负词的子文件夹。 但是我的程序找不到这个。 我正在做的是可能的,如果可以的话,在HDFS上加载文件的正确方法是什么。

编辑

我现在已经尝试:

with open('hdfs://localhost:9000/mapreduce/WordLists/negative_words.txt', 'r')

这似乎有所作为,但是现在我得到了这样的输出:

13/08/27 21:18:50 INFO streaming.StreamJob:  map 0%  reduce 0%
13/08/27 21:18:50 INFO streaming.StreamJob:  map 50%  reduce 0%
13/08/27 21:18:50 INFO streaming.StreamJob:  map 0%  reduce 0%

然后工作失败。 所以还是不对。 有任何想法吗?

编辑2:

重新阅读API之后,我注意到我可以在终端中使用-files选项来指定文件。 API指出:

-files选项在任务的当前工作目录中创建指向该文件本地副本的符号链接。

在此示例中,Hadoop在任务的当前工作目录中自动创建一个名为testfile.txt的符号链接。 此符号链接指向testfile.txt的本地副本。

-files hdfs://host:fs_port/user/testfile.txt

因此,我运行:

./hadoop/bin/hadoop jar contrib/streaming/hadoop-streaming-1.1.2.jar -D mapred.reduce.tasks=0 -files hdfs://localhost:54310/mapreduce/SentimentWordLists/positive_words.txt#positive_words -files hdfs://localhost:54310/mapreduce/SentimentWordLists/negative_words.txt#negative_words -file hadoop_map.py -mapper hadoop_map.py -input /toBeProcessed -output /Completed

根据对API的理解,这会创建符号链接,因此我可以在代码中使用“ positive_words”和“ negative_words”,如下所示:

with open('negative_words.txt', 'r')

但是,这仍然行不通。 任何人都可以提供的任何帮助将不胜感激,因为在我解决此问题之前我无能为力。

编辑3:

我可以使用以下命令:

-file ~/Twitter/SentimentWordLists/positive_words.txt

以及我其余的命令来运行Hadoop作业。 这将在我的本地系统而不是HDFS上找到文件。 不会引发任何错误,因此它在某处被接受为文件。 但是,我不知道如何访问该文件。

经过大量评论后的解决方案:)

在python中读取数据文件:使用-file发送数据并将以下内容添加到脚本中:

import sys

有时需要在import后添加:

sys.path.append('.')

(与Hadoop Streaming中的 @DrDee注释有关-无法找到文件错误

以编程方式处理HDFS时,应查看FileSystem,FileStatus和Path。 这些是hadoop API类,使您可以在程序中访问HDFS。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM