我正在尝试使用pyspark连接到MS-SQL数据库:
"jdbcDF = spark.read.format("jdbc") \
.option("url", "jdbc:sqlserver://localhost:1433;databaseName=xxx") \
.option("dbtable", "xxx") \
.option("user", "xxx") \
.option("password", "xxx").load()"
但出现以下错误
Error occurring post execution is: Py4JJavaError: An error occurred while calling o148.load.
: java.sql.SQLException: No suitable driver
我该如何解决?
答案 0 :(得分:0)
您需要将驱动程序复制到python脚本所在的文件夹中,并且必须使用.set
选项定义它,如下所示:
.set("spark.driver.extraClassPath","mssql-jdbc-7.4.1.jre8.jar")
用于火花上下文,连接到ms sql以及从ms sql中选择1个表的完整代码如下所示:
from pyspark import SparkContext, SparkConf, SQLContext
appName = "PySpark SQL Server Example - via JDBC"
master = "local"
conf = SparkConf() \
.setAppName(appName) \
.setMaster(master) \
.set("spark.driver.extraClassPath","mssql-jdbc-7.4.1.jre8.jar")
sc = SparkContext.getOrCreate(conf=conf)
sqlContext = SQLContext(sc)
spark = sqlContext.sparkSession
# Loading data from a JDBC source
jdbcDF = spark.read \
.format("jdbc") \
.option("url", "jdbc:postgresql:dbserver") \
.option("url", "jdbc:sqlserver://188.188.188.188:10004;databaseName=dbnme") \
.option("dbtable", "dbo.tablename") \
.option("user", "usernmame") \
.option("password", "pawwrod") \
.load()
您可以按照this教程来正确设置与MS SQL的连接。