Tuesday, April 26, 2011

How to use cassandra with php

PHP application reading/writing to Cassandra :-


Ok, so let's say we finished Cassandra installation and generated PHP interface file with Thrift.

Now we can write small php application, which will connect to Cassandra and write and read some data.
I found on Internet multiple high level libraries, which allows you to manipulate with Cassandra data. In this example I will not go through all of them, bnut I will try just simple connection with low level API generated by Thrift to prove, that my connection from PHP to Cassandra really work.


Because I still missed some libraries from Thrift, I downloaded complete Thrift package from here: http://incubator.apache.org/thrift/download/
and extracted it to d:\cassandra\trift

Here http://wiki.apache.org/cassandra/ThriftExamples#PHP you can find example of connection from PHP to Cassandra, so let's make some corrections to fit it to my test and paths on my machine:
(I had to copy Cassandra files generated by Thrift to d:\cassandra\trift\lib\php\src\packages\cassandra)
open();


/* Insert some data into the Standard1 column family from the default config */

// Keyspace specified in storage=conf.xml
$keyspace = 'Keyspace1';

// reference to specific User id
$keyUserId = "1";

// Constructing the column path that we are adding information into.
$columnPath = new cassandra_ColumnPath();
$columnPath->column_family = 'Standard1';
$columnPath->super_column = null;
$columnPath->column = 'email';

// Timestamp for update
$timestamp = time();

// We want the consistency level to be ZERO which means async operations on 1 node
$consistency_level = cassandra_ConsistencyLevel::ZERO;

// Add the value to be written to the table, User Key, and path.
$value = "foobar@example.com";
$client->insert($keyspace, $keyUserId, $columnPath, $value, $timestamp, $consistency_level);

// Add a new column path to be altered.
$columnPath->column = 'age';
//Get a current timestamp
$timestamp = time();
// Update the value to be inserted for the updated column Path
$value = "24";
$client->insert($keyspace, $keyUserId, $columnPath, $value, $timestamp, $consistency_level);

/*
* use batch_insert to insert a supercolumn and its children using the standard config
* builds the structure
*
* Super1 : {
* KeyName : {
* SuperColumnName : {
* foo : fooey value
* bar : bar like thing
* }
* }
* }
*/

// build columns to insert
$column1 = new cassandra_Column();
$column1->name = 'foo';
$column1->value = 'fooey value';
$column1->timestamp = time();

$column2 = new cassandra_Column();
$column2->name = 'bar';
$column2->value = 'bar like thing';
$column2->timestamp = time();

// build super column containing the columns
$super_column = new cassandra_SuperColumn();
$super_column->name = 'SuperColumnName';
$super_column->columns = array($column1, $column2);

// create columnorsupercolumn holder class that batch_insert uses
$c_or_sc = new cassandra_ColumnOrSuperColumn();
$c_or_sc->super_column = $super_column;

// create the mutation (a map of ColumnFamily names to lists ColumnsOrSuperColumns objects
$mutation['Super1'] = array($c_or_sc);

$client->batch_insert($keyspace, 'KeyName', $mutation, $consistency_level);

/* Query for data */

// Specify what Column Family to query against.
$columnParent = new cassandra_ColumnParent();
$columnParent->column_family = "Standard1";
$columnParent->super_column = NULL;


$sliceRange = new cassandra_SliceRange();
$sliceRange->start = "";
$sliceRange->finish = "";
$predicate = new cassandra_SlicePredicate();
list() = $predicate->column_names;
$predicate->slice_range = $sliceRange;

// We want the consistency level to be ONE which means to only wait for 1 node
$consistency_level = cassandra_ConsistencyLevel::ONE;

// Issue the Query
$keyUserId = 1;
$result = $client->get_slice($keyspace, $keyUserId, $columnParent, $predicate, $consistency_level);


print_r($result);
$transport->close();



} catch (TException $tx) {
print 'TException: '.$tx->why. ' Error: '.$tx->getMessage() . "\n";
}
?>

No comments:

Post a Comment