Spark - PySpark sql 错误

Posted

技术标签:

【中文标题】Spark - PySpark sql 错误【英文标题】:Spark - PySpark sql error 【发布时间】:2016-10-14 06:08:36 【问题描述】:

我有一个简单的 pyspark 代码,但我无法运行它。我尝试在 Ubuntu 系统上运行它并使用 PyCharm IDE。我想连接到 Oracle XE 数据库并且我想打印我的测试表。

我的 spark python 代码来了:

from pyspark import SparkContext
from pyspark.sql import SQLContext

sc = SparkContext()

sqlContext = SQLContext(sc)

demoDf = sqlContext.read.format("jdbc").options(
    url="jdbc:oracle:thin:@10.10.10.10:1521:XE",
    driver="oracle.jdbc.driver.OracleDriver",
    table="tst_table",
    user="xxx",
    password="xxx").load()

demoDf.show()

这是我的踪迹:

Traceback (most recent call last):
  File "/home/kebodev/PycharmProjects/spark_tst/cucc_spark.py", line 13, in <module>
    password="xxx").load()
  File "/home/kebodev/spark-2.0.1/python/pyspark/sql/readwriter.py", line 153, in load
    return self._df(self._jreader.load())
  File "/home/kebodev/spark-2.0.1/python/lib/py4j-0.10.3-src.zip/py4j/java_gateway.py", line 1133, in __call__
  File "/home/kebodev/spark-2.0.1/python/pyspark/sql/utils.py", line 63, in deco
    return f(*a, **kw)
  File "/home/kebodev/spark-2.0.1/python/lib/py4j-0.10.3-src.zip/py4j/protocol.py", line 319, in get_return_value
py4j.protocol.Py4JJavaError: An error occurred while calling o27.load.
: java.lang.RuntimeException: Option 'dbtable' not specified
    at scala.sys.package$.error(package.scala:27)
    at org.apache.spark.sql.execution.datasources.jdbc.JDBCOptions$$anonfun$2.apply(JDBCOptions.scala:30)
    at org.apache.spark.sql.execution.datasources.jdbc.JDBCOptions$$anonfun$2.apply(JDBCOptions.scala:30)
    at scala.collection.MapLike$class.getOrElse(MapLike.scala:128)
    at org.apache.spark.sql.execution.datasources.CaseInsensitiveMap.getOrElse(ddl.scala:117)
    at org.apache.spark.sql.execution.datasources.jdbc.JDBCOptions.<init>(JDBCOptions.scala:30)
    at org.apache.spark.sql.execution.datasources.jdbc.JdbcRelationProvider.createRelation(JdbcRelationProvider.scala:33)
    at org.apache.spark.sql.execution.datasources.DataSource.resolveRelation(DataSource.scala:330)
    at org.apache.spark.sql.DataFrameReader.load(DataFrameReader.scala:149)
    at org.apache.spark.sql.DataFrameReader.load(DataFrameReader.scala:122)
    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:237)
    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:745)


Process finished with exit code 1

有人可以帮助我吗?

【问题讨论】:

【参考方案1】:

像这样从table改成dbtable

demoDf = sqlContext.read.format("jdbc").options(
    url="jdbc:oracle:thin:@10.10.10.10:1521:XE",
    driver="oracle.jdbc.driver.OracleDriver",
    dbtable="tst_table",
    user="xxx",
    password="xxx").load()

【讨论】:

哦,谢谢你.. 现在我得到:java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver 我将应用你的答案,但你能帮我吗? oracle驱动放在哪里? :) 谢谢! 您必须将 oracle jdbc 驱动程序添加到您的项目中 ***.com/a/33831421/5673997 @solarenqu 你能看看这个答案吗? 谢谢你,我读到了,我在 spark-defaults.conf 中添加了这一行:spark.driver.extraClassPath /Users/gabor_dev/Documents/ojdbc/ojdbc6.jar 但我仍然收到此错误. 如果我这样运行: sh spark-submit --jars /Users/gabor_dev/Documents/ojdbc/ojdbc6.jar /Users/gabor_dev/PycharmProjects/spark_new_test/load.py 这样它的工作方式。 . :) 但不适用于 pycharm :(【参考方案2】:

试试这样的:

def testQuery(query):
        df = sqlContext.read.format("jdbc").options(
             url="jdbc:oracle:thin:@10.10.10.10:1521:XE",
             driver="oracle.jdbc.driver.OracleDriver",
             dbtable="( " + query + " ) as temp",
             user="xxx",
             password="xxx").load()
        return df

【讨论】:

以上是关于Spark - PySpark sql 错误的主要内容,如果未能解决你的问题,请参考以下文章

创建 SparkSession 的 Spark 3.0.0 错误:pyspark.sql.utils.IllegalArgumentException:<exception str() fail

Pyspark:使用 Python 从 Spark 2.4 连接到 MS SQL Server 2017 时没有合适的驱动程序错误

如何处理我在尝试通过 Pyspark 从 SQL 写入 Kudu 时遇到的这个错误

pyspark.sql 无法实例化 HiveMetaStoreClient - noclassfound from org.apache.commons.dbcp.connectionfactory

Apache Spark JDBC SQL 注入 (pyspark)

Pyspark:spark-submit 不像 CLI 那样工作