sparksql如何将数据批量插入mysql?

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

全部。最近我需要使用sparksql向mysql中插入大约100000000个数据,但是速度很慢,大约需要1个小时。有人能有一个有效的方法插入吗?
mysql配置=>

prop.setProperty("user", "user")
prop.setProperty("password", "password")
prop.setProperty("rewriteBatchedStatements", "true")
prop.setProperty("batchsize", "1000000")
prop.setProperty("numPartitions", "3000")
56lgkhnf

56lgkhnf1#

尝试添加 ?rewriteBatchedStatements=true 到您的mysql uri。您的uri应该是这样的: jdbc:mysql://host:port/db?rewriteBatchedStatements=true 希望有帮助。

更新

试试看,对我有用。设置 driver 财产起了作用。

val prop = new Properties()

prop.setProperty("user", dbUser)
prop.setProperty("password", dbPassword)
prop.put("driver", "com.mysql.jdbc.Driver");

df.write.mode("append").jdbc("jdbc:mysql://" + dbHost + "/" + dbName + "?rewriteBatchedStatements=true", "TABLE_NAME", prop)

我还必须导入javamysql连接器。

<dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.39</version>
    </dependency>

相关问题