package date_20250422

import org.apache.spark.sql.SparkSession

object ParquetSQLExample {
    def main(args: Array[String]): Unit = {

        System.setProperty("HADOOP_USER_NAME","root")

        // 创建 SparkSession
        val spark = SparkSession.builder()
          .appName("Spark SQL Parquet Example")
          .master("local[*]") // 本地运行
          .getOrCreate()

        // 读取 Parquet 文件
        val ratingsDF = spark.read.parquet("hdfs://hadoop102:8020/input/data_6/users.parquet")

        ratingsDF.show()
        // 注册临时视图
        ratingsDF.createOrReplaceTempView("users")


        // 将查询结果写入到 HDFS 自定义目录
        ratingsDF.select("name", "favorite_color", "favorite_numbers")
          .write.save("hdfs://hadoop102:8020/output/data_6/users_output2")

        // 停止 SparkSession
        spark.stop()
    }
}