【问题标题】:Why is a jar file needed while executing MapReduce code in hadoop but not needed in executing any other Non-MapReduce Java code in hadoop为什么在 hadoop 中执行 MapReduce 代码时需要 jar 文件,但在 hadoop 中执行任何其他非 MapReduce Java 代码时不需要
【发布时间】:2014-04-05 13:42:02
【问题描述】:

我想知道为什么 jar 文件不需要 .class 文件来在 hadoop 中执行 MapReduce 代码。那么如果使用 Jar 文件,那么为什么在 hadoop 中执行任何其他非 MapReduce Java 代码时不这样做呢?此外,在 hadoop 中执行任何其他非 MapReduce Java 代码时,为什么在命令行中直接提到编译类以及 hadoop 关键字,例如。如果我有一个程序可以使用 url 在 hadoop 中显示文件,即。对于 FileSystemCat 类

    public class FileSystemCat {
    public static void main(String[] args) throws Exception {
    String uri = args[0];
    Configuration conf = new Configuration();
    FileSystem fs = FileSystem.get(URI.create(uri), conf);
    InputStream in = null;
    try {
    in = fs.open(new Path(uri));
    IOUtils.copyBytes(in, System.out, 4096, false);
    } finally {
    IOUtils.closeStream(in);

}
}
}

编译程序后执行程序的命令是“hadoop FileSystemCat”而不是“hadoop Java FileSystemCat”。在普通环境中,执行程序的步骤是:

Javac FileSystemCat.java
Java FileSystemCat.class

【问题讨论】:

    标签: java hadoop jar mapreduce


    【解决方案1】:
     hadoop jar <jar> [mainClass] args... 
    

    运行一个 jar 文件。用户可以将他们的 Map Reduce 代码捆绑在一个 jar 文件中并使用此命令执行它。

     hadoop CLASSNAME 
    

    hadoop 脚本可用于调用任何类。

    上面的这些命令做了两个思考。

    1)将Hadoop安装lib目录下的所有jar添加到正在运行的jar或类的classpath中。

    2)将hadoop安装的配置目录添加到classpath中。

    因此,正在运行的 JAR 或 CLASS 将获取 hadoop 安装类路径中的所有类以及安装的所有配置文件。

    如果您使用简单的Java CLASSNAME 命令运行 JAR 或 CLASS,则必须将上述两个组件分别添加到 java 类路径中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-14
      • 1970-01-01
      • 2018-10-15
      • 1970-01-01
      相关资源
      最近更新 更多