首先更正一点,昨天处理数据的时候是有问题的,直接从网页中拷贝的文件的空格是有问题的,直接拷贝然后新建的文件中的空格可能有一个两个、三个的,所以要把两个或者三个的都换为一个,在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. 动态创建分页 LINQ+EF

    public class Message { public int MessageId { get; set; } public string MessageTitle { get; set; } p ...

  2. WebView cookies清理

    今天在项目中发现一个BUG 在使用新浪微博账户登录应用时,webview会自动登录上次的微博帐号!(因为webview 记录了微博帐号和密码的cookies) 所以,需要清除SessionCookie ...

  3. yum localinstall rpm

  4. 自己写一个strcmp函数(C++)

    题目说明: 写一个函数,实现两个字符串的比较.即自己写一个strcmp函数,函数原型为int strcmp( char * p1, char * p2); 设p1指向字符串s1,p2指向字符串s2.要 ...

  5. php操作路径的经典方法

    function create_folders($dir){    return is_dir($dir) or ( create_folders( dirname( $dir ) ) and mkd ...

  6. iOS学习,需要懂的一些基础

    1.  KVC 与 KVO 全称是Key-value coding,翻译成键值编码.顾名思义,在某种程度上跟map的关系匪浅.它提供了一种使用字符串而不是访问器方法去访问一个对象实例变量的机制. 全称 ...

  7. wcf系列学习5天速成——第五天 服务托管

    今天是系列的终结篇,当然要分享一下wcf的托管方面的知识. wcf中托管服务一般有一下四种: Console寄宿:             利于开发调试,但不是生产环境中的最佳实践. winform寄 ...

  8. 关于中文乱码的解决方法(URL方式)

    假设keyWord ='阳光'; url="play.jsp? keyWord ="+ keyWord 若按照上述的地址直接访问,则中文会变成乱码.必须使用encodeURI()进 ...

  9. Python核心编程读笔 12:OOP

    第13章 面向对象编程 一.基本概念 1.object类是所有类的基类,如果你的类没有继承任何其他父类,object 将作为默认的父类. 2.python创建实例时无需new: myFirstObje ...

  10. C++中将int转变成string和string转变成int

    int to string #include<iostream> #include<string> using namespace std; int main() { stri ...