gpt4 book ai didi

oracle - pyspark读取格式jdbc生成ORA-00903 : invalid table name Error

转载 作者:行者123 更新时间:2023-12-02 19:52:37 27 4
gpt4 key购买 nike

通过在远程服务器上运行 pysqpark,我可以使用 jdbc 连接到另一台服务器上的 Oracle 数据库,但我运行的任何有效查询都会返回 ORA-00903: invalid table name Error .

我可以使用 cx_Oraclepyodbc 从本地计算机连接到数据库。当我从本地连接时,返回上述错误的那些查询运行没有问题。

我改变了在本地或远程运行的查询,但无论我运行什么类型的有效查询

  ORACLE_JAR = "ojdbc7.jar"
JAR_LOC = os.path.join(os.environ["JARS_DIR"], ORACLE_JAR)
spark = SparkSession.builder \
.appName("GetData") \
.config("spark.jars", "local://" + JAR_LOC) \
.getOrCreate()

exadata_instances = ["xxx.yyy.zzz", "aaa.bbb.cc"]

db_host = "xxx.yyy.zzz"
user = 'username'
password = 'passW0rd'

driver = "oracle.jdbc.OracleDriver"
sid = "eee.fff.ggg"
address_string = ""

for exadata_instance in exadata_instances:
address_string += f"(ADDRESS=(PROTOCOL=TCP)(HOST={exadata_instance})(PORT=1521))"

tns = f"(DESCRIPTION= \
(ADDRESS_LIST= \
(LOAD_BALANCE=OFF) \
(FAILOVER=ON) \
{address_string}) \
(CONNECT_DATA=(SERVICE_NAME={sid})(SERVER=DEDICATED)))"
url = f"jdbc:oracle:thin:@{tns}"

以下是我尝试过的一些查询的变体。基本上,我认为我已经用尽了大/小写表和 View 名称的组合,无论是否有 ; 终止。

  dbtable = 'SELECT owner, table_name FROM all_tables'
dbtable = 'SELECT owner, table_name FROM all_tables;'
dbtable = 'SELECT owner, table_name FROM ALL_TABLES'
dbtable = 'SELECT owner, table_name FROM ALL_TABLES;'
dbtable = 'SELECT col1, col2 FROM V_MY_VIEW'
dbtable = 'SELECT col1, col2 FROM V_MY_VIEW;'
dbtable = 'SELECT COL1, COL2 FROM v_my_view'

最后,通过上述设置,我运行以下 pyspark 命令:

  jdbc_df = spark.read.format("jdbc").option("url", url) \
.option("dbtable", dbtable) \
.option("driver", driver) \
.option("user", user) \
.option("inferSchema", True) \
.option("password", password).load()

这会导致错误(完整):

Py4JJavaError                             Traceback (most recent call last)
in engine
----> 1 jdbc_df = spark.read.format("jdbc").option("url", url) .option("dbtable", dbtable) .option("driver", driver) .option("user", user) .option("inferSchema", True) .option("password", password).load()

/opt/cloudera/parcels/SPARK2/lib/spark2/python/pyspark/sql/readwriter.py in load(self, path, format, schema, **options)
163 return self._df(self._jreader.load(self._spark._sc._jvm.PythonUtils.toSeq(path)))
164 else:
--> 165 return self._df(self._jreader.load())
166
167 @since(1.4)

/conda/miniconda3/envs/python3.6.8/lib/python3.6/site-packages/py4j/java_gateway.py in __call__(self, *args)
1255 answer = self.gateway_client.send_command(command)
1256 return_value = get_return_value(
-> 1257 answer, self.gateway_client, self.target_id, self.name)
1258
1259 for temp_arg in temp_args:

/opt/cloudera/parcels/SPARK2/lib/spark2/python/pyspark/sql/utils.py in deco(*a, **kw)
61 def deco(*a, **kw):
62 try:
---> 63 return f(*a, **kw)
64 except py4j.protocol.Py4JJavaError as e:
65 s = e.java_exception.toString()

/conda/miniconda3/envs/python3.6.8/lib/python3.6/site-packages/py4j/protocol.py in get_return_value(answer, gateway_client, target_id, name)
326 raise Py4JJavaError(
327 "An error occurred while calling {0}{1}{2}.\n".
--> 328 format(target_id, ".", name), value)
329 else:
330 raise Py4JError(

Py4JJavaError: An error occurred while calling o1253.load.
: java.sql.SQLSyntaxErrorException: ORA-00903: invalid table name

at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:450)
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:399)
at oracle.jdbc.driver.T4C8Oall.processError(T4C8Oall.java:1059)
at oracle.jdbc.driver.T4CTTIfun.receive(T4CTTIfun.java:522)
at oracle.jdbc.driver.T4CTTIfun.doRPC(T4CTTIfun.java:257)
at oracle.jdbc.driver.T4C8Oall.doOALL(T4C8Oall.java:587)
at oracle.jdbc.driver.T4CPreparedStatement.doOall8(T4CPreparedStatement.java:225)
at oracle.jdbc.driver.T4CPreparedStatement.doOall8(T4CPreparedStatement.java:53)
at oracle.jdbc.driver.T4CPreparedStatement.executeForDescribe(T4CPreparedStatement.java:774)
at oracle.jdbc.driver.OracleStatement.executeMaybeDescribe(OracleStatement.java:925)
at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1111)
at oracle.jdbc.driver.OraclePreparedStatement.executeInternal(OraclePreparedStatement.java:4798)
at oracle.jdbc.driver.OraclePreparedStatement.executeQuery(OraclePreparedStatement.java:4845)
at oracle.jdbc.driver.OraclePreparedStatementWrapper.executeQuery(OraclePreparedStatementWrapper.java:1501)
at org.apache.spark.sql.execution.datasources.jdbc.JDBCRDD$.resolveTable(JDBCRDD.scala:62)
at org.apache.spark.sql.execution.datasources.jdbc.JDBCRelation.<init>(JDBCRelation.scala:113)
at org.apache.spark.sql.execution.datasources.jdbc.JdbcRelationProvider.createRelation(JdbcRelationProvider.scala:47)
at org.apache.spark.sql.execution.datasources.DataSource.resolveRelation(DataSource.scala:306)
at org.apache.spark.sql.DataFrameReader.load(DataFrameReader.scala:178)
at org.apache.spark.sql.DataFrameReader.load(DataFrameReader.scala:146)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at py4j.reflection.MethodInvoker.invoke(MethodInvoker.java:244)
at py4j.reflection.ReflectionEngine.invoke(ReflectionEngine.java:357)
at py4j.Gateway.invoke(Gateway.java:280)
at py4j.commands.AbstractCommand.invokeMethod(AbstractCommand.java:132)
at py4j.commands.CallCommand.execute(CallCommand.java:79)
at py4j.GatewayConnection.run(GatewayConnection.java:214)
at java.lang.Thread.run(Thread.java:748)

我的直觉是,这不是我的代码中的某些内容,而是服务器或驱动程序中的某些设置,我不知道如何访问或控制。

如果有人可以告诉我如何调试问题或直接修复它,我将非常感激。谢谢。

最佳答案

From the documentation对于dbtable:

The JDBC table that should be read from or written into. Note that when using it in the read path anything that is valid in a FROM clause of a SQL query can be used. For example, instead of a full table you could also use a subquery in parentheses.

所以在你的例子中你可以这样做:

dbtable = '(SELECT owner, table_name FROM ALL_TABLES)'

可选地使用别名:

dbtable = '(SELECT owner, table_name FROM ALL_TABLES) t'

作为替代方案,您可以使用 query 代替(不太好)dbtable:

A query that will be used to read data into Spark. The specified query will be parenthesized and used as a subquery in the FROM clause. Spark will also assign an alias to the subquery clause.

...实际上是同一件事,但可能会让你的代码更容易理解(当然,完全是主观的),即类似:

query = 'SELECT owner, table_name FROM ALL_TABLES'

然后:

  jdbc_df = spark.read.format("jdbc").option("url", url) \
.option("query", query) \
.option("driver", driver) \
...

关于oracle - pyspark读取格式jdbc生成ORA-00903 : invalid table name Error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57802017/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com