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

推薦閱讀
- 精通JavaScript+jQuery:100%動(dòng)態(tài)網(wǎng)頁設(shè)計(jì)密碼
- Apache Spark 2.x Machine Learning Cookbook
- JavaScript+Vue+React全程實(shí)例
- Java設(shè)計(jì)模式及實(shí)踐
- Mathematica Data Analysis
- 精通MATLAB(第3版)
- Arduino家居安全系統(tǒng)構(gòu)建實(shí)戰(zhàn)
- Internet of Things with ESP8266
- Practical Microservices
- Python數(shù)據(jù)預(yù)處理技術(shù)與實(shí)踐
- JSP應(yīng)用與開發(fā)技術(shù)(第3版)
- 讀故事學(xué)編程:Python王國歷險(xiǎn)記
- R語言與網(wǎng)站分析
- Scratch超人漫游記:創(chuàng)意程序設(shè)計(jì):STEAM創(chuàng)新教育指南
- JavaScript前端開發(fā)程序設(shè)計(jì)教程(微課版)