SparkSQL通过JDBC连接Mysql

本文的前提条件: SparkSQL in Java
参考地址:JDBC To Other Databases

1.POM添加Mysql JDBC驱动

        <dependency>
            <groupId>com.mysql</groupId>
            <artifactId>mysql-connector-j</artifactId>
            <version>8.0.33</version>
        </dependency>

2.连接使用

package cn.coreqi;

import org.apache.spark.SparkConf;
import org.apache.spark.sql.*;

public class Main {
    public static void main(String[] args) {
        // 创建SparkConf对象
        SparkConf sparkConf = new SparkConf()
                .setMaster("local[*]")
                .setAppName("sparkSql");

        SparkSession spark = SparkSession
                .builder()
                .config(sparkConf)
                .getOrCreate();

        // 读取Mysql的数据
        Dataset<Row> df = spark.read()
                .format("jdbc")
                .option("url", "jdbc:mysql://localhost:3306/coreqi?useSSL=false&serverTimezone=Asia/Shanghai")
                .option("driver", "com.mysql.cj.jdbc.Driver")
                .option("dbtable", "user")
                .option("user", "root")
                .option("password", "fanqi")
                .load();
        // 显示数据
        df.show();

        // 保存数据
        df.write()
                .format("jdbc")
                .option("url", "jdbc:mysql://localhost:3306/coreqi?useSSL=false&serverTimezone=Asia/Shanghai")
                .option("driver", "com.mysql.cj.jdbc.Driver")
                .option("dbtable", "user")
                .option("user", "root")
                .option("password", "fanqi")
                .mode(SaveMode.Append)
                .save();
        
        // 关闭
        spark.close();
    }
}
posted @ 2024-01-15 10:20  SpringCore  阅读(129)  评论(0编辑  收藏  举报