• 粗鲁的关闭(手动):
      使用yarn工具,将spark app 粗鲁地关掉
      ①进入web UI界面,点击app,之后点击 kill application
      ②yarn app -kill appid

      优雅地关闭(自动):
      调用streamingContext.stop()

      核心: ①什么时候调用?
      SparkStreaming开发的app是一个流式应用,需要24h不停地计算,通常不关闭的!
      只有在需要关闭时,才关闭!

      程序怎么知道什么时候需要关闭?
      需要给程序一个信号!

      ②在哪里调用?
      额外启动一个线程,在线程中,判断需要关闭时,再关闭!

    接收信号关闭思维:
    另起一个线程负责监控信号点,接收到信号则stop

    1. package test1
    2. import org.apache.hadoop.conf.Configuration
    3. import org.apache.hadoop.fs.{FileSystem, Path}
    4. import org.apache.spark.streaming.Durations.seconds
    5. import org.apache.spark.streaming.StreamingContext
    6. import org.junit.Test
    7. import java.net.URI
    8. class GracefullCloseApp {
    9. @Test
    10. def test()={
    11. val context = new StreamingContext("local[*]", "GClose", seconds(5))
    12. val ds = context.socketTextStream("hadoop102", 3333)
    13. val ds1 = ds.map((_, 1))
    14. ds1.print(1000)
    15. context.start()
    16. new Thread(){
    17. setDaemon(true)
    18. override def run(): Unit = {
    19. def getSignal ={
    20. val conf = new Configuration
    21. val fileSystem: FileSystem = FileSystem.get(new URI("hdfs://hadoop102:9820"), conf, "atguigu")
    22. !fileSystem.exists(new Path("/GClose"))
    23. }
    24. while (getSignal){
    25. Thread.sleep(5000)
    26. }
    27. println("关闭系统")
    28. context.stop(true,true)
    29. }
    30. }.start()
    31. context.awaitTermination()
    32. }
    33. }