scala spark dataframe 修改字段类型

package classifierAlg
import config.conf.{sc, spark_session}
import config.conf.spark_session.implicits._
import org.apache.spark.sql.functions._
import org.apache.spark.sql.types._
import org.apache.spark.sql.{Column, DataFrame}
object euclideanDist {
  def main(args: Array[String]): Unit = {
    val path:String = "data/irsdf/part-00000-ca2d6ce7-bcd0-4c24-aba9-e8cb01dcc04c-c000.csv"
    val df: DataFrame = spark_session.read.csv(path).toDF("ft1","ft2","ft3","ft4","label")
    val cols: Array[String] = df.columns
    val n: Int = cols.length
//     val colNames : Array[String] = df.schema.fieldNames
    val colsd: Array[Column] = cols.map(f => df(f).cast(DoubleType))
    val df2: DataFrame = df.select(colsd: _*)
    df2.printSchema()

  }
}

  

 

posted @ 2020-04-03 11:37  洺剑残虹  阅读(2867)  评论(0编辑  收藏  举报