GroupByKey会使数据混乱。并且可以通过使用combineByKey或reduceByKey来实现GroupByKey功能。所以应该何时使用此API?有没有用例?
答案 0 :(得分:3)
组合和减少也最终会改组,但它们具有更好的内存和速度性能特性,因为它们能够在洗牌之前做更多工作来减少的数据量。
考虑是否必须通过组RDD [(group,num)]对数字属性求和。 groupByKey
将为您提供RDD [(group,List [num])],然后您可以使用map
手动缩减。 shuffle需要将所有单独的num
移动到目标分区/节点以获取该列表 - 许多行被洗牌。
因为reduceByKey
知道你在使用num
做什么(即将它们相加),所以它可以在洗牌之前对每个单独的分区求和 - 所以你最多只有一个每个group
的行被写出来随机分区/节点。
答案 1 :(得分:1)
根据以下链接,应避免使用GroupByKey。
答案 2 :(得分:1)
当合并字段中的数据将减少为单个值时,请避免使用GroupByKey。例如。如果是特定键的总和。
当您知道合并字段不会减少为单个值时,请使用GroupByKey
。 Eg: List reduce(_++_)
->避免这种情况。
减少列表的原因将同时在映射侧和减少侧创建内存。那是在不拥有密钥的执行者上创建的内存将在随机播放期间被浪费。 最好的例子是TopN。
答案 3 :(得分:0)
I woud say if groupByKey is last transformation in your chain of work (or you do anything after that has narrow dependency only), they you may consider it.
The reason reducebyKey is preferred is 1. Combine as alister mentioned above 2. ReduceByKey also partitions the data so that sum/agg becomes narrow ie can happen within partitions