SparkScala:使用$符号的功能差异?

以下两个表达式之间是否存在功能差异?结果对我来说看起来一样,但很好奇是否有未知的未知数。该$符号表示什么/如何读取?

df1.orderBy($"reasonCode".asc).show(10, false)
    
df1.orderBy(asc("reasonCode")).show(10, false)

回答

这两个语句是等效的,将导致相同的结果。

$符号是 Scala Spark 的特殊符号,它指的是一种隐式StringToColumn方法,它将后续字符串“reasonCode”解释为Column

implicit class StringToColumn(val sc: StringContext) {
  def $(args: Any*): ColumnName = {
    new ColumnName(sc.s(args: _*))
  }
}

在 Scala Spark 中,您有多种选择列的方法。我在另一个答案中写下了完整的语法变体列表,关于从 spark dataframe 中选择特定列。

使用不同的符号不会对性能产生任何影响,因为它们都通过 Spark 的 Catalyst 优化器转换为同一组 RDD 指令。

  • As far as I am aware it is all syntactic sugar and in the end it gets translated/compiled to the same RDD instruction by the Catalyst optimizer of Spark.

以上是SparkScala:使用$符号的功能差异?的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>