There is no difference between it and Core, that is, foreachRDD is called. The following is an example of stream processing to save wordcount to mysql
object TestStream2 {
def main(args: Array[String]): Unit = {
val conf = new SparkConf().setMaster("local[2]").setAppName("NetworkWordCount")
val ssc = new StreamingContext(conf, Seconds(5))
ssc.sparkContext.setLogLevel("WARN")
val dataDS: ReceiverInputDStream[String] = ssc.socketTextStream("192.168.182.147",9998)
val wordDS: DStream[String] = dataDS.flatMap(_.split(" "))
val tupleDS: DStream[(String, Int)] = wordDS.map((_,1))
tupleDS.reduceByKey(_+_).foreachRDD(rdd=>{
rdd.foreach(word=>{
Class.forName("com.mysql.jdbc.Driver")
//获取mysql连接
val conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "")
//把数据写入mysql
try {
var totalcount = word._2
var sql = "";
var querysql="select count from wordcount where titleName='"+word._1+"'"
val queryresult: ResultSet = conn.prepareStatement(querysql).executeQuery()
if(queryresult.next()){
totalcount = queryresult.getString("count").toInt+word._2
sql = "update wordcount set count='"+totalcount+"' where titleName='"+word._1+"'"
}else{
sql = "insert into wordcount (titleName,count) values ('" + word._1 + "','" + totalcount + "')"
}
conn.prepareStatement(sql).executeUpdate()
println("保存结束--------------------------------------------------------------")
} finally {
conn.close()
}
})
})
ssc.start()
ssc.awaitTermination()
}
}