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() } }
自动化学习。