Mahout源码MeanShiftCanopyDriver分析之二MeanShiftCanopyMapper仿造

首先更正一点,昨天处理数据的时候是有问题的,直接从网页中拷贝的文件的空格是有问题的,直接拷贝然后新建的文件中的空格可能有一个两个、三个的,所以要把两个或者三个的都换为一个,在InputMapper中下面的代码:

private static final Pattern SPACE = Pattern.compile(" ");
String[] numbers = SPACE.split(values.toString());
可以看到这个代码是以一个空格来区分的,可以在linux的terminal中输入下面的命令来进行替换:

Sed -I "s/   / /g" `grep    -l synthetic_control.data`   -- 替换三个空格为一个空格
Sed -I "s/  / /g" `grep    -l synthetic_control.data`    -- 替换两个空格为一个空格
通过上面的命令,然后在上传,使用昨天的命令进行meanshiftCanopyDriver的调用。
不过补充一点,因为在InputMapper中对这个数据的处理还有下面的代码:

 for (String value : numbers) {
      if (!value.isEmpty()) {
        doubles.add(Double.valueOf(value));
      }
    }
这个代码就表示如果是空字符串的话,就不进行添加,所以说输入数据和前面保持一致也是可以的,即昨天的数据和今天修改的数据其结果一样。
MeansShiftCanopyDriver的run方法跳转如下:

run(159行)-->buildClusters(282行)-->buildClustersMR(353行)-->runIterationMR(412行),这里说明几点:

在159行开始run方法进入后,进行第一个判断inputIsCanopies,如下:

if (inputIsCanopies) {
      clustersIn = input;
    } else {
      createCanopyFromVectors(conf, input, clustersIn, measure, runSequential);
    }
因为在前面的测试中我们已经使用了InputDriver把输入数据转换为了canopy,所以这里直接进入了clustersIn=input,然后往下面走;

在282行的buildClusters方法进入后因为是默认在Hadoop中跑的程序,所以是使用MR算法的,进入到else中,如下:

if (runSequential) {
      return buildClustersSeq(clustersIn, output, measure, kernelProfile, t1,
          t2, convergenceDelta, maxIterations, runClustering);
    } else {
      return buildClustersMR(conf, clustersIn, output, measure, kernelProfile,
          t1, t2, convergenceDelta, maxIterations, runClustering);
    }
在353行中的方法buildClustersMR进入后,即开始进行循环,混合的主体是412行的runIterationMR方法。本篇主要分析此Job的Mapper和Reducer类,

这两个类分别是MeanShiftCanopyMapper、MeanShiftCanopyReducer。下面的代码是MeanShiftCanopyMapper的仿造代码,可以直接使用此代码进行调试,这样就可以看到MeanShiftCanopyMapper的数据逻辑流了,今晚又太晚了,明天还要早起。就下次再分析了,代码如下:

package mahout.fansy.meanshift;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.Writable;
import org.apache.mahout.clustering.iterator.ClusterWritable;
import org.apache.mahout.clustering.meanshift.MeanShiftCanopy;
import org.apache.mahout.clustering.meanshift.MeanShiftCanopyClusterer;
import org.apache.mahout.clustering.meanshift.MeanShiftCanopyConfigKeys;
import org.apache.mahout.common.iterator.sequencefile.PathFilters;
import org.apache.mahout.common.iterator.sequencefile.PathType;
import org.apache.mahout.common.iterator.sequencefile.SequenceFileDirValueIterable;

import com.google.common.collect.Lists;

public class MeanShiftCanopyMapperFollow {

	/**
	 * MeanShiftCanopyMapper仿造代码
	 * @author fansy
	 * @param args
	 */
	public static void main(String[] args) {
		cleanup();// 调试cleanup函数
	}
	
