创建RDD队列流

  1. import time
  2.  
  3. from pyspark import SparkContext
  4. from pyspark.streaming import StreamingContext
  5.  
  6. if __name__ == "__main__":
  7.  
  8. sc = SparkContext(appName="PythonStreamingQueueStream")
  9. ssc = StreamingContext(sc, 1)
  10.  
  11. # Create the queue through which RDDs can be pushed to
  12. # a QueueInputDStream
  13. rddQueue = []
  14. for i in range(5):
  15. rddQueue += [ssc.sparkContext.parallelize([j for j in range(1, 1001)], 10)]
  16.  
  17. # Create the QueueInputDStream and use it do some processing
  18. inputStream = ssc.queueStream(rddQueue)
  19. mappedStream = inputStream.map(lambda x: (x % 10, 1))
  20. reducedStream = mappedStream.reduceByKey(lambda a, b: a + b)
  21. reducedStream.pprint()
  22.  
  23. ssc.start()
  24. time.sleep(6)
  25. ssc.stop(stopSparkContext=True, stopGraceFully=True)

程序就开始运行,就可以看到类似下面的结果:

-------------------------------------------                                     
Time: 1479522100000 ms
-------------------------------------------
(4,10)
(0,10)
(6,10)
(8,10)
(2,10)
(1,10)
(3,10)
(7,10)
(9,10)
(5,10)

转载于:https://www.cnblogs.com/SoftwareBuilding/p/9449889.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值