问题描述
我正在尝试建立一个Sparkstreaming代码,该代码从Kafka服务器读取行,但使用另一个本地文件中编写的规则对其进行处理.我正在为流数据创建streamingContext,并为其他应用所有其他spark功能(例如字符串操作,读取本地文件等)的sparkContext创建
I am trying to set up a Sparkstreaming code which reads line from the Kafka server but processes it using rules written in another local file. I am creating streamingContext for the streaming data and sparkContext for other applying all other spark features - like string manipulation, reading local files etc
val sparkConf = new SparkConf().setMaster("local[*]").setAppName("ReadLine")
val ssc = new StreamingContext(sparkConf, Seconds(15))
ssc.checkpoint("checkpoint")
val topicMap = topics.split(",").map((_, numThreads.toInt)).toMap
val lines = KafkaUtils.createStream(ssc, zkQuorum, group, topicMap).map(_._2)
val sentence = lines.toString
val conf = new SparkConf().setAppName("Bi Gram").setMaster("local[2]")
val sc = new SparkContext(conf)
val stringRDD = sc.parallelize(Array(sentence))
但这会引发以下错误
Exception in thread "main" 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:82)
org.apache.spark.streaming.StreamingContext$.createNewSparkContext(StreamingContext.scala:874)
org.apache.spark.streaming.StreamingContext.<init>(StreamingContext.scala:81)
推荐答案
一个应用程序只能有一个SparkContext
. StreamingContext
在SparkContext
上创建.只需使用SparkContext创建ssc StreamingContext
One application can only have ONE SparkContext
. StreamingContext
is created on SparkContext
. Just need to create ssc StreamingContext using SparkContext
val sc = new SparkContext(conf)
val ssc = new StreamingContext(sc, Seconds(15))
如果使用以下构造函数.
If using the following constructor.
StreamingContext(conf: SparkConf, batchDuration: Duration)
它在内部创建另一个SparkContext
this(StreamingContext.createNewSparkContext(conf), null, batchDuration)
SparkContext
可以通过
ssc.sparkContext
这篇关于SparkContext和StreamingContext可以在同一程序中共存吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!