1.安装启动检查Mysql服务。
netstat -tunlp (3306)
service mysql start sudo netstat -tap | grep mysql
–cp /usr/local/hive/lib/mysql-connector-java-5.1.40-bin.jar /usr/local/spark/jars
pyspark \ --jars /usr/local/spark/jars/mysql-connector-java-8.0.25/mysql-connector-java-8.0.25.jar \ --driver-class-path /usr/local/spark/jars/mysql-connector-java-8.0.25/mysql-connector-java-8.0.25.jar
3.启动 Mysql shell,新建数据库spark,表student。
select * from student;
sudo mysql -u root -p
mysql> create database spark; mysql> use spark; mysql> create table student (id int(4), name char(20), gender char(4), age int(4)); mysql> alter table student change id id int auto_increment primary key; mysql> insert into student values(1,‘Xueqian‘,‘F‘,23); mysql> insert into student values(2,‘Weiliang‘,‘M‘,24); mysql> select * from student;
4.spark读取MySQL数据库中的数据
spark.read.format("jdbc").option("url", "jdbc:mysql://localhost:3306/spark?useSSL=false") ... .load()
5.spark向MySQL数据库写入数据
studentDF.write.format(‘jdbc’).option(…).mode(‘append’).save()
mysql> select * from student;