starxzj 发表于 2016-12-11 11:08:44

hadoop mapreduce程序jar包版本冲突解决方法

  写MR程序时往往会使用到第三方包, 如果这些包在集群中不存在, 可以通过多种方式提交到集群供 MR 程序使用, 但如果集群中存在的jar与用户MR程序用到的JAR存在版本冲突时该如何解决?


下面是我碰到的问题及解决方式, 简单记录如下, 碰到同样问题的同学可以参考下:


昨天使用 commons-net-3.2.jar 包连接FTP采集日志,


调用方法片段:

FTPClient ftpClient = new FTPClient();
ftpClient.setConnectTimeout(1000);
  // 这个方法在commons-net-3.2.jar包中有, 而在 commons-net-1.4.1.jar 中没有


一般情况下,使用hadoop jar 执行mr的时候,会首先加载$HADOOP_HOME/lib下的jar包,

由于使用的hadoop中带了commons-net-1.4.1.jar,所以会优先加载1.4.1版本,而忽略用户自己指定的3.2版本,所以报异常,


Error: org.apache.commons.net.ftp.FTPClient.setConnectTimeout(I)V    

//异常提示调用setConnectTimeout 方法有问题。
  查看源码TaskRunner.java

/**
*/
static List<String> getClassPaths(JobConf conf, File workDir,
TaskDistributedCacheManager taskDistributedCacheManager)
throws IOException {
// Accumulates class paths for child.
List<String> classPaths = new ArrayList<String>();

boolean userClassesTakesPrecedence = conf.userClassesTakesPrecedence();
// 这个参数项可以改变系统classpath加载的优先顺序, 默认应该是false

if (!userClassesTakesPrecedence) {   // 默认是false, tasktrack机器的系统classpath总是优先加载
// start with same classpath as parent process
appendSystemClasspaths(classPaths);
}

// include the user specified classpath
appendJobJarClasspaths(conf.getJar(), classPaths);

// Distributed cache paths
if (taskDistributedCacheManager != null)
classPaths.addAll(taskDistributedCacheManager.getClassPaths());

// Include the working dir too
classPaths.add(workDir.toString());

if (userClassesTakesPrecedence) {
// parent process's classpath is added last
appendSystemClasspaths(classPaths);
}

return classPaths;
}
  通过上面源码可以看出 参数项 -Dmapreduce.task.classpath.user.precedence 可以改变系统classpath加载的优先顺序


验证:


hadoop jar collect_log.jar com.collect.LogCollectJob -Dmapreduce.task.classpath.user.precedence=true

-libjars commons-net-3.2.jar /new_log_collect/input /new_log_collect/output


程序执行成功。
页: [1]
查看完整版本: hadoop mapreduce程序jar包版本冲突解决方法