简体   繁体   English

在同一JVM中检测到多个SparkContext

[英]Multiple SparkContext detected in the same JVM

according my last question I have to define the Multiple SparkContext for my unique JVM. 根据我的上一个问题,我必须为我独特的JVM定义Multiple SparkContext。

I did it in the next way (using Java): 我是用下一种方式做的(使用Java):

SparkConf conf = new SparkConf();
conf.setAppName("Spark MultipleContest Test");
conf.set("spark.driver.allowMultipleContexts", "true");
conf.setMaster("local");

After that I create the next source code: 之后我创建了下一个源代码:

SparkContext sc = new SparkContext(conf);
SQLContext sqlContext = new org.apache.spark.sql.SQLContext(sc);

and later in the code: 后来在代码中:

JavaSparkContext ctx = new JavaSparkContext(conf);
JavaRDD<Row> testRDD = ctx.parallelize(AllList);

After the code executing I got next error message: 代码执行后,我得到了下一条错误消息:

16/01/19 15:21:08 WARN SparkContext: Multiple running SparkContexts detected in the same JVM!
org.apache.spark.SparkException: Only one SparkContext may be running in this JVM (see SPARK-2243). To ignore this error, set spark.driver.allowMultipleContexts = true. The currently running SparkContext was created at:
org.apache.spark.SparkContext.<init>(SparkContext.scala:81)
test.MLlib.BinarryClassification.main(BinaryClassification.java:41)
    at org.apache.spark.SparkContext$$anonfun$assertNoOtherContextIsRunning$1.apply(SparkContext.scala:2083)
    at org.apache.spark.SparkContext$$anonfun$assertNoOtherContextIsRunning$1.apply(SparkContext.scala:2065)
    at scala.Option.foreach(Option.scala:236)
    at org.apache.spark.SparkContext$.assertNoOtherContextIsRunning(SparkContext.scala:2065)
    at org.apache.spark.SparkContext$.setActiveContext(SparkContext.scala:2151)
    at org.apache.spark.SparkContext.<init>(SparkContext.scala:2023)
    at org.apache.spark.api.java.JavaSparkContext.<init>(JavaSparkContext.scala:61)
    at test.MLlib.BinarryClassification.main(BinaryClassification.java:105)

The numbers 41 and 105 are the lines, where both objects are defined in Java code. 数字41105是行,其中两个对象都是用Java代码定义的。 My question is, is it possible to execute multiple SparkContext on the same JVM and how to do it, if I already use the set -method ? 我的问题是,如果我已经使用set -method,是否可以在同一个JVM上执行多个SparkContext以及如何执行它?

Are you sure you need the JavaSparkContext as a separate context? 您确定需要将JavaSparkContext作为单独的上下文吗? The previous question that you refer to doesn't say so. 你提到的上一个问题并没有这么说。 If you already have a Spark Context you can create a new JavaSparkContext from it, rather than create a separate context: 如果您已有Spark Context,则可以从中创建新的JavaSparkContext,而不是创建单独的上下文:

SparkConf conf = new SparkConf();
conf.setAppName("Spark MultipleContest Test");
conf.set("spark.driver.allowMultipleContexts", "true");
conf.setMaster("local");

SparkContext sc = new SparkContext(conf);
SQLContext sqlContext = new org.apache.spark.sql.SQLContext(sc);

//Create a Java Context which is the same as the scala one under the hood
JavaSparkContext.fromSparkContext(sc)

默认情况下SparkContext正在运行,所以你必须停止这个上下文:sc.stop然后你可以继续没有任何pb

Rather than using SparkContext you should use builder method on SparkSession which more roubustly instantiates the spark and SQL context and ensures that there is not context conflict. 您应该在SparkSession上使用builder方法,而不是使用SparkContext ,它更加粗略地实例化spark和SQL上下文,并确保不存在上下文冲突。

import org.apache.spark.sql.SparkSession
val spark = SparkSession.builder().appName("demo").getOrCreate()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM