How to build Spark source code and run its built-in example in IntelliJ
on 10/3 2017
One way to dig and debug into the Spark's codebase is to build Spark source code and run its built-in examples (e.g., LogQuery and GroupByTest) in IntelliJ. This gives you full control over Spark source code. For example, you can hack into the codebase such as adding your code on top of Spark, adding breakpoints and observing code behavior. However, setting up Spark sourcecode and running its example in IntelliJ is a little bit tricky. This tutorial shows you how to build Spark source code and run its LogQuery example in local mode in IntelliJ.
- IntelliJ with Scala Plugin and ANTLR Plugin installed
- Clone Spark repo (the spark release version at the time of writing is 2.2.0)
git clone email@example.com:apache/spark.git
- Open IntelliJ and import downloaded Spark project folder
- Select Maven when asking for Import project from external mode
- Select Import Maven projects automatically
- For the rest of import configurations, use default configurations by simply clicking Next
- Go to Terminal, and run following commands to compile Spark
./build/mvn -DskipTests clean package
- Go back to IntelliJ and navigate to File -> Project Structure. Then, select spark-streaming-flume-sink module and set scala-2.11 -> src_manages -> * (in target folder) to Sources while excluding others in this path on the right panel as shown below
- In IntelliJ, go to View -> Tool Windows -> Maven Project and click Generate Sources and Update Folders for All Projects
- Again, navigate back to File -> Project Structure. Then, select spark-catalyst module and set generated_sources -> antlr4 -> * (in target ->generated_sources folder) to Sources while excluding others in this path on the right panel as shown below
- Go to pom.xml file under examples module and comment out <provided> under properties and <scope>provided</scope> under dependency such as following
- Navigate back to File -> Project Structure and select spark-examples module. On the right panel, click dependencies tab. Change provided scope into compile for all dependencies.
- In IntelliJ, go to Run -> Edit Configurations and create a new application with the following configuration (note the VM options: -DSpark.master=local is required)
- The final step is to run created application
- If you encounter Exception in thread "main" java.lang.NoClassDefFoundError when you run the application, double check that no dependency is in provided scope, as described in step 6 and 7 above.