spark jdbc读取并发度优化 GC overhead

背景

spark 读取mysql数据库表中数据表,数据体量五百万条

代码:

		String url = "jdbc:mysql://localhost:3306/demo?useSSL=true&characterEncoding=utf-8&serverTimezone=UTC";
		// 查找的表名
		String table = "test";
		// mysql 增加数据库的用户名(user)密码(password),指定test数据库的驱动(driver)
		Properties connectionProperties = new Properties();
		connectionProperties.put("user", "***");
		connectionProperties.put("password", "***");
//		connectionProperties.put("password", "***");
		connectionProperties.put("driver", "com.mysql.cj.jdbc.Driver");
		// 读取表中所有数据
		Dataset<Row> datasetRdd = session.read().jdbc(url, table, connectionProperties);

错误:

1、java.lang.OutOfMemoryError: GC overhead limit exceeded
2、Container exited with a non-zero exit code 143.
Killed by external signal
在这里插入图片描述

错误原因分析:

没有设置分区,该操作的并发度为1,你所有的数据都会在一个partition中进行操作,意味着无论你给的资源有多少,只有一个task会执行任务,执行效率可想而之,并且在稍微大点的表中进行操作分分钟就会OOM。

解决方案:

        Dataset<Row> count = session.read().jdbc(url, "(select count(*) as allnum from demo) as t1", connectionProperties);
		long allcount = count.head().getAs("allnum");
		
		Dataset<Row> datasetRdd = session.read().jdbc(url, table, "id", 1, allcount, 10, connectionProperties);

【注】上边的(select count(*) as allnum from demo) as t1,一定要加别名as t1,否则报错信息如下:java.sql.SQLSyntaxErrorException: Every derived table must have its own alias

参考:
1、https://blog.csdn.net/weixin_44769733/article/details/102491612
2、https://www.cnblogs.com/Kaivenblog/p/12622008.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值