将文件另存为 csv 后 UDF 数据值发生变化

发布于 2025-01-19 14:31:03 字数 641 浏览 2 评论 0原文

我使用 UDF 生成了一个随机 userId ,并将其分配给 df.show() 之后使用 withColumn 的表

,这是表:

userIDname
bdjq7vn3Jhon
8br64v24Ronan

但是,当我将新创建的 df 作为 csv 文件写入本地计算机时,用户 ID 为 changeD

本地 csv 表:

userIDname
3r8fmkt1Jhon
fmwxgc28Ronan

这是什么魔法?

I've generated a random userId using UDF and assigned it to the table with withColumn

after df.show() this is the table:

userIDname
bdjq7vn3Jhon
8br64v24Ronan

however when i write the newly created df to my local computer as a csv file, the userID is changeD

local csv table:

userIDname
3r8fmkt1Jhon
fmwxgc28Ronan

what sorcery is this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

趴在窗边数星星i 2025-01-26 14:31:03

这种不一致的原因就像艾玛的commentshowsave 是两个操作,您的 UDF 将被调用两次,因此有两个不同的随机 ID。

如果您需要随机用户 ID,那么您需要生成一次(使用您的 UDF 或 monotonically_increasing_idrand),然后保存它。您的 ID 将被永久存储并且不会再更改。

The reason for this inconsistency is like Emma's comment, show and save 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 or rand), then save it. Your ID would be permanently stored and won't change anymore.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文