从pyspark连接到mssql

enyaitl3  于 2021-05-16  发布在  Spark
关注(0)|答案(1)|浏览(431)

我正在尝试使用spark.read.jdbc从pyspark连接到mssqldb。

import os
from pyspark.sql import *
from pyspark.sql.functions import *
from pyspark import SparkContext;
from pyspark.sql.session import SparkSession
sc = SparkContext('xx')
spark = SparkSession(sc)

    spark.read.jdbc('DESKTOP-XXXX\SQLEXPRESS',
"""(select COL1, COL2 from tbl1 WHERE COL1 = 2) """,
properties={'user': sa, 'password': 12345, 'driver': xxxx})

我不知道 sc = SparkContext('xx') 以及 'driver': x 我应该传递哪些参数?

6yjfywim

6yjfywim1#

替换 serveraddress 数据库地址:

sc = SparkContext()
spark = SparkSession(sc)
spark.read \
     .format('jdbc') \
     .option('url', 'jdbc:sqlserver://serveraddress:1433') \
     .option('user', 'sa') \
     .option('password', '12345') \
     .option('dbtable', '(select COL1, COL2 from tbl1 WHERE COL1 = 2)')

相关问题