本文整理汇总了Python中pyspark.streaming.StreamingContext.addStreamingListener方法的典型用法代码示例。如果您正苦于以下问题:Python StreamingContext.addStreamingListener方法的具体用法?Python StreamingContext.addStreamingListener怎么用?Python StreamingContext.addStreamingListener使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pyspark.streaming.StreamingContext
的用法示例。
在下文中一共展示了StreamingContext.addStreamingListener方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: MyStreamingListener
# 需要导入模块: from pyspark.streaming import StreamingContext [as 别名]
# 或者: from pyspark.streaming.StreamingContext import addStreamingListener [as 别名]
class MyStreamingListener(StreamingListener):
"""
Uses py4j framework to send Java objects to the pyspark process.
The parameters to the callbacks are Java objects with members variables as objects.
They are not sent as primitive data types.
"""
def onBatchStarted(self, batchStarted):
# 'batchStarted' instance of org.apache.spark.streaming.api.java.JavaStreamingListenerBatchStarted
print('>>> Batch completed...number of records: ', batchStarted.batchInfo().numRecords())
def onBatchCompleted(self, batchCompleted):
# 'batchStarted' instance of org.apache.spark.streaming.api.java.JavaStreamingListenerBatchCompleted
print('>>> Batch completed...time taken (ms) = ', batchCompleted.batchInfo().totalDelay())
if __name__ == '__main__':
ssc = StreamingContext(\
SparkContext(conf = SparkConf().setAppName('TestStreamingListenerJob')), \
5)
ssc.addStreamingListener(MyStreamingListener())
ssc\
.socketTextStream('localhost', 9999)\
.flatMap(lambda line: line.split(' '))\
.count()\
.pprint()
ssc.start()
ssc.awaitTermination()
示例2: model_prediction
# 需要导入模块: from pyspark.streaming import StreamingContext [as 别名]
# 或者: from pyspark.streaming.StreamingContext import addStreamingListener [as 别名]
#output_file.write("KMeans Prediction, %.3f\n"%(end_pred-end_train))
#return predictions
def model_prediction(rdd):
pass
##########################################################################################################################
# Start Streaming App
ssc_start = time.time()
ssc = StreamingContext(sc, STREAMING_WINDOW)
batch_collector = BatchInfoCollector()
ssc.addStreamingListener(batch_collector)
#kafka_dstream = KafkaUtils.createStream(ssc, KAFKA_ZK, "spark-streaming-consumer", {TOPIC: 1})
#kafka_param: "metadata.broker.list": brokers
# "auto.offset.reset" : "smallest" # start from beginning
kafka_dstream = KafkaUtils.createDirectStream(ssc, [TOPIC], {"metadata.broker.list": METABROKER_LIST,
"auto.offset.reset" : "smallest"}) #, fromOffsets=fromOffset)
ssc_end = time.time()
output_file.write("Spark SSC Startup, %d, %d, %s, %.5f\n"%(spark_cores, -1, NUMBER_PARTITIONS, ssc_end-ssc_start))
#####################################################################
# Scenario Count
#global counts