Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write a DataFrame into a MySQL table

Sorry if it sounds vague but can one explain the steps to writing an existing DataFrame "df" into MySQL table say "product_mysql" and the other way around.

like image 847
adarsh16 Avatar asked Nov 09 '17 23:11

adarsh16


1 Answers

please see 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)
like image 175
Ram Ghadiyaram Avatar answered Sep 29 '22 11:09

Ram Ghadiyaram