如何将 DataFrame 写入 MySQL 表

Posted

技术标签:

【中文标题】如何将 DataFrame 写入 MySQL 表【英文标题】:How to write a DataFrame into a MySQL table 【发布时间】:2017-11-09 23:11:43 【问题描述】:

抱歉,如果听起来含糊不清,但可以解释将现有 DataFrame“df”写入 mysql 表的步骤,比如“product_mysql”,反之亦然。

【问题讨论】:

【参考方案1】:

请看this databricks article : Connecting to SQL Databases using JDBC.

import org.apache.spark.sql.SaveMode

val df = spark.table("...")
println(df.rdd.partitions.length)
// given the number of partitions above, users can reduce the partition value by calling coalesce() or increase it by calling repartition() to manage the number of connections.
df.repartition(10).write.mode(SaveMode.Append).jdbc(jdbcUrl, "product_mysql", connectionProperties)

【讨论】:

感谢您的帮助拉姆!

以上是关于如何将 DataFrame 写入 MySQL 表的主要内容,如果未能解决你的问题,请参考以下文章