scala - Spark:groupByKey,右侧带有“Iterator”而不是“Iterable”
问题描述
我有一个rdd。我想按一些属性对其进行分组并将每个组保存到一个单独的文件中(并获取结果文件名列表)。最天真的方式:
val rdd : RDD[Long] = ???
val byLastDigit: RDD[(Int, Long)] = rdd.map(n => ((n % 10).toInt, n))
val saved: Array[String] = byLastDigit.groupByKey().map((numbers: (Int, Iterable[Long])) => {
//save numbers into a file
???
}).collect()
这种方法的缺点是它同时在内存中保存一个键的所有值。所以它在巨大的数据集上效果不佳。
替代方法:
byLastDigit.partitionBy(new HashPartitioner(1000)).mapPartitions((numbers: Iterator[(Int, Long)]) => {
//assume that all numbers in a partition have the same key
???
}).collect()
由于分区的数量远高于键的数量,每个分区很可能只保存一个键的数字。
它适用于大型数据集。但这很丑陋,而且更容易出错。
能不能做得更好?
解决方案
推荐阅读
- flutter - 选择按钮时如何取消选择其他按钮?
- 3d - 在 R 中使用 PCA 绘制 3D 平面
- css - 如何将图标浮动到高分辨率但在低分辨率文本之后添加它?
- python - 在我的用户在我的输入提示上按下回车后,我如何重复要求输入
- javascript - svg 上的事件监听器与 d3.mitchTree.boxedTree
- c - 内存泄漏 | LeakSanitizer:检测到内存泄漏 | C 编程
- c++ - 拆分标头和实现会导致显着减速
- php - PHPSpreadsheet 调用一个成员函数 has() on null
- amazon-web-services - 有时间限制的从 ec2 到 s3 存储桶的数据传输
- sql - T-SQL:计算员工的总工资