我有一個準備好的函式用來做一些計算
def calculateFund(cColName: String, inst: Int)(df: DataFrame): DataFrame = {
val colsToSelect = df.columns.toSeq
val days = 30
val cSeq = Seq("c_01", "c_02")
df
.withColumn("amount_ta", lit(1.0) - col(cColName))
.withColumn("high_pmr", round(pow(lit(1.0) $"funding_ca" $"funding_pa", lit(1.0/12)) - lit(1.0), 4))
.withColumn("rate_mpo", $"high_pmr" lit(1.0))
.withColumn("amount_pi", $"amount_ta"/lit(inst))
.withColumn("c_01", lit(-1.0)*when(lit(1) <= lit(inst), (pow($"rate_mpo", (1.0*days-1)/days) - lit(1.0))*$"amount_pi").otherwise(0.0))
.withColumn("c_02", lit(-1.0)*when(lit(2) <= lit(inst), (pow($"rate_mpo", (2.0*days-1)/days) - lit(1.0))*$"amount_pi").otherwise(0.0))
.withColumn(cColName, round(cSeq.map(col).reduce(_ _), 4))
.select(colsToSelect.map(col):_*)
}
使用函式示例
df.transform(calculateFund("credit_col", 1))
它作業正常,但我需要更改c_01和c_02withColumn 條件中的固定值以使用來自名為的列的值set_days
前:
(1.0*days-1)/days)
后:
(1.0*days - $"set_days")/days)
所以,在這個減法中,我試圖使用來自set_days列的值而不是使用 de 固定1值
當我嘗試直接放置列時出現此錯誤,就像我在上面的示例中所做的那樣
error: overloaded method value - with alternatives:
(x: Double)Double <and>
(x: Float)Double <and>
(x: Long)Double <and>
(x: Int)Double <and>
(x: Char)Double <and>
(x: Short)Double <and>
(x: Byte)Double
cannot be applied to (org.apache.spark.sql.Column)
我已經嘗試在函式呼叫上添加一個 val,但仍然無法正常作業。有人可以幫我嗎?
uj5u.com熱心網友回復:
中使用的整個運算式pow應該是一個固定值或一個Column.
(1.0*days - $"set_days")/days運算式是Column固定值的混合,更具體地說,您正在使用-固定值和 a 之間的操作Column,這是不支持的。這就是錯誤訊息所說的:-不能Column作為第二個運算元應用。
您應該將固定值包裝在Columnusing 中lit:
(lit(1.0*days) - $"set_days") / lit(days)
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/526115.html
