pyspark 中的哈希码生成

hash code generation in pyspark

我正在尝试为数据框中的每一行生成哈希码,并且需要将哈希码作为新列 "pitid" 附加到数据框中。

我试过下面的代码但出现错误

h=hashlib.md5(c)

这里 c 是一个数据帧。

错误

TypeError: object supporting the buffer API required

在 pyspark 中试试下面的代码 这里 c 是数据帧

h=c.rdd.map(lambda x: hash(x)) //generate hash code

r=Row("pitid")
h1=h.map(r).toDF() // converting rdd h to dataframe

使用monotonically_increasing_id

连接两个数据帧
h2=h1.withColumn("rowId", monotonically_increasing_id())

c1=c.withColumn("rowId", monotonically_increasing_id())

c1.join(h2,c1.rowId==h2.rowId,'inner').drop(c1.rowId).drop(h2.rowId).show()

希望这有效