MR全局变量的设置

mapreduce全局变量设置

这两天正好在做倒排索引,需要利用总文档数来完成一项job,但是发现通过在最外层类设置static变量进行全局变量的共享时,发现并没用,在main函数给static变量赋值后,mapper类中取出的是0或者null。后面发现可能是由于每个mapper和reducer并不是存在同一个线程,所以导致无法共享static变量。

后来在网上查了一下,大概有这么三种方法,共享全局变量:

1、通过configuration共享变量 (适合数据量较小)

main函数中
    Configuration conf = new Configuration();
    conf.setInt("DocSum", cnt); //注意,需放在Job获得job对象之前

mapper或者reducer类
    Configuration conf = context.getConfiguration();
    int docSum = conf.getInt("DocSum",1); // 1 默认值 

2、 通过hdfs文件(适合数据量较大)
通过共享hdfs文件,共享变量,缺点是慢。

3、DistributedCache
该方法还没试过。

猜你喜欢

转载自blog.csdn.net/nice_wen/article/details/78363504
今日推荐