开发环境: IDEA
本人搭建MySQL数据库环境版本: 5.7.28
具体搭建教程见: mysql-5.7.28详细安装教程及配置_xitaocao9352的博客-CSDN博客
(1) 需要导入pom依赖
mysql mysql-connector-java5.1.30
若需要导入mysql-connector-java相关jar包,见百度链接:链接:https://pan.baidu.com/s/1QnqSEMSmV2yRIXDLUN7jrw
提取码:iuvc
(2) 通过sparkSQL构造数据集Dataframe
val conf = new SparkConf().setAppName("WriteDataToMySQL").setMaster("local[*]") val sparkSession = SparkSession.builder().config(conf).getOrCreate() val sc = sparkSession.sparkContext val spark = new SQLContext(sc) val dataframe = spark.createDataframe(Seq( (1, 1, "2", "5"), (2, 2, "3", "6"), (3, 2, "35", "68"), (6, 2, "34", "67"), (7, 2, "38", "68"), (9, 2, "36", "69"), (10, 3, null, null), (11, 3, null, "43") )).toDF("id", "label", "col1", "col2") dataframe.show()
(3)将dataframe结构化数据写入到MySQL
val jdbl_url = "jdbc:mysql://IP地址:3306/数据库" //需要提前在Mysql上创建好数据库 val table = "test" val username = "root" val userpwd = "123456" dataframe.write .format("jdbc") .option("url", jdbl_url) //数据库连接url .option("dbtable", table) //业务表 .option("user", username) .option("password", userpwd) .mode(SaveMode.Append) //可以配置插入mode,overwrite覆盖,append追加,ignore忽略,error默认表存在报错 .save()
说明:数据写入过程,只需要提前在MySQL数据库创建数据库,表结构等无需创建,程序自动会创建。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)