How to specify different JDK versions for Spark Application

Source: Internet
Author: User

How to specify different JDK versions for Spark Application

Preface

With the increasing number of internal business systems and JVM-based services, multiple JDK sets may run different services in the online environment. As we all know, services written based on the higher Java specification run on the lower version of JVM:java.lang.UnsupportedClassVersionError.

Spark 2.2 began to remove support for Java 7. In most cases, our Spark Application is a JDK public to the Hadoop system. If Hadoop depends on JDK 7, then we run the Application compiled based on JDK 8 and there will be problems.

This article describes how to specify different JDK versions for Spark Application in different scenarios.

The specified JDK version has been deployed in the cluster.

Assume that the JDK deployment path for each node in the cluster is/usr/java/jdk1.8.

Spark providesspark.executorEnv.[EnvironmentVariableName]Configuration, which can be used to add environment variables to the Executor process. If the Cluster Manager used by Spark Application is Standalone, you only needspark.executorEnv.JAVA_HOMEMake the jdk path for the Executor end as follows:

$SPARK_HOME/bin/spark-submit \ --conf "spark.executorEnv.JAVA_HOME=/usr/java/jdk1.8" \ ...

In YARN mode, you also need to specify different JAVA_HOME environment variables for the Application Master, as follows:

$SPARK_HOME/bin/spark-submit \ --conf "spark.executorEnv.JAVA_HOME=/usr/java/jdk1.8" \ --conf "spark.yarn.appMasterEnv.JAVA_HOME=/usr/java/jdk1.8" \ ...

When deployed on YARN as a cluster,spark.yarn.appMasterEnv.JAVA_HOMEIt is equivalent to setting a specific JDK version for the Driver of Spark Application;

When deployed in client mode,spark.yarn.appMasterEnv.JAVA_HOMEOnly a specific JDK version is set for Executor Launcher.

The JDK version on the Driver side is the same as the SPARK_HOME environment variable on the machine where spark-submit is located, and you just need to specify it in the spark-env.sh.

The cluster lacks a specific JDK version and has no management permission on the cluster.

In some special scenarios, we do not have the management permission for the cluster. We can only submit the Application through YARN, and the JDK version we need is not deployed in the cluster, in this case, you need to submit the JDK installation package together.

Here we require that our JDK installation package must be in the gz format, and your code package jar package in the same directory, suppose we download the JDK installation package: jdk-8u141-linux-x64.tar.gz.

The key configurations are as follows:

$SPARK_HOME/bin/spark-submit \ --conf "spark.yarn.dist.archives=jdk-8u141-linux-x64.tar.gz" \ --conf "spark.executorEnv.JAVA_HOME=./jdk-8u141-linux-x64.tar.gz/jdk1.8.0_141" \ --conf "spark.yarn.appMasterEnv.JAVA_HOME=./jdk-8u141-linux-x64.tar.gz/jdk1.8.0_141" \ ...

You can specifyspark.yarn.dist.archivesConfigure and distribute the JDK installation package to the working directory of all Executors (including the directory jdk1.8.0 _ 141 after the Application handler is decompressed, then our specific JDK directory is :. /jdk-8u141-linux-x64.tar.gz/jdk1.8.0 _ 141, different JDK versions and so on.

Note:Spark Standalone does not provide distribution of JDK installation packages and automatic decompression, so this method can only be used in YARN.

Verify

You can use ps-ef grep to query related process information. The startup path of java is java in our specific JDK directory, indicating that the configuration is successful.

In YARN mode, the process startup information of Executor of JDK is specified as follows:

stan  590751 590745 0 20:45 ?  00:00:14 ./jdk-8u141-linux-x64.tar.gz/jdk1.8.0_141/bin/java -server -Xmx512m -XX:+UseG1GC -XX:+UnlockDiagnosticVMOptions -XX:+G1SummarizeConcMark -XX:InitiatingHeapOccupancyPercent=35 -XX:PermSize=256M -XX:+PrintGCDetails -XX:+PrintGCDateStamps -Xloggc:./gc.log -verbose:gc -Djava.io.tmpdir=/home/stan/tmp/hadoop-stan/nm-local-dir/usercache/stan/appcache/application_1508397483453_0095/container_1508397483453_0095_01_000004/tmp -Dspark.driver.port=52986 -Dspark.yarn.app.container.log.dir=/home/stan//hadoop-2.6.4/logs/userlogs/application_1508397483453_0095/container_1508397483453_0095_01_000004 -XX:OnOutOfMemoryError=kill %p org.apache.spark.executor.CoarseGrainedExecutorBackend --driver-url spark://CoarseGrainedScheduler@10.0.0.110:52986 --executor-id 3 --hostname stan --cores 1 --app-id application_1508397483453_0095 --user-class-path file:/home/stan/tmp/hadoop-stan/nm-local-dir/usercache/stan/appcache/application_1508397483453_0095/container_1508397483453_0095_01_000004/__app__.jar

Appendix: solutions to incompatibility errors during spark application running

17/06/27 14:34:41 INFO deprecation: mapred.map.tasks is deprecated. Instead, use mapreduce.job.maps 17/06/27 14:34:41 INFO MemoryStore: Block broadcast_0 stored as values in memory (estimated size 788.8 KB, free 1246.5 MB) 17/06/27 14:34:41 INFO MemoryStore: Block broadcast_0_piece0 stored as bytes in memory (estimated size 54.0 KB, free 1246.4 MB) 17/06/27 14:34:41 INFO BlockManagerInfo: Added broadcast_0_piece0 in memory on 10.50.70.121:37335 (size: 54.0 KB, free: 1247.2 MB) 17/06/27 14:34:41 INFO SparkContext: Created broadcast 0 from rdd at TradeInfoOutlier.scala:30 Exception in thread "main" java.lang.NoSuchMethodError: scala.reflect.api.JavaUniverse.runtimeMirror(Ljava/lang/ClassLoader;)Lscala/reflect/api/JavaUniverse$JavaMirror; at com.fangdd.data.profile.outlier.TradeInfoOutlier$.main(TradeInfoOutlier.scala:30) at com.fangdd.data.profile.outlier.TradeInfoOutlier.main(TradeInfoOutlier.scala) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.apache.spark.deploy.SparkSubmit$.org$apache$spark$deploy$SparkSubmit$$runMain(SparkSubmit.scala:745) at org.apache.spark.deploy.SparkSubmit$.doRunMain$1(SparkSubmit.scala:181) at org.apache.spark.deploy.SparkSubmit$.submit(SparkSubmit.scala:206) at org.apache.spark.deploy.SparkSubmit$.main(SparkSubmit.scala:121) at org.apache.spark.deploy.SparkSubmit.main(SparkSubmit.scala) 17/06/27 14:34:42 INFO SparkContext: Invoking stop() from shutdown hook 

This error occurs because the production environment uses the scala 2.10 + spark1.6.3 runtime environment and the local application jar uses the scala2.11 + spark.1.6.3 compiling environment. Therefore, the preceding error is reported when the cluster is added to the production environment, changed scala version and re-typed the jar package to run successfully.

Summary

The above is all the content of this article. I hope the content of this article has some reference and learning value for everyone's learning or work. If you have any questions, please leave a message to us, thank you for your support.

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.