Spark写PGSQL分区表

这里写目录标题

需求

spark程序计算后的数据需要往PGSQL中的分区表进行写入。

碰到的问题

格式问题

使用了字符串格式,导致插入报错。

scala 复制代码
val frame = df.withColumn("insert_time",current_timestamp()))
yaml 复制代码
Batch entry 0 INSERT INTO t ("a","insert_time") VALUES 
(1,'2023-08-01 10:00:00') was aborted: ERROR: column 
"insert_time" is of type timestamp without time zone but 
expression is of type character varying

分区问题(重点)

一直都是spark计算完后写单表或者hive的表,都需要去手动去维护分区。但是写PGSQL空表(只有表字段,还没有数据,没有创建分区),需要手动先创建分区,否则会报错。

报错信息

yaml 复制代码
Partition key of the failing row contains (insert_time) = 
(2023-08-04 21:14:09.641).  Call getNextException to see other 
errors in the batch.

插入失败的行的分区键包含的时间戳值 2023-08-04 21:14:09.641 在分区表中找不到对应的分区范围。

解决

最终的解决方案是在插入数据之前,通过代码去添加分区,添加好分区后再写入数据即可。

scala 复制代码
object WritePgSQL {

  def main(args: Array[String]): Unit = {

        val spark = SparkSession.builder()
          .appName("SparkPostgreSQLPartitionedTable")
          .config("spark.master", "local")
          .getOrCreate()

        // 设置PostgreSQL连接信息
        val postgresUrl = "jdbc:postgresql://192.168.160.123:5432/test"
        val connectionProperties = new java.util.Properties()
        connectionProperties.setProperty("user", "test")
        connectionProperties.setProperty("password", "123456")

        // 创建测试数据
        val data = Seq(
              (1, "2023-08-01 10:00:00"),
              (2, "2023-08-02 12:00:00"),
              (3, "2023-08-03 15:00:00")
        )

        val columns = Seq("a", "insert_time1")
        val df = spark.createDataFrame(data).toDF(columns: _*)



        val frame = df.drop("insert_time1")
          .withColumn("insert_time", current_timestamp().cast("timestamp"))

        
        // 动态创建分区范围
        // p1 可以换成p20230804这样的分区格式
        // t为表名
        // (TIMESTAMP '2023-08-04 00:00:00') 分区开始范围,一般通过代码生成,为计算时间的零点
        // (TIMESTAMP '2023-08-05 00:00:00') 分区结束范围,一般通过代码生成,为计算时间的下一天零点
        val createPartitionSql =
              s"""
          CREATE TABLE "p1" PARTITION OF t FOR VALUES FROM (TIMESTAMP '2023-08-04 00:00:00') TO (TIMESTAMP '2023-08-05 00:00:00') ;
          """

        println(createPartitionSql)

        // 执行创建分区 SQL
        val connection = java.sql.DriverManager.getConnection(postgresUrl, connectionProperties)
        val statement = connection.createStatement()
        statement.executeUpdate(createPartitionSql)
        connection.close()
        // 将数据写入PostgreSQL分区表
        frame.write
          .mode("append")
          .jdbc(postgresUrl, "t", connectionProperties)
  }
}

完整代码

自动生成当天日期和分区名称

scala 复制代码
object WritePgSQL {

  def main(args: Array[String]): Unit = {

        val spark = SparkSession.builder()
          .appName("SparkPostgreSQLPartitionedTable")
          .config("spark.master", "local")
          .getOrCreate()

        // 设置PostgreSQL连接信息
        val postgresUrl = "jdbc:postgresql://192.168.160.123:5432/test"
        val connectionProperties = new java.util.Properties()
        connectionProperties.setProperty("user", "test")
        connectionProperties.setProperty("password", "123456")
        // 创建测试数据
        val data = Seq(
              (1, "2023-08-01 10:00:00"),
              (2, "2023-08-02 12:00:00"),
              (3, "2023-08-03 15:00:00")
        )

        val columns = Seq("a", "insert_time1")
        val df = spark.createDataFrame(data).toDF(columns: _*)

        val frame = df.drop("insert_time1")
          .withColumn("insert_time", current_timestamp().cast("timestamp"))

        // 获取今天和明天的时间范围
        // 获取当前日期
        val currentDate = LocalDate.now()
        // 获取下一天的日期
        val nextDayDate = currentDate.plusDays(1)
        // 创建固定的时间部分(00:00:00)
        val startTime = LocalTime.of(0, 0, 0)
       // 组合日期和时间来得到完整的日期时间,并格式化为字符串
       val currentDateTimeString = LocalDateTime.of(currentDate, startTime).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))
       val nextDayDateTimeString = LocalDateTime.of(nextDayDate, startTime).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))

       // 格式化为yyyyMMdd字符串
       val dateFormatter = DateTimeFormatter.ofPattern("yyyyMMdd")
       val currentDateString = currentDate.format(dateFormatter)

       // 动态创建分区范围
        val createPartitionSql =
              s"""
          CREATE TABLE "p$currentDateString" PARTITION OF t
          FOR VALUES FROM (TIMESTAMP '$currentDateTimeString') TO (TIMESTAMP '$nextDayDateTimeString') ;
          """
        // 执行创建分区 SQL
        val connection = java.sql.DriverManager.getConnection(postgresUrl, connectionProperties)
        val statement = connection.createStatement()
        statement.executeUpdate(createPartitionSql)
        connection.close()
        // 将数据写入PostgreSQL分区表
        frame.write
          .mode("append")
          .jdbc(postgresUrl, "t", connectionProperties)
  }
}

效果

相关推荐
大数据CLUB1 小时前
基于spark的澳洲光伏发电站选址预测
大数据·hadoop·分布式·数据分析·spark·数据开发
ratbag6720131 小时前
当环保遇上大数据:生态环境大数据技术专业的课程侧重哪些领域?
大数据
计算机编程小央姐3 小时前
跟上大数据时代步伐:食物营养数据可视化分析系统技术前沿解析
大数据·hadoop·信息可视化·spark·django·课程设计·食物
ajax_beijing3 小时前
zookeeper是啥
分布式·zookeeper·云原生
智数研析社4 小时前
9120 部 TMDb 高分电影数据集 | 7 列全维度指标 (评分 / 热度 / 剧情)+API 权威源 | 电影趋势分析 / 推荐系统 / NLP 建模用
大数据·人工智能·python·深度学习·数据分析·数据集·数据清洗
潘达斯奈基~4 小时前
《大数据之路1》笔记2:数据模型
大数据·笔记
寻星探路4 小时前
数据库造神计划第六天---增删改查(CRUD)(2)
java·大数据·数据库
翰林小院6 小时前
【大数据专栏】流式处理框架-Apache Fink
大数据·flink
孟意昶7 小时前
Spark专题-第一部分:Spark 核心概述(2)-Spark 应用核心组件剖析
大数据·spark·big data
IT学长编程8 小时前
计算机毕业设计 基于Hadoop的健康饮食推荐系统的设计与实现 Java 大数据毕业设计 Hadoop毕业设计选题【附源码+文档报告+安装调试】
java·大数据·hadoop·毕业设计·课程设计·推荐算法·毕业论文