【发布时间】:2019-05-27 17:02:06
【问题描述】:
我正在审查开发代码,我需要避免或使用不同的方式在数据框中使用“withColumn”函数来添加列;但我有以下疑问:
- 使用嵌套的'withColumn',创建新表(如下面的代码)?使用 6 'withColumn',在内存表中创建 6 个新的?
newDataframe = table
.withColumn("name", col("consolidate").cast(DecimalType(17,2)))
.withColumn("name", col("consolidate").cast(DecimalType(17,2)))
-
如果使用许多“withColumn”会增加内存使用量并降低性能(如果为真),如何在数据框中添加列时避免使用“withColumn”并获得相同的结果?
有没有一种方法可以在不使用'withColumn'的情况下消耗更少的内存并且运行速度更快,但得到相同的结果?,即添加了6列的数据帧
我不知道该怎么做。
要优化的代码是这样的:
def myMethod(table: DataFrame): DataFrame = {
newDataframe = table
.withColumn("name", col("consolidate").cast(DecimalType(17,2)))
.withColumn("id_value", col("east").cast(DecimalType(17,2)))
.withColumn("x_value", col("daily").cast(DecimalType(17,2)))
.withColumn("amount", col("paid").cast(DecimalType(17,2)))
.withColumn("client", col("lima").cast(DecimalType(17,2)))
.withColumn("capital", col("econo").cast(DecimalType(17,2)))
newDataframe
}
【问题讨论】:
-
提供的答案是正确的,但这里有一些你可以用 withColumn 暗示 foldLeft 的东西,这很可能是你的意思:medium.com/@manuzhang/…
标签: scala apache-spark