大数据全系列 教程
1869个小节阅读:467.2k
目录
408考研
JAVA全系列 教程
面向对象的程序设计语言
Python全系列 教程
Python3.x版本,未来主流的版本
人工智能 教程
顺势而为,AI创新未来
大厂算法 教程
算法,程序员自我提升必经之路
C++ 教程
一门通用计算机编程语言
微服务 教程
目前业界流行的框架组合
web前端全系列 教程
通向WEB技术世界的钥匙
大数据全系列 教程
站在云端操控万千数据
AIGC全能工具班
A A
White Night
window(windowLength, slideInterval): 基于对源DStream窗化的批次进行计算返回一个新的Dstream
xxxxxxxxxx
package com.itbaizhan.streaming
import org.apache.spark.broadcast.Broadcast
import org.apache.spark.streaming.dstream.{DStream, ReceiverInputDStream}
import org.apache.spark.streaming.{Durations, StreamingContext}
import org.apache.spark.{SparkConf, SparkContext}
object WindowDemo {
def main(args: Array[String]): Unit = {
val conf = new SparkConf()
conf.setMaster("local[2]")
conf.setAppName("test")
val sc = new SparkContext(conf)
sc.setLogLevel("Error")
val ssc = new StreamingContext(sc,Durations.seconds(5))
val lines: ReceiverInputDStream[String] = ssc.socketTextStream("node1",9999)
//黑名单列表
val blackList = List[String]("zhangsan","lisi","wangwu")
//将本地集合变量定义为一个广播变量
val broadcast: Broadcast[List[String]] = sc.broadcast(blackList)
val windowDs: DStream[String] = lines.window(Durations.seconds(15),Durations.seconds(5))
//输入内容 hello namexx
val result: DStream[String] = windowDs.filter(one => {
val blackNameList: List[String] = broadcast.value
!blackNameList.contains(one.split(" ")(1))
})
result.print()
ssc.start()
ssc.awaitTermination()
}
}
测试
在node1上
xxxxxxxxxx
[root@node1 ~]# nc -lk 9999
在IDEA中运行程序
在node1上
xxxxxxxxxx
[root@node1 ~]# nc -lk 9999
hello tuhao
hello zhangsan
hello diaosi
hello lisi
查看IDEA控制台