有两种机制可以将其他 jar 包含到您的作业的类路径中:
如果您尚未将它们存储在 HDFS 中,则可以使用 GenericOptionsParser 的-libjars
参数。这将导致 JobClient 为您的作业将 jars 上传到 HDFS 中的临时目录,并将它们包含在您的作业的分布式缓存中。为此,您需要通过 ToolRunner.run 界面运行您的作业:
public class MyJob extends COnfigured implements Tool {
public int run(String args[]) {
Job job = new Job(getConf());
// configure your job
// ..
return job.waitForCompletion() ? 0 : 1;
}
public static void main(String args[]) throws Exception {
ToolRunner.run(new MyJob(), args));
}
}
然后您将按如下方式运行您的作业(将 jars 1-3 添加到作业类路径):
#> hadoop jar myjob.jar MyJob -libjars jar1.jar,jar2.jar,jar3.jar [other args]
如果您的 jars 已经在 HDFS 中,那么您只需将 jars 添加到分布式缓存中:
public int run(String args[]) {
Job job = new Job(getConf());
// configure your job
// ..
// acquire job configuration
Configuration conf = job.getConf();
// create a FileSystem
FileSystem fs = FileSystem.get(fs);
DistributedCache.addFileToClassPath(new Path("/myapp/jar1.jar"), conf, fs);
DistributedCache.addFileToClassPath(new Path("/myapp/jar2.jar"), conf, fs);
DistributedCache.addFileToClassPath(new Path("/myapp/jar3.jar"), conf, fs);
return job.waitForCompletion() ? 0 : 1;
}
第二种方法的唯一缺点是您不能在作业配置中引用这些 jar 中的任何类(除非您也有副本客户端,并且您配置了HADOOP_CLASSPATH
env 变量)。