pyspark中的哈希码生成

问题描述 投票:-1回答:1

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

我试过下面的代码,但收到错误

h=hashlib.md5(c)

这里c是一个数据帧。

错误

TypeError: object supporting the buffer API required
apache-spark pyspark pyspark-sql
1个回答
1
投票

在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()

希望这有效

© www.soinside.com 2019 - 2024. All rights reserved.