2014-09-12 27 views
2

我有一個Hadoop作業,我想將輸出文件夾的複製數設置爲1,我想在Java代碼中執行此操作。我們服務器上的默認值是3.另一個導入方面是在輸出寫入之前設置複製編號。這意味着我不想用3個副本寫出整個輸出,然後將其減少到1.我希望在開始寫入輸出文件夾之前設置它,以便只有一個複製。原因是輸出可能很大,我想騰出一些空間。在執行之前在Java代碼中設置Hadoop輸出文件夾複製

@Override 
public int run(String[] args) throws Exception { 

    /** Get configuration */ 
    Configuration conf = getConf(); 
    conf.setStrings("args", args); 

    /** Job configuration */ 
    Job job = Job.getInstance(conf, "HadoopSearch"); 
    job.setJarByClass(Search.class); 
    job.setOutputKeyClass(Text.class); 
    job.setOutputValueClass(NullWritable.class); 

    /** Set Mapper and Reducer, use identity reducer*/ 
    job.setMapperClass(Map.class); 
    job.setReducerClass(Reducer.class); // identity 

    /** Set input and output formats */ 
    job.setInputFormatClass(TextInputFormat.class); 
    job.setOutputFormatClass(TextOutputFormat.class); 

    /** Set input and output path */ 
    FileInputFormat.addInputPath(job, new Path("IN PATH")); 
    FileOutputFormat.setOutputPath(job, new Path("OUT PATH")); 

    job.waitForCompletion(true); 
    return 0; 
} 

我知道,我可以把這個使用FileSystem.setReplication(Path p, short s)但這只是每個文件的工作,我希望它爲整個文件夾。我可以循環瀏覽文件夾內的文件,但更重要的是,這似乎只在作業完成並且文件已存在之後才起作用。正如我所假設的那樣,複製的流程已經在運行,我可以用磁盤空間來解決我想避免的問題。

回答

1

在Mapreduce中,您可以使用設置作業配置來設置dfs.replication屬性,以便在該作業內創建的文件將具有指定的複製因子。希望這會有所幫助。

Configuration conf = new Configuration(); 
    conf.set("dfs.replication", "1"); 
    Job job = new Job(conf); 
相關問題