1. 程式人生 > >spark sql 查詢hive表並寫入到PG中

spark sql 查詢hive表並寫入到PG中

clas sel append nec pro 增加 word postgres erro

import java.sql.DriverManager
import java.util.Properties

import com.zhaopin.tools.{DateUtils, TextUtils}
import org.apache.log4j.{Level, Logger}
import org.apache.spark.sql.SparkSession

/**
  * Created by xiaoyan on 2018/5/21.
  */
object IhrDownloadPg {
  def main(args: Array[String]){
    //設置spark日誌級別
    Logger.getLogger("org.apache.spark").setLevel(Level.ERROR)
    System.setProperty("HADOOP_USER_NAME","hive")
    val spark = SparkSession
      .builder()
      .master("local[*]")
      .appName("hive ->> ihr_oper_download")
      .config("spark.sql.warehouse.dir", "spark-warehouse")
      .config("hive.metastore.uris", "thrift://master:9083")
      .enableHiveSupport()
      .getOrCreate()
    import spark.sql

    val dt = if(!args.isEmpty) args(0) else "20180506"
    val yesterday = DateUtils.dateAdd(dt, -1)

    val url = "jdbc:postgresql://192.168.9.222:5432/safe_base"
    Class.forName("org.postgresql.Driver")
    val conn = DriverManager.getConnection(url,"secu_man","secu_man")
    val stmt = conn.createStatement()
    stmt.execute("delete from ihr_oper_download where dt = ‘" + yesterday+"‘")

    //查詢RDD
    val re1 = sql("select oper_date, " +
      "       acct_id, " +
      "       acct_name, " +
      "       module_name, " +
      "       oper_desc, " +
      "       ip, " +
      "       dt"  +
      " from safe.fact_ihr_oper_download t " +
      " where t.dt > ‘20180320‘ and t.dt <"+yesterday+"");

    val connectionProperties = new Properties()
    //增加數據庫的用戶名(user)密碼(password),指定postgresql驅動(driver)
    connectionProperties.put("user", "secu_man");
    connectionProperties.put("password", "secu_man");
    connectionProperties.put("driver", "org.postgresql.Driver");
    re1.toDF().write.mode("append").jdbc(url, "ihr_oper_download", connectionProperties);
    System.err.print("ihr_oper_download insert complete!! ");
  }
}

  註意:如果PG表不存在,默認會自動創建一張表,且字段類型為text

spark sql 查詢hive表並寫入到PG中