本期內(nèi)容:
成都創(chuàng)新互聯(lián)公司專(zhuān)注于麗水網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗(yàn)。 熱誠(chéng)為您提供麗水營(yíng)銷(xiāo)型網(wǎng)站建設(shè),麗水網(wǎng)站制作、麗水網(wǎng)頁(yè)設(shè)計(jì)、麗水網(wǎng)站官網(wǎng)定制、微信平臺(tái)小程序開(kāi)發(fā)服務(wù),打造麗水網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供麗水網(wǎng)站排名全網(wǎng)營(yíng)銷(xiāo)落地服務(wù)。
1. Spark Streaming中RDD為空處理
2. Streaming Context程序停止方式
Spark Streaming運(yùn)用程序是根據(jù)我們?cè)O(shè)定的Batch Duration來(lái)產(chǎn)生RDD,產(chǎn)生的RDD存在partitons數(shù)據(jù)為空的情況,但是還是會(huì)執(zhí)行foreachPartition,會(huì)獲取計(jì)算資源,然后計(jì)算一下,這種情況就會(huì)浪費(fèi)
集群計(jì)算資源,所以需要在程序運(yùn)行的時(shí)候進(jìn)行過(guò)濾,參考如下代碼:
package com.dt.spark.sparkstreaming
import org.apache.spark.SparkConf
import org.apache.spark.streaming.{Seconds, StreamingContext}
object OnlineForeachRDD2DB {
def main(args: Array[String]){
val conf = new SparkConf() //創(chuàng)建SparkConf對(duì)象
conf.setAppName("OnlineForeachRDD2DB") //設(shè)置應(yīng)用程序的名稱(chēng),在程序運(yùn)行的監(jiān)控界面可以看到名稱(chēng)
conf.setMaster("spark://Master:7077") //此時(shí),程序在Spark集群
/**
* 設(shè)置batchDuration時(shí)間間隔來(lái)控制Job生成的頻率并且創(chuàng)建Spark Streaming執(zhí)行的入口
*/
val ssc = new StreamingContext(conf, Seconds(300))
val lines = ssc.socketTextStream("Master", 9999)
val words = lines.flatMap(line => line.split(" "))
val wordCounts = words.map(word => (word,1)).reduceByKey(_ + _)
wordCounts.foreachRDD{ rdd =>
/**
* 例如:rdd為空,rdd為空會(huì)產(chǎn)生什么問(wèn)題呢?
* rdd沒(méi)有任何元素,但是也會(huì)做做foreachPartition,也會(huì)進(jìn)行寫(xiě)數(shù)據(jù)庫(kù)的操作或者把數(shù)據(jù)寫(xiě)到HDFS上,
* rdd里面沒(méi)有任何記錄,但是還會(huì)獲取計(jì)算資源,然后計(jì)算一下,消耗計(jì)算資源,這個(gè)時(shí)候純屬浪費(fèi)資源,
* 所以必須對(duì)空rdd進(jìn)行處理;
* 例如:使用rdd.count()>0,但是rdd.count()會(huì)觸發(fā)一個(gè)Job;
* 使用rdd.isEmpty()的時(shí)候,take也會(huì)觸發(fā)Job;
* def isEmpty(): Boolean = withScope {
* partitions.length == 0 || take(1).length == 0
* }
*
* rdd.partitions.isEmpty里判斷的是length是否等于0,就代表是否有partition
* def isEmpty: Boolean = { length == 0 }
* 注:rdd.isEmpty()和rdd.partitions.isEmpty是兩種概念;
*/
//
if(rdd.partitions.length > 0) {
rdd.foreachPartition{ partitonOfRecord =>
if(partitionOfRecord.hasNext) // 判斷下partition中是否存在數(shù)據(jù)
{
val connection = ConnectionPool.getConnection()
partitonOfRecord.foreach(record => {
val sql = "insert into streaming_itemcount(item,rcount) values('" + record._1 + "'," + record._2 + ")"
val stmt = connection.createStatement()
stmt.executeUpdate(sql)
stmt.close()
})
ConnectionPool.returnConnection(connection)
}
}
}
}
ssc.start()
ssc.awaitTermination()
}
}
二、SparkStreaming程序停止方式
第一種是不管接受到數(shù)據(jù)是否處理完成,直接被停止掉。
第二種是接受到數(shù)據(jù)全部處理完成才停止掉,一般采用第二種方式。
第一種停止方式:
/**
* Stop the execution of the streams immediately (does not wait for all received data
* to be processed). By default, if `stopSparkContext` is not specified, the underlying
* SparkContext will also be stopped. This implicit behavior can be configured using the
* SparkConf configuration spark.streaming.stopSparkContextByDefault.
*
* 把streams的執(zhí)行直接停止掉(并不會(huì)等待所有接受到的數(shù)據(jù)處理完成),默認(rèn)情況下SparkContext也會(huì)被停止掉,
* 隱式的行為可以做配置,配置參數(shù)為spark.streaming.stopSparkContextByDefault。
*
* @param stopSparkContext If true, stops the associated SparkContext. The underlying SparkContext
* will be stopped regardless of whether this StreamingContext has been
* started.
*/
def stop(stopSparkContext: Boolean = conf.getBoolean("spark.streaming.stopSparkContextByDefault", true)
): Unit = synchronized {
stop(stopSparkContext, false)
}
第二種停止方式:
/**
* Stop the execution of the streams, with option of ensuring all received data
* has been processed.
*
* 所有接受到的數(shù)據(jù)全部被處理完成,才把streams的執(zhí)行停止掉
*
* @param stopSparkContext if true, stops the associated SparkContext. The underlying SparkContext
* will be stopped regardless of whether this StreamingContext has been
* started.
* @param stopGracefully if true, stops gracefully by waiting for the processing of all
* received data to be completed
*/
def stop(stopSparkContext: Boolean, stopGracefully: Boolean): Unit = {
var shutdownHookRefToRemove: AnyRef = null
if (AsynchronousListenerBus.withinListenerThread.value) {
throw new SparkException("Cannot stop StreamingContext within listener thread of" +
" AsynchronousListenerBus")
}
synchronized {
try {
state match {
case INITIALIZED =>
logWarning("StreamingContext has not been started yet")
case STOPPED =>
logWarning("StreamingContext has already been stopped")
case ACTIVE =>
scheduler.stop(stopGracefully)
// Removing the streamingSource to de-register the metrics on stop()
env.metricsSystem.removeSource(streamingSource)
uiTab.foreach(_.detach())
StreamingContext.setActiveContext(null)
waiter.notifyStop()
if (shutdownHookRef != null) {
shutdownHookRefToRemove = shutdownHookRef
shutdownHookRef = null
}
logInfo("StreamingContext stopped successfully")
}
} finally {
// The state should always be Stopped after calling `stop()`, even if we haven't started yet
state = STOPPED
}
}
if (shutdownHookRefToRemove != null) {
ShutdownHookManager.removeShutdownHook(shutdownHookRefToRemove)
}
// Even if we have already stopped, we still need to attempt to stop the SparkContext because
// a user might stop(stopSparkContext = false) and then call stop(stopSparkContext = true).
if (stopSparkContext) sc.stop()
}
本文題目:(版本定制)第18課:SparkStreaming中空RDD處理及流處理程序優(yōu)雅的停止
文章出自:http://aaarwkj.com/article32/pjchsc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站建設(shè)、自適應(yīng)網(wǎng)站、微信小程序、域名注冊(cè)、做網(wǎng)站、網(wǎng)站收錄
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)