【问题标题】:generic function to cast spark dataframe columns用于转换 spark 数据框列的通用函数
【发布时间】:2019-07-15 11:55:34
【问题描述】:

我编写了以下函数来转换 spark 数据框列,但我想让它使用类型化函数:

def castCol(newName: String, previosuNmae: String, datatType: DataType): DataFrame = {
  df.withColumn(newName, col(previosuNmae).cast(datatType))

我想把它变成这样:

def castCol[DataType](newName: String, previosuNmae: String): DataFrame = {
  df.withColumn(newName, col(previosuNmae).cast(DataType)) 

但这不起作用。 你知道如何将 dataType 作为类型而不是参数传递给函数

【问题讨论】:

    标签: scala apache-spark typeclass


    【解决方案1】:

    这个怎么样:

    def castCol[T<:DataType](newColName: String, newColType:T, previousColName: String): DataFrame = 
        df.withColumn(newColName, col(previousColName).cast(newColType))
    

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 2012-06-30
      • 2021-08-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-14
      • 2018-11-27
      • 2016-02-17
      相关资源
      最近更新 更多