- 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:

推薦閱讀
- C++程序設計教程
- 移動UI設計(微課版)
- 兩周自制腳本語言
- Django開發從入門到實踐
- 我的第一本算法書
- Mastering C# Concurrency
- Python高效開發實戰:Django、Tornado、Flask、Twisted(第3版)
- Visual Basic程序設計
- 利用Python進行數據分析
- 蘋果的產品設計之道:創建優秀產品、服務和用戶體驗的七個原則
- Statistical Application Development with R and Python(Second Edition)
- Python+Tableau數據可視化之美
- Unity Character Animation with Mecanim
- 后臺開發:核心技術與應用實踐
- Java程序設計教程