Mahout中关于KMeans方法调用的代码:
Path directoryContainingConvertedInput = new Path(output, DIRECTORY_CONTAINING_CONVERTED_INPUT);
log.info("Preparing Input");
InputDriver.runJob(input, directoryContainingConvertedInput, "org.apache.mahout.math.RandomAccessSparseVector");
log.info("Running random seed to get initial clusters");
Path clusters = new Path(output, "random-seeds");
clusters = RandomSeedGenerator.buildRandom(conf, directoryContainingConvertedInput, clusters, k, measure);
log.info("Running KMeans with k = "+ k);
KMeansDriver.run(conf, directoryContainingConvertedInput, clusters, output, convergenceDelta,
maxIterations, true, 0.0, false);
// run ClusterDumper
Path outGlob = new Path(output, "clusters-*-final");
Path clusteredPoints = new Path(output,"clusteredPoints");
log.info("Dumping out clusters from clusters:"+outGlob+" and clusteredPoints: "+ clusteredPoints);
// ClusterDumper clusterDumper = new ClusterDumper(outGlob, clusteredPoints);
clusterDumper.printClusters(null);
这儿初始化聚类中心点是随机选择的,通过调用 KMeansDriver 的run方法;我们可以得到了结果文件clusters-*-final 和 clusteredPoints 。但是文件都是二进制文件,没法直接查看;我们可以通过Mahout 提供了 ClusterDumper 类,调用它的打印方法查看结果。很多时候,由于聚类的样本空间比较大,直接打印效果并不理想;那有没有保存至文本中的方法呢?答案是有的。
网上已有的解决方案:
参考地址 = 》 http://blog.csdn.net/fansy1990/article/details/17589287
不过查看Mahout官网资料的时候,发现它提供了解决方式:
--seqFileDir <MAHOUT_HOME>/examples/output/clusters-10 --pointsDir <MAHOUT_HOME>/examples/output/clusteredPoints --output <MAHOUT_HOME>/examples/output/clusteranalyze.txt官网地址 = 》 http://mahout.apache.org/users/clustering/cluster-dumper.html
查看了 ClusterDumper 源代码,发现并没有 output 属性;但是发现 ClusterDumper 的父类 AbstractJob 有两个关键属性 outputPath 和 outputFile;所以我想在实例化ClusterDumper 构造函数的时候设置 outputFile 或 outputPath 属性。
/**
* 保存至本地文件
* @param seqFileDir
* @param pointsDir
* @param outputPath
*/
public ClusterDumper(Path seqFileDir, Path pointsDir,String outputPath) {
super();
this.seqFileDir = seqFileDir;
this.pointsDir = pointsDir;
try {
this.outputFile = new File(outputPath);
} catch (Exception e) {
// TODO Auto-generated catch block
System.out.println("********************* something was wrong....."+ e.getMessage());
}
init();
}
运行成功!通过重载构造函数,设置 outputFile ;终于实现聚类中心点结果保存至文件中。
但是到这儿的时候还是遇到了一个问题:由于outputFile 是 java.io.File ,由于只能本地创建文件,没法实现HDFS上面文件创建写入?
这个问题一时也没想到好的解决方法,先写到这吧!如果有高手有幸看到,求指点一二。