SparkStreaming
SparkStreaming是流式处理框架,是Spark API的扩展,支持可扩展、高吞吐量、容错的实时数据流处理,实时数据的来源可以是:Kafka, Flume, Twitter, ZeroMQ或者TCP sockets,并且可以使用高级功能的复杂算子来处理流数据。
Flink & Storm & SparkStreaming 区别:
Strom:纯实时处理数据,吞吐量小 --水龙头滴水
SparkStreaming : 准实时处理数据,微批处理数据,吞吐量大 --河道中开闸关闸
Flink:纯实时处理数据,吞吐量大 --河流远远不断
val session = SparkSession.builder.appName("sparkstreaming").master("local[3]").getOrCreate()
val streamingContext = new StreamingContext(session.sparkContext, Durations.seconds(5))
val ds1: ReceiverInputDStream[String] = streamingContext.socketTextStream("node14", 9999)
val dStream: DStream[(String, Int)] = ds1.flatMap(_.split(" ")).map((_, 1)).reduceByKey(_+_)
dStream.print()
streamingContext.start()
streamingContext.awaitTermination()
streamingContext.stop()
foreachRDD 注意事项
1.foreachRDD中可以拿到DStream中的RDD,对RDD进行操作,但是一点要使用RDD的action算子触发执行,不然DStream的逻辑也不会执行
2.froeachRDD算子内,拿到的RDD算子操作外,这段代码是在Driver端执行的,可以利用这点做到动态的改变广播变量
result.foreachRDD(wordCountRDD=>{
println("******* produce in Driver *******")
val sortRDD: RDD[(String, Int)] = wordCountRDD.sortByKey(false)
val result: RDD[(String, Int)] = sortRDD.filter(tp => {
println("******* produce in Executor *******")
true
result.foreach(println)
})
updateStateByKey:对之前的数据进行汇总统计
streamingContext.checkpoint("./data/ck")
val ds1: ReceiverInputDStream[String] = streamingContext.socketTextStream("node14", 9999)
val pairWords: DStream[(String, Int)] = ds1.flatMap(_.split(" ")).map((_, 1))
/**
* currentValue :当前批次某个 key 对应所有的value 组成的一个集合
* preValue : 以往批次当前key 对应的总状态值
*/
val result: DStream[(String, Int)] = pairWords.updateStateByKey((currentValue: Seq[Int], preValue: Option[Int]) => {
var totalValue = preValue.getOrElse(0)
for (v <- currentValue) {
totalValue += v
}
Option(totalValue)
})
result.print()
streamingContext.start()
streamingContext.awaitTermination()
reduceByKeyAndWindow:窗口函数
//没有优化的窗口函数
//第一个参数:处理的函数(v1:Int,v2:Int)=>{v1+v2}
//第二个参数:窗口长度
//第三个阐述:滑动间隔
val result: DStream[(String, Int)] = pairWords.reduceByKeyAndWindow(
(v1:Int, v2:Int)=>{v1+v2}, Durations.seconds(15), Durations.seconds(5))
result.print()
//优化的窗口函数(会将状态保存)
//第一个参数:加上新增的(v1:Int,v2:Int)=>{v1+v2}
//第二个参数:减去过期的(v1:Int,v2:Int)=>{v1-v2}
//第三个参数:窗口长度
//第四个阐述:滑动间隔
val result2: DStream[(String, Int)] = pairWords.reduceByKeyAndWindow(
(v1:Int, v2:Int)=>{v1+v2}, (v1:Int, v2:Int)=>{v1-v2},
Durations.seconds(15), Durations.seconds(5))
result2.print()
transform:进入一个RDD,返回一个RDD
val transformRDD = lines.transform(rdd => {
println("------------即将执行filter-----------")
val transformRDD: RDD[String] = rdd.filter(line => {
println("####执行filter####")
!"zhangsan".equals(line.split(" ")(1))
})
transformRDD.map(line => {
(line.split(" ")(1), 1)
})
})
SparkStreaming监控目录
val streamingContext: StreamingContext = new StreamingContext(conf, Durations.seconds(5))
streamingContext.sparkContext.setLogLevel("error")
streamingContext.checkpoint("./data/ck")
val lines: DStream[String] = streamingContext.textFileStream("./data/copyStreamingFile")
val pairRDD = lines.flatMap(line => {
line.trim.split(" ")
}).map((_, 1))
val result: DStream[(String, Int)] = pairRDD.reduceByKeyAndWindow((v1: Int, v2: Int) => {
v1 + v2
}, (v1: Int, v2: Int) => {
v1 - v2
},
Durations.seconds(15), Durations.seconds(5))
result.print()
Driver HA
第一:提交任务层面,在提交任务的时候加上选项 --supervise,当Driver挂掉的时候会自动重启Driver。
第二:代码层面
//若能获取回来StreamingContext,就不会执行CreateStreamingContext这个方法创建,否则就会创建
//这个方法首先会从ckDir目录中获取StreamingContext【 因为StreamingContext是序列化存储在Checkpoint目录中,恢复时会尝试反序列化这些objects。
//如果用修改过的class可能会导致错误,此时需要更换checkpoint目录或者删除checkpoint目录中的数据,程序才能起来
val streamingContext = StreamingContext.getOrCreate(ckDir, createStreamingContext)
streamingContext.start()
streamingContext.awaitTermination()
streamingContext.stop()
def createStreamingContext()={
println("=======Create new StreamingContext =======")
val conf = new SparkConf()
conf.setMaster("local")
conf.setAppName("DriverHA")
val streamingContext: StreamingContext = new StreamingContext(conf,Durations.seconds(5))
streamingContext.sparkContext.setLogLevel("Error")
/**
* 默认checkpoint 存储:
* 1.配置信息
* 2.DStream操作逻辑
* 3.job的执行进度
* 4.offset
*/
streamingContext.checkpoint(ckDir)
Spark优化: https://blog.csdn.net/happiless/article/details/107308772