我正在尝试将 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 驱动程序,并且我已检查它是否能够正确地针对数据库进行身份验证。
有什么想法可能会发生这种情况或者我可以尝试任何替代方案吗?
最佳答案
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/