I am trying to write some unit testing for my Java Spark code. Below are my first simple try
public class SparkHelperTest {
private JavaSparkContext sparkCtx;
@Before
public void setUp() throws IllegalArgumentException, IOException {
SparkConf conf = new SparkConf();
conf.setMaster("local[2]");
conf.setAppName("sparkHelperTest");
sparkCtx = new JavaSparkContext(conf);
}
@Test
public void test() {
final List<Integer> nums = new ArrayList<Integer>();
nums.add(3);
nums.add(4);
nums.add(2);
JavaRDD<Integer> rdd = sparkCtx.parallelize(nums,1);
assertEquals(3, rdd.count());
}
}
However I got this error
java.lang.NoClassDefFoundError: com/sun/jersey/spi/container/servlet/ServletContainer
at org.apache.spark.status.api.v1.ApiRootResource$.getServletHandler(ApiRootResource.scala:187)
at org.apache.spark.ui.SparkUI.initialize(SparkUI.scala:70)
at org.apache.spark.ui.SparkUI.<init>(SparkUI.scala:76)
at org.apache.spark.ui.SparkUI$.create(SparkUI.scala:195)
at org.apache.spark.ui.SparkUI$.createLiveUI(SparkUI.scala:146)
at org.apache.spark.SparkContext.<init>(SparkContext.scala:473)
at org.apache.spark.api.java.JavaSparkContext.<init>(JavaSparkContext.scala:59)
at com.firsttry.spark.SparkHelperTest.setUp(SparkHelperTest.java:29)
......
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: java.lang.ClassNotFoundException: com.sun.jersey.spi.container.servlet.ServletContainer
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 30 more
I googled online and basically it is because the jersey version 1 is required. However, I found the jersey v1 was commented in our build.gradle as below
compile (
'io.dropwizard:dropwizard-core:' + dropwizardVersion,
......
)
compile('org.apache.spark:spark-core_2.10:' + sparkVersion) {
exclude group: 'org.apache.hadoop', module: 'hadoop-common'
}
configurations.all {
// all v1 versions of jersey should be excluded. Spark has some of these dependencies
exclude group: 'com.sun.jersey', module: 'jersey-server'
......
}
if I commented this "exclude group: 'com.sun.jersey', module: 'jersey-server'", this error will be gone for unit test. However, after I build the task and run the spark job, I will got another error (note that with this "exclude", the spark job is running fine, so I guess there is a reason people put this exclude there).
However, the mystery is if I simply copy the code in the test() method of the unit testing into the "normal run" code, the spark job is also running fine without requiring this jersey v1 issue. Can any body help, please?