package org.jeecg.sy.java;

import org.apache.spark.sql.SparkSession;
import org.apache.spark.sql.Dataset;
import org.apache.spark.sql.Row;
import org.apache.spark.sql.functions;

public class MusicSpark {
    public static void main(String[] args) {
        SparkSession spark = SparkSession.builder()
                .appName("Music Shot Analysis")
                .master("local[2]")
                .getOrCreate();

        // 读取数据
        Dataset<Row> data = spark.read()
                .format("csv")
                .option("header", "true")
                .load("path_to_your_csv_file");

        // 投篮成功率分析
        Dataset<Row> shotTypeSuccess = data
                .groupBy("shot_type")
                .agg(
                        functions.avg(data.col("result").cast("integer")).alias("success_rate")
                );

        shotTypeSuccess.show();

        // 每个球赛节次的得分情况
        Dataset<Row> scoringByQuarter = data
                .groupBy("qtr")
                .agg(
                        functions.sum(data.col("lebron_team_score")).alias("total_points")
                );

        scoringByQuarter.show();

        // 根据投篮距离计算得分分布
        Dataset<Row> scoreDistribution = data
                .withColumn("distance_range", functions.when(data.col("distance_ft").$less(10), "0-10")
                        .otherwise(functions.when(data.col("distance_ft").$less(20), "10-20")
                                .otherwise("20+")))
                .groupBy("distance_range")
                .agg(
                        functions.count("shot_type").alias("shots_made"),
                        functions.sum(data.col("result").cast("integer")).alias("shots_scored")
                );

        scoreDistribution.show();

        // 关闭Spark会话
        spark.stop();
    }
}

