-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRemoveDataSkew.scala
72 lines (50 loc) · 1.53 KB
/
RemoveDataSkew.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
package com.gjeevanm.spark
import org.apache.spark.SparkConf
import org.apache.spark.sql.SparkSession
object RemoveDataSkewness extends App {
val sparkconf = new SparkConf()
.setMaster("local[*]")
.setAppName("RemoveDataSkewness")
val spark = SparkSession
.builder()
.config(sparkconf)
.getOrCreate()
//we can set using sqlcontext
// spark.sql("SET spark.sql.adaptive.enabled=true")
spark.conf.set("spark.sql.adaptive.enabled", "true")
spark.conf.set("spark.sql.autoBroadcastJoinThreshold", "-1")
spark.conf.set("spark.sql.shuffle.partitions", "3")
/*
Lets create df1 with column ID which is having skew
*/
import org.apache.spark.sql.functions._
/*
DataFrame (df1) has column ID
*/
var df1 = spark.range(1000000000)
.withColumn("id", lit("x"))
val extravalues = spark.range(4)
.withColumn("id", lit("y"))
val moreextravalues = spark.range(4)
.withColumn("id", lit("z"))
df1 = df1.union(extravalues).union(moreextravalues)
df.select("id").count(*).show(100, false)
/*
Lets create df2 with column ID
*/
var df2 = spark.range(10000000)
.withColumn("id", lit("x"))
val morevalues = Seq(
("y"),
("z")).toDF
df2 = df2.union(morevalues)
/*
Join two Df's on ID column
*/
// lets enable AQE using below property- try running join with and without AQE enabled
spark.conf.set("spark.sql.adaptive.enabled", "true")
df1.join(
df2,
df1.col("id") <=> df2.col("id")
).select(df1("id")).groupBy(df1("id")).agg(count("*")).show(100, false)
}