我正在用Java写一个归档程序。将被归档的文件已经驻留在HDFS中。我需要能够将文件从HDFS中的一个位置移动到另一个位置,最后的文件使用Gzip进行压缩。要移动的文件可能非常大,因此使用HDFS API移动它们并对它们进行压缩可能效率很低。所以我想我可以在我的代码中写一个mapreduce作业来为我做这件事。使用MapReduce API使用Gzip压缩在HDFS中复制文件
但是,我一直无法找到任何示例,告诉我如何使用MapReduce API复制这些文件,并使它们以gzip格式输出。实际上,我甚至努力寻找一个程序化的例子来说明如何通过mapreduce复制HDFS中的文件。
任何人都可以介绍一下如何使用MapReduce API完成此任务吗?
编辑:这里是工作的配置代码,我到目前为止,这是改编自帮助,阿马尔给我:
conf.setBoolean("mapred.output.compress", true);
conf.set("mapred.output.compression.codec","org.apache.hadoop.io.compress.GzipCodec");
Job job = new Job(conf);
job.setJarByClass(LogArchiver.class);
job.setJobName("ArchiveMover_"+dbname);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class);
//job.setMapperClass(IdentityMapper.class);
//job.setReducerClass(IdentityReducer.class);
job.setInputFormatClass(NonSplittableTextInputFormat.class);
job.setOutputFormatClass(TextOutputFormat.class);
job.setNumReduceTasks(0);
FileInputFormat.setInputPaths(job, new Path(archiveStaging+"/"+dbname+"/*/*"));
FileOutputFormat.setOutputPath(job, new Path(archiveRoot+"/"+dbname));
job.submit();
这里是类声明NonSplittableTextInputFormat这是LogArchiver类
内public class NonSplittableTextInputFormat extends TextInputFormat {
public NonSplittableTextInputFormat() {
}
@Override
protected boolean isSplitable(JobContext context, Path file) {
return false;
}
}
不知道这是否可以帮助,但你有没有看过Hadoop Streaming? http://wiki.apache.org/hadoop/HadoopStreaming – AlexIIP 2013-02-14 17:32:31
它可能很好,我需要诉诸于此,但我更喜欢通过MapReduce API来做到这一点。 – 2013-02-14 18:04:44