Search code examples
cassandrauuidphpcassa

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!


Solution

  • 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