views:

138

answers:

4

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!

+7  A: 

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.

Amber
+1 for UUID are always 128 bits.
Thilo
Why so many up votes when he has not actually full answered his question? you admit yourself that "standard ones are 128 bits"... what about non-standard?
Matt H
@Matt, how can something non-standard be "universal" when it's only known to you?-) And it's _way_ harder to be "unique" in just 64 bits (high chance of some accidental clashes when you've generated just a few billion "non-standard non-universal unlikely-to-be-unique IDs" -- rule of thumb: square root of the number of distinct possibilities you generate is about when you start worrying, and `sqrt(2**64)` is `2**32`, i.e., about 4 billions)
Alex Martelli
@Matt H: *Anything* can be non-standard.
Amber
@Alex, that's why in my answer I didn't call it universal anymore. I was reading between the lines on what he was wanting to do, not on what the meaning of UUID is.
Matt H
+1  A: 

The stock definition of UUID is 128 bits.

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

Joshua
+1 for the second sentence.
Stephen C
+2  A: 

Depending on how unique you need it 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. e.g. does it need to be unique across computers? or just is on the one computer ok?

If only unique on the one platform then you can get away with removing the node id section at the end. i.e. the last 6 bytes.

Also, you might like to consider how often these numbers are being 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 64bits easily.

By the way, the L symbolizes a long int which usually means 64bit length, rather than the default 32 on most machines. It's just what happens when you print out the number but internally it doesn't have L in it's value.

Matt H
+1  A: 

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

Adrian Shum