I have a valid PostgreSQL query : when I copy/paste it in PSQL, I get the desired result.
But when I run with Spark SQL it leads to a NullPointerException.
Here is the snippet of code causing the error:
extractDataFrame().show()
private def extractDataFrame(): DataFrame = {
val query =
"""(
SELECT events.event_facebook_id, events.name, events.tariffrange,
eventscounts.attending_count, eventscounts.declined_count, eventscounts.interested_count,
eventscounts.noreply_count,
artists.facebookid as artist_facebook_id, artists.likes as artistlikes,
organizers.organizerid, organizers.likes as organizerlikes,
places.placeid, places.capacity, places.likes as placelikes
FROM events
LEFT JOIN eventscounts on eventscounts.event_facebook_id = events.event_facebook_id
LEFT JOIN eventsartists on eventsartists.event_id = events.event_facebook_id
LEFT JOIN artists on eventsartists.artistid = artists.facebookid
LEFT JOIN eventsorganizers on eventsorganizers.event_id = events.event_facebook_id
LEFT JOIN organizers on eventsorganizers.organizerurl = organizers.facebookurl
LEFT JOIN eventsplaces on eventsplaces.event_id = events.event_facebook_id
LEFT JOIN places on eventsplaces.placefacebookurl = places.facebookurl
) df"""
spark.sqlContext.read.jdbc(databaseURL, query, connectionProperties)
}
The SparkSession is defined as follows:
val databaseURL = "jdbc:postgresql://dbHost:5432/ticketapp"
val spark = SparkSession
.builder
.master("local[*]")
.appName("tariffPrediction")
.getOrCreate()
val connectionProperties = new Properties
connectionProperties.put("user", "simon")
connectionProperties.put("password", "root")
And here is the complete stacktrace:
[SparkException: Job aborted due to stage failure: Task 0 in stage 27.0 failed 1 times, most recent failure: Lost task 0.0 in stage 27.0 (TID 27, localhost): java.lang.NullPointerException
at org.apache.spark.sql.catalyst.expressions.codegen.UnsafeRowWriter.write(UnsafeRowWriter.java:210)
at org.apache.spark.sql.catalyst.expressions.GeneratedClass$GeneratedIterator.processNext(Unknown Source)
at org.apache.spark.sql.execution.BufferedRowIterator.hasNext(BufferedRowIterator.java:43)
at org.apache.spark.sql.execution.WholeStageCodegenExec$$anonfun$8$$anon$1.hasNext(WholeStageCodegenExec.scala:370)
at org.apache.spark.sql.execution.SparkPlan$$anonfun$4.apply(SparkPlan.scala:246)
at org.apache.spark.sql.execution.SparkPlan$$anonfun$4.apply(SparkPlan.scala:240)
at org.apache.spark.rdd.RDD$$anonfun$mapPartitionsInternal$1$$anonfun$apply$24.apply(RDD.scala:784)
at org.apache.spark.rdd.RDD$$anonfun$mapPartitionsInternal$1$$anonfun$apply$24.apply(RDD.scala:784)
at org.apache.spark.rdd.MapPartitionsRDD.compute(MapPartitionsRDD.scala:38)
at org.apache.spark.rdd.RDD.computeOrReadCheckpoint(RDD.scala:319)
at org.apache.spark.rdd.RDD.iterator(RDD.scala:283)
at org.apache.spark.scheduler.ResultTask.runTask(ResultTask.scala:70)
at org.apache.spark.scheduler.Task.run(Task.scala:85)
at org.apache.spark.executor.Executor$TaskRunner.run(Executor.scala:274)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
Driver stacktrace:]
The most surprising part is that if I remove one (whichever one) of the LEFT JOIN clauses in the SQL query, I don't get any errors...