DataFrame中的转换算子操作1

2022/8/30 6:25:04

本文主要是介绍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操作结束------------------")
}


这篇关于DataFrame中的转换算子操作1的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程