2

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

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

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

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

以下是我尝试过的一些查询的变体。基本上,我想我已经用尽了大写/小写表格和视图名称的组合,以或不以;.

  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)

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

如果有人能告诉我如何调试问题或直接修复它,我将不胜感激。谢谢你。

4

1 回答 1

5

从文档dbtable

应该读取或写入的 JDBC 表。请注意,在读取路径中使用它时,可以使用在FROMSQL 查询的子句中有效的任何内容。例如,您也可以在括号中使用子查询,而不是完整的表。

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

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

可选的别名:

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

作为替代方案,您可以使用query代替 (not as well as) dbtable

将用于将数据读入 Spark 的查询。指定的查询将用括号括起来并用作FROM子句中的子查询。Spark 还将为子查询子句分配一个别名。

...如此有效地同一件事,但可能使您的代码更易于理解(当然,完全是主观的),例如:

query = 'SELECT owner, table_name FROM ALL_TABLES'

进而:

  jdbc_df = spark.read.format("jdbc").option("url", url) \
                                     .option("query", query) \
                                     .option("driver", driver) \
                                     ...
于 2019-09-05T11:53:56.790 回答