Search code examples
cassandraphpcassa

Cassandra convert UUID to string and back


If i use UUID1 for my column names and then retrieve them with php how can i convert that UUID to readable string so i could insert that string to the HTML and then later on use it to select that same column by converting that string back to UUID? Is that even possible?

I could go with UTF8 or something else but i want to avoid collisions and get ordered wide rows, and i really need to store those column names to the HTML, i can't see any other way to do it.

I'm using phpcassa.


Solution

  • You can cast UUID objects to strings to get a nice printable version. That same string can be used with UUID::import() to create an identical UUID object again:

    use phpcassa\UUID;
    
    $uuid = UUID::uuid1();
    $pretty_uuid = (string)$uuid;
    echo("Printable version: " . $pretty_uuid . "\n");
    $uuid_copy = UUID::import($pretty_uuid);
    assert ($uuid == $uuid_copy);