	/**
	 * 仿造map操作
	 */
	public static Collection<MeanShiftCanopy> map(){
		Collection<MeanShiftCanopy> canopies = Lists.newArrayList();
		List<ClusterWritable> data=getMapData(); // 获取map的输入值
		MeanShiftCanopyClusterer clusterer=setup();  // 获取setup函数中经过设置的值
		for (ClusterWritable clusterWritable : data){  // 这里设置断点,查看程序初始数据
			MeanShiftCanopy canopy = (MeanShiftCanopy)clusterWritable.getValue();
		      clusterer.mergeCanopy(canopy.shallowCopy(), canopies);
		}
		return canopies;
	}
	/**
	 * 仿造setup函数
	 * @return 返回经过设置值的MeanShiftCanopyClusterer
	 */
	public static MeanShiftCanopyClusterer setup(){
		String measureClassName="org.apache.mahout.common.distance.EuclideanDistanceMeasure";
		String kernelProfileClassName="org.apache.mahout.common.kernel.TriangularKernelProfile";
		double convergenceDelta=0.5;
		double t1=47.6;
		double t2=1;
		boolean runClustering=true;
		Configuration conf =new Configuration();
		
		conf.set(MeanShiftCanopyConfigKeys.DISTANCE_MEASURE_KEY, measureClassName);
	    conf.set(MeanShiftCanopyConfigKeys.KERNEL_PROFILE_KEY,
	        kernelProfileClassName);
	    
		conf.set(MeanShiftCanopyConfigKeys.CLUSTER_CONVERGENCE_KEY, String
	        .valueOf(convergenceDelta));
	   
		conf.set(MeanShiftCanopyConfigKeys.T1_KEY, String.valueOf(t1));
	    conf.set(MeanShiftCanopyConfigKeys.T2_KEY, String.valueOf(t2));
	
	    conf.set(MeanShiftCanopyConfigKeys.CLUSTER_POINTS_KEY, String
	        .valueOf(runClustering));
		MeanShiftCanopyClusterer clusterer = new MeanShiftCanopyClusterer(conf);
		
		return clusterer;
	}
	/**
	 * 仿造cleanup函数
	 */
	public static Map<Text,Collection<ClusterWritable>> cleanup(){
		int numReducers=1; // 自己设定,这里为了方便直接设置为1
		Map<Text,Collection<ClusterWritable>> map=new HashMap<Text,Collection<ClusterWritable>>();
		Collection<MeanShiftCanopy> canopies=map(); // 获得map的输出
		MeanShiftCanopyClusterer clusterer =setup();// 获得setup的输出
		int reducer = 0;
		Collection<ClusterWritable> v=Lists.newArrayList();
	    for (MeanShiftCanopy canopy : canopies) {
	      clusterer.shiftToMean(canopy);
	      ClusterWritable clusterWritable = new ClusterWritable();
	      clusterWritable.setValue(canopy);
	      v.add(clusterWritable);
	      reducer++;
	      if (reducer >= numReducers) {
	    	  reducer=0;
	      }
	    }
	    map.put(new Text(String.valueOf(reducer)), v);
	    return map;
	}
	/**
	 * 获得map的输入数据,输入数据的value是ClusterWritable类型的
	 * @return
	 */
	public static List<ClusterWritable> getMapData(){
		Path input=new Path("hdfs://ubuntu:9000/user/test/input/real_input/part-m-00000");
		Configuration conf=new Configuration();
		conf.set("mapred.job.tracker", "ubuntu:9001");
		List<ClusterWritable> clusters = new ArrayList<ClusterWritable>();
    	for (Writable value : new SequenceFileDirValueIterable<Writable>(input, PathType.LIST,
    	        PathFilters.partFilter(), conf)) {
    	      Class<? extends Writable> valueClass = value.getClass();
    	      if (valueClass.equals(ClusterWritable.class)) {
    	        ClusterWritable clusterWritable = (ClusterWritable) value;
    	        clusters.add( clusterWritable);
    	      } else {
    	        throw new IllegalStateException("can't read " + input);
    	      }
    	    }
    	return clusters;
	}

}


今天培训还听讲师说不要抱怨,额,好吧,现在感觉天天都是1点半之后或者左右的时间睡觉了,严重感觉睡眠不足,哎,难道这就是程序员的名?今天讲师还说确定目标后有四个阶段:初始兴奋期、寂寞期、煎熬期、成功期,我现在还在哪个阶段熬着呀。额,好吧,慢慢来,坚持。。。


分享,快乐,成长


转载请注明出处:http://blog.csdn.net/fansy1990




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值