09 spark连接mysql数据库
Posted 金腰带小拳石
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了09 spark连接mysql数据库相关的知识,希望对你有一定的参考价值。
安装启动检查mysql服务。
spark 连接mysql驱动程序。
启动 Mysql shell,新建数据库spark,表student。
create database spark; use spark; create table student (id int(4), name char(20), gender char(4), age int(4)); alter table student change id id int auto_increment primary key; insert into student values(1,\'Xueqian\',\'F\',23); insert into student values(2,\'Weiliang\',\'M\',24); select * from student;
spark读取MySQL数据库中的数据
jdbcDF = spark.read.format("jdbc").option("url", "jdbc:mysql://localhost:3306/spark").option("driver","com.mysql.jdbc.Driver").option("dbtable", "student").option("user", "root").option("password", "123456").load() jdbcDF.show()
spark向MySQL数据库写入数据
from pyspark.sql.types import Row from pyspark.sql.types import StructType from pyspark.sql.types import StructField from pyspark.sql.types import StringType from pyspark.sql.types import IntegerType studentRDD = spark.sparkContext.parallelize(["3 Rongcheng M 26","4 Guanhua M 27"]).map(lambda line : line.split(" ")) schema = StructType([StructField("name", StringType(), True),StructField("gender", StringType(), True),StructField("age",IntegerType(), True)]) rowRDD = studentRDD.map(lambda p : Row(p[1].strip(), p[2].strip(),int(p[3]))) studentDF = spark.createDataFrame(rowRDD, schema) prop = {} prop[\'user\'] = \'root\' prop[\'password\'] = \'hadoop\' prop[\'driver\'] = "com.mysql.jdbc.Driver" studentDF.write.jdbc("jdbc:mysql://localhost:3306/spark",\'student\',\'append\', prop)
select * from student;
以上是关于09 spark连接mysql数据库的主要内容,如果未能解决你的问题,请参考以下文章