hadoop全局变量与数据传递

首先明确:

1.Hadoop不支持全局变量,也不建议使用全局变量。

   我的理解是,这是因为hadoop具有map类和reducer类,并且不同的task一般执行的是不同的map或reduce。所以全局变量是无法传递的。(但是一般情况下,我们也许会需要一个对于所有map和reduce都能访问的全局变量),暂时我知道的解决方法如下:

2.如果Mapper类和Reducer类都是主类的内部类,可以在主类中使用 private static string  global = "global variable";

   但是这种方法不通用,因为Mapper类和Reducer类在概念上是与主类无关的。很多情况下,他们不是主类的内部类。所以该task所拥有的信息,只能从context上下文中获得,这也正体现了常说的:充足的上下文信息才是关键!

   修正:Mapper类(或Reducer类) 使用 主类名.global 也可以获取全局变量。。

3.设置xml文件,然后在map函数中读取即可;

  这种方法有一个缺点,值只能从客户端传递到Mapper中,reduce不能读出来。实际上reducer所知道的只有context信息。

4.直接在Configuration中设置属性值,然后读取这个属性值,在Mapper或Reducer中使用变量来存储这个值即可。

  可以看出这种方式最为麻烦。可以将各个属性值放入文件中,然后在文件读取即可。

   关键代码如下:

//初始化configuration后,使用
conf.set("propertyName“,properyValue);

 //在mapper或reducer中,
 Configuration conf = context.getConfiguration();
 String g = conf.get("propertyName");

   //g作为可以使用的全局变量即可;

这里的文章讲述了hadoop下数据的传递:http://blog.csdn.net/jackydai987/article/details/6441241

代码如下

public class xml_test {  
    public static int getFileInt(String filename) //从文件中读取预设值  
    {  
        int temp = 0;  
        Configuration config = new Configuration();  
        FSDataInputStream dis = null;  
        try {  
                ByteArrayOutputStream baos = new ByteArrayOutputStream();   
                FileSystem hdfs = FileSystem.get(config);  
                dis = hdfs.open(new Path(filename));  
                IOUtils.copyBytes(dis, baos, 4096, false); //写入ByteArrayOutputStream  
                String str = baos.toString();  //这里我假定只有一行,多行可以使用循环。  
                str = str.substring(0, str.length() - 1); //最后一个是回车,需要过滤掉,不然在整形转换时会出错  
                temp = Integer.parseInt(str);  
            } catch (IOException e) {  
                // TODO Auto-generated catch block  
                e.printStackTrace();  
            }     
            finally{  
                IOUtils.closeStream(dis);  
            }  
        return temp;  
}  
    public static  class wordcountMapper extends  
        Mapper<LongWritable, Text, Text, IntWritable>{  
        int temp2 = getFileInt("/user/jackydai/int"); //从文件中读取预设的值  
        //这里也可以通过args参数传一个文件进来,这样更灵活  
        private Text word = new Text();  
        public void map(LongWritable key, Text value, Context context)throws IOException, InterruptedException{  
            int temp1 = 0;  
            Configuration mapconf = context.getConfiguration();  
            temp1 = mapconf.getInt("count", 0); //map读取值  
            IntWritable one = new IntWritable(temp1 + temp2); //求和后输出,检测是否正确  
            String line = value.toString();  
            StringTokenizer itr = new StringTokenizer(line);  
            while(itr.hasMoreElements()){  
                word.set(itr.nextToken());  
                context.write(word, one);  
            }  
        }  
    }  
    public static  class wordcountReduce extends  
        Reducer<Text, IntWritable, Text, IntWritable>{  
        public void reduce(Text key, Iterable<IntWritable>values, Context context)throws IOException, InterruptedException{  
            int sum = 0;  
            for (IntWritable str : values){  
                sum += str.get();  
            }  
            context.write(key, new IntWritable(sum));  
        }  
    }  
    public static  void main(String args[])throws Exception{  
          
        Configuration conf = new Configuration();  
          
        conf.setInt("count", 2); //设置值为2,需要注意的是设置值需要在new job之前  
          
        Job job = new Job(conf, "xml_test");  
          
        job.setJarByClass(xml_test.class);  
          
        job.setInputFormatClass(TextInputFormat.class);  
          
        job.setOutputKeyClass(Text.class);  
        job.setOutputValueClass(IntWritable.class);  
          
        job.setMapperClass(wordcountMapper.class);  
        job.setReducerClass(wordcountReduce.class);  
        job.setCombinerClass(wordcountReduce.class);  
          
        FileInputFormat.setInputPaths(job, new Path(args[1]));  
        FileOutputFormat.setOutputPath(job, new Path(args[2]));  
          
        job.waitForCompletion(true);  
    }  
}
posted @ 2013-03-08 20:55  dandingyy  阅读(5396)  评论(0编辑  收藏  举报