获取PSQLException:ERROR:在带有Postgres的spark jdbc中使用查询而不是tablename时,在“SELECT”处或附近出现语法错误

2024-09-27 00:21:45 发布

您现在位置:Python中文网/ 问答频道 /正文

对于以下通用sql:

 showTablesSql =  """SELECT table_catalog,table_schema,table_name
    FROM information_schema.tables
    ORDER BY table_schema,table_name"""

当提交给spark jdbc进行postgresql时,发生以下异常:

py4j.protocol.Py4JJavaError: An error occurred while calling o34.load.
: org.postgresql.util.PSQLException: ERROR: syntax error at or near "SELECT"
  Position: 15
    at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2578)

下面是正在使用的代码:

url =  f"jdbc:postgresql://{c['db.host']}/{c['db.name']}?user={c['db.user']}&password={c['db.password']}"
print(url)
empDF = spark.read \
    .format("jdbc") \
    .option("url", url) \
    .option("dbtable", showTablesSql) \
    .option("user", c['db.user']) \
    .option("password", c['db.password']) \
    .load()

以下是堆栈跟踪详细信息:

Setting default log level to "WARN".
To adjust logging level use sc.setLogLevel(newLevel). For SparkR, use setLogLevel(newLevel).
jdbc:postgresql://localhost/bluej?user=bluej&password=mypassword
Traceback (most recent call last):
  File "/git/bluej/fusion/python/pointr/bluej/util/sparkmgr.py", line 37, in <module>
    tab = readTab(db, tname)
  File "/git/bluej/fusion/python/pointr/bluej/util/sparkmgr.py", line 23, in readTab
    empDF = spark.read \
  File "/shared/spark3/python/pyspark/sql/readwriter.py", line 166, in load
    return self._df(self._jreader.load())
  File "/shared/spark3/python/lib/py4j-0.10.8.1-src.zip/py4j/java_gateway.py", line 1285, in __call__
  File "/shared/spark3/python/pyspark/sql/utils.py", line 98, in deco
    return f(*a, **kw)
  File "/shared/spark3/python/lib/py4j-0.10.8.1-src.zip/py4j/protocol.py", line 326, in get_return_value
py4j.protocol.Py4JJavaError: An error occurred while calling o34.load.
: org.postgresql.util.PSQLException: ERROR: syntax error at or near "SELECT"
  Position: 15
    at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2578)
    at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2313)
    at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:331)
    at org.postgresql.jdbc.PgStatement.executeInternal(PgStatement.java:448)
    at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:369)
    at org.postgresql.jdbc.PgPreparedStatement.executeWithFlags(PgPreparedStatement.java:159)
    at org.postgresql.jdbc.PgPreparedStatement.executeQuery(PgPreparedStatement.java:109)
    at org.apache.spark.sql.execution.datasources.jdbc.JDBCRDD$.resolveTable(JDBCRDD.scala:61)
    at org.apache.spark.sql.execution.datasources.jdbc.JDBCRelation$.getSchema(JDBCRelation.scala:226)
    at org.apache.spark.sql.execution.datasources.jdbc.JdbcRelationProvider.createRelation(JdbcRelationProvider.scala:35)
    at org.apache.spark.sql.execution.datasources.DataSource.resolveRelation(DataSource.scala:339)
    at org.apache.spark.sql.DataFrameReader.loadV1Source(DataFrameReader.scala:240)
    at org.apache.spark.sql.DataFrameReader.$anonfun$load$2(DataFrameReader.scala:229)
    at scala.Option.getOrElse(Option.scala:189)
    at org.apache.spark.sql.DataFrameReader.load(DataFrameReader.scala:229)
    at org.apache.spark.sql.DataFrameReader.load(DataFrameReader.scala:179)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at py4j.reflection.MethodInvoker.invoke(MethodInvoker.java:244)
    at py4j.reflection.ReflectionEngine.invoke(ReflectionEngine.java:357)
    at py4j.Gateway.invoke(Gateway.java:282)
    at py4j.commands.AbstractCommand.invokeMethod(AbstractCommand.java:132)
    at py4j.commands.CallCommand.execute(CallCommand.java:79)
    at py4j.GatewayConnection.run(GatewayConnection.java:238)
    at java.base/java.lang.Thread.run(Thread.java:834)

Tags: orgdbsqlpostgresqlapacheloadjavaat
1条回答
网友
1楼 · 发布于 2024-09-27 00:21:45

@BjarniRagnarsson在评论中暗示dbtable实际上是一个subquery。我从尊敬的@zero323网站上找到了一些关于这个的信息 https://stackoverflow.com/a/32629170/1056563

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:

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

在使sql成为subquery时,我看到了正确解析的结果:没有数据返回,但很可能会返回

相关问题 更多 >

    热门问题