gpt4 book ai didi

unit-testing - 如何让 HBaseTestingUtility 在 map reduce 作业中查找类?

转载 作者:可可西里 更新时间:2023-11-01 16:19:09 33 4
gpt4 key购买 nike

我们正在使用 cdh3u4、Hadoop 和 HBase。在启动 HBaseTestingUtility 提供的 miniMapReduceCluster 之后,我正在尝试运行一个单元测试以启动 MapReduce 作业。

作业失败并在 map 和 reducer 任务 stderr 日志中显示:

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/hadoop/mapred/Child
Caused by: java.lang.ClassNotFoundException: org.apache.hadoop.mapred.Child
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
Could not find the main class: org.apache.hadoop.mapred.Child. Program will exit.
java.lang.Throwable: Child Error

这几天我一直在努力解决这个问题。我猜这是一个错误的配置,并且由于错误配置的 fs/hdfs 配置值,集群没有找到我的任何 jar。我的测试设置代码如下所示(请原谅打字错误,因为这是从 Scala 翻译过来的):

HBaseTestingUtility htest = new HBaseTestingUtility();
Configuration c = htest.getConfiguration();
c.set("hadoop.log.dir", "/tmp/hadoop-test-logs"); // required or else can't start the miniMapReduceCluster
htest.startMiniCluster();
htest.startMiniMapReduceCluster();

// create and run a MapReduce job that works in production but not in test

如果这很重要,我们正在使用 Play!带有 Specs2 测试框架和 Scala 的框架 2.0(使用 SBT)。我认为这无关紧要(我们没有使用 Java + JUnit)。

有没有人见过这个?关于在哪里看的任何指示?

提前致谢

标记

最佳答案

原来我们必须在 minicuster 上手动设置类路径。这可能是 SBT/Ivy 的事情 - 因为我见过的所有示例都不必这样做,并且可能使用 Maven(和 Java)。

以下是我如何解决类路径问题(在 Scala 中):

// unit test setup:
val htest = new HBaseTestingUtility
htest.startMiniCluster()

val conf = htest.getConfiguration
conf.set("hadoop.log.dir", "/tmp/hadoop-test-logs") // required to prevent NPE when starting miniMapReduceCluster
htest.startMiniMapReduceCluster()

// Set up cluster classpath:
val fs = FileSystem.get(conf)
val jarsDir = new Path("/tmp/hadoop-test-lib")
fs.mkdirs(jarsDir)

// copy jars over to hdfs and add to classpath:
for (jar <- myjars) {
val localJarPath = new Path("file://%s".format(jar.getAbsolutePath))
val hdfsJarPath = new Path("/tmp/hadoop-test-lib/%s".format(jar.getName))
fs.copyFromLocalFile(localJarPath, hdfsJarPath)
DistributedCache.addFileToClassPath(hdfsJarPath)
}

// now Map and Reduce tasks can find classes

关于unit-testing - 如何让 HBaseTestingUtility 在 map reduce 作业中查找类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12308980/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com