pyspark读取和写入mysql

读取mysql

from pyspark.sql import SparkSession
from pyspark.conf import SparkConf
from pyspark.sql import SQLContext

conf = SparkConf().setAppName('数据总览')
spark = SparkSession.builder.config(conf=conf).enableHiveSupport().getOrCreate()

url = 'jdbc:mysql://192.168.0.30:3306/test?useSSL=false'
ctx = SQLContext(spark)

df1 = ctx.read.format('jdbc').options(url=url, 
											 driver="com.mysql.jdbc.Driver",
                                             dbtable="test",
                                             user="root",
                                             password="root").load()

写入

url = 'jdbc:mysql://192.168.0.30:3306/test?useSSL=false'
table = 'test'
auth_mysql = {"user": "root", "password": "root"}
'''
:param mode: specifies the behavior of the save operation when data already exists.
* ``append``: Append contents of this :class:`DataFrame` to existing data.
* ``overwrite``: Overwrite existing data.
* ``ignore``: Silently ignore this operation if data already exists.
* ``error`` or ``errorifexists`` (default case): Throw an exception if data already exists.
'''
df_res.write.jdbc(url, table, mode='overwrite', properties=auth_mysql)

你可能感兴趣的:(python,Hadoop,Spark)