spark代码 读、写、删除 postgresql数据库

package TEST

import java.sql.{Connection, DriverManager, PreparedStatement}
import java.util
import java.util.Properties
import org.apache.spark.sql.types.{StringType, StructField, StructType}
import org.apache.spark.sql.{DataFrame, Row, SparkSession}

/**
  * description:  spark 读、写、删除 postgressql数据库  代码测试
  * author: 徐国胜
  * since: 2021/11/26 15:37
  * version: 1.0
  */
object PostgresqlTest {
  def main(args: Array[String]): Unit = {

    val spark: SparkSession = SparkSession.builder().appName("PostgresqlTest")
      .config("spark.ui.showConsoleProgress", "true")
      .config("spark.shuffle.reduceLocality.enabled", "false")
      .enableHiveSupport()
      .getOrCreate()

    //造数据
    val dataList = new util.ArrayList[Row]()
    dataList.add(Row("1", "徐国胜1", "22"))
    dataList.add(Row("2", "邱良东2", "22"))
    val schema = StructType(List(StructField("id", StringType, true), StructField("name", StringType, true), StructField("age", StringType, true)))
    val DF: DataFrame = spark.createDataFrame(dataList, schema)

    //DataFrame写入PG库
    WriteDfToPG(DF, "testdb", "bigdata.student")


    /**
      * 查询PG库中某个表的数据
      * 如果需要查询某个的字段,直接在后面接select就行
      */
    val dataDF: DataFrame = QueryPG(spark, "testdb", "bigdata.student")

    //按条件筛选,删除数据库中的数据;如果需要删除全表,可以把删选条件写成 1=1
    DeleteFromPG("testdb", "bigdata.student", "name='邱良东'")

    //谨慎使用
    TruncatePGTable("testdb","bigdata.student")
  }

  def QueryPG(spark: SparkSession, databaseName: String, tableName: String): DataFrame = {
    val url: String = s"jdbc:postgresql://172.16.221.208:15432/$databaseName"
    val prop: Properties = new Properties()
    prop.put("user", "postgres")
    prop.put("password", "bm@123")
    prop.put("driver", "org.postgresql.Driver")
    spark.read.jdbc(url, tableName, prop)
  }


  def WriteDfToPG(df: DataFrame, databaseName: String, tableName: String): Unit = {
    val url: String = s"jdbc:postgresql://172.16.221.208:15432/$databaseName"
    val prop: Properties = new Properties()
    prop.put("user", "postgres")
    prop.put("password", "bm@123")
    prop.put("driver", "org.postgresql.Driver")
    df.write.mode("Append").jdbc(url, tableName, prop)
  }


  def DeleteFromPG(databaseName: String, tableName: String, condition: String): Unit = {
    Class.forName("org.postgresql.Driver")
    val url: String = s"jdbc:postgresql://172.16.221.208:15432/$databaseName"
    val prop: Properties = new Properties()
    prop.put("user", "postgres")
    prop.put("password", "bm@123")
    prop.put("driver", "org.postgresql.Driver")
    val connection: Connection = DriverManager.getConnection(url, prop)
    val delSql = s"delete from $tableName where $condition"
    val delPS: PreparedStatement = connection.prepareStatement(delSql)
    delPS.execute()
    delPS.close()
    println("delete query :" + delSql)
  }
  
  /**
    * 谨慎使用有风险  不支持回滚  数据量大的时候删除效率高
    */

  def TruncatePGTable(databaseName:String,tableName: String):Unit={
    Class.forName("org.postgresql.Driver")
    val url: String = s"jdbc:postgresql://172.16.221.208:15432/$databaseName"
    val prop: Properties = new Properties()
    prop.put("user", "postgres")
    prop.put("password", "bm@123")
    prop.put("driver", "org.postgresql.Driver")
    val connection: Connection = DriverManager.getConnection(url, prop)
    val Sql = s"truncate $tableName "
    val TruncatePS: PreparedStatement = connection.prepareStatement(Sql)
    TruncatePS.execute()
    TruncatePS.close()
    println("TruncateTableName:" + tableName)
  }

}

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值