1 回答
TA贡献1780条经验 获得超1个赞
您可以在 spark 中进行非对等连接。您不一定需要匹配的键。
这是在scala中,我很确定它在python中几乎相同。让我知道它是否不起作用。也会更新 pyspark 中的答案。
scala> df1.join(df2 , 'date1 > 'date2 && 'date1 < 'date3).show
+------+----------+----------+----------+
|emp_id| date1| date2| date3|
+------+----------+----------+----------+
| 12345|02-28-2017|01-28-2017|03-15-2017|
+------+----------+----------+----------+
Pyspark 解决方案:
>>> from pyspark.sql.functions import unix_timestamp
>>> from pyspark.sql.functions import from_unixtime
>>> x = [(67891 ,'11-13-2015'),(12345, '02-28-2017'),(34567,'04-07-2017')]
>>> df1 = spark.createDataFrame(x,['emp_id','date1'])
>>> y = [('01-28-2017','03-15-2017'),('07-13-2017','11-13-2017'),('06-07-2018','09-07-2018')]
>>> df2 = spark.createDataFrame(y,['date2','date3'])
>>> df1a = df1.select('emp_id', from_unixtime(unix_timestamp('date1', 'MM-dd-yyy')).alias('date1'))
>>> df2a = df2.select(from_unixtime(unix_timestamp('date2', 'MM-dd-yyy')).alias('date2'),from_unixtime(unix_timestamp('date3', 'MM-dd-yyy')).alias('date3'))
>>> df1a.join(df2a, on=[df1a['date1'] > df2a['date2'], df1a['date1'] < df2a['date3']]).show()
+------+-------------------+-------------------+-------------------+
|emp_id| date1| date2| date3|
+------+-------------------+-------------------+-------------------+
| 12345|2017-02-28 00:00:00|2017-01-28 00:00:00|2017-03-15 00:00:00|
+------+-------------------+-------------------+-------------------+
添加回答
举报