2017-05-12 135 views
1

我想從使用pyspark的sql server讀取數據。下面提到的代碼在使用以下命令(我傳遞sqljdbc驅動程序路徑)執行時工作正常,但當我嘗試使用PyCharm IDE(在Windows上)運行它時失敗。pyspark從sql server讀取數據

spark-submit --driver-class-path C:\drivers\sqljdbc_6.0.8112.100_enu\sqljdbc_6.0\enu\jre8\sqljdbc42.jar ReadSQLServerData.py 

如何加入或設置驅動器路徑,同時通過PyCharm IDE中運行相同的代碼?

代碼:

from pyspark.sql import SQLContext, Row 
from pyspark import SparkConf, SparkContext 

conf = SparkConf().setAppName("ReadSQLServerData") 
sc = SparkContext(conf=conf) 
query = "(SELECT top 10 * from users) as users" 
sqlctx = SQLContext(sc) 

df = sqlctx.read.format("jdbc").options(url="jdbc:sqlserver://mssqlserver:1433;database=user_management;user=pyspark;password=pyspark", dbtable=query).load() 

例外:

Traceback (most recent call last): 
    File "H:/Mine/OneDrive/Python/PySpark01/ReadSQLServerData.py", line 9, in <module> 
    df = sqlctx.read.format("jdbc").options(url="jdbc:sqlserver://mssqlserver:1433;database=user_management;user=pyspark;password=pyspark", dbtable=query).load() 
    File "C:\spark\python\pyspark\sql\readwriter.py", line 155, in load 
    return self._df(self._jreader.load()) 
    File "C:\spark\python\lib\py4j-0.10.4-src.zip\py4j\java_gateway.py", line 1133, in __call__ 
    File "C:\spark\python\pyspark\sql\utils.py", line 63, in deco 
    return f(*a, **kw) 
    File "C:\spark\python\lib\py4j-0.10.4-src.zip\py4j\protocol.py", line 319, in get_return_value 
py4j.protocol.Py4JJavaError: An error occurred while calling o27.load. 
: java.sql.SQLException: No suitable driver 
    at java.sql.DriverManager.getDriver(DriverManager.java:315) 
    at org.apache.spark.sql.execution.datasources.jdbc.JDBCOptions$$anonfun$7.apply(JDBCOptions.scala:84) 
    at org.apache.spark.sql.execution.datasources.jdbc.JDBCOptions$$anonfun$7.apply(JDBCOptions.scala:84) 
    at scala.Option.getOrElse(Option.scala:121) 
    at org.apache.spark.sql.execution.datasources.jdbc.JDBCOptions.<init>(JDBCOptions.scala:83) 
    at org.apache.spark.sql.execution.datasources.jdbc.JDBCOptions.<init>(JDBCOptions.scala:34) 
    at org.apache.spark.sql.execution.datasources.jdbc.JdbcRelationProvider.createRelation(JdbcRelationProvider.scala:32) 
    at org.apache.spark.sql.execution.datasources.DataSource.resolveRelation(DataSource.scala:330) 
    at org.apache.spark.sql.DataFrameReader.load(DataFrameReader.scala:152) 
    at org.apache.spark.sql.DataFrameReader.load(DataFrameReader.scala:125) 
    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) 

回答

0

不知道,如果你想通了這一點,但想我能幫助別人。

你必須設置驅動級的路徑,你可以把它作爲像下面

spark = SparkSession \ 
.builder \ 
.appName("Python Spark SQL basic example") \ 
.config("spark.driver.extraClassPath","/Users/Desktop/drivers/sqljdbc42.jar") \ 
.getOrCreate() 
配置選項