【问题标题】:Using predicates in Spark JDBC read method在 Spark JDBC 读取方法中使用谓词
【发布时间】:2018-07-18 13:46:51
【问题描述】:

我正在将数据从 sql server 拉到 hdfs。这是我的sn-p,

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()

我的 Intellij IDE 一直这么说

"类型不匹配,预期为 Boolean 或 Long 或 Double 或 String,实际: 数组[字符串]"

在谓词中。不知道这有什么问题。谁能看出这有什么问题?另外我如何在这里使用获取大小?

谢谢。

【问题讨论】:

    标签: scala hadoop apache-spark intellij-idea jdbc


    【解决方案1】:

    option 方法仅接受 Booleans、Longs、Doubles 或 Strings。要将predicates 作为Array[String] 传递,您必须使用jdbc 方法,而不是在format 方法中指定它。

    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.
    )
    

    你可以看一个例子here

    【讨论】:

    • 谢谢,它对我有用。如果您可以在其上添加所有属性,那就太好了(对于将来寻找它的任何人都会有用。)
    猜你喜欢
    • 2018-01-07
    • 1970-01-01
    • 2015-12-10
    • 1970-01-01
    • 2021-03-05
    • 1970-01-01
    • 1970-01-01
    • 2021-01-02
    • 1970-01-01
    相关资源
    最近更新 更多