首页 / MYSQL / spark 读取mysql分区优化
spark 读取mysql分区优化
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了spark 读取mysql分区优化,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2086字,纯文字阅读大概需要3分钟。
内容图文
![spark 读取mysql分区优化](/upload/InfoBanner/zyjiaocheng/908/36ce8717e6344346bbae63a0dcdee146.jpg)
当数据增加,我们又无法无限制的增加硬件,我们就要利用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, upperBound |
These 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. |
numPartitions |
The 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 读取mysql分区优化全部内容,希望文章能够帮你解决spark 读取mysql分区优化所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。