If you are using spark 2.2+ then you can use any of these MAPJOIN/BROADCAST/BROADCASTJOIN
hints.
Refer to this Jira and this for more details regarding this functionality.
Example: below i have used broadcast but you can use either mapjoin/broadcastjoin hints will result same explain plan.
>>> spark.range(1000000000).createOrReplaceTempView("t")
>>> spark.range(1000000000).createOrReplaceTempView("u")
>>>sql("select /*+ Broadcast(t,u) */* from t join u on t.id=u.id").explain()
== Physical Plan ==
*BroadcastHashJoin [id#0L], [id#16L], Inner, BuildRight
:- *Range (0, 1000000000, step=1, splits=56)
+- BroadcastExchange HashedRelationBroadcastMode(List(input[0, bigint, false]))
+- *Range (0, 1000000000, step=1, splits=56)
(or)
if you are using Spark < 2 then we need to use dataframe API to persist then registering as temp table we can achieve in memory join.
>>> df=hc.range(10000000)
>>> df.persist() --persist the df in memory
>>> df.registerTempTable("u") --register temp table
>>> df1=hc.range(10000000)
>>> df1.persist()
>>> df1.registerTempTable("t")
>>> hc.sql("select * from t join u on t.id=u.id").explain()
== Physical Plan ==
Project [id#11L,id#26L]
+- SortMergeJoin [id#11L], [id#26L]
:- Sort [id#11L ASC], false, 0
: +- TungstenExchange hashpartitioning(id#11L,200), None
: +- InMemoryColumnarTableScan [id#11L], InMemoryRelation [id#11L], true, 10000, StorageLevel(false, true, false, false, 1), ConvertToUnsafe, None
+- Sort [id#26L ASC], false, 0
+- TungstenExchange hashpartitioning(id#26L,200), None
+- InMemoryColumnarTableScan [id#26L], InMemoryRelation [id#26L], true, 10000, StorageLevel(false, true, false, false, 1), ConvertToUnsafe, None
By using DataFrames without creating any temp tables
>>>from pyspark.sql.functions import *
>>> df=hc.range(10000000)
>>> df1=hc.range(10000000)
>>> df.join(broadcast(df1),['id']).explain()
== Physical Plan ==
Project [id#26L]
+- BroadcastHashJoin [id#26L], [id#11L], BuildRight
:- ConvertToUnsafe
: +- Scan ExistingRDD[id#26L]
+- ConvertToUnsafe
+- Scan ExistingRDD[id#11L]
in addition Broadcast joins are done automatically in Spark.
There is a parameter is "spark.sql.autoBroadcastJoinThreshold" which is set to 10mb by default.
To change the default value then
conf.set("spark.sql.autoBroadcastJoinThreshold", 1024*1024*<mb_value>)
for more info refer to this link regards to spark.sql.autoBroadcastJoinThreshold.