How to use SQL query to define table in dbtable?

前端 未结 2 1205
遇见更好的自我
遇见更好的自我 2020-12-04 00:03

In JDBC To Other Databases I found the following explanation of dbtable parameter:

The JDBC table that should be read. Note that anythin

相关标签:
2条回答
  • 2020-12-04 00:26

    Code In Scala

    val checkQuery = "(SELECT * FROM " + inputTableName + " ORDER BY " + columnName + " DESC LIMIT 1) AS timetable"
    
    val timeStampDf = spark.read.format("jdbc").option("url", url).option("dbtable", checkQuery).load()
    

    Adding an alias is also necessary after the query in parenthesis.

    0 讨论(0)
  • 2020-12-04 00:28

    Since dbtable is used as a source for the SELECT statement it has be in a form which would be valid for normal SQL query. If you want to use subquery you should pass a query in parentheses and provide an alias:

    CREATE TEMPORARY TABLE jdbcTable
    USING org.apache.spark.sql.jdbc
    OPTIONS (
        url "jdbc:postgresql:dbserver",
        dbtable "(SELECT * FROM mytable) tmp"
    );
    

    It will be passed to the database as:

    SELECT * FROM (SELECT * FROM mytable) tmp WHERE 1=0
    
    0 讨论(0)
提交回复
热议问题