Use the Best Data Format
Apache Spark supports several data formats, including CSV, JSON, ORC, and Parquet, but just because Spark supports a given data storage or format doesn’t mean you’ll get the same performance with all of them. Parquet is a columnar storage format designed to only select data from columns that we actually are using, skipping over those that are not requested. This format reduces the size of the files dramatically and makes the Spark SQL query more efficient. The following picture from [24] about parquet file, explains what we could achieve with Column Pruning (projection push down) and Predicate Push Down:
As an example, we can imagine having a vehicle dataset containing information like:
1
{
2
"id": "AA-BB-00-77",
3
"type": "truck",
4
"origin": "ATL",
5
"destination": "LGA",
6
"depdelay": 0.0,
7
"arrdelay": 0.0,
8
"distance": 762.0
9
}
Copied!
Here is the code to persist a vehicles DataFrame as a table consisting of Parquet files partitioned by the destination column:
1
df.write.format("parquet")
2
.partitionBy("destination")
3
.option("path", "/data/vehicles")
4
.saveAsTable("vehicles")
Copied!
Below is the resulting directory structure as shown by a Hadoop list files command:
1
hadoop fs -ls /data/vehicles
2
3
/data/vehicles/destination=ATL
4
/data/vehicles/destination=BOS
5
/data/vehicles/destination=CLT
6
/data/vehicles/destination=DEN
7
/data/vehicles/destination=DFW
8
/data/vehicles/destination=EWR
9
/data/vehicles/destination=IAH
10
/data/vehicles/destination=LAX
11
/data/vehicles/destination=LGA
12
/data/vehicles/destination=MIA
13
/data/vehicles/destination=ORD
14
/data/vehicles/destination=SEA
15
/data/vehicles/destination=SFO
Copied!
Given a table of vehicles containing the information as above, using the Column pruning technique if the table has 7 columns, but in the query, we list only 2, the other 5 will not be read from disk. Predicate pushdown is a performance optimization that limits with what values will be scanned and not what columns. So, if you apply a filter on column destination to only return records with value BOS, the predicate push down will make parquet read-only blocks that may contain values BOS. So, improve performance by allowing Spark to only read a subset of the directories and files. For example, the following query reads only the files in the destination=BOS partition directory in order to query the average arrival delay for vehicles destination to Boston:
1
df.filter("destination = 'BOS' and arrdelay > 1")
2
.groupBy("destination").avg("arrdelay") .sort(desc("avg(arrdelay)")).show()
3
4
+-----------+------------------+
5
|destination| avg(depdelay)|
6
+-----------+------------------+
7
| EWR |54.352020860495436|
8
| MIA | 48.95263157894737|
9
| SFO |47.189473684210526|
10
| ORD | 46.47721518987342|
11
| DFW |44.473118279569896|
12
| CLT |37.097744360902254|
13
| LAX |36.398936170212764|
14
| LGA | 34.59444444444444|
15
| BOS |33.633187772925766|
16
| IAH | 32.10775862068966|
17
| SEA |30.532345013477087|
18
| ATL | 29.29113924050633|
19
+-----------+------------------+
Copied!
You can see the physical plan for a DataFrame calling the explain method as follow:
1
df.filter("destination = 'BOS' and arrdelay > 1")
2
.groupBy("destination")
3
.avg("arrdelay")
4
.sort(desc("avg(arrdelay)"))
5
.explain
6
7
== Physical Plan ==
8
TakeOrderedAndProject(limit=1001, orderBy=[avg(arrdelay)#304 DESC NULLS LAST],
9
output=[src#157,dst#149,avg(arrdelay)#314])
10
+- *(2) HashAggregate(keys=[destination#157, arrdelay#149],
11
functions=[avg(arrdelay#152)],
12
output=[destination#157, avg(arrdelay)#304])
13
+- Exchange hashpartitioning(destination#157, 200)
14
+- *(1) HashAggregate(keys=[destination#157],
15
functions=[partial_avg(arrdelay#152)],
16
output=[destination#157, sum#321, count#322L])
17
+- *(1) Project[arrdelay#152, destination#157]
18
+- *(1)Filter (isnotnull(arrdelay#152) && (arrdelay#152 > 1.0))
19
+- *(1) FileScan parquet default.flights[arrdelay#152,destination#157]
20
Batched: true, Format: Parquet,
21
Location: PrunedInMemoryFileIndex[dbfs:/data/vehicles/destination=BOS],
22
PartitionCount: 1,
23
PartitionFilters: [isnotnull(destination#157), (destination#157 = BOS)],
24
PushedFilters: [IsNotNull(arrdelay), GreaterThan(arrdelay,1.0)],
25
ReadSchema: struct<destination:string,arrdelay>
Copied!
Here in PartitionFilters, we can see partition filter push down, which means that the destination=BOS filter is pushed down into the Parquet file scan. This minimizes the files and data scanned and reduces the amount of data passed back to the Spark engine for the aggregation average on the arrival delay.
Last modified 11mo ago
Copy link