本文介绍了pyspark读取格式jdbc生成ORA-00903:无效的表名错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

With a pysqpark running on a remote server, I am able to connect to an Oracle database on another server with jdbc, but any valid query I run returns a ORA-00903: invalid table name Error.

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

I am able to connect to the database from my local machine with cx_Oracle or pyodbc. When I connect from local those queries that return the above error run without problems.

我改变了在本地或远程运行的查询,但是无论我运行哪种有效查询

I've varied the queries that I run in either locally or remotely, but no matter what type of valid query I run

  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}"

以下是我尝试过的某些查询的变体.基本上,我认为我已经用尽了大小写表和视图名称的组合,并以;结尾或不以;结尾.

The below are variations of some of the queries I've tried. Basically, I think I've exhausted the combinations of upper/lower case table and view names, terminated with or without a ;.

  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命令:

Finally, with the above settings, I run the below pyspark command:

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

引起错误的原因(全部):

Which gives rise to the error (in full):

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)

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

My gut feeling is that this isn't something in my code, but some setting either on the server or in the driver that I don't know how to access or control.

如果有人可以告诉我如何调试该问题或直接解决该问题,我将不胜感激.谢谢.

I'd be really grateful if someone could either tell me how I might go about debugging the issue or directly fix it. Thank you.

推荐答案

根据文档中的dbtable:

因此,在您的示例中,您可以执行以下操作:

So in your examples you could do:

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

(可选)具有别名:

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

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

As an alternative you could use query instead of (not as well as) dbtable:

...实际上是一回事,但可能会使您的代码更易懂(当然,完全是主观的),即:

... so effectively the same thing, but might make your code more understandable (entirely subjective, of course), i.e. something like:

query = 'SELECT owner, table_name FROM ALL_TABLES'

然后:

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

这篇关于pyspark读取格式jdbc生成ORA-00903:无效的表名错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 04:34