你好,游客 登录 注册 搜索
背景:
阅读新闻

Hadoop的“全局变量”

[日期:2012-10-01] 来源:Linux社区  作者:fansy1990 [字体:大 中 小]

Mapper:

  1. package org.fansy.date927; 
  2.  
  3. import java.io.BufferedReader; 
  4. import java.io.FileReader; 
  5. import java.io.IOException; 
  6. import java.util.ArrayList; 
  7. import java.util.List; 
  8.  
  9. import org.apache.commons.logging.Log; 
  10. import org.apache.commons.logging.LogFactory; 
  11. import org.apache.Hadoop.filecache.DistributedCache; 
  12. import org.apache.hadoop.fs.Path; 
  13. import org.apache.hadoop.io.IntWritable; 
  14. import org.apache.hadoop.io.LongWritable; 
  15. import org.apache.hadoop.io.Text; 
  16. import org.apache.hadoop.mapreduce.Mapper; 
  17.  
  18. public class KmeansM extends Mapper<LongWritable,Text,IntWritable,DataPro>{ 
  19.     private static Log log=LogFactory.getLog(KmeansM.class); 
  20.      
  21.     private double[][] centers; 
  22.     private int dimention_m;  //  this is the k   
  23.     private int dimention_n;   //  this is the features   
  24.  
  25.      
  26.     static enum Counter{Fansy_Miss_Records}; 
  27.     @Override 
  28.     public void setup(Context context) throws IOException,InterruptedException{ 
  29.         <SPAN style="COLOR: #ff0000">Path[] caches=DistributedCache.getLocalCacheFiles(context.getConfiguration());</SPAN> 
  30.         if(caches==null||caches.length<=0){ 
  31.             log.error("center file does not exist"); 
  32.             System.exit(1); 
  33.         } 
  34.         @SuppressWarnings("resource") 
  35.         BufferedReader br=new BufferedReader(new FileReader(caches[0].toString())); 
  36.         String line; 
  37.         List<ArrayList<Double>> temp_centers=new ArrayList<ArrayList<Double>>(); 
  38.         ArrayList<Double> center=null; 
  39.         //  get the file data  
  40.         <SPAN style="COLOR: #ff0000">while((line=br.readLine())!=null){ 
  41.             center=new ArrayList<Double>(); 
  42.             String[] str=line.split("\t"); 
  43.             for(int i=0;i<str.length;i++){ 
  44.                 center.add(Double.parseDouble(str[i])); 
  45.             } 
  46.             temp_centers.add(center); 
  47.         }</SPAN> 
  48.         //  fill the centers   
  49.         @SuppressWarnings("unchecked") 
  50.         ArrayList<Double>[] newcenters=temp_centers.toArray(new ArrayList[]{}); 
  51.          dimention_m=temp_centers.size(); 
  52.          dimention_n=newcenters[0].size(); 
  53.         centers=new double[dimention_m][dimention_n]; 
  54.         for(int i=0;i<dimention_m;i++){ 
  55.             Double[] temp_double=newcenters[i].toArray(new Double[]{}); 
  56.             for(int j=0;j<dimention_n;j++){ 
  57.                 centers[i][j]=temp_double[j]; 
  58.         //      System.out.print(temp_double[j]+",");  
  59.             } 
  60.     //      System.out.println();  
  61.         } 
  62.     } 
  63.              
  64.     public void map(LongWritable key,Text value,Context context)throws IOException,InterruptedException{ 
  65.         String[] values=value.toString().split("\t"); 
  66.          
  67.         if(values.length!=dimention_n){ 
  68.             context.getCounter(Counter.Fansy_Miss_Records).increment(1); 
  69.             return; 
  70.         } 
  71.         double[] temp_double=new double[values.length]; 
  72.         for(int i=0;i<values.length;i++){ 
  73.             temp_double[i]=Double.parseDouble(values[i]); 
  74.         } 
  75.         //  set the index  
  76.         double distance=Double.MAX_VALUE; 
  77.         double temp_distance=0.0; 
  78.         int index=0; 
  79.         for(int i=0;i<dimention_m;i++){ 
  80.             double[] temp_center=centers[i]; 
  81.             temp_distance=getEnumDistance(temp_double,temp_center); 
  82.             if(temp_distance<distance){ 
  83.                  index=i; 
  84.                 distance=temp_distance; 
  85.             } 
  86.         } 
  87.         DataPro newvalue=new DataPro(); 
  88.         newvalue.set(value, new IntWritable(1)); 
  89.         context.write(new IntWritable(index), newvalue); 
  90.          
  91.     } 
  92.     public static double getEnumDistance(double[] source,double[] other){  //  get the distance  
  93.         double distance=0.0; 
  94.         if(source.length!=other.length){ 
  95.             return Double.MAX_VALUE; 
  96.         } 
  97.         for(int i=0;i<source.length;i++){ 
  98.             distance+=(source[i]-other[i])*(source[i]-other[i]); 
  99.         } 
  100.         distance=Math.sqrt(distance); 
  101.         return distance; 
  102.     } 
  103. } 

红色代码部分是读取文件值,然后赋值为这个job的全局变量值,这样这个map任务就可以把 centers当作全局变量来使用了(同时centers里面存放了centers文件中的值)

 

linux
相关资讯       Hadoop应用 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款