将SQLserver jdbc驱动程序连接到Dataproc群集

时间:2018-11-26 09:57:29

标签: pyspark pyspark-sql google-cloud-dataproc

我正在研究用于分析航空数据的PySpark应用程序。该数据库是MS SQLServer DB。连接到服务器上的数据库时。我收到“没有合适的驱动程序”的错误消息。但是,当我使用CLI在本地计算机上运行并将JDBC驱动程序jar文件添加到driver-class-path时,它将运行并与DB连接。但是,当我尝试在Dataproc群集上运行时,它会引发“没有合适的驱动程序”错误。

代码段如下:

from pyspark import SparkContext
from pyspark.sql import SparkSession
from pyspark.sql.dataframe import DataFrame
from pyspark.sql.functions import *

spark = SparkSession.builder
    .appName('Test')
    .getOrCreate()

df = spark.read.format("jdbc").options(
        url="jdbc:sqlserver:XYXYXY",
        database="data1",
        user="YYYY", password="XXXX",
        dbtable="db")
    .load()

错误是:

Py4JJavaError: An error occurred while calling o209.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:34)
    at org.apache.spark.sql.execution.datasources.DataSource.resolveRelation(DataSource.scala:307)
    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)

还有其他方法可以将JDBC jar文件添加到Dataproc集群吗?

1 个答案:

答案 0 :(得分:0)

这里的questionanswer非常相似,显示了如何使用gcloud命令将JDBC驱动程序添加到Spark Driver类路径:

$ gcloud dataproc jobs submit spark ... \
    --jars=gs://<BUCKET>/<DIRECTORIES>/<JAR_NAME> \
    --properties=spark.driver.extraClassPath=<JAR_NAME>