Mapper:
- package org.fansy.date928;
- 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{
- Path[] caches=DistributedCache.getLocalCacheFiles(context.getConfiguration());
- if(caches==null||caches.length<=0){
- log.error("center file does not exist");
- System.exit(1);
- }
- 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
- while((line=br.readLine())!=null){
- center=new ArrayList<Double>();
- String[] str=line.split("\t");
- // String[] str=line.split("\\s+");
- for(int i=0;i<str.length;i++){
- center.add(Double.parseDouble(str[i]));
- // center.add((double)Float.parseFloat(str[i]));
- }
- temp_centers.add(center);
- }
- try {
- br.close();
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- // 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");
- // String[] values=value.toString().split("\\s+");
- 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));
- // System.out.println("the map out:"+index+","+value);
- 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;
- }
- }
Combiner:
- package org.fansy.date928;
- import java.io.BufferedReader;
- import java.io.FileReader;
- import java.io.IOException;
- 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.Text;
- import org.apache.hadoop.mapreduce.Reducer;
- public class KmeansC extends Reducer<IntWritable,DataPro,IntWritable,DataPro> {
- private static int dimension=0;
- private static Log log =LogFactory.getLog(KmeansC.class);
- // the main purpose of the sutup() function is to get the dimension of the original data
- public void setup(Context context) throws IOException{
- Path[] caches=DistributedCache.getLocalCacheFiles(context.getConfiguration());
- if(caches==null||caches.length<=0){
- log.error("center file does not exist");
- System.exit(1);
- }
- BufferedReader br=new BufferedReader(new FileReader(caches[0].toString()));
- String line;
- while((line=br.readLine())!=null){
- String[] str=line.split("\t");
- // String[] str=line.split("\\s+");
- dimension=str.length;
- break;
- }
- try {
- br.close();
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- public void reduce(IntWritable key,Iterable<DataPro> values,Context context)throws InterruptedException, IOException{
- double[] sum=new double[dimension];
- int sumCount=0;
- // operation two
- for(DataPro val:values){
- String[] datastr=val.getCenter().toString().split("\t");
- // String[] datastr=val.getCenter().toString().split("\\s+");
- sumCount+=val.getCount().get();
- for(int i=0;i<dimension;i++){
- sum[i]+=Double.parseDouble(datastr[i]);
- }
- }
- // calculate the new centers
- // double[] newcenter=new double[dimension];
- StringBuffer sb=new StringBuffer();
- for(int i=0;i<dimension;i++){
- sb.append(sum[i]+"\t");
- }
- // System.out.println("combine text:"+sb.toString());
- // System.out.println("combine sumCount:"+sumCount);
- DataPro newvalue=new DataPro();
- newvalue.set(new Text(sb.toString()), new IntWritable(sumCount));
- context.write(key, newvalue);
- }
- }
Reducer:
- package org.fansy.date928;
- import java.io.BufferedReader;
- import java.io.FileReader;
- import java.io.IOException;
- 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.NullWritable;
- import org.apache.hadoop.io.Text;
- import org.apache.hadoop.mapreduce.Reducer;
- public class KmeansR extends Reducer<IntWritable,DataPro,NullWritable,Text> {
- private static int dimension=0;
- private static Log log =LogFactory.getLog(KmeansC.class);
- // the main purpose of the sutup() function is to get the dimension of the original data
- public void setup(Context context) throws IOException{
- Path[] caches=DistributedCache.getLocalCacheFiles(context.getConfiguration());
- if(caches==null||caches.length<=0){
- log.error("center file does not exist");
- System.exit(1);
- }
- BufferedReader br=new BufferedReader(new FileReader(caches[0].toString()));
- String line;
- while((line=br.readLine())!=null){
- String[] str=line.split("\t");
- dimension=str.length;
- break;
- }
- try {
- br.close();
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- public void reduce(IntWritable key,Iterable<DataPro> values,Context context)throws InterruptedException, IOException{
- double[] sum=new double[dimension];
- int sumCount=0;
- for(DataPro val:values){
- String[] datastr=val.getCenter().toString().split("\t");
- // String[] datastr=val.getCenter().toString().split("\\s+");
- sumCount+=val.getCount().get();
- for(int i=0;i<dimension;i++){
- sum[i]+=Double.parseDouble(datastr[i]);
- }
- }
- // calculate the new centers
- // double[] newcenter=new double[dimension];
- StringBuffer sb=new StringBuffer();
- for(int i=0;i<dimension;i++){
- sb.append(sum[i]/sumCount+"\t");
- // sb.append(sum[i]/sumCount+"\\s+");
- }
- context.write(null, new Text(sb.toString()));
- }
- }
LastMapper:
- package org.fansy.date928;
- 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 KmeansLastM extends Mapper<LongWritable,Text,IntWritable,Text>{
- private static Log log=LogFactory.getLog(KmeansLastM.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{
- Path[] caches=DistributedCache.getLocalCacheFiles(context.getConfiguration());
- 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
- 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);
- }
- // 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");
- // String[] values=value.toString().split("\\s+");
- 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;
- }
- }
- context.write(new IntWritable(index), value);
- }
- 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;
- }
- }
LastReducer:
- package org.fansy.date928;
- import java.io.IOException;
- import org.apache.hadoop.io.IntWritable;
- import org.apache.hadoop.io.Text;
- import org.apache.hadoop.mapreduce.Reducer;
- public class KmeansLastR extends Reducer<IntWritable,Text,IntWritable,Text> {
- public void reduce(IntWritable key,Iterable<Text> values,Context context)throws InterruptedException, IOException{
- // output the data directly
- for(Text val:values){
- context.write(key, val);
- }
- }
- }
