spark 读取mysql分区优化

当数据增加,我们又无法无限制的增加硬件,我们就要利用RDD的partition。将获取一个大表的任务拆分成多个任务,一个一个来执行,每个任务只获取一小部分数据,这样通过多个连接同时去取数据,速度反而更快。

 

我的配置目前是 master 1 8g,slave 3 8g

 

Dataset<Row> dataset = spark.read().format("jdbc")
.option("url", JDBCUtil.getJdbcUrl(datasourceModel))
.option("dbtable", tableName)
.option("user", datasourceModel.getUserName())
.option("password", datasourceModel.getPassword())
.option("partitionColumn", "ID")
.option("lowerBound", 10000)
.option("upperBound", 100000000)
.option("numPartitions", 10000)
.load();

参数具体意义

partitionColumn, lowerBound, upperBoundThese options must all be specified if any of them is specified. In addition, numPartitions must be specified. They describe how to partition the table when reading in parallel from multiple workers. partitionColumn must be a numeric column from the table in question. Notice that lowerBound and upperBound are just used to decide the partition stride, not for filtering the rows in table. So all rows in the table will be partitioned and returned. This option applies only to reading.
numPartitionsThe maximum number of partitions that can be used for parallelism in table reading and writing. This also determines the maximum number of concurrent JDBC connections. If the number of partitions to write exceeds this limit, we decrease it to this limit by calling coalesce(numPartitions) before writing.

partitionColumn:根据哪个字段分区,必须是数字类型,int是可以的,一般用id

lowerBound:分区下界,假如是10000,那么10000条数据之前都是在一个任务执行

upperBound:分区上届,lowerBound和upperBound的数据会被拆分,而边界外围的会单独作为分区

numPartitions:分区边界之间的数据要分多少分区。

至于到底分了多少块,边界之外的数据怎么分的块,没必要纠结,只要知道,数据肯定是全部取回来了。

 

另外只需要部分数据的,可以按照sql的方式:

.option("dbtable", "test_table")

可以改写成:

.option("dbtable", "(select * from test_table where dt >= '2017-05-01') as T")

参考:

http://spark.apache.org/docs/latest/configuration.html

spark读写数据库大表分区性能优化

 

转载于:https://www.cnblogs.com/itxw/p/11021706.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值