将文件另存为 csv 后 UDF 数据值发生变化
我使用 UDF 生成了一个随机 userId ,并将其分配给 df.show() 之后使用 withColumn 的表
,这是表:
userID | name |
---|---|
bdjq7vn3 | Jhon |
8br64v24 | Ronan |
但是,当我将新创建的 df 作为 csv 文件写入本地计算机时,用户 ID 为 changeD
本地 csv 表:
userID | name |
---|---|
3r8fmkt1 | Jhon |
fmwxgc28 | Ronan |
这是什么魔法?
I've generated a random userId using UDF and assigned it to the table with withColumn
after df.show() this is the table:
userID | name |
---|---|
bdjq7vn3 | Jhon |
8br64v24 | Ronan |
however when i write the newly created df to my local computer as a csv file, the userID is changeD
local csv table:
userID | name |
---|---|
3r8fmkt1 | Jhon |
fmwxgc28 | Ronan |
what sorcery is this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这种不一致的原因就像艾玛的comment、
show
和save
是两个操作,您的 UDF 将被调用两次,因此有两个不同的随机 ID。如果您需要随机用户 ID,那么您需要生成一次(使用您的 UDF 或
monotonically_increasing_id
或rand
),然后保存它。您的 ID 将被永久存储并且不会再更改。
The reason for this inconsistency is like Emma's comment,
show
andsave
are two actions and your UDF would be called twice, hence two different random IDs.If random user ID is your requirement, then you would want to generate it once (using either your UDF or
monotonically_increasing_id
orrand
), then save it. Your ID would be permanently stored and won't change anymore.