Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using predicates in Spark JDBC read method

I am pulling data from sql server to hdfs. Here is my snippet for that,

val predicates = Array[String]("int_id < 500000", "int_id >= 500000 && int_id < 1000000")

  val jdbcDF = spark.read.format("jdbc")
      .option("url", dbUrl)
      .option("databaseName", "DatabaseName")
      .option("dbtable", table)
      .option("user", "***")
      .option("password", "***")
      .option("predicates", predicates)
      .load()

My Intellij IDE keeps saying that

"Type mismatch, expected Boolean or Long or Double or String, Actual : Array[String]"

in predicates. Not sure whats wrong with this. Can anyone see whats wrong with this? Also how do I use fetch size here?

Thanks.

like image 868
ds_user Avatar asked Jul 18 '26 15:07

ds_user


1 Answers

The option method accepts only Booleans, Longs, Doubles or Strings. To pass the predicates as an Array[String] you have to use the jdbc method instead of specifying it in the format method.

val predicates = Array[String]("int_id < 500000", "int_id >= 500000 && int_id < 1000000")

val jdbcDF = spark.read.jdbc(
  url = dbUrl,
  table = table,
  predicates = predicates,
  connectionProperties = new Properties(???) // user, pass, db, etc.
)

You can see an example here.

like image 197
stefanobaghino Avatar answered Jul 20 '26 04:07

stefanobaghino



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!