http://blog.sequenceiq.com/blog/2014/08/22/spark-submit-in-java/
In our previous Apache Spark related post we showed you how to write a simple machine learning job. In this post we’d like to show you how to submit a Spark job from code. At SequenceIQ we submit jobs to different clusters – based on load, customer profile, associated SLAs, etc. Doing this the
way was cumbersome so we needed a way to submit Spark jobs (and in general all of our jobs running in a YARN cluster) from code. Also due to the clusters, and changing job configurations we can’t use hardcoded parameters – in a previous blog post we highlighted how are we doing all these.Basically as you from the Spark documentation, you have to use the spark-submit script to submit a job. In nutshell SparkSubmit is called by the spark-class script with a lots of decorated arguments. In our example we examine only the YARN part of the submissions. As you can see in SparkSubmit.scala the YARN Client is loaded and its main method invoked (based on the arguments of the script).
It’s a pretty straightforward way to submit a Spark job to a YARN cluster, though you will need to change manually the parameters which as passed as arguments.
In case if you would like to submit a job to YARN from Java code, you can just simply use this Client class directly in your application. (but you have to make sure that every environment variable what you will need is set properly).
In the main method the org.apache.hadoop.conf.Configuration object is not passed to the Client class. A
is created explicitly in the constructor, which is actually okay (then client configurations are loaded from $HADOOP_CONF_DIR/core-site.xml and $HADOOP_CONF_DIR/yarn-site.xml). But what if you want to use (for example) an Ambari Configuration Service for retrieve your configuration, instead of using hardcoded ones?Fortunately, the configuration can be passed here (there is a
field in the Client), but you have to write your own main method.In our example we also use the 2 client XMLs as configuration (for demonstration purposes only), the main difference here is that we read the properties from the XMLs and filling them in the Configuration. Then we pass the Configuration object to the Client (which is directly invoked here).
To build the project use this command from the spark-submit directory:
After building it you find the required jars in spark-submit-runner/build/libs (
with all required dependencies) and spark-submit-app/build/libs. Put them in the same directory (do this also with this config folder too). After that run this command:During the submission note that: not just the app jar, but the spark-submit-runner jar is also uploaded (which is an SPARK_JAR environment variable.
) to the HDFS. To avoid this, you have to upload it to the HDFS manually and set theIf you get “Permission denied” exception on submit, you should set the HADOOP_USER_NAME environment variable to root (or something with proper rights).
As usual for us we ship the code – you can get it from our GitHub samples repository; the sample input is available here.
If you would like to play with Spark, you can use our Spark Docker container available as a trusted build on Docker.io repository.
For updates follow us on LinkedIn, Twitter or Facebook.