首先更正一点,昨天处理数据的时候是有问题的,直接从网页中拷贝的文件的空格是有问题的,直接拷贝然后新建的文件中的空格可能有一个两个、三个的,所以要把两个或者三个的都换为一个,在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,ClusterWritable> cleanup(){
int numReducers=1; // 自己设定,这里为了方便直接设置为1
Map<Text,ClusterWritable> map=new HashMap<Text,ClusterWritable>();
Collection<MeanShiftCanopy> canopies=map(); // 获得map的输出
MeanShiftCanopyClusterer clusterer =setup();// 获得setup的输出
int reducer = 0;
for (MeanShiftCanopy canopy : canopies) {
clusterer.shiftToMean(canopy);
ClusterWritable clusterWritable = new ClusterWritable();
clusterWritable.setValue(canopy);
map.put(new Text(String.valueOf(reducer)), clusterWritable);
reducer++;
if (reducer >= numReducers) {
reducer=0;
}
}
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"); //路径是经过InputDriver后的输出路径
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

Mahout源码MeanShiftCanopyDriver分析之二MeanShiftCanopyMapper仿造的更多相关文章

  1. Mybatis的基本操作案列增加以及源码的分析(二)

    一.构建一个框架的项目的思路 首先我们先建立一个web项目,我们需要jar,mybatis-config.xml和studentDao.xml的配置随后就是dao.daoimpl.entity.的架构 ...

  2. [java源码解析]对HashMap源码的分析(二)

    上文我们讲了HashMap那骚骚的逻辑结构,这一篇我们来吹吹它的实现思想,也就是算法层面.有兴趣看下或者回顾上一篇HashMap逻辑层面的,可以看下HashMap源码解析(一).使用了哈希表得“拉链法 ...

  3. mahout源码KMeansDriver分析之五CIMapper

    接上文重点分析map操作: Vector probabilities = classifier.classify(value.get());// 第一行 Vector selections = pol ...

  4. mahout源码KMeansDriver分析之五CIMapper初探

    接着上篇,继续分析代码.下面就到了MR的循环了,这里MR应该算是比较好理解的,重点是退出循环的条件设置,即如何判断前后两次中心点误差小于给定阈值. 首先,while循环: while (iterati ...

  5. mahout源码KMeansDriver分析之四

    昨天说到为什么Configuration没有设置conf.set("mapred.job.tracker","hadoop:9000")仍然可以访问hdfs文件 ...

  6. nova创建虚拟机源码系列分析之二 wsgi模型

    openstack nova启动时首先通过命令行或者dashborad填写创建信息,然后通过restful api的方式调用openstack服务去创建虚拟机.数据信息从客户端到达openstack服 ...

  7. 【原】Android热更新开源项目Tinker源码解析系列之二:资源文件热更新

    上一篇文章介绍了Dex文件的热更新流程,本文将会分析Tinker中对资源文件的热更新流程. 同Dex,资源文件的热更新同样包括三个部分:资源补丁生成,资源补丁合成及资源补丁加载. 本系列将从以下三个方 ...

  8. MapReduce的ReduceTask任务的运行源码级分析

    MapReduce的MapTask任务的运行源码级分析 这篇文章好不容易恢复了...谢天谢地...这篇文章讲了MapTask的执行流程.咱们这一节讲解ReduceTask的执行流程.ReduceTas ...

  9. MapReduce的MapTask任务的运行源码级分析

    TaskTracker任务初始化及启动task源码级分析 这篇文章中分析了任务的启动,每个task都会使用一个进程占用一个JVM来执行,org.apache.hadoop.mapred.Child方法 ...

随机推荐

  1. 没有产品,没有用户的,绝对不要浪费时间去联系风投——没有过home run的创业人,想办法先做出产品,找到少量用户,没有任何销售成本

    著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处.作者:Kuan Huang链接:http://www.zhihu.com/question/19641135/answer/1353 ...

  2. CentOS 7 安装 Gitlab

    https://segmentfault.com/a/1190000002729796

  3. A fatal error has been detected by the Java Runtime Environment:

    在Eclipse中运行项目 遇到如下错误: ## A fatal error has been detected by the Java Runtime Environment:## EXCEPTIO ...

  4. dhtmlgrid修改,支持IE10

    因为项目IE升级,导致原来使用的dhtmlgrid无法正常显示,同时通过loadxml接口还有属性不支持. 花了半天时间对dhtmlgrid进行了修改,能够支持IE10正常加载显示. edit by ...

  5. [置顶] 与小伙伴共勉的java有关jvm的知识(一),小鸟尽量写得详细哦,欢迎讨论,谢绝喷子

    JAVA运行在JVM之上,JVM的运行状况会对程序产生很大的影响,因此了解一些JVM的东东,对于编写稳定的,高性能的java程序至关重要.这是JVM的规范中定义的标准结构图: 以上标准是JVM标准中定 ...

  6. AndroidUI 布局动画-为布局添加动画

    除了可以为视图添加动画以外,还可以为视图的布局添加动画: <RelativeLayout xmlns:android="http://schemas.android.com/apk/r ...

  7. java.lang.OutOfMemoryError: PermGen space 解决方案

    只需两步: 将值改为512或者1024,然后CTRL+S,重启tomcat 和eclipse即可.

  8. 基于Schema的AOP 配置使用详解

    原文地址:http://jinnianshilongnian.iteye.com/blog/1418598 基于Schema的AOP从Spring2.0之后通过"aop"命名空间来 ...

  9. C#核编之一个简单的C#程序

    构建一个简单的C#应用程序需要注意一下几点: 1.C#要求所有的程序逻辑都包含在一个类型定义中       --->这里的类型指的是(类,接口,结构,枚举,委托中的一个或多个) 2.与其他语言不 ...

  10. php缓存生成及更新实现参考哦

    <?php //如果在find/findAll里传入了参数,则该参数即为key ORM::factory('article')->where('user_id', '=', '2')-&g ...