在2-3个小时内将1.5亿条记录加载到MySQL

时间:2018-10-15 23:31:44

标签: mysql amazon-web-services apache-spark amazon-emr

我有一些火花散文,它们在进行一些计算,然后将其插入到MySQL表中,所有计算都在40-50分钟内完成。写入表需要2-3个小时(取决于数据库使用情况)。我试图做批处理大小

val db_url_2 = "jdbc:mysql://name.amazonaws.com:port/db_name?rewriteBatchedStatements=true" 

df_trsnss.write.format("jdbc").option("url", db_url_2).option("dbtable", output_table_name).option("user", db_user).option("password", db_pwd).option("truncate","true").option("batchsize", 5000).mode("overwrite").save()

但是它仍然需要花费很多时间来加载,我每天仅花2到4个小时来计算和将数据写入表中就不起。

  

有什么方法可以加快此过程吗?

开始考虑写入CSV,然后将其从CSV加载到db中,这样我可以减少EMR时间。

1 个答案:

答案 0 :(得分:1)

尝试类似的方法-实际上是《 DataBricks指南》中的

JDBC写入

Spark的分区规定了用于通过JDBC API推送数据的连接数。您可以根据现有分区数通过调用coalesce()或repartition()来控制并行性。减少分区数量时调用合并,而在增加分区数量时重新分区。

尝试看看这与您的写入方法相比如何,并让我们知道。

import org.apache.spark.sql.SaveMode

val df = spark.table("diamonds")
println(df.rdd.partitions.length)

// Given the number of partitions above, you 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, "diamonds", connectionProperties)