我正在努力解决一件事。我有 700mb csv,其中包含超过 600 万行。过滤后含有~300万。
我需要通过 jdbc 将其直接写入 azure sql。速度 super 慢,输入300万行需要20分钟。
我的集群有 14GB 内存和 4 个核心。这是我的代码。
(clearedDF.repartition(4)
.write
.format("jdbc")
.option("driver", "com.microsoft.sqlserver.jdbc.SQLServerDriver")
.option("batchsize", 10000)
.option("url", jdbcUrl)
.option("dbtable", "dbo.weather")
.option("user", properties["user"])
.option("password", properties["password"])
.mode("append")
.save()
)
有什么办法可以加快这个过程吗?
最佳答案
谢谢Alex Ott 。发布您的suggestion作为帮助社区成员的答案。
“您可以根据现有分区数量调用coalesce(<N>)
或repartition(<N>)
来控制并行度。减少分区数量时调用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)
关于python - 如何提高jdbc的spark.write性能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68985396/