这些清楚了,在解析libjars这个参数时,会将指定的jar全部放到conf文件的tmpjars参数中,然后加载到classloader中。这意味着,也可以在config文件中指定tmpjars来设置项目依赖的jar文件。
再看看getLibJars这方法:
public static URL[] getLibJars(Configuration conf) throws IOException {
String jars = conf.get("tmpjars");
if(jars==null) {
return null;
}
String[] files = jars.split(",");
URL[] cp = new URL[files.length];
for (int i=0;i<cp.length;i++) {
Path tmp = new Path(files[i]);
cp[i] = FileSystem.getLocal(conf).pathToFile(tmp).toURI().toURL();
}
return cp;
}
用红色标记的字体表示,这个libjar文件需要放在文件系统中,不能是hdfs,也不能是s3n/s3或者其他的系统,必须是Hadoop所在的windows或者Linux的文件系统。否则你会得到这个错误:
Exception in thread "main" java.lang.IllegalArgumentException: This file system object (file:///) does not support access to the request path 's3n://userprofile/lib/mahout.jar' You possibly called FileSystem.get(conf) when you should have called FileSystem.get(uri, conf) to obtain a file system supporting your path. at org.apache.hadoop.fs.FileSystem.checkPath(FileSystem.java:351) at org.apache.hadoop.fs.RawLocalFileSystem.pathToFile(RawLocalFileSystem.java:47) at org.apache.hadoop.fs.LocalFileSystem.pathToFile(LocalFileSystem.java:49) at org.apache.hadoop.util.GenericOptionsParser.getLibJars(GenericOptionsParser.java:309) at org.apache.hadoop.util.GenericOptionsParser.processGeneralOptions(GenericOptionsParser.java:263) at org.apache.hadoop.util.GenericOptionsParser.parseGeneralOptions(GenericOptionsParser.java:387) at org.apache.hadoop.util.GenericOptionsParser.<init>(GenericOptionsParser.java:154) at org.apache.hadoop.util.GenericOptionsParser.<init>(GenericOptionsParser.java:139) at org.apache.hadoop.util.ToolRunner.run(ToolRunner.java:59)
此外在看看这两行加载jar的代码:
conf.setClassLoader(new URLClassLoader(libjars, conf.getClassLoader()));
Thread.currentThread().setContextClassLoader(...)
这意味这,这些包都被加载到当前classloader的子loader中,不是当前classloader中,所以,你不能在当前的job中直接使用这些包。
