package date_20250429

import org.apache.spark.sql.{DataFrame, Dataset, SparkSession}


object sparksqlwordcount {
    def main(args: Array[String]): Unit = {
        val session = SparkSession.builder()
          .appName("WordCount")
          .master("local[*]")
          .getOrCreate()

        val lines: Dataset[String] =
            session.read.textFile("hdfs://hadoop102:8020/input/data_7/wordcount03.txt")

        import session.implicits._
        val words: Dataset[String] = lines.flatMap(_.split(" "))

        val df: DataFrame = words.withColumnRenamed("value", "word")

        df.createTempView("v_words")
        val result: DataFrame =
            session.sql(
                """
                  |SELECT word,count(1) num
                  |FROM v_words
                  |GROUP BY word
                  |ORDER BY num DESC
                  |""".stripMargin)

        result.show()
        session.close()

    }
}