使用maven在hadoop-cluster上运行java程序

时间:2014-07-17 16:37:07

标签: java maven hadoop mahout

我可以通过在hadoop-cluster上使用maven构建它来执行java程序。

我的程序的源代码

package com.ait.summer.clusterermapreduce;

import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.mahout.clustering.canopy.CanopyDriver;
import org.apache.mahout.clustering.kmeans.KMeansDriver;
import org.apache.mahout.common.HadoopUtil;
import org.apache.mahout.common.distance.EuclideanDistanceMeasure;

public class ClustererMapReduce {

    public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException {

        Configuration conf = new Configuration();
        FileSystem hdfs = FileSystem.get(conf);

        Path ProjectFolder = new Path (args[0]);

        Path InputDir = new Path(args[0], "in");
        Path OutputDir = new Path(args[0], "out");
        Path canopyCentroids = new Path(args[0], "canopyCentroids");
        HadoopUtil.delete(conf, OutputDir);

        double clusterClassificationThreshold = 0.5;
        double t1 = 20;
        double t2 = 30;
                // how to set up correctly param?

        CanopyDriver.run(conf, InputDir, canopyCentroids, new EuclideanDistanceMeasure(),
                t1, t2, false, clusterClassificationThreshold, false);        

        KMeansDriver.run(conf, InputDir, new Path(canopyCentroids, "clusters-0"), OutputDir, new EuclideanDistanceMeasure(), 0.5 , 5, true, 0.5 , false);

    }
}

我的pom.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.ait.summer</groupId>
    <artifactId>ClustererMapReduce</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <repositories>
    <repository>
      <id>apache.snapshots</id>
      <name>Apache Snapshot Repository</name>
      <url>http://repository.apache.org/snapshots</url>
      <releases>
        <enabled>false</enabled>
      </releases>
    </repository>

    <repository>
      <id>cdh.repo</id>
      <url>https://repository.cloudera.com/artifactory/cloudera-repos</url>
      <name>Cloudera Repositories</name>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
    </repository>

    <repository>
      <id>cdh.snapshots.repo</id>
      <url>https://repository.cloudera.com/artifactory/libs-snapshot-local</url>
      <name>Cloudera Snapshots Repository</name>
      <snapshots>
        <enabled>true</enabled>
      </snapshots>
      <releases>
        <enabled>false</enabled>
      </releases>
    </repository>
  </repositories>
    <dependencies>

        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-common</artifactId>
            <version>2.0.0-cdh4.7.0</version>
            <type>jar</type>
        </dependency>
        <dependency>
            <groupId>org.apache.mahout</groupId>
            <artifactId>mahout-core</artifactId>
            <version>0.7-cdh4.7.0</version>
            <type>jar</type>
        </dependency>
        <dependency>
            <groupId>jfree</groupId>
            <artifactId>jfreechart</artifactId>
            <version>1.0.8a</version>
        </dependency>
    </dependencies>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
    </properties>
</project>

从命令行编译此代码后(我位于项目目录中)

$ mvn install

我用

运行它
$ hadoop jar

并收到此错误日志

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/mahout/common/distance/DistanceMeasure
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:270)
    at org.apache.hadoop.util.RunJar.main(RunJar.java:201)
Caused by: java.lang.ClassNotFoundException: org.apache.mahout.common.distance.DistanceMeasure
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
    ... 3 more

我使用的是CDH-4.7.0 JDK 1.7.0.65 使用NetBeans IDE 8.0

这个问题可能是什么原因?

1 个答案:

答案 0 :(得分:0)

如果您使用maven创建了JAR,则可以运行以下命令:

在Linux中: export CLASSPATH=<path_jars_dependencies and jar_ClustererMapReduce> java -cp $CLASSPATH com.ait.summer.clusterermapreduce.ClustererMapReduce

[path_jars_dependencies]:您可以使用帖子导出应用程序的依赖项: How can I create an executable JAR with dependencies using Maven?

[jar_ClustererMapReduce]:这是使用命令maven install生成的JAR