如何使用monotonically_increasing_id连接两个没有公共列的pyspark数据框?

问题描述:

我有两个具有相同行数的pyspark数据框,但它们没有任何公共列。所以我用monotonically_increasing_id()作为如何使用monotonically_increasing_id连接两个没有公共列的pyspark数据框?

from pyspark.sql.functions import monotonically_increasing_id as mi 
id=mi() 
df1 = df1.withColumn("match_id", id) 
cont_data = cont_data.withColumn("match_id", id) 
cont_data = cont_data.join(df1,df1.match_id==cont_data.match_id, 'inner').drop(df1.match_id) 

但加入后得到的数据帧具有更少的行数增加新的列到他们两个。 我在这里错过了什么。谢谢

+0

如果数据集没有任何共同之处,不排序相关的结果将如何模样?我会说你的数据集是如何排序的隐含/隐式连接条件。 –

你只是不。这不是monotonically_increasing_id的适用用例,根据定义它是非确定性的。相反:

  • 转换到RDD
  • zipWithIndex
  • 转换回DataFrame
  • join