从Hadoop mapreduce作业打开HDFS上的文件

2024-04-20 08:09:32 发布

您现在位置:Python中文网/ 问答频道 /正文

通常,我可以打开一个像这样的新文件:

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上的一个文件夹,word列出了包含否定词的子文件夹。但我的程序找不到这个。如果可能的话,在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声明:

The -files option creates a symlink in the current working directory of the tasks that points to the local copy of the file.

In this example, Hadoop automatically creates a symlink named testfile.txt in the current working directory of the tasks. This symlink points to the local copy of 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的理解,这会创建符号链接,这样我就可以在代码中使用“正”和“负”两个词,如下所示:

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

但是,这个仍然不起作用。任何人能提供的帮助都将非常感谢,因为在我解决这个问题之前我无能为力。

编辑3:

我可以使用这个命令:

-file ~/Twitter/SentimentWordLists/positive_words.txt

以及运行Hadoop作业的其他命令。这会在本地系统而不是HDFS上找到文件。这个不会抛出任何错误,所以它被接受为一个文件。但是,我不知道如何访问文件。


Tags: 文件thetxthadoopmapreducewithfiles