一、StreamingContext
初始化一个Spark Streaming程序时必须要创建StreamingContext作为程序的入口。
example:
import org.apache.spark._
import org.apache.spark.streaming._
import org.apache.spark.streaming.StreamingContext._ // not necessary since Spark 1.3
// Create a local StreamingContext with two working thread and batch interval of 1 second.
// The master requires 2 cores to prevent a starvation scenario.
val conf = new SparkConf().setMaster("local[2]").setAppName("NetworkWordCount")
val ssc = new StreamingContext(conf, Seconds(1)) //Seconds(1)设定切分的批次,必须设置,多久执行一次,根据需求和集群资源确定
二、StreamingContext的使用
一旦StreamingContext定义好以后,就可以做如下的事情
1定义输入源通过创建输入DStreams
2.定义流的操作使用transformation输出操作到Dsteams
3.开始接收数据和进行启动streamingContext.start()
4.等待进程的停止streamingContext.awaitTermination()或者手动停止streamingContext.stop().
注意事项:
StreamingContext启动后,新的流计算将部能被添加设置
StreamingContext停止在后,不能重启,可以把整个作业停掉,在重启。
只有一个StreamingContext被激活在JVM同一个时间点