其他分享
首页 > 其他分享> > DataFrame与rdd之间的转换(val rdd1 = dataFrame.rdd)

DataFrame与rdd之间的转换(val rdd1 = dataFrame.rdd)

作者:互联网

核心语句val rdd1 = dataFrame.rdd

package SparkSQL.DataFreamCreate.dataframetordd

import org.apache.spark.SparkConf
import org.apache.spark.rdd.RDD
import org.apache.spark.sql.types.{DataTypes, StructField, StructType}
import org.apache.spark.sql.{DataFrame, Row, SparkSession}
import scala.beans.BeanProperty

object dftordd1 {
  def main(args: Array[String]): Unit = {
    val conf = new SparkConf().setAppName("dataFrameCreate").setMaster("local[*]")
    val sparkSession = SparkSession.builder().config(conf).getOrCreate()

    val seq:Seq[Student] = Array(Student("zs",20,"男"),Student("ls",21,"女"),Student("ww",22,"男"))
    val rdd:RDD[Student] = sparkSession.sparkContext.makeRDD(seq)
    val dataFrame:DataFrame = sparkSession.createDataFrame(rdd,classOf[Student])
    dataFrame.show()

    val rdd1 = dataFrame.rdd
    val rdd2: RDD[Row] = rdd1.map(row => {
      Row(row.getAs[String]("name"), row.getAs[Int]("age") + 5, row.getAs[String]("sex"))
    })
    val structType = StructType(Array(
      StructField("name", DataTypes.StringType),
      StructField("age", DataTypes.IntegerType),
      StructField("sex", DataTypes.StringType)
    ))
    val frame: DataFrame = sparkSession.createDataFrame(rdd2, structType)
    frame.show()
  }
}
case class Student(@BeanProperty var name:String,@BeanProperty var age:Int,@BeanProperty var sex:String)

标签:rdd1,String,val,dataFrame,rdd,Student,import
来源: https://www.cnblogs.com/jsqup/p/16638035.html