MapReduce下的数据传递

来源:互联网 发布:淘宝论文降重靠谱吗 编辑:程序博客网 时间:2024/05/21 09:08
首先明确:

 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中使用变量来存储这个值即可。

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

   关键代码如下:

[java] view plaincopy
  1. //初始化configuration后,使用  
  2. conf.set("propertyName“,properyValue);  
  3.   
  4.  //在mapper或reducer中,  
  5.  Configuration conf = context.getConfiguration();  
  6.  String g = conf.get("propertyName");  
  7.   
  8.    //g作为可以使用的全局变量即可;  


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

代码如下:

[java] view plaincopy
  1. public class xml_test {  
  2.     public static int getFileInt(String filename) //从文件中读取预设值  
  3.     {  
  4.         int temp = 0;  
  5.         Configuration config = new Configuration();  
  6.         FSDataInputStream dis = null;  
  7.         try {  
  8.                 ByteArrayOutputStream baos = new ByteArrayOutputStream();   
  9.                 FileSystem hdfs = FileSystem.get(config);  
  10.                 dis = hdfs.open(new Path(filename));  
  11.                 IOUtils.copyBytes(dis, baos, 4096false); //写入ByteArrayOutputStream  
  12.                 String str = baos.toString();  //这里我假定只有一行,多行可以使用循环。  
  13.                 str = str.substring(0, str.length() - 1); //最后一个是回车,需要过滤掉,不然在整形转换时会出错  
  14.                 temp = Integer.parseInt(str);  
  15.             } catch (IOException e) {  
  16.                 // TODO Auto-generated catch block  
  17.                 e.printStackTrace();  
  18.             }     
  19.             finally{  
  20.                 IOUtils.closeStream(dis);  
  21.             }  
  22.         return temp;  
  23. }  
  24.     public static  class wordcountMapper extends  
  25.         Mapper<LongWritable, Text, Text, IntWritable>{  
  26.         int temp2 = getFileInt("/user/jackydai/int"); //从文件中读取预设的值  
  27.         //这里也可以通过args参数传一个文件进来,这样更灵活  
  28.         private Text word = new Text();  
  29.         public void map(LongWritable key, Text value, Context context)throws IOException, InterruptedException{  
  30.             int temp1 = 0;  
  31.             Configuration mapconf = context.getConfiguration();  
  32.             temp1 = mapconf.getInt("count"0); //map读取值  
  33.             IntWritable one = new IntWritable(temp1 + temp2); //求和后输出,检测是否正确  
  34.             String line = value.toString();  
  35.             StringTokenizer itr = new StringTokenizer(line);  
  36.             while(itr.hasMoreElements()){  
  37.                 word.set(itr.nextToken());  
  38.                 context.write(word, one);  
  39.             }  
  40.         }  
  41.     }  
  42.     public static  class wordcountReduce extends  
  43.         Reducer<Text, IntWritable, Text, IntWritable>{  
  44.         public void reduce(Text key, Iterable<IntWritable>values, Context context)throws IOException, InterruptedException{  
  45.             int sum = 0;  
  46.             for (IntWritable str : values){  
  47.                 sum += str.get();  
  48.             }  
  49.             context.write(key, new IntWritable(sum));  
  50.         }  
  51.     }  
  52.     public static  void main(String args[])throws Exception{  
  53.           
  54.         Configuration conf = new Configuration();  
  55.           
  56.         conf.setInt("count"2); //设置值为2,需要注意的是设置值需要在new job之前  
  57.           
  58.         Job job = new Job(conf, "xml_test");  
  59.           
  60.         job.setJarByClass(xml_test.class);  
  61.           
  62.         job.setInputFormatClass(TextInputFormat.class);  
  63.           
  64.         job.setOutputKeyClass(Text.class);  
  65.         job.setOutputValueClass(IntWritable.class);  
  66.           
  67.         job.setMapperClass(wordcountMapper.class);  
  68.         job.setReducerClass(wordcountReduce.class);  
  69.         job.setCombinerClass(wordcountReduce.class);  
  70.           
  71.         FileInputFormat.setInputPaths(job, new Path(args[1]));  
  72.         FileOutputFormat.setOutputPath(job, new Path(args[2]));  
  73.           
  74.         job.waitForCompletion(true);  
  75.     }  
原创粉丝点击