Spark,在 Scala 中添加具有相同值的新列

Spark, add new Column with the same value in Scala

我在 Spark-Scala 环境中使用 withColumn 函数时遇到一些问题。 我想像这样在我的 DataFrame 中添加一个新列:

+---+----+---+
|  A|   B|  C|
+---+----+---+
|  4|blah|  2|
|  2|    |  3|
| 56| foo|  3|
|100|null|  5|
+---+----+---+

变成了:

+---+----+---+-----+
|  A|   B|  C|  D  |
+---+----+---+-----+
|  4|blah|  2|  750|
|  2|    |  3|  750|
| 56| foo|  3|  750|
|100|null|  5|  750|
+---+----+---+-----+

一个值中的 D 列对我的 DataFrame 中的每一行重复 N 次。

代码是这样的:

var totVehicles : Double = df_totVehicles(0).getDouble(0); //return 750

变量 totVehicles returns 正确的值,有效!

第二个DataFrame要计算2个字段(id_zipcode,n_vehicles),并添加第三列(相同的值-750):

var df_nVehicles =
df_carPark.filter(
      substring($"id_time",1,4) < 2013
    ).groupBy(
      $"id_zipcode"
    ).agg(
      sum($"n_vehicles") as 'n_vehicles
    ).select(
      $"id_zipcode" as 'id_zipcode,
      'n_vehicles
    ).orderBy(
      'id_zipcode,
      'n_vehicles
    );

最后,我添加了具有 withColumn 函数的新列:

var df_nVehicles2 = df_nVehicles.withColumn(totVehicles, df_nVehicles("n_vehicles") + df_nVehicles("id_zipcode"))

但是 Spark returns 我这个错误:

 error: value withColumn is not a member of Unit
         var df_nVehicles2 = df_nVehicles.withColumn(totVehicles, df_nVehicles("n_vehicles") + df_nVehicles("id_zipcode"))

你能帮帮我吗? 非常感谢!

lit 函数用于将文字值添加为列

import org.apache.spark.sql.functions._
df.withColumn("D", lit(750))