在使用SparkStreaming時,使用StreamingContext.getOrCreate(checkpointDirectory, functionToCreateContext _)創建StreamingContext。代碼示例如下:
// Function to create and setup a new StreamingContext
def functionToCreateContext(): StreamingContext = {
val conf = new SparkConf().setAppName("UserBrowse")
val ssc = new StreamingContext(conf, batchInterval)
//通過LogHubCursorPosition.BEGIN_CURSOR指定消費Cursor的模式。
val loghubStream = LoghubUtils.createStream(...)
loghubStream.checkpoint(batchInterval * 5).foreachRDD { rdd =>
val spark = SparkSession.builder.config(rdd.sparkContext.getConf)
.config("spark.serializer", "org.apache.spark.serializer.KryoSerializer")
.getOrCreate()
}
ssc.checkpoint(checkpointDirectory) // set checkpoint directory
ssc
}
// Get StreamingContext from checkpoint data or create a new one
val context = StreamingContext.getOrCreate(checkpointDirectory, functionToCreateContext _)
此時通過控制檯提交Spark任務命令如下:
--class com.test.StreamingText
--jars /spark-it/loghub-spark-0.6.13_2.4.3-1.0.4.jar,/spark-it/loghub-client-lib-0.6.13.jar
--driver-memory 1G
--driver-cores 1
--executor-cores 3
--executor-memory 3G
--num-executors 1
--name spark_on_loghub
/spark-it/sparkstreaming-0.0.1-SNAPSHOT.jar
/tmp/checkpoint_location_test
其中/tmp/checkpoint_location_test 爲StreamingContext的checkpoint路徑。
運行一段時間後,用戶期望修改executor-cores爲4,executor-memory 爲12G,num-executors爲3。那如何修改呢?
由於SparkStreming的運行機制是長久運行,以及checkpoint的設置是爲了任務異常能從checkpoint恢復數據。
首次提交任務後,StremingContext會把Spark的配置信息寫入到Checkpoint中,包括:executor-cores、num-executors、executor-memory等配置信息。
當任務異常或者重啓後,StremingContext會從Checkpoint中讀取Spark的配置信息。所以這時如果在控制檯修改executor-cores、executor-memory等配置信息,StremingContext不會讀取的。
如果需要修改executor-cores、executor-memory等配置信息需要清除Checkpoint路徑,或者重新指定一個新的Checkpoint路徑。