http://www.cnblogs.com/spork/archive/2010/04/12/1710294.html

  通过前面两篇文章的分析,对Hadoop的作业提交流程基本明了了,下面我们就可以开始编写代码模拟这个流程。

  第一步要做的是添加Hadoop的依赖库和配置文件到classpath。最常用的方法就是用一个容器先把各个要添加到classpath的文件或文件夹存储起来,后面再作为类加载器的URL搜索路径。

/**
* Add a directory or file to classpath.
*
*
@param component
*/
public static void addClasspath(String component) {
if ((component != null) && (component.length() > 0)) {
try {
File f
= new File(component);
if (f.exists()) {
URL key
= f.getCanonicalFile().toURL();
if (!classPath.contains(key)) {
classPath.add(key);
}
}
}
catch (IOException e) {
}
}
}

  上面的classPath变量就是我们声明用来装载classpath组件的容器。

private static ArrayList<URL> classPath = new ArrayList<URL>();

  由于需要添加一些文件夹下的所有Jar包,所以我们还要实现一个遍历添加某文件夹下文件的方法。

/**
* Add all jars in directory to classpath, sub-directory is excluded.
*
*
@param dirPath
*/
public static void addJarsInDir(String dirPath) {
File dir
= new File(dirPath);
if (!dir.exists()) {
return;
}
File[] files
= dir.listFiles();
if (files == null) {
return;
}
for (int i = 0; i < files.length; i++) {
if (files[i].isDirectory()) {
continue;
}
else {
addClasspath(files[i].getAbsolutePath());
}
}
}

相关文章: