我有一个 hadoop (2.2.0) map-reduce 作业,它从指定路径(比如 INPUT_PATH)读取文本,并进行一些处理。我不想对输入路径进行硬编码(因为它来自每周更改的其他来源)。
我相信 hadoop 中应该有一种方法可以在通过命令行运行时指定 xml 属性文件。我应该怎么做?
我想到的一种方法是设置一个指向属性文件位置的环境变量,然后在代码中读取这个环境变量,然后读取属性文件。这是可行的,因为 env 变量的值每周都可以更改,而无需更改代码。但我觉得这是一种加载属性和覆盖的丑陋方式。
请告诉我最简单的方法。
最佳答案
没有内置的方法来读取任何输入/输出的配置文件。
我可以建议的一种方法是实现执行以下操作的 Java M/R 驱动程序,
- 读取配置(XML/属性/任何东西)(可能由其他进程生成/更新)
- 设置作业属性
- 使用您的 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);
}
}
关于hadoop - 在 hadoop 作业中指定作业属性和覆盖属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34344582/