postgresql - 使用Spark JdbcRDD读取PostgreSQL表出错

标签 postgresql scala apache-spark jdbc rdd

我正在尝试将 PostgreSQL 9.6 中的表读取到 RDD 中的 Spark 2.1.1 中,为此我在 Scala 中有以下代码。

import org.apache.spark.rdd.JdbcRDD
import java.sql.DriverManager
import org.apache.spark.SparkContext

val sc = SparkContext.getOrCreate()

val rdd = new org.apache.spark.rdd.JdbcRDD(
    sc,
    () => {DriverManager.getConnection(
    "jdbc:postgresql://my_host:5432/my_db", "my_user", "my_pass")},
    sql = "select * from my_table",
    0, 100000, 2)

但是,它返回以下错误:

org.apache.spark.SparkException: Job aborted due to stage failure: Task 0 in stage 1.0 failed 4 times, most recent failure: Lost task 0.3 in stage 1.0 (TID 7, 10.0.0.13, executor 1): org.postgresql.util.PSQLException: The column index is out of range: 1, number of columns: 0.

我正在使用最新的 PostgreSQL JDBC 驱动程序,并且我已检查它是否能够正确地针对数据库进行身份验证。

有什么想法可能会发生这种情况或者我可以尝试任何替代方案吗?

最佳答案

来自spark documentation

The query must contain two ? placeholders for parameters used to partition the results

lowerBound the minimum value of the first placeholder param; upperBound the maximum value of the second placeholder

所以你的查询应该看起来更像

select * from my_table where ? <= id and id <= ?

关于postgresql - 使用Spark JdbcRDD读取PostgreSQL表出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44590463/

相关文章:

postgresql - 在连接时创建 PostgreSQL 数据库

postgresql - OLAP 和 postgresql- 工具还是方法论?

scala - 如何在scala中获得元组数组的总和

scala 集合 : map a list and carry some state?

apache-spark - 在 for 循环内将映射函数附加到 PySpark RDD

Windows PostgreSQL 上的 RStudio 与 RPostgreSQL 的 SSL 连接

oracle - Oracle 到 Postgres 转换中的包级常量

Scala 类型参数和事例类

python - 无法在 Linux 上打开 Pyspark Shell

python - java.lang.NoSuchFieldError : DECIMAL128 mongoDB spark