I'm trying to read a postgres/postgis table into a spark 2.0 dataframe like this.
val jdbcUrl = s"jdbc:postgresql://${host}:${port}/${dbName}"
val connectionProperties = new Properties()
connectionProperties.put("user", s"${user}")
connectionProperties.put("password", s"${password}")
connectionProperties.setProperty("Driver", "org.postgresql.Driver")
def readTable ( table: String ): DataFrame = {
spark.read.jdbc(jdbcUrl, s"(select st_astext(geom) as geom from
${table}) as t;", connectionProperties)
}
readTable("myschema.mytable")
I get this error:
org.postgresql.util.PSQLException: ERROR: syntax error at or near "WHERE"
I'm pretty sure this is caused by a where clause being added to the query as described in this question.
However according to the docs this method should work https://docs.databricks.com/spark/latest/data-sources/sql-databases.html#pushdown-query-to-database-engine
I need to use a query as a table name because I need to get the postgis geometry as a wkt string. My question is, has anyone found a way to read a table with a query as a table name like this? Or does anyone see anything wrong with my code? Or perhaps another way? thanks