rdd中的数据放到mysql表中

uinbv5nw  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(425)

我必须将数据从spark rdd移动到mysql表。有人能帮我吗?

3bygqnnd

3bygqnnd1#

一个可能的例子,但这里有一个风格让你去:

import org.apache.spark.sql.SaveMode

// Write out and create the table as well to mysql via DF Writer API 

val RDD = spark.sparkContext.parallelize(Seq(
    ("A2", "X", "done"),
    ("A2", "Y", "done"),
    ("C2", "Y", "done"),
    ("B2", "Y", "done"),
    ("Z2", "Z", "done")
  ))

val jdbcDF = RDD.toDF("Company", "Type", "Status")

// Saving data to a JDBC source - creates the table as well 

jdbcDF.write
   .format("jdbc")
   .option("url", "jdbc:mysql://db4free.net:3306/mySQLDB")
   .option("dbtable", "tabname")
   .option("user", "xxx")
   .option("password", "yyy")
 //.save()
   .mode(SaveMode.Append)

你可能需要设置驱动程序,因为我在databricks下运行了这个。

相关问题