How to use CROSS JOIN and CROSS APPLY in Spark SQL

…衆ロ難τιáo~ 提交于 2021-01-27 13:51:38

问题


I am very new to Spark and Scala, I writing Spark SQL code. I am in situation to apply CROSS JOIN and CROSS APPLY in my logic. Here I will post the SQL query which I have to convert to spark SQL.

select Table1.Column1,Table2.Column2,Table3.Column3
from Table1 CROSS JOIN Table2 CROSS APPLY Table3

I need the above query to convert in to SQLContext in Spark SQL. Kindly help me. Thanks in Advance.


回答1:


First set the below property in spark conf

spark.sql.crossJoin.enabled=true

then dataFrame1.join(dataFrame2) will do Cross/Cartesian join,

we can use below query also for doing the same

sqlContext.sql("select * from table1 CROSS JOIN table2 CROSS JOIN table3...")



回答2:


Set Spark Configuration ,

var sparkConf: SparkConf = null

 sparkConf = new SparkConf()

.set("spark.sql.crossJoin.enabled", "true")

Explicit Cross Join in spark 2.x using crossJoin Method

crossJoin(right: Dataset[_]): DataFrame

var df_new = df1.crossJoin(df2);

Note : Cross joins are one of the most time consuming joins and often should be avoided.



来源:https://stackoverflow.com/questions/40763682/how-to-use-cross-join-and-cross-apply-in-spark-sql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!