- Apache Spark 2.x for Java Developers
- Sourav Gulati Sumit Kumar
- 118字
- 2021-07-02 19:02:01
Finding the sum of all even numbers in an RDD of integers
Let's first create an RDD of integers as follows:
scala>valintRDD = sc.parallelize(Array(1,4,5,6,7,10,15))
intRDD: org.apache.spark.rdd.RDD[Int] = ParallelCollectionRDD[0] at parallelize at <console>:24
The next step is to filter all the even elements in this RDD. So, we will execute a filter operation on the RDD, as follows:
scala>valevenNumbersRDD=intRDD.filter(i => (i%2==0))
evenNumbersRDD: org.apache.spark.rdd.RDD[Int] = MapPartitionsRDD[3] at filter at <console>:26
The preceding operation will fetch those elements for which 2 is a factor, that is, even elements .
Now, we will sum all the elements of this RDD as follows:
scala>val sum =evenNumbersRDD.sum
sum: Double = 20.0
scala> sum
res5: Double = 20.0
It is shown in the following screenshot:

推薦閱讀
- Visual FoxPro程序設計教程(第3版)
- Python數據分析(第2版)
- Linux操作系統基礎案例教程
- Expert Data Visualization
- Beginning C++ Game Programming
- 從Power BI到Analysis Services:企業級數據分析實戰
- Learning VMware vSphere
- 零基礎輕松學C++:青少年趣味編程(全彩版)
- 創意UI Photoshop玩轉移動UI設計
- NGUI for Unity
- Extending Docker
- Raspberry Pi Robotic Projects
- 軟件再工程:優化現有軟件系統的方法與最佳實踐
- Three.js Essentials
- C語言從入門到精通(微視頻精編版)