2000字范文,分享全网优秀范文,学习好帮手!
2000字范文 > spark写入oracle 优化 spark读写数据库大表分区性能优化

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

时间:2020-11-10 01:34:48

相关推荐

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

spark读写数据库大表分区性能优化:经常会遇到spark读写数据库再做分析,像mysql或oracle。

在数据量很大的情况下,如果只有一个worker一个excutor一个task,那你excutor的内存足够大或者worker足够多,没问题,否则就要内存溢出Out of memory。

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

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

Datasetdataset=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,numPartitionsmust be specified. They describe how to partition the table when reading in parallel from multiple workers.partitionColumnmust be a numeric column from the table in question. Notice thatlowerBoundandupperBoundare 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 callingcoalesce(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 >= '-05-01') as T")

注意:要设置数据库连接数,如果过少,就会出异常

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。