-
Notifications
You must be signed in to change notification settings - Fork 16
/
Exp #9156-S - Hash Partitioning Skew-n-Spill.scala
139 lines (100 loc) · 4.37 KB
/
Exp #9156-S - Hash Partitioning Skew-n-Spill.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// Databricks notebook source
// MAGIC %md
// MAGIC <table>
// MAGIC <tr>
// MAGIC <td></td>
// MAGIC <td>VM</td>
// MAGIC <td>Quantity</td>
// MAGIC <td>Total Cores</td>
// MAGIC <td>Total RAM</td>
// MAGIC </tr>
// MAGIC <tr>
// MAGIC <td>Driver:</td>
// MAGIC <td>**i3.xlarge**</td>
// MAGIC <td>**1**</td>
// MAGIC <td>**4 cores**</td>
// MAGIC <td>**30.5 GB**</td>
// MAGIC </tr>
// MAGIC <tr>
// MAGIC <td>Workers:</td>
// MAGIC <td>**i3.16xlarge**</td>
// MAGIC <td>**32**</td>
// MAGIC <td>**2048 cores**</td>
// MAGIC <td>**15616 GB**</td>
// MAGIC </tr>
// MAGIC </table>
// COMMAND ----------
sc.setJobDescription("Step A-1: Basic initialization")
import org.apache.spark.sql.functions._
// Disable IO cache so as to minimize side effects
spark.conf.set("spark.databricks.io.cache.enabled", false)
val trxPath = "dbfs:/mnt/training/global-sales/transactions/2011-to-2018_int-only_1tb.delta"
// COMMAND ----------
sc.setJobDescription("Step A-2: Utility Function")
def partitionStats(df:org.apache.spark.sql.Dataset[Row], maximum:Long):Unit = {
val results = df.mapPartitions(it => Array(it.size).iterator).collect().map(_.toLong)
val overCount = results.filter(_>maximum).length
val under = results.filter(r => r>0 && r<=maximum)
var html = f"""
<h2 style="margin:0">Partition Statistics</h2>
<table>
<tr><td>Empty:</td> <td style="text-align:right">${results.filter(_==0).length}%,d</td></tr>
<tr><td>Not Empty:</td><td style="text-align:right">${results.filter(_!=0).length}%,d</td></tr>
<tr><td style="border-top:1px solid black; padding-bottom:10px">Total:</td><td style="text-align:right; border-top:1px solid black; padding-bottom:10px">${results.length}%,d</td></tr>
<tr><td>Over Count:</td> <td style="text-align:right">${overCount}%,d</td></tr>
<tr><td style="padding-bottom:10px">Largest:</td><td style="text-align:right; padding-bottom:10px">${results.max}%,d</td></tr>
<tr><td>Under Count:</td><td style="text-align:right">${under.length}%,d</td></tr>
<tr><td>Under Avg:</td> <td style="text-align:right">${under.sum/under.length}%,d</td></tr>
<tr><td>Under Sum:</td> <td style="text-align:right">${under.sum}%,d</td></tr>
</table> """
html = html + f"<h3 style='margin:0; margin-top:1em'>Partitions with more than $maximum%,d records</h3><table>"
if (overCount > 0) {
for ( (r,i) <- results.filter(_>maximum).view.zipWithIndex)
html = html + f"<tr><td>#$i: $r%,d</td></tr>"
} else html = html + "<tr><td>-none found-</td></tr>"
html = html + f"</table>"
displayHTML(html)
}
// COMMAND ----------
sc.setJobDescription("Step B: No skew")
val resultsDF = spark
.read.format("delta").load(trxPath) // Load the transactions table
.groupBy("city_id").count // Group by city_id and count
display(resultsDF)
// COMMAND ----------
sc.setJobDescription("Step C: General Statistics")
val records = spark.read.format("delta").load(trxPath).count
val partitions = spark.read.format("delta").load(trxPath).rdd.getNumPartitions
val cities = spark.read.format("delta").load(trxPath).select("city_id").distinct.count
// Keep the number of partitions we started with.
spark.conf.set("spark.sql.shuffle.partitions", partitions)
displayHTML(f"""
<h2 style="margin:0">General Statistics</h2>
<table>
<tr><td>Cities: </td> <td style="text-align:right;">$cities%,d</td></tr>
<tr><td>Records: </td> <td style="text-align:right;">$records%,d</td></tr>
""")
// COMMAND ----------
sc.setJobDescription("Step D-1: Before - Records/Partition")
val beforeDF = spark.read.format("delta").load(trxPath)
val resultsBeforeDF = beforeDF
.mapPartitions(it => Array(it.size).iterator)
.withColumn("spark_partition_id", spark_partition_id())
.orderBy($"value".desc)
display(resultsBeforeDF)
// COMMAND ----------
sc.setJobDescription("Step D-2: Before - Stats")
partitionStats(beforeDF, 4100000)
// COMMAND ----------
sc.setJobDescription("Step E: After - Records/Partition")
val afterDF = spark
.read.format("delta").load(trxPath)
.repartition(partitions, $"city_id")
val resultsAfterDF = afterDF
.mapPartitions(it => Array(it.size).iterator)
.withColumn("spark_partition_id", spark_partition_id())
.orderBy($"value".desc)
display(resultsAfterDF)
// COMMAND ----------
sc.setJobDescription("Step D-2: After - Stats")
partitionStats(afterDF, 134000000)