I am trying to pull data from Oracle DB and putting it to AWS S3 using Apache Spark 2.3.1. The job is running fine till the last stage and getting stuck there. I don't think the data is skewed because each stage has an equal number of records. Below is the query I'm using in spark.
url = "jdbc:oracle:thin:@IP:PORT/SID"
user = "user"
password = "password"
driver = "oracle.jdbc.driver.OracleDriver"
table = "table"
fetchSize = 1000
partitionColumn = "num_rows"
date1 = (datetime.today() - td(days=42)).date().strftime('%d-%b-%Y')
date2 = (datetime.today() - td(days=2)).date().strftime('%d-%b-%Y')
query = "(select min(rownum) as min, max(rownum) as max from "+table+" where date>='"+str(date1)+"' and date<='"+str(date2)+"') tmp1"
print(query)
DF = spark.read.format("jdbc").option("url", url) \
.option("dbtable", query) \
.option("user", user) \
.option("password", password) \
.option("driver", driver) \
.load()
lower_bound, upper_bound = DF.first()
lower_bound = int(lower_bound)
upper_bound = int(upper_bound)
numPartitions = int(upper_bound/fetchSize)+1
print(lower_bound,upper_bound)
print(numPartitions)
query = "(select t1.*, ROWNUM as num_rows from (select * from " + table + " where date>='"+str(date1)+"' and date<='"+str(date2)+"') t1) tmp2"
print(query)
DF = spark.read.format("jdbc").option("url", url) \
.option("dbtable", query) \
.option("user", user) \
.option("password", password) \
.option("fetchSize",fetchSize) \
.option("numPartitions", numPartitions) \
.option("partitionColumn", partitionColumn) \
.option("lowerBound", lower_bound) \
.option("upperBound", upper_bound) \
.option("driver", driver) \
.load()
path = "s3://my_path"
DF.write.mode("overwrite").parquet(path)
The code basically pulls last 42 days data and put it into S3 bucket. Below is the output till the write statement. Code was run on '10-Sep-2018'
(select min(rownum) as min, max(rownum) as max from table where date>='30-Jul-2018' and date<='08-Sep-2018') tmp1
(1, 2195427)
2196
(select t1.*, ROWNUM as num_rows from (select * from table where date>='30-Jul-2018' and date<='08-Sep-2018') t1) tmp2
As you can see,
- the total number of records = 2195427
- records per partition = 1000
- number of partitions = 2196
So the job has 2196 stages and each stage pulls 1000 records. The job is getting stuck at 2191/2196 with 5 more stages to go.
Hardware Specs:
I am using r4.xlarge machines. My cluster is 1 Master, 2 Slaves of r4.xlarge. Below are my driver and executor specs.
spark.driver.cores 8
spark.driver.memory 24g
spark.driver.memoryOverhead 3072M
spark.executor.cores 1
spark.executor.memory 3g
spark.executor.memoryOverhead 512M
spark.yarn.am.cores 1
spark.yarn.am.memory 3g
spark.yarn.am.memoryOverhead 512M
The stages 1 to 2191 got completed in 1.3 hrs but remaining 5 stages are stuck for more than three hours.
Please find the log here : https://github.com/rinazbelhaj/stackoverflow/blob/master/Spark_Log_10_Sept_2018
I am not able to figure out the root cause of this issue.