springboot對接spark大資料

初见如月發表於2024-04-17

1、特別申明,請注意JDK版本,最好用JDK1.8,用JDK17會導致很多報錯

2、匯入pom依賴 JDK1.8直接匯入spark依賴就行。

        <dependency>
            <groupId>org.apache.spark</groupId>
            <artifactId>spark-sql_2.13</artifactId>
            <version>3.4.1</version>
        </dependency>

        <dependency>
            <groupId>org.apache.spark</groupId>
            <artifactId>spark-core_2.13</artifactId>
            <version>3.4.1</version>
        </dependency>

3、如果是JDK17需要匯入JDK1.8的(JDK1.8版本跳過下面依賴)

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-nop</artifactId>
            <version>1.6.0</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>4.0.1</version>
        </dependency>        

4、直接上程式碼

    @Operation(summary = "spark處理")
    @PostMapping("sparkHander")
    public Resp sparkTest() throws IOException { 
        //System.setProperty("hadoop.home.dir", "C:\\hadoop-common-2.2.0");
        SparkConf sparkConf = new SparkConf();
        sparkConf.set("spark.ui.enabled", "false");
        sparkConf.set("spark.some.config.option", "some-value");
        SparkSession spark = SparkSession
                .builder()
                .master("local")
                .appName("SparkSQLTest4")
                .config(sparkConf)
                .getOrCreate();
//本地字尾名.json的檔案 Dataset
<Row> df = spark.read().json("C:\\test.json"); df.printSchema(); df.show();
     //表名可以隨便寫如:"test" df.createOrReplaceTempView("test");
     //如果上面寫test這裡你就可以寫查詢條件select * from test Dataset
<Row> dataset = spark.sql("select * from test"); dataset.show();
     //得到JSON的字串 List
<String> strings = dataset.toJSON().collectAsList(); spark.stop(); return Resp.of(strings); }

5、JSON格式如下:

[
    {
        "port": "1",
        "name": "測試伺服器1",
        "showPassword": false,
        "status": "0"
    },
    {
        "port": "2",
        "name": "測試伺服器2",
        "showPassword": false,
        "status": "0"
    },{
        "port": "3",
        "name": "測試伺服器3",
        "showPassword": false,
        "status": "0"
    }
]

6、(JDK1.8無需配置)如果是JDK17還需要配置:vm options

--add-opens
java.base/java.io=ALL-UNNAMED
--add-opens
java.base/java.nio=ALL-UNNAMED
--add-exports
java.base/sun.nio.ch=ALL-UNNAMED
--add-opens
java.base/java.javax=ALL-UNNAMED

相關文章