JDBC 不带 '?' 的 RDD 查询语句

JDBC RDD Query Statement without '?'

我正在将 Spark 与 Scala 结合使用,并尝试使用 JdbcRDD 从数据库中获取数据。

val rdd = new JdbcRDD(sparkContext,
    driverFactory,
    testQuery,
    rangeMinValue.get,
    rangeMaxValue.get,
    partitionCount,
    rowMapper)
    .persist(StorageLevel.MEMORY_AND_DISK)    

查询中没有 ?要设置的值(因为查询很长,所以我没有把它放在这里。)所以我收到一条错误消息,

java.sql.SQLException:参数索引超出范围(1>参数个数,即0)。

我不知道问题出在哪里。有人可以提出任何解决方案吗?

您的问题是 Spark 预计您的查询字符串有几个 ?参数。

来自 Spark 用户列表:

In order for Spark to split the JDBC query in parallel, it expects an upper and lower bound for your input data, as well as a number of partitions so that it can split the query across multiple tasks.

For example, depending on your data distribution, you could set an upper and lower bound on your timestamp range, and spark should be able to create new sub-queries to split up the data.

Another option is to load up the whole table using the HadoopInputFormat class of your database as a NewHadoopRDD.

遇到了同样的问题。 用过这个:

SELECT * FROM tbl WHERE ... AND ? = ?

然后用下限1、上限1和分区1调用它。 永远运行只有一个分区。