2016-12-16 4 views
1

私は、以下のようなSQLのcase文に基づいて2つのdataFrameに参加したいと考えています。この状況に対処する最良の方法は何ですか?PySpark Join case statemnetに基づいて結合

from df1 
    left join df2 d 
     on d."Date1" <= Case when v."DATE2" >= v."DATE3" then df1."col1" else df1."col2" end 

答えて

0

私は個人的にはブール値を返すUDFに入れます。したがって、ビジネスロジックは、Pythonコードになってしまいますし、SQLを清潔に滞在します:

>>> from pyspark.sql.types import BooleanType 

>>> def join_based_on_dates(left_date, date0, date1, col0, col1): 
>>>  if(date0 >= date1): 
>>>   right_date = col0 
>>>  else: 
>>>   right_date = col1 
>>>  return left_date <= right_date 

>>> sqlContext.registerFunction("join_based_on_dates", join_based_on_dates, BooleanType()) 

>>> join_based_on_dates("2016-01-01", "2017-01-01", "2018-01-01", "res1", "res2"); 
True 

>>> sqlContext.sql("SELECT join_based_on_dates('2016-01-01', '2017-01-01', '2018-01-01', 'res1', 'res2')").collect(); 
[Row(_c0=True)] 

あなたのクエリのようなものに終わるだろう:

FROM df1 
LEFT JOIN df2 ON join_based_on_dates('2016-01-01', '2017-01-01', '2018-01-01', 'res1', 'res2') 

希望を、これはスパークを楽しんでいる、ことができます!

関連する問題