当前位置: 代码迷 >> Eclipse >> 运用hadoop eclipse plugin提交Job并添加多个第三方jar(完美版)
  详细解决方案

运用hadoop eclipse plugin提交Job并添加多个第三方jar(完美版)

热度:85   发布时间:2016-04-23 13:07:52.0
使用hadoop eclipse plugin提交Job并添加多个第三方jar(完美版)

通过 "conf.set("tmpjars", jars);" 可以设置第三方jar,之前一直只是添加一个jar,运行OK,今天打算添加多个jar的时候发现mapreduce在运行时找不到class(ClassNotFoundException),跟踪代码发现jar文件的确上传到了HDFS中,所以甚是无解,后来上传jar到hdfs,然后使用DistributedCache.addFileToClassPath()方法也不行。郁闷半天,后来看到job.xml中有一段奇怪的设置,mapred.job.classpath.files的value为"/user/heipark/lib/commons-lang-2.3.jar;/user/heipark/lib/guava-r08.jar",可以看到这个分隔符是分号(我的OS是windows),在linux系统和hadoop系统一般都是逗号和冒号分隔,然后我继续挖,发现DistributedCache.addArchiveToClassPath()方法(tmpjars也会用这个方法)中使用了“System.getProperty("path.separator")”,于是灵感闪现,修改该值为linux系统的冒号,我嚓,居然成功了,搞了我4个小时,eclipse终于可以添加多个第三方jar包了。封装了方法,在main方法直接添加jar包就可以了。

?

调用:

?

addTmpJar("D:/Java/new_java_workspace/scm/lib/guava-r08.jar", conf);
?

?

方法定义:

?

	/**	 * 为Mapreduce添加第三方jar包	 * 	 * @param jarPath	 *            举例:D:/Java/new_java_workspace/scm/lib/guava-r08.jar	 * @param conf	 * @throws IOException	 */	public static void addTmpJar(String jarPath, Configuration conf) throws IOException {		System.setProperty("path.separator", ":");		FileSystem fs = FileSystem.getLocal(conf);		String newJarPath = new Path(jarPath).makeQualified(fs).toString();		String tmpjars = conf.get("tmpjars");		if (tmpjars == null || tmpjars.length() == 0) {			conf.set("tmpjars", newJarPath);		} else {			conf.set("tmpjars", tmpjars + "," + newJarPath);		}	}
?

?

  相关解决方案