如何从mahout运行kmean集群?

yrefmtwq  于 2021-06-04  发布在  Hadoop
关注(0)|答案(1)|浏览(333)

嗨,我试着运行mahout在第7章(k-均值聚类)中的例子。有人能指导我如何在hadoop集群(单节点cdh-4.2.1)和mahout(0.7)中运行这个示例吗
以下是我遵循的步骤:
将代码(从github)复制到本地计算机上的eclipseide中。
将这些jar嵌入到我的eclipse项目中。
hadoop-common-2.0.0-cdh4.2.1.jar
hadoop-hdfs-2.0.0-cdh4.2.1.jar
hadoop-mapreduce-client-core-2.0.0-cdh4.2.1.jar
mahout-core-0.7-cdh4.3.0.jar
mahout-core-0.7-cdh4.3.0-job.jar
mahout-math-0.7-cdh4.3.0.jar
为这个项目制作了一个jar,并将这个jar复制到我的hadoop集群上
执行了这个命令
user@infph01463u用法:~$hadoop jar/home/user/apurv/kmean.jar tryout.simplekmeans群集
这给了我以下的错误

Exception in thread "main" java.lang.NoClassDefFoundError: FileSystem
        at java.lang.Class.getDeclaredMethods0(Native Method)
        at java.lang.Class.privateGetDeclaredMethods(Class.java:2427)
        at java.lang.Class.getMethod0(Class.java:2670)
        at java.lang.Class.getMethod(Class.java:1603)
        at org.apache.hadoop.util.RunJar.main(RunJar.java:202)
Caused by: java.lang.ClassNotFoundException: FileSystem
        at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
        ... 5 more

有人能帮我解决我所缺少的问题吗?或者我的执行方式错了吗?
其次,我想知道如何在csv文件上运行k-mean聚类??
提前感谢:)

kkbh8khc

kkbh8khc1#

给定的代码有误导性

Cluster cluster = new Cluster(vec, i, new EuclideanDistanceMeasure());
    writer.append(new Text(cluster.getIdentifier()), cluster);
}
writer.close();

KMeansDriver.run(conf, new Path("testdata/points"), new Path("testdata/clusters"),
  new Path("output"), new EuclideanDistanceMeasure(), 0.001, 10,
  true, false);

SequenceFile.Reader reader = new SequenceFile.Reader(fs,
    new Path("output/" + Cluster.CLUSTERED_POINTS_DIR
             + "/part-m-00000"), conf);

应替换为

Kluster cluster = new Kluster(vec, i, new EuclideanDistanceMeasure());
    writer.append(new Text(cluster.getIdentifier()), cluster);
}
writer.close();

KMeansDriver.run(conf, new Path("testdata/points"), new Path("testdata/clusters"),
  new Path("output"), new EuclideanDistanceMeasure(), 0.001, 10,
  true, false);

SequenceFile.Reader reader = new SequenceFile.Reader(fs,
    new Path("output/" + Kluster.CLUSTERED_POINTS_DIR
             + "/part-m-00000"), conf);

cluster是一个接口,而kluster是一个类。有关更多信息,请查看mahout api javadoc。
要用csv文件运行kmeans,首先必须创建一个sequencefile作为kmeansdriver中的参数传递。下面的代码读取csv文件“points.csv”的每一行,并将其转换为向量,然后将其写入序列文件“points.seq”

try (
            BufferedReader reader = new BufferedReader(new FileReader("testdata2/points.csv"));
            SequenceFile.Writer writer = new SequenceFile.Writer(fs, conf,new Path("testdata2/points.seq"), LongWritable.class, VectorWritable.class)
        ) {
            String line;
            long counter = 0;
            while ((line = reader.readLine()) != null) {
                String[] c = line.split(",");
                if(c.length>1){
                    double[] d = new double[c.length];
                    for (int i = 0; i < c.length; i++)
                            d[i] = Double.parseDouble(c[i]);
                    Vector vec = new RandomAccessSparseVector(c.length);
                    vec.assign(d);

                VectorWritable writable = new VectorWritable();
                writable.set(vec);
                writer.append(new LongWritable(counter++), writable);
            }
        }
        writer.close();
    }

希望有帮助!!

相关问题