簡體   English   中英

Hadoop:提供目錄作為MapReduce作業的輸入

[英]Hadoop : Provide directory as input to MapReduce job

我正在使用Cloudera Hadoop。 我能夠運行簡單的mapreduce程序,我提供了一個文件作為MapReduce程序的輸入。

此文件包含mapper函數要處理的所有其他文件。

但是,我陷入了困境。

/folder1
  - file1.txt
  - file2.txt
  - file3.txt

如何將MapReduce程序的輸入路徑指定為"/folder1" ,以便它可以開始處理該目錄中的每個文件?

有任何想法嗎 ?

編輯:

1)Intiailly,我提供了inputFile.txt作為mapreduce程序的輸入。 它工作得很好。

>inputFile.txt
file1.txt
file2.txt
file3.txt

2)但是現在,我想在命令行上提供一個輸入目錄作為arg [0],而不是給出一個輸入文件。

hadoop jar ABC.jar /folder1 /output

問題是FileInputFormat不會在輸入路徑dir中遞歸讀取文件。

解決方案:使用以下代碼

FileInputFormat.setInputDirRecursive(job, true); 在Map Reduce Code下面的行之前

FileInputFormat.addInputPath(job, new Path(args[0]));

您可以在此處查看修復的版本。

您可以使用FileSystem.listStatus從給定的dir獲取文件列表,代碼如下:

//get the FileSystem, you will need to initialize it properly
FileSystem fs= FileSystem.get(conf); 
//get the FileStatus list from given dir
FileStatus[] status_list = fs.listStatus(new Path(args[0]));
if(status_list != null){
    for(FileStatus status : status_list){
        //add each file to the list of inputs for the map-reduce job
        FileInputFormat.addInputPath(conf, status.getPath());
    }
}

您可以使用hdfs 通配符來提供多個文件

所以,解決方案:

hadoop jar ABC.jar /folder1/* /output

要么

hadoop jar ABC.jar /folder1/*.txt /output

使用MultipleInputs類。

MultipleInputs. addInputPath(Job job, Path path, Class<? extends InputFormat> 
inputFormatClass, Class<? extends Mapper> mapperClass)

看看工作代碼

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM