mahout源码KMeansDriver分析之四

昨天说到为什么Configuration没有设置conf.set("mapred.job.tracker","hadoop:9000")仍然可以访问hdfs文件系统(我又换回虚拟机了,因为我加了2G内存。。。所以改为了hadoop:9000),实验证明,是可以的。比如编写下面的测试程序:

package mahout.fansy.test.kmeans.middle;
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
public class TestWritePolicy {
	/**
	 * 测试ClusterClassifier中的writePolicy方法
	 * 此方法的configuration没有进行设定,所以应该是访问不到的才对,但是这里居然访问到了,所以这里是什么问题?
	 * @throws IOException  
	 */
	public static void main(String[] args) throws IOException {
		String path="hdfs://hadoop:9000/user/fansy/input/1";
		Configuration conf=new Configuration();
		Path f=new Path(path);
		FileSystem fs=FileSystem.get(f.toUri(), conf);
		boolean flag=fs.mkdirs(f);
		System.out.println("done:"+flag);
	}
}

可以发现没有设置jt同样可以访问fs,所以得出的结论是如果要访问jt,比如说要运行job的时候要设置这个参数,但是单单是访问fs的时候可以不用设置,因为在路径里面就已经默认了fs 为hadoop:9000,进行debug调试可以看到Path的uri里面其实是有fs信息的,比如:

解决这个疑问,就继续看下面的代码了:

 Path priorClustersPath = new Path(output, Cluster.INITIAL_CLUSTERS_DIR);
    ClusteringPolicy policy = new KMeansClusteringPolicy(convergenceDelta);
    ClusterClassifier prior = new ClusterClassifier(clusters, policy);
    prior.writeToSeqFiles(priorClustersPath);

这两个类,之前没有见过,单单从英文的分析来看,一个应该是和阈值有关的,另外的一个应该是分类用的。看writeToSeqFiles方法:

public void writeToSeqFiles(Path path) throws IOException {
    writePolicy(policy, path);
    Configuration config = new Configuration();
    FileSystem fs = FileSystem.get(path.toUri(), config);
    SequenceFile.Writer writer = null;
    ClusterWritable cw = new ClusterWritable();
    for (int i = 0; i < models.size(); i++) {
      try {
        Cluster cluster = models.get(i);
        cw.setValue(cluster);
        writer = new SequenceFile.Writer(fs, config,
            new Path(path, "part-" + String.format(Locale.ENGLISH, "%05d", i)), IntWritable.class,
            ClusterWritable.class);
        Writable key = new IntWritable(i);
        writer.append(key, cw);
      } finally {
        Closeables.closeQuietly(writer);
      }
    }
  }


第一行的writePolicy即是把policy变量写入一个文件中,后面的怎么感觉是把所有的中心点分别写入一个文件的样子?额好吧,测试的结果确是要产生k个文件的。

额 ,总感觉最近看代码的进度很慢,哎,或许是太急了?



分享,快乐,成长


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



原文地址:https://www.cnblogs.com/pangblog/p/3241346.html