apache-spark - 如何在没有 Only 的情况下生成 spark sql 截断查询
问题描述
我正在使用 Spark(3.0.0_preview)并从/向 GreenPlum(5.24 版)读取/写入。Greenplum 5.24 版不支持“truncate table only $table_name”命令。
使用 Spark 3.0.0_preview 和 jdbcdriver(org.postgresql" % "postgresql" % "42.2.5),Spark 生成的命令是“truncate table only $table_name”。
df.write.format("jdbc").option("url", "jdbc:postgresql://test:5432/sample")
.option("user", "sample")
.option("password", "sample")
.option("dbtable", "test.employer")
.option("truncate", true) // **Genearte truncate table only**
.mode(SaveMode.Overwrite)
.save();
我想生成没有 ONLY 选项的截断命令。由于 Greenplum V5.24 不支持唯一的选项
解决方案
正如@mazaneicha 所提到的,Spark 的 PostgreSQL 方言只能生成 TRUNCATE ONLY
为了让这对我有用,我正在使用 Scala 截断我的表。不是一个好的修复,但在我们升级到只支持 TRUNCATE TABLE 的 GreenPlum 6 之前可以使用
truncate(""test.employer", "jdbc:postgresql://test:5432/sample","sample","sample" )
df.write.format("jdbc").option("url", "jdbc:postgresql://test:5432/sample")
.option("user", "sample")
.option("password", "sample")
.option("dbtable", "test.employer")
.mode(SaveMode.Append)
.save();
def truncate(tableName: String, jdbcUrl: String, username: String, password:
String) = {
val connection = DriverManager.getConnection(jdbcUrl, username, password)
connection.setAutoCommit(true)
val statement = connection.createStatement()
statement.execute(s"TRUNCATE TABLE $tableName")
}
推荐阅读
- javascript - 当猫鼬模型模式文件中需要函数时,Mongoose.model('model_name') 返回空对象
- python - Flask restful 和 flask jwt - 如何一起使用?
- html - 使用 flex 和 line-height+vh 时如何对齐尺寸?
- xml - 如何在 Linux shell 脚本中仅提取 xml 树响应中的顶级元素?
- api - React Native 获取本地 API
- python - 如何在不使用现成模块的情况下将以下字符串转换为列表?
- node.js - 使用 aws sdk 和 nodejs 将 varbinary 数据转换为 xlsx 文件
- java - Oracle JRE 1.8.201 在 Windows 10 Citrix 上打开 FileDialog“链接所指的元素“hcmd.exe”已更改或移动”
- node.js - 如何将 Firestore 子集合数据导出到 CSV
- spring-data-elasticsearch - 如何在弹簧引导数据弹性搜索中搜索嵌套列表对象