官术网_书友最值得收藏!

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:

主站蜘蛛池模板: 慈利县| 双牌县| 阳原县| 顺昌县| 铁岭市| 专栏| 鹿邑县| 库尔勒市| 浏阳市| 迁安市| 山东省| 疏勒县| 蒙城县| 龙里县| 五寨县| 淮安市| 石渠县| 临颍县| 平江县| 浙江省| 白城市| 山东省| 辽中县| 枞阳县| 大石桥市| 木里| 澎湖县| 黄龙县| 蓬安县| 井研县| 夏津县| 宁城县| 衡东县| 来安县| 东乌| 化州市| 抚州市| 手游| 四川省| 新绛县| 三明市|