其他分享
首页 > 其他分享> > DataFrame中的转换算子操作1

DataFrame中的转换算子操作1

作者:互联网

val sparkConf = new SparkConf().setMaster("local[2]").setAppName("tran")
val sparkSession = SparkSession.builder().config(sparkConf).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])

case class Student(@BeanProperty var name:String,@BeanProperty var age:Int,@BeanProperty var sex:String)

limit

def limitOpt(dataFrame: DataFrame): Unit = {
  println("------------------limit操作开始------------------")
  // select name,age from student limit 2
  val frame: DataFrame = dataFrame.limit(2).select("name", "age")
  frame.show()
  println("------------------limit操作结束------------------")
}

where

def whereOpt(dataFrame: DataFrame) = {
  // select * from student where age>21
  println("------------------where操作开始------------------")
  val frame = dataFrame.where("age > 21").select("*")
  frame.show()
  println("------------------where操作结束------------------")
  println("------------------filter操作开始------------------")
  val frame1 = dataFrame.filter("age > 21").select("*")
  frame1.show()
  println("------------------filter操作结束------------------")
}

select

def selectOpt(dataFrame: DataFrame) = {
  println("------------------select操作开始------------------")
  val frame = dataFrame.select("name", "age")
  frame.show()
  println("------------------select操作结束------------------")
}

def selectExprOpt(dataFrame: DataFrame) = {
  println("------------------selectExpr操作开始------------------")
  val frame = dataFrame.selectExpr("name as na", "age as ag")
  frame.show()
  println("------------------selectExpr操作结束------------------")
}

标签:转换,val,------------------,frame,dataFrame,DataFrame,算子,println,select
来源: https://www.cnblogs.com/jsqup/p/16637891.html