如何在执行作业时将OracleDriver添加到hadoop类路径?

时间:2012-12-18 09:40:25

标签: java hadoop mapreduce hdfs

我正在使用hadoop处理来自数据库的大量数据。我正在使用oracle的jdbc驱动程序连接到Oracle DB并进行处理。但是当我尝试通过bin/hadoop使用打包的JAR文件执行hadoop作业时,它显示找不到OracleDriver类。我该如何解决?

$ bin/hadoop jar ~/hadoop1.jar name.hadoop.Hadoop1 ~/output


Exception in thread "main" java.lang.RuntimeException: Error in configuring object
    at org.apache.hadoop.util.ReflectionUtils.setJobConf(ReflectionUtils.java:93)
    at org.apache.hadoop.util.ReflectionUtils.setConf(ReflectionUtils.java:64)
    at org.apache.hadoop.util.ReflectionUtils.newInstance(ReflectionUtils.java:117)
    at org.apache.hadoop.mapred.JobConf.getInputFormat(JobConf.java:575)
    at org.apache.hadoop.mapred.JobClient.writeOldSplits(JobClient.java:1051)
    at org.apache.hadoop.mapred.JobClient.writeSplits(JobClient.java:1043)
    at org.apache.hadoop.mapred.JobClient.access$700(JobClient.java:179)
    at org.apache.hadoop.mapred.JobClient$2.run(JobClient.java:959)
    at org.apache.hadoop.mapred.JobClient$2.run(JobClient.java:912)
    at java.security.AccessController.doPrivileged(Native Method)
    at javax.security.auth.Subject.doAs(Subject.java:415)
    at org.apache.hadoop.security.UserGroupInformation.doAs(UserGroupInformation.java:1136)
    at org.apache.hadoop.mapred.JobClient.submitJobInternal(JobClient.java:912)
    at org.apache.hadoop.mapred.JobClient.submitJob(JobClient.java:886)
    at org.apache.hadoop.mapred.JobClient.runJob(JobClient.java:1323)
    at name.shahalpk.poc.hadoop.Hadoop1.main(Hadoop1.java:73)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at org.apache.hadoop.util.RunJar.main(RunJar.java:156)
Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at org.apache.hadoop.util.ReflectionUtils.setJobConf(ReflectionUtils.java:88)
    ... 20 more
Caused by: java.lang.RuntimeException: java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver
    at org.apache.hadoop.mapred.lib.db.DBInputFormat.configure(DBInputFormat.java:271)
    ... 25 more
Caused by: java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:186)
    at org.apache.hadoop.mapred.lib.db.DBConfiguration.getConnection(DBConfiguration.java:123)
    at org.apache.hadoop.mapred.lib.db.DBInputFormat.configure(DBInputFormat.java:266)
    ... 25 more

2 个答案:

答案 0 :(得分:1)

将ojdbc5.jar添加到类路径中:

${JRE_HOME}\jre\lib\ext

注意:

${JRE_HOME} means JRE(Java Runtime Environment) Installed Directory; Like below
${JRE_HOME}=C:\Program Files\Java\jre6\

答案 1 :(得分:0)

您可以使用-libjars generic选项来更轻松地实现此目的,并且它还可以处理jar到集群节点的分发:

$ bin/hadoop jar ~/hadoop1.jar name.hadoop.Hadoop1 -libjars ojdbc5.jar ~/output

这假设你的主类(name.hadoop.Hadoop1)正在使用ToolRunner.run()方法来启动你的工作:

public class Hadoop1 extends Configured implements Tool {
  public static void main(String args[]) throws Exception {
    ToolRunner.run(new Hadoop1(), args);
  }

  public int run(String args[]) {
    JobConf job = new JobConf(getConf());

    // rest of your job init code...

    RunningJob rj = JobClient.runJob(job);
    rj.waitForCompletion();
    return rj.isSuccessful() ? 0 : 1;
  }
}

(手型代码,为任何拼写错误或编译错误道歉)