Cassandra CQL PHP

After lots of googling I figured out how to access counter columns value via PHPCassa + CQL. Though it might not be efficient, this is all I was able to figure out after a long struggle :)

First Connecting to and Selection from Cassandra Instance via PHP using PHPCassa
$pool = new ConnectionPool("Stats",$servers);
$raw = $pool->get();
$rows = $raw->client->execute_cql_query("SELECT 'a' FROM Impressions WHERE KEY='xxx'", cassandra_Compression::NONE);
To Return Connection
$pool->return_connection($this->raw);
To Update Counter value via PHPCassa
$pool = new ConnectionPool("Stats",$servers);
$raw = $pool->get();
$rows = $raw->client->execute_cql_query("UPDATE TestCounter SET 'counter'='counter'+1 WHERE KEY='xxx'", cassandra_Compression::NONE);
You can find a wrapper here

So, once you are done with fetch successfully. This is the result you may get
object(cassandra_CqlResult)#17 (3) {
  ["type"]=>
  int(1)
  ["rows"]=>
  array(1) {
    [0]=>
    object(cassandra_CqlRow)#18 (2) {
      ["key"]=>
      string(4) "xxx"
      ["columns"]=>
      array(1) {
        [0]=>
        object(cassandra_Column)#19 (4) {
          ["name"]=>
          string(11) "a"
          ["value"]=>
          string(8) " "                       // Actually there is the value between the quotes
          ["timestamp"]=>
          int(1318230159648)
          ["ttl"]=>
          NULL
        }
      }
    }
  }
  ["num"]=>
  NULL
} 
The problem is CQL returns counter value as string rather int.

So, one general solution everyone would consider is hexdec($rows->rows[0]->columns[0]->value)

But the encoding of string returned is not a hex string. So, hexdec won't help.

So, I figured out a function to convert the string returned to a hex string
function strToHex($string)
{
$hex='';
for ($i=0; $i < strlen($string); $i++)
     {
         $hex .= dechex(ord($string[$i]));  //ord returns the ascii equivalent of given char
     }
     return $hex;
}
Now u have the hex string. Final step
$val = hexdec(strToHex($rows->rows[0]->columns[0]->value))
One more solution for the same
$data = unpack("H*",$rows->rows[0]->columns[0]->value);
$val = hexdec($data["1"]);

Reference: https://gist.github.com/1024060 Hope it helps :)




Comments

  1. I understand how you are using the execute_cql_query directly from within PHPcassa but how are you extracting the data you need out of the raw thrift data that is returned?

    ReplyDelete
    Replies
    1. Mithrix, I had no problems retrieving data using execute_cql_query as phpcassa does all processing for me before returning the result set. I have even shown the result set object in the above code. I get data as per the default_validation_class and comparator types. Can you post some sample raw thrift data you are getting back from phpcassa using execute_cql_query

      Delete

Post a Comment

Popular posts from this blog

Headless Chrome/Firefox Selenium Java in Amazon EC2

Cassandra PHPCassa & Composite Types

Selenium Webdriver in Nodejs + Javascript