scala - 优化一段使用地图动作的代码
问题描述
以下代码在集群中的 4Gb 原始数据上花费了大量时间:
df.select("type", "user_pk", "item_pk","timestamp")
.withColumn("date",to_date(from_unixtime($"timestamp")))
.filter($"date" > "2018-04-14")
.select("type", "user_pk", "item_pk")
.map {
row => {
val typef = row.get(0).toString
val user = row.get(1).toString
val item = row.get(2).toString
(typef, user, item)
}
}
输出应该是类型Dataset[(String,String,String)]
。
我想这map
部分需要很多时间。有没有办法优化这段代码?
解决方案
我严重怀疑map
是问题所在,但我根本不会使用它并使用标准Dataset
转换器
import df.sparkSession.implicits._
df.select("type", "user_pk", "item_pk","timestamp")
.withColumn("date",to_date(from_unixtime($"timestamp")))
.filter($"date" > "2018-04-14")
.select($"type" cast "string", $"user_pk" cast "string", $"item_pk" cast "string")
.as[(String,String,String)]
推荐阅读
- python - 更新的 pandas 显示旧版本并在使用爆炸功能时引发属性错误
- python - 按日期加密密码实验
- javascript - axios 无法获取响应数据
- image-processing - 想不出解决这个图像分割问题
- c - 如何在具有 Web 扩展原生消息传递 API 的原生 C 应用程序中使用`stdin`?
- swift - 如何删除上一个单元格的 UITableViewCell 分隔符?
- mysql - MySQL最后一行有序视图
- c - 如何为嵌入式编程配置vim?
- linux - 如何在 cronjob 上动态获取版本文件夹名称?
- firebase - 使用firebase颤振谷歌登录