【问题标题】:Create empty column of StructType in spark dataframe在 spark 数据框中创建 StructType 的空列
【发布时间】:2018-11-06 13:20:35
【问题描述】:

我需要将一个空的 StructType 列添加到现有的 DataFrame 中。

尝试以下:

df = df.withColumn("features", typedLit(StructType(Nil)))

还有:

df = df.withColumn("features", lit(new GenericRowWithSchema(Array(), StructType(Nil))))

但是,在上述两种情况下,都会因不支持的文字类型而出现错误。

【问题讨论】:

    标签: scala apache-spark


    【解决方案1】:

    粗略地,可以使用用户定义的函数来添加一列空行:

    def addEmptyRowColumn(df: DataFrame, newColumnName: String): DataFrame = {
      val addEmptyRowUdf = udf( () =>
        new GenericRowWithSchema(Array(), StructType(Nil)), StructType(Nil))
    
      df.withColumn(newColumnName, addEmptyRowUdf())
    }
    
    df = addEmptyRowColumn(df, "features")
    

    【讨论】:

      猜你喜欢
      • 2016-10-29
      • 2020-11-03
      • 1970-01-01
      • 2020-02-26
      • 2016-07-01
      • 2019-08-25
      • 1970-01-01
      • 1970-01-01
      • 2020-01-20
      相关资源
      最近更新 更多