package date_20250422

import org.apache.spark.sql.SparkSession

case class Person(id: Int, name: String, age: Int)

object Dataset_Return_DateF {
    def main(args: Array[String]): Unit = {
        //创建或得到SparkSession
        val spark = SparkSession.builder()
          .appName("SparkSQLJDBC")
          .master("local[*]")
          .config("spark.testing.memory", "2147480000")
          .getOrCreate()

        val d1 = spark.read.textFile("hdfs://hadoop102:8020/input/data_6/person.txt")
        import spark.implicits._

        val personDataset = d1.map(line => {
            val fields = line.split(",")
            val id = fields(0).toInt
            val name = fields(1)
            val age = fields(2).toInt
            Person(id, name, age)
        })
        // 将Dataset转为DataFrame
        val pdf = personDataset.toDF()
        // 在DataFrame上创建一个临时视图“v_person”
        pdf.createTempView("v_person")
        // 使用SparkSession对象执行SQL查询
        val result = spark.sql("select * from v_person order by age desc")
        // 调用show()方法输出结果数据
        result.show()
    }
}