MapReduce中设置全局变量
实际项目中遇到这样一个场景,需要运行一个MapReduce统计一些数据中的最大最小平均值等特性,将结果存入到HBase中。存结果的同时还要 记录这次分析任务的编号,即所有的Reduce产生的结果中都要包含这个任务编号这个字段。当然我们可以把这个任务编号放到输入文件中的每一行中,作为输 入数据的一部分,不过这样做显然太不专业,无端的增加了要处理的数据量,加重网络负担。经过网上搜索,发现可以用Configuration来实现。具体 过程:
提交job的函数中
- Configuration conf = new Configuration();
- conf.setStrings("job_parms", "aaabbc"); //关键就是这一句
- Job job = new Job(conf, "load analysis");
- job.setJarByClass(LoadAnalysis.class);
- job.setMapperClass(LoadMapper.class);
- job.setReducerClass(LoadIntoHbaseReduce.class);
- job.setMapOutputKeyClass(Text.class);
- job.setMapOutputValueClass(Text.class);
- FileInputFormat.addInputPath(job, new Path(otherArgs[0]));
        
Mapper类中重写setup函数
- @Override
- protected void setup(Context context)
- throws IOException, InterruptedException {
- try {
- //从全局配置获取配置参数
- Configuration conf = context.getConfiguration();
- String parmStr = conf.get("job_parms"); //这样就拿到了
- ......
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
当然 Reduce类中也可以同样操作
问题解决!

 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号