(转载)一个有关mapreduce全局变量的问题

https://blog.csdn.net/zeqblog/article/details/39006395


最近在写mapreduce时,遇到一个问题,在class中定义的全局变量,在用eclipse本地开发运行时,mapreduce函数内部能取到参数变量,但是打包成jar包,用hadoop jar xxx.jar className 运行的时候,发现map或者reduce函数中取不到全局变量!!!最后想了一下有可能这跟分布式有关!

换了一种思路,最后解决了!!

可以用Configuration来实现。具体过程为,在main方法或run方法中的Configuration的定义部分将变量定义:

Configuration conf= HBaseConfiguration.create();  
conf.setStrings("job_parameter", "job_param_value"); 

然后在Mapper类或Reducer类中重写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();
    }
            
}



猜你喜欢

转载自blog.csdn.net/little_fire/article/details/80580354