package org.jeecg.sy.java;

import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.JavaSparkContext;
import org.apache.spark.sql.SparkSession;
import org.apache.spark.SparkConf;
import java.util.Arrays;
import java.util.List;

public class SparkUtils {
    private static SparkSession sparkSession = null;

    // 初始化SparkSession
    public static SparkSession initSpark(String appName) {
        if (sparkSession == null) {
            SparkConf conf = new SparkConf().setAppName(appName).setMaster("local[2]");
            sparkSession = SparkSession.builder().config(conf).getOrCreate();
        }
        return sparkSession;
    }

    // 关闭SparkSession
    public static void stopSpark() {
        if (sparkSession != null) {
            sparkSession.stop();
            sparkSession = null;
        }
    }

    // 示例方法：创建一个整数RDD并将每个元素乘以2
    public static JavaRDD<Integer> doubleNumbers() {
        JavaSparkContext sc = new JavaSparkContext(sparkSession.sparkContext());
        JavaRDD<Integer> numbersRDD = sc.parallelize(Arrays.asList(1, 2, 3, 4, 5));
        return numbersRDD.map(number -> number * 2);
    }

    // 示例方法：打印RDD的内容
    public static void printRDD(JavaRDD<?> rdd) {
        List<?> result = rdd.collect();
        result.forEach(System.out::println);
    }
}
