如何在Python中生成long类型的UUID(供java程序使用)?

发布于 2024-09-15 09:15:30 字数 1213 浏览 2 评论 0 原文

如何使用 Python 生成 long 类型(64 位 - 由 java 程序使用)的 UUID?

我读到了UUID 模块。所以我玩了一下:

>>> import uuid
>>> uuid.uuid1().int
315596929882403038588122750660996915734L

为什么uuid.uuid1().int生成的整数末尾有一个“L”?如果它是一个整数,它不应该由纯数字组成吗?

另外根据文档 uuid.uuid1().int 将生成一个 128 位整数。我需要一个 long 类型的整数(对于 java 程序),这意味着它需要是 64 位。是否可以生成 64 位而不是 128 位的 UUID?

另一个问题是,根据 Python doc< /a> Python int 是 32 位,而 Python long 具有“无限精度”。 “无限精度”是什么意思?我需要一个 64 位长的 int - 那会是什么 Python 类型?

编辑: 谢谢大家的回复。看起来 UUID 的定义是 128 位。在这种情况下,我可能不应该使用术语 UUID。我想要做的是生成一个长整型(64 位)的唯一 ID。我以为 UUID 可以完成这项工作,但看起来它不能。

我需要该 ID 作为 Solr 搜索引擎的文档 ID。我正在使用实时索引 Solr 插件 Zoie 。根据文档,“Zoie 假设要索引的每条记录都必须具有唯一键并且类型为long

因此,这就是我需要,你知道我可以做什么来生成 long 类型的唯一 ID 吗?

谢谢你!

How do you generate UUID of type long (64 bits - to be consumed by a java program) using Python?

I read about the UUID module. So I played with it a bit:

>>> import uuid
>>> uuid.uuid1().int
315596929882403038588122750660996915734L

Why is there an "L" at the end of the integer generated by uuid.uuid1().int? If it's an integer shouldn't it be made up of pure numbers?

Also according to the documentation uuid.uuid1().int will generate a 128 bits integer. I need an integer of type long (for a java program), that would mean it need to be 64 bits. Is it possible to generate a UUID of 64 bits instead of 128 bits?

Another problem is that according to the Python doc a Python int is 32 bits while a Python long has "unlimited precision." What does "unlimited precision" mean? I need a 64 bits long int - what Python type would that be?

EDIT:
Thanks for everyone's replies. Looks like UUID's are by definition 128 bits. In that case I probably shouldn't be using the term UUID. What I want to do is to generate a unique ID that is of type long (64 bits). I thought UUID would do the job, but it looks like it can't.

I need that ID as the document ID for Solr search engine. I'm using a real-time indexing Solr plugin Zoie. And according to the documentation, "Zoie assumes every record to be indexed must have a unique key and of type long."

So given that's what I need, do you know what I can do to generate unique ID of type long?

Thank you!

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

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

发布评论

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

评论(4

默嘫て 2024-09-22 09:15:30

L 表示它是一个长整数值(大于 32 位)。

标准 UUID 始终为 128 位;如果您想要仅 64 位的内容,则需要仅使用 UUID 的子部分,或者使用 UUID 以外的其他内容。

The L signifies that it's a long integer value (greater than 32 bits).

Standard UUIDs are always 128 bits; if you want something that's only 64 bits, you'll need to either only use a sub-part of a UUID, or use something other than a UUID.

小猫一只 2024-09-22 09:15:30

根据您需要的唯一性,您可以生成自己的 UUID 形式,它更像是机器范围唯一 ID,而不是通用唯一 ID (即它在不同计算机上可能不是唯一的,但在一台计算机上是可以的)。

如果 ID 只需要在一个平台上唯一,那么您可以删除末尾的节点 id 部分(最后 6 个字节)。

另外,您可能会考虑生成这些数字的频率。它们是每秒最多生成一次还是非常频繁?看看这里 UUID 是如何生成的,您就会明白我的意思: http://tools.ietf.org/id/draft-leach-uuids-guids-01.txt

您可能可以根据您的情况推出自己的版本,但仍然满足您的要求和适合轻松在 64 位内。

顺便说一下,“L”代表一个 long int,通常表示 64 位长度,而不是 32 位。它只是在您打印号码时附加的。

Depending on how unique you need it to be, you may be able to generate your own form of UUID that is more of a machine wide unique ID rather than a universally unique ID (i.e. it may not be unique across computers but on the one computer is okay).

If the ID need only be unique on the one platform then you can get away with removing the node id section at the end (the last 6 bytes).

Also, you might consider how often these numbers are being generated. Are they generated once per second max or very frequently? Have a look here at how a UUID is generated and you'll see what I mean: http://tools.ietf.org/id/draft-leach-uuids-guids-01.txt

You can probably roll your own version for your situation and yet still meet your requirement and fit within 64 bits easily.

By the way, the "L" symbolizes a long int which usually means 64-bit length, rather than 32-bit. It's just appended when you print out the number.

永言不败 2024-09-22 09:15:30

UUID的标准定义是128位。

我敢打赌 64 位不足以保证非同步的唯一性。

The stock definition of UUID is 128 bits.

I'd wager that 64 bits is not enough to guarantee non-synchronized uniqueness.

尛丟丟 2024-09-22 09:15:30

需要补充一点的是,即使 Java 的 UUID 也是 128 位长。
如果您打算让 Java 使用它,则必须将其设置为 128 位长,而不是 64 位。
http://download-llnw. oracle.com/javase/1.5.0/docs/api/java/util/UUID.html

One point to add, even Java's UUID is 128-bit long.
If you are going to have it consumed by Java, you gotta make it 128-bit long instead of 64.
http://download-llnw.oracle.com/javase/1.5.0/docs/api/java/util/UUID.html

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