1:载mysql-connector 放入 jars下
2:在spark-env.sh中 配置EXTRA_SPARK_CLASSPATH环境变量
3:export SPARK_CLASSPATH=/opt/spark/spark-2.4.0-bin-hadoop2.7/jars
from pyspark.sql import SparkSession
from pyspark.sql import SQLContext
sc = SparkSession.builder.appName("Python Spark SQL basic example")\
.config('spark.some.config,option0','some-value')\
.getOrCreate()
ctx = SQLContext(sc)
jdbcDf=ctx.read.format("jdbc").options(url="jdbc:mysql://localhost:3306/stock",
driver="com.mysql.jdbc.Driver",
dbtable="(SELECT * FROM stock) tmp",user="root",
password="0845").load()
print(jdbcDf.printSchema())
print(jdbcDf.show())
root
|-- open: double (nullable = true)
|-- high: double (nullable = true)