package date_20250429

import org.apache.spark.rdd.RDD
import org.apache.spark.sql.types.{IntegerType, StringType, StructField, StructType}
import org.apache.spark.sql.{Row, SparkSession}

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

        import org.apache.spark.sql.functions._

        val arr = Array(
            "2024-11-01,0001",
            "2024-11-01,0002",
            "2024-11-01,0001",
            "2024-11-02,0003",
            "2024-11-02,0003",
            "2024-11-02,0004",
            "2024-11-02,0001",
            "2024-11-02,0001",
            "2024-11-02,0004",
            "2024-11-03,0002",
            "2024-11-03,0003",
            "2024-11-03,0001",
            "2024-11-03,0005",
            "2024-11-03,0003",
            "2024-11-03,0003",
            "2024-11-04,0004"
        )

        val rowRDD: RDD[Row] = spark.sparkContext.makeRDD(arr)
          .map(line => Row(line.split(",")(0), line.split(",")(1).toInt)
        )
        val structType: StructType = StructType(Array(StructField("date", StringType, true),
            StructField("userid", IntegerType, true)))

        val df = spark.createDataFrame(rowRDD,structType)

        df.groupBy("date")
          .agg(countDistinct("userid") as "count")
          .show()
    }
}