Spark JDBC操作mysql

package com.mao.scala.java;

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

import java.util.Properties;

/**
 * spark jdbc操作mysql
 */
public class SQLJDBCJava {
    public static void main(String[] args) {
        SparkConf conf = new SparkConf();
        conf.setMaster("local") ;
        conf.setAppName("SQLJava");
        SparkSession sess = SparkSession.builder()
                            .appName("SQLJava")
                            .config("spark.master","local")
                            .getOrCreate();
        String url = "jdbc:mysql://192.168.174.1:3306/mybatis" ;
        String table = "persons" ;

        //查询数据库
        Dataset df = sess.read()
                .format("jdbc")
                .option("url", url)
                .option("dbtable", table)
                .option("user", "root")
                .option("password", "root")
                .option("driver", "com.mysql.jdbc.Driver")
                .load();
        df.show();

        //投影查询
        Dataset df2 = df.select(new Column("name"),new Column("phone"));
        //过滤
        df2 = df2.where("phone like '135%'");
        //去重
        df2 = df2.distinct();


        Properties prop = new Properties();
        prop.put("user", "root");
        prop.put("password", "root");
        prop.put("driver", "com.mysql.jdbc.Driver");

        //写入
        df2.write().jdbc(url,"subpersons",prop);
        df2.show();

    }
}

 

你可能感兴趣的:(Spark)