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

推薦閱讀
- 觸·心:DT時代的大數(shù)據精準營銷
- Android開發(fā)精要
- JMeter 性能測試實戰(zhàn)(第2版)
- Getting Started with PowerShell
- 概率成形編碼調制技術理論及應用
- Learning FuelPHP for Effective PHP Development
- SQL Server從入門到精通(第3版)
- C語言程序設計
- Linux C編程:一站式學習
- Test-Driven Development with Django
- INSTANT Yii 1.1 Application Development Starter
- Access 2010中文版項目教程
- 零基礎學Kotlin之Android項目開發(fā)實戰(zhàn)
- Mastering ArcGIS Enterprise Administration
- Java 從入門到項目實踐(超值版)