Scala 高级算子

发布时间:2020-06-11 18:43:56 作者:菜鸟的征程
来源:网络 阅读:1719

==> mapPartitionsWithIndex

    ---> 定义: def mapPartitionsWithIndex[U](f:(Int, Iterator[T]) => Iterator[U], preserversPartitioning: Boolean = false)

    ---> 作用: 对 RDD 每个分区进行操作,带有分区号

    ---> 示例:输出分区号和内容

// 创建一个RDD
val rdd1 = sc.parallelize(List(1,2,3,4,5,6,7,8,9))
// 创建一个函数,作为 f 的值
def func(index:Int, iter:Iterator[Int]):Iterator[String] = {
    iter.toList.map(x=>"[PartID: " + index + ", value= " + x + "]").iterator
}

// 调用
rdd1.mapPartitionsWithIndex(func).colect

// 结果
res15: Array[String] = Array([PartitionID: 0,value=1], [PartitionID: 0,value=2], [PartitionID: 0,value=3], [PartitionID: 0,value=4], 
                             [PartitionID: 1,value=5], [PartitionID: 1,value=6], [PartitionID: 1,value=7], [PartitionID: 1,value=8], [PartitionID: 1,value=9])


==> aggregate

    ---> 定义:def aggregate[U: ClassTag](zeroValue: U)(seqOp:(U, T) => U, combOp: (U, U) => U): U

        ---- (zeroValue: U)            初始值

        ---- seqOp:(U, T) => U    局部操作

        ---- combOp:(U, U) => U        全局操作

    ---> 作用:先对局部进行操作,再对全局进行操作

    ---> 示例:

// 求两个分区最大值的和,初始值为0
val rdd1 = sc.parallelize(List(1,2,3,4,5,6,7,8,9))
rdd1.aggregate(0)(math.max(_,_), _+_)
// 结果为:res16: Int = 13


==> aggregateByKey

    ---> 定义:

    ---> 作用:对 key-value 格式 的数据进行 aggregate 操作

    ---> 示例:

// 准备一个 key-value 格式的 RDD
val parRDD = sc.parallelize(List(("cat", 2),("cat", 5),("mouse", 4),("cat", 12),("dog", 12),("mouse", 2)), 2)

// 计算每个分区中的动物最多的个数求和
parRDD.aggregateByKey(0)(math.max(_, _), _+_)
// 结果为:  Array[(String, Int)] = Array((dog,12), (cat,17), (mouse,6))

// 计算每种动物的总数量

parRDD.aggregateByKey(0)(_+_, _+_).collect        // 方法一
parRDD.reduceByKey(_+_).collect



==> coalesce 与 repartition    

    ---> 作用:将 RDD 中的分区进行重分区

    ---> 区别: coalesce 默认不会进行 shuffle(false)

                        repartition 会进行 shuffle(true), 会将数据真正通过网络进行重分区

    ---> 示例:

// 定义一个 RDD 
val rdd = sc.parallelize(List(1,2,3,4,5,6,7,8), 2)

// 显示分区中的分区号和分区号中的内容
def func(index:Int, iter:Iterator[Int]):Iterator[String] = {
    iter.toList.map(x=>"[PartID: " + index + ", value= " + x + "]").iterator
}

// 查看 rdd 中的分区情况
rdd.mapPartitionsWithIndex(func).collect
// 结果为: Array[String] = Array(
// [PartID: 0, value= 1], [PartID: 0, value= 2], [PartID: 0, value= 3], [PartID: 0, value= 4], 
// [PartID: 1, value= 5], [PartID: 1, value= 6], [PartID: 1, value= 7], [PartID: 1, value= 8])

// 使用 repartition 将分区数改为3
val rdd2 = rdd1.repartition(3)
val rdd3 = rdd1.coalesce(3, true)

// 查看rdd2 与rdd3 的分区情况
rdd2.mapPartitionsWithIndex(func).collect
rdd3.mapPartitionsWithIndex(func).collect

// 结果为:Array[String] = Array(
// [PartID: 0, value= 3], [PartID: 0, value= 6], 
// [PartID: 1, value= 1], [PartID: 1, value= 4], [PartID: 1, value= 7], 
// [PartID: 2, value= 2], [PartID: 2, value= 5])



推荐阅读:
  1. 使用 Scala 写WordContext程序
  2. Scala基本知识点

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

高级 算子 la

上一篇:Fruit Ninja(水果忍者)游戏源代码下载、分析(中)---可运行Android,Ios,Window,Mac,Html5平台

下一篇:Redis笔记-String类型及操作(二)

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》