开发者

How does UUID work with PHPCassa

I am using:

CassandraUtil::uuid1();开发者_运维百科

This is what I get:

ämªðÏBà=0£Ï‰

I though it would output a int.

What is going on? Is it normal?

Also should I use uuid1 or 2 or 3 or 4 or ...?

Thanks in advance!


There are a few parts to UUIDs in phpcassa. First, how to generate one. The following functions are useful for this:

$my_uuid_string = phpcassa\UUID::uuid1();
$my_uuid_string = phpcassa\UUID::uuid4();

uuid1() generates a v1 UUID, which has a timestamp component, and is called TimeUUIDType in Cassandra. uuid4() generates a totally random UUID, and is called LexicalUUIDType in Cassandra. (The other uuidX() functions aren't generally that useful.) What this function gives you back is a byte array representation of the UUID -- basically a 16 byte string. This is what your "ämªðÏBà=0£Ï‰" string is. When you are trying to insert a UUID into Cassandra, this is what you want to use.

It's possible to make a UUID object which has useful methods and attributes from this byte array:

$my_uuid = phpcassa\UUID::import($my_uuid_string);

With $my_uuid, you can get a pretty string representation like 'd881bf7c-cf8f-11e0-85e5-00234d21610a' by getting $my_uuid->string. You can get back the byte representation by doing $my_uuid->bytes. Any uuid data that you get back from Cassandra will by in the byte array format, so you need to use UUID::import() on it if you want a UUID object.

Also, UUID::import() also works on the pretty string representation as well (the one that looks like ''d881bf7c-cf8f-11e0-85e5-00234d21610a').

Last, don't forget about the documentation for the UUID class.

EDIT: updated links and class names to match the latest phpcassa API


uuid1() generates a UUID based on the current time and the MAC address of the machine.

  • Pros: Useful if you want to be able to sort your UUIDs by creation time.
  • Cons: Potential privacy leakage since it reveals which computer it was generated on and at what time.
  • Collisions possible: If two UUIDs are generated at the exact same time (within 100 ns) on the same machine. (Or a few other unlikely marginal cases.)

uuid2() doesn't seem to be used anymore.

uuid3() generates a UUID by taking an MD5 hash of an arbitrary name that you choose within some namespace (e.g. URL, domain name, etc).

  • Pros: Provides a nice way of assigning blocks of UUIDs to different namespaces. Easy to reproduce the UUID from the name.
  • Cons: If you have a unique name already, why do you need a UUID?
  • Collisions possible: If you reuse a name within a namespace, or if there is a hash collision.

uuid4() generates a completely random UUID.

  • Pros: No privacy concerns. Don't have to generate unique names.
  • Cons: No structure to UUIDs.
  • Collisions possible: If you use a bad random number generator, reuse a random seed, or are very, very unlucky.

uuid5() is the same as uuid3(), except using a SHA-1 hash instead of MD5. Officially preferred over uuid3().

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