Create new column with function in Spark Dataframe

后端 未结 1 1476
长发绾君心
长发绾君心 2020-12-12 19:21

I\'m trying to figure out the new dataframe API in Spark. seems like a good step forward but having trouble doing something that should be pretty simple. I have a datafram

相关标签:
1条回答
  • 2020-12-12 20:06

    Let's say you have "Amt" column in your Schema:

    import org.apache.spark.sql.functions._
    val myDF = sqlContext.parquetFile("hdfs:/to/my/file.parquet")
    val coder: (Int => String) = (arg: Int) => {if (arg < 100) "little" else "big"}
    val sqlfunc = udf(coder)
    myDF.withColumn("Code", sqlfunc(col("Amt")))
    

    I think withColumn is the right way to add a column

    0 讨论(0)
提交回复
热议问题