在hadoop作业中指定作业属性和重写属性

tquggr8v  于 2021-06-02  发布在  Hadoop
关注(0)|答案(1)|浏览(270)

我有一个hadoop(2.2.0)map reduce作业,它从指定的路径(比如输入路径)读取文本,并进行一些处理。我不想硬编码输入路径(因为它来自于每周都会更改的其他源代码)。
我相信在hadoop中应该有一种方法可以在运行命令行时指定xml属性文件。我该怎么做?
我想到的一种方法是设置一个指向属性文件位置的环境变量,然后在代码中读取这个env变量,然后读取属性文件。这可以起作用,因为env变量的值可以每周更改,而无需更改代码。但我觉得这是一个丑陋的方式加载属性和覆盖。
请告诉我最简单的方法。

cbeh67ev

cbeh67ev1#

没有内置的方式来读取任何输入/输出的配置文件。
我建议的一种方法是实现一个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);
        }
}

相关问题