Java Web Submission task to spark

Source: Internet
Author: User

Related software versions:

Spark1.4.1, hadoop2.6,scala2.10.5, Myeclipse2014,intellij idea14,jdk1.8,tomcat7

Machine:

Windows7 (including Jdk1.8,myeclipse2014,intellij IDEA14,TOMCAT7);

centos6.6 Virtual Machine (Hadoop pseudo-distributed cluster, Spark standalone cluster, JDK1.8);

Centos7 virtual Machine (tomcat,jdk1.8);

1. Scene:

1. The Windows Simple Java program calls Spark and executes the Scala-developed Spark program, which contains two modes:

1> submit the task to the spark cluster and execute it using standalone mode;

2> submit the task to the yarn cluster, using the yarn-client mode;

2. The Windows Development Java Web program calls Spark and executes the Scala-developed Spark program, which also contains two modes, reference 1.

3. Linux runs the Java Web program called Spark, which executes the Scala-developed Spark program, which contains two modes, reference 1.


2. Achieve:

1. Simple Scala program, the function of this program is to read the log log file in HDFs, filter log file warn and error records, and finally write the filtered records into HDFs, the code is as follows:

Import Org.apache.spark. {sparkconf, sparkcontext}/** * Created by Administrator on 2015/8/23. */object scala_test {  def main (args:array[string]): Unit ={    if (args.length!=2) {      System.err.println (" Usage:scala_test <input> <output> ")    }    //Initialize sparkconf    val conf = new sparkconf (). Setappname (" Scala filter ")    val sc = new Sparkcontext (conf)    //  read in data    val lines = sc.textfile (args (0))    //Conversion    val errorsrdd = lines.filter (line = + line.contains ("ERROR"))    val warningsrdd = lines.filter (line = Line.contains ("WARN"))    val  badlinesrdd = errorsrdd.union (WARNINGSRDD)    //write Data    Badlinesrdd.saveastextfile (args (1))    //Close sparkconf    sc.stop ()  }}
Use IntelliJ idea and make a jar package backup (LZ named Spark_filter.jar);

2. Java calls the Scala_test file in Spark_filter.jar and uses the Spark standalone mode, the Java code is as follows:

Package Test;import Java.text.simpledateformat;import Java.util.date;import org.apache.spark.deploy.sparksubmit;/* * * @author Fansy * */public class Submitscalajobtospark {public static void main (string[] args) {SimpleDateFormat datefor Mat = new SimpleDateFormat ("Yyyy-mm-dd-hh-mm-ss"); String filename = Dateformat.format (new Date ()); String Tmp=thread.currentthread (). Getcontextclassloader (). GetResource (""). GetPath (); TMP =tmp.substring (0, Tmp.length ()-8); String[] arg0=new string[]{"--master", "spark://node101:7077", "--deploy-mode", "Client", "--name", "Test Java Submit" Job to Spark ","--class "," Scala_test ","--executor-memory "," 1G ",//" Spark_filter.jar ", tmp+" Lib/spark_filter.jar ",// "Hdfs://node101:8020/user/root/log.txt", "Hdfs://node101:8020/user/root/badlines_spark_" +filename}; Sparksubmit.main (arg0);}}

To do this, use MyEclipse to create a new Java Web project and put Spark_filter.jar and Spark-assembly-1.4.1-hadoop2.6.0.jar (the file is in the Lib directory of the spark compressed file, and the file is large and the copy takes a certain amount of time) to copy to the Webroot/web-inf/lib directory. (Note: You can create a Java Web project directly, run Java code directly when testing Java calls, and turn on Tomcat when testing a Web project)

Java calls the Scala_test file in Spark_filter.jar and uses yarn mode. Using yarn mode, you cannot use simple change master to "Yarn-client" or "yarn-cluster", use this when using Spark-shell or Spark-submit, and configure Hadoop_conf_ The dir path is possible, but here, the configuration of Hadoop is not read, so here's another way to use yarn. Clent commits the Java code as follows:

Package Test;import Java.text.simpledateformat;import Java.util.date;import org.apache.hadoop.conf.Configuration; Import Org.apache.spark.sparkconf;import Org.apache.spark.deploy.yarn.client;import Org.apache.spark.deploy.yarn.clientarguments;public class Submitscalajobtoyarn {public static void main (string[] args {SimpleDateFormat DateFormat = new SimpleDateFormat ("Yyyy-mm-dd-hh-mm-ss"); String filename = Dateformat.format (new Date ()); String Tmp=thread.currentthread (). Getcontextclassloader (). GetResource (""). GetPath (); TMP =tmp.substring (0, Tmp.length ()-8); String[] arg0=new string[]{"--name", "Test Java submit job to yarn", "--class", "Scala_test", "--executor-memory", "1G",// "Webroot/web-inf/lib/spark_filter.jar",//"--jar", tmp+ "Lib/spark_filter.jar",//"--arg", "hdfs://node101:8020/ User/root/log.txt ","--arg "," Hdfs://node101:8020/user/root/badlines_yarn_ "+filename,"--addJars "," hdfs://node101 : 8020/user/root/servlet-api.jar ",//"--archives "," Hdfs://node101:8020/user/root/servlet-api.jar "//};//SpaRksubmit.main (arg0); Configuration conf = new configuration (); String OS = System.getproperty ("Os.name"); Boolean cross_platform =false;if (Os.contains ("Windows")) {Cross_platform = true;} Conf.setboolean ("Mapreduce.app-submission.cross-platform", cross_platform);//configuration using cross-platform submit task Conf.set ("Fs.defaultFS" , "hdfs://node101:8020");//Specify Namenodeconf.set ("Mapreduce.framework.name", "yarn"); Specifies the use of yarn frame Conf.set ("yarn.resourcemanager.address", "node101:8032"); Specify Resourcemanagerconf.set ("Yarn.resourcemanager.scheduler.address", "node101:8030");//Specify the resource-allocation Conf.set (" Mapreduce.jobhistory.address "," node101:10020 "); System.setproperty ("Spark_yarn_mode", "true"); sparkconf sparkconf = new sparkconf (); Clientarguments Cargs = new Clientarguments (arg0, sparkconf); new Client (cargs,conf,sparkconf). Run ();}}
3. Java Web Test task submitted to spark two modes, here in the simplest way, directly configure the servlet, its web. xml file is as follows:

<?xml version= "1.0" encoding= "UTF-8"? ><web-app version= "3.0" xmlns= "Http://java.sun.com/xml/ns/javaee" xml Ns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee/http Java.sun.com/xml/ns/javaee/web-app_3_0.xsd "> <servlet> <description>this is the description of my EE    Component</description> <display-name>this is the display name of my EE component</display-name> <servlet-name>SparkServlet</servlet-name> <servlet-class>servlet. Sparkservlet</servlet-class> </servlet> <servlet> <description>this is the description of my Java EE component</description> <display-name>this is the display name of my EE COMPONENT&LT;/DISPLAY-NAME&G    T <servlet-name>YarnServlet</servlet-name> <servlet-class>servlet. yarnservlet</servlet-class> </servlet> <servlet-mapping> &LT;SERVLET-NAME&GT Sparkservlet</servlet-name> <url-pattern>/servlet/SparkServlet</url-pattern> </ servlet-mapping> <servlet-mapping> <servlet-name>YarnServlet</servlet-name> <url-pattern& Gt;/servlet/yarnservlet</url-pattern> </servlet-mapping></web-app>

Sparkservlet as follows:

Package Servlet;import Java.io.ioexception;import Java.io.printwriter;import javax.servlet.servletexception;import Javax.servlet.http.httpservlet;import Javax.servlet.http.httpservletrequest;import Javax.servlet.http.httpservletresponse;import test. Submitscalajobtospark;public class Sparkservlet extends HttpServlet {/** * Constructor of the object. */public Sparkservle T () {super ();} /** * Destruction of the servlet. <br> */public void Destroy () {Super.destroy ();//Just puts "destroy" string in log//Put your code here}/** * the D Oget method of the servlet. <br> * This method was called when a form have its tag value method equals to get. * * @param request the request send by the client to the server * @param response the response send by the server to the Client * @throws servletexception If an error occurred * @throws IOException If an error occurred */public void Doget (Http ServletRequest request, HttpServletResponse response) throws Servletexception, IOException {this.doPost (request, response);} /** * The DoPost method of the servlet. <br> * This method was called when a form have its tag value method equals to post. * * @param request the request send by the client to the server * @param response the response send by the server to the Client * @throws servletexception If an error occurred * @throws IOException If an error occurred */public void DoPost (HTT Pservletrequest request, HttpServletResponse response) throws Servletexception, IOException {System.out.println (" Start Submitscalajobtospark call ... "); Submitscalajobtospark.main (null);//yarnservlet is just a different System.out.println here ("Complete Submitscalajobtospark call! "); Response.setcontenttype (" text/html "); PrintWriter out = Response.getwriter (); Out.println ("<! DOCTYPE HTML public \ "-//W3C//DTD HTML 4.01 transitional//en\" > ") out.println (" <HTML> "); Out.println (" < Head><title>a servlet</title>
This is just a call to the Java-written task invocation class. At the same time, Sparservlet and yarnservlet are just different places to call.

In the Web test, first test directly on MyEclipse, then copy the project Webroot to Centos7, run Tomcat again and test.

3. Summary and question 1. Test results:

1> Java code submits the task directly to spark and yarn, and the log file is filtered, and the test runs successfully. You can see the relevant information in yarn and spark monitoring:



At the same time, the output file can be seen in HDFs:


2> Java Web Submission task to spark and yarn, First, you need to delete the Javax.servlet folder in the Spark-assembly-1.4.1-hadoop2.6.0.jar because it will conflict with Tomcat's Servlet-api.jar.

A. Launch tomcat on Windows and Linux, submit tasks to spark StandAlone, test run successfully;

B. Start tomcat on Windows and Linux, submit the task to yarn, and the test fails;

2. Problems encountered:

1> Java Web Submission task to yarn, will fail with the main log of the failure as follows:

15/08/25 11:35:48 ERROR yarn. Applicationmaster:user class threw exception:java.lang.noclassdeffounderror:javax/servlet/http/ Httpservletresponsejava.lang.noclassdeffounderror:javax/servlet/http/httpservletresponse
This is because Javax.servlet's bag was deleted, and the clash with Tomcat.

Also, you can see in the log:

15/08/26 12:39:27 info client:setting up container launch context for our AM15/08/26 12:39:27 info client:preparing reso Urces for our AM container15/08/26 12:39:27 INFO client:uploading Resource file:/d:/workspase_scala/sparkwebtest/ Webroot/web-inf/lib/spark-assembly-1.4.1-hadoop2.6.0.jar, hdfs://node101:8020/user/administrator/. SPARKSTAGING/APPLICATION_1440464833795_0012/SPARK-ASSEMBLY-1.4.1-HADOOP2.6.0.JAR15/08/26 12:39:32 INFO Client: Uploading Resource File:/d:/workspase_scala/sparkwebtest/webroot/web-inf/lib/spark_filter.jar, hdfs://node101 : 8020/user/administrator/.sparkstaging/application_1440464833795_0012/spark_filter.jar15/08/26 12:39:33 INFO Client:uploading Resource file:/c:/users/administrator/appdata/local/temp/ Spark-46820caf-06e0-4c51-a479-3bb35666573f/__hadoop_conf__5465819424276830228.zip-Hdfs://node101:8020/user /administrator/.sparkstaging/application_1440464833795_0012/__hadoop_conf__5465819424276830228.zip15/08/26 12:39:33 INFO Client:source andDestination file systems is the same. Not copying Hdfs://node101:8020/user/root/servlet-api.jar15/08/26 12:39:33 WARN client:resource hdfs://node101:8020/ User/root/servlet-api.jar added multiple times to distributed cache.

Here, when the environment is initialized, two jars are uploaded, one is Spark-assembly-1.4.1-hadoop2.6.0.jar and one is our custom jar. Uploaded Spark-assembly-1.4.1-hadoop2.6.0.jar Inside no Javax.servlet folder, so will error. In Java directly call (without deleting the Javax.servlet) will also see such a log, the same upload, at that time is possible, that is, it is actually deleted the package folder relationship. So how to fix it?

Upload Servlet-api to HDFs while using yarn. When the client submits the task, add the relevant parameters, see the parameters here, find two more relevant parameters,--addjars and--archive parameters, add these two parameters, see the log actually put this jar package as the job share file, but Java The Web submission task to yarn still reports errors that the class cannot find. So this is not a viable solution!

Use yarn. The client submits the task to yarn reference http://blog.sequenceiq.com/blog/2014/08/22/spark-submit-in-java/.



Share, grow, be happy

Down-to-earth, focus

Reprint Please specify blog address: http://blog.csdn.net/fansy1990


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Java Web Submission task to spark

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.