Search code examples
javapythonguiduuidlong-integer

How to generate a UUID of type long (to be consumed by a java program) in Python?


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!


Solution

  • 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.