Mapper:
- package org.fansy.date927;
- import java.io.BufferedReader;
- import java.io.FileReader;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.List;
- import org.apache.commons.logging.Log;
- import org.apache.commons.logging.LogFactory;
- import org.apache.Hadoop.filecache.DistributedCache;
- import org.apache.hadoop.fs.Path;
- import org.apache.hadoop.io.IntWritable;
- import org.apache.hadoop.io.LongWritable;
- import org.apache.hadoop.io.Text;
- import org.apache.hadoop.mapreduce.Mapper;
- public class KmeansM extends Mapper<LongWritable,Text,IntWritable,DataPro>{
- private static Log log=LogFactory.getLog(KmeansM.class);
- private double[][] centers;
- private int dimention_m; // this is the k
- private int dimention_n; // this is the features
- static enum Counter{Fansy_Miss_Records};
- @Override
- public void setup(Context context) throws IOException,InterruptedException{
- <SPAN style="COLOR: #ff0000">Path[] caches=DistributedCache.getLocalCacheFiles(context.getConfiguration());</SPAN>
- if(caches==null||caches.length<=0){
- log.error("center file does not exist");
- System.exit(1);
- }
- @SuppressWarnings("resource")
- BufferedReader br=new BufferedReader(new FileReader(caches[0].toString()));
- String line;
- List<ArrayList<Double>> temp_centers=new ArrayList<ArrayList<Double>>();
- ArrayList<Double> center=null;
- // get the file data
- <SPAN style="COLOR: #ff0000">while((line=br.readLine())!=null){
- center=new ArrayList<Double>();
- String[] str=line.split("\t");
- for(int i=0;i<str.length;i++){
- center.add(Double.parseDouble(str[i]));
- }
- temp_centers.add(center);
- }</SPAN>
- // fill the centers
- @SuppressWarnings("unchecked")
- ArrayList<Double>[] newcenters=temp_centers.toArray(new ArrayList[]{});
- dimention_m=temp_centers.size();
- dimention_n=newcenters[0].size();
- centers=new double[dimention_m][dimention_n];
- for(int i=0;i<dimention_m;i++){
- Double[] temp_double=newcenters[i].toArray(new Double[]{});
- for(int j=0;j<dimention_n;j++){
- centers[i][j]=temp_double[j];
- // System.out.print(temp_double[j]+",");
- }
- // System.out.println();
- }
- }
- public void map(LongWritable key,Text value,Context context)throws IOException,InterruptedException{
- String[] values=value.toString().split("\t");
- if(values.length!=dimention_n){
- context.getCounter(Counter.Fansy_Miss_Records).increment(1);
- return;
- }
- double[] temp_double=new double[values.length];
- for(int i=0;i<values.length;i++){
- temp_double[i]=Double.parseDouble(values[i]);
- }
- // set the index
- double distance=Double.MAX_VALUE;
- double temp_distance=0.0;
- int index=0;
- for(int i=0;i<dimention_m;i++){
- double[] temp_center=centers[i];
- temp_distance=getEnumDistance(temp_double,temp_center);
- if(temp_distance<distance){
- index=i;
- distance=temp_distance;
- }
- }
- DataPro newvalue=new DataPro();
- newvalue.set(value, new IntWritable(1));
- context.write(new IntWritable(index), newvalue);
- }
- public static double getEnumDistance(double[] source,double[] other){ // get the distance
- double distance=0.0;
- if(source.length!=other.length){
- return Double.MAX_VALUE;
- }
- for(int i=0;i<source.length;i++){
- distance+=(source[i]-other[i])*(source[i]-other[i]);
- }
- distance=Math.sqrt(distance);
- return distance;
- }
- }
红色代码部分是读取文件值,然后赋值为这个job的全局变量值,这样这个map任务就可以把 centers当作全局变量来使用了(同时centers里面存放了centers文件中的值)
