栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

Spark读取Mysql实现

Spark读取Mysql实现

import com.alibaba.fastjson.JSONObject;
import com.mx.config.mysql.MysqlConfig;
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.JavaSparkContext;
import org.apache.spark.rdd.JdbcRDD;
import scala.reflect.ClassManifestFactory$;
import scala.runtime.AbstractFunction0;
import scala.runtime.AbstractFunction1;

import java.io.Serializable;
import java.sql.*;
import java.util.Properties;


public class MysqlUtils implements Serializable{
    private static final org.apache.log4j.Logger LOGGER = org.apache.log4j.Logger.getLogger(MysqlUtils.class);
    static String MYSQL_DRIVER;
    static String MYSQL_CONNECTION_URL;
    static String MYSQL_USERNAME;
    static String MYSQL_PWD;
    private static JavaSparkContext sc = null;

    public MysqlUtils(JavaSparkContext sc) {
        MysqlUtils.sc = sc;
        MYSQL_DRIVER = MysqlConfig.MYSQL_DRIVER;
        MYSQL_CONNECTION_URL = MysqlConfig.MYSQL_CONNECTION_URL;
        MYSQL_USERNAME = MysqlConfig.MYSQL_USERNAME;
        MYSQL_PWD = MysqlConfig.MYSQL_PWD;
    }

    
    public JavaRDD getData(String sql) {
        DbConnection dbConnection = new DbConnection(MYSQL_DRIVER, MYSQL_CONNECTION_URL, MYSQL_USERNAME, MYSQL_PWD);
        JdbcRDD jdbcRDD = new JdbcRDD<>(sc.sc(), dbConnection, sql, 0, 10, 2, new MapResult(), ClassManifestFactory$.MODULE$.fromClass(JSONObject.class));

        return JavaRDD.fromRDD(jdbcRDD, ClassManifestFactory$.MODULE$.fromClass(JSONObject.class));
    }

    
    public Integer insert(String sql) {
        int autoInckey = -1;
        try {
            DbConnection dbConnection = new DbConnection(MYSQL_DRIVER, MYSQL_CONNECTION_URL, MYSQL_USERNAME, MYSQL_PWD);
            Connection conn = dbConnection.apply();
            PreparedStatement pstmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
            pstmt.executeUpdate();
            ResultSet rs = pstmt.getGeneratedKeys();
            if (rs.next()) {
                autoInckey = rs.getInt(1);
            } else {
                return autoInckey;
            }
            rs.close();
            pstmt.close();
            conn.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return autoInckey;
    }

    
    public Boolean insertBak(String sql) {
        boolean isSuccess = false;
        try {
            DbConnection dbConnection = new DbConnection(MYSQL_DRIVER, MYSQL_CONNECTION_URL, MYSQL_USERNAME, MYSQL_PWD);
            Connection conn = dbConnection.apply();
            Statement stmt = conn.createStatement();
            isSuccess = stmt.execute(sql);
            conn.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return isSuccess;
    }

    
    public Boolean update(String updateRunningInfoSql) {
        boolean isSuccess = false;
        try {
            DbConnection dbConnection = new DbConnection(MYSQL_DRIVER, MYSQL_CONNECTION_URL, MYSQL_USERNAME, MYSQL_PWD);
            Connection conn = dbConnection.apply();
            Statement stmt = conn.createStatement();
            isSuccess = stmt.execute(updateRunningInfoSql);
            conn.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return isSuccess;
    }

    
    static class DbConnection extends AbstractFunction0 implements Serializable {
        private final String driverClassName;
        private final String connectionUrl;
        private final String userName;
        private final String password;

        public DbConnection(String driverClassName, String connectionUrl, String userName, String password) {
            this.driverClassName = driverClassName;
            this.connectionUrl = connectionUrl;
            this.userName = userName;
            this.password = password;
        }

        @Override
        public Connection apply() {
            try {
                Class.forName(driverClassName);
            } catch (ClassNotFoundException e) {
                LOGGER.error("Failed to load driver class", e);
            }

            Properties properties = new Properties();
            properties.setProperty("user", userName);
            properties.setProperty("password", password);

            Connection connection = null;
            try {
                connection = DriverManager.getConnection(connectionUrl, properties);
            } catch (SQLException e) {
                LOGGER.error("Connection failed", e);
            }

            return connection;
        }
    }

    
    static class MapResult extends AbstractFunction1 implements Serializable {
        @Override
        public JSONObject apply(ResultSet resultSet) {
            ResultSetmetaData metaData;
            JSONObject jsonObj = new JSONObject();
            try {
                metaData = resultSet.getmetaData();
                int columnCount = metaData.getColumnCount();
                for (int i = 1; i <= columnCount; i++) {
                    String columnName = metaData.getColumnLabel(i);
                    String value = resultSet.getString(columnName);
                    jsonObj.put(columnName, value);
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return jsonObj;
        }
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/695651.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号