【问题标题】:Specify job properties and override properties in hadoop jobs在 hadoop 作业中指定作业属性和覆盖属性
【发布时间】:2015-12-17 21:29:45
【问题描述】:
我有一个 hadoop (2.2.0) map-reduce 作业,它从指定路径(比如 INPUT_PATH)读取文本,并进行一些处理。我不想硬编码输入路径(因为它来自每周更改的其他来源)。
我相信 hadoop 中应该有一种方法可以在通过命令行运行时指定一个 xml 属性文件。我该怎么做?
我认为的一种方法是设置一个指向属性文件位置的环境变量,然后在代码中读取这个环境变量,然后读取属性文件。这可能会起作用,因为 env 变量的值可以每周更改而无需更改代码。但我觉得这是一种丑陋的加载属性和覆盖的方式。
请让我知道这样做最简单的方法。
【问题讨论】:
标签:
hadoop
properties
mapreduce
hadoop2
【解决方案1】:
没有内置方法可以读取任何输入/输出配置文件。
我可以建议的一种方法是实现一个 Java M/R 驱动程序,该程序执行以下操作,
- 读取配置(XML/properties/anything)(可能由其他进程生成/更新)
- 设置作业属性
- 使用您的 hadoop 命令提交作业(将配置文件作为参数传递)
类似的,
public class SampleMRDriver
extends Configured implements Tool {
@Override
public int run(
String[] args)
throws Exception {
// Read from args the configuration file
Properties prop = new Properties();
prop.loadFromXML(new FileInputStream(args[0]));
Job job = Job.getInstance(getConf(), "Test Job");
job.setJarByClass(SampleMRDriver.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class);
job.setMapperClass(TestMapper.class);
job.setReducerClass(TestReducer.class);
FileInputFormat.setInputPaths(job, new Path(prop.get("input_path")));
FileOutputFormat.setOutputPath(job, new Path(prop.get("output_path")));
boolean success = job.waitForCompletion(true);
return success ? 0 : 1;
}
public static void main(
String[] args)
throws Exception {
ToolRunner.run(new BatteryAnomalyDetection(), args);
}
}