package shadow
import org.apache.spark.sql.{DataFrame, SparkSession}
import org.apache.spark.{SparkConf, SparkContext}
// 定义需要放在方法的作用域之外
case class Person(id:Int,name:String,age:Int)
//主方法
object Man {def main(args: Array[String]): Unit = {
//构建sc
val spark: SparkSession = SparkSession.builder().appName("baidu").master("local[2]").getOrCreate()
val sc: SparkContext = spark.sparkContext//设置日志级别
sc.setLogLevel("WARN")
//加载数据
val Lines: RDD[Array[String]] = sc.textFile("E:\\ab.txt").map(_.split(" "))
//将RDD与Person类关联
val personRDD: RDD[Person] = Lines.map(x=> Person(x(0).toInt,x(1),x(2).toInt))
//创建DataFrame,必须隐士转换。
import spark.implicits._
val personDF: DataFrame = personRDD.toDF
//DSL语法操作
// personDF.show()
// personDF.printSchema()// personDF.select("name").show()
//将DataFrame注册成表
personDF.createOrReplaceTempView("t_person")
// spark.sql("select * from t_person" ).show()// spark.sql("select * from t_person where name='zhangsan'").show()
spark.sql("select * from t_person order by age desc").show()
sc.stop()
}
}