Call us Toll-Free:
Live ChatEmail us


Cassandra PHP Wrapper 0.7

Mike Peters, 07-29-2010
A new version of the SoftwareProjects Cassandra PHP Wrapper (db_cassandra.php) is now available, for Cassandra 0.7.

This version of the PHP Wrapper adds support for:
* Adding Keyspaces and Column families on the fly
* Better error logging
* Disconnect on destructor to prevent java fd leaks
* Reconnect on timeout, up to 10 times
* Deletes
* Updated all API calls to 0.7

You can download the Cassandra 0.7 PHP wrapper, modify and distribute it freely.

Please let us know if you find any issues.

Usage Examples

Adding a new Keyspace with one column family

// Initialize Cassandra
$cassandra = new CassandraDB("mytest_keyspace");

// Debug on

// Prepare Column Family
// (You can add as many as you need to the cfdef array)
$cfdef = null;
$record = array();
$record['keyspace']      = "mytest_keyspace";
$record['name']        = "mytable";
$record['column_type']    = "Standard";
$record['comparator_type']  = "BytesType";
$record['row_cache_size']  = 0;
$record['key_cache_size']  = 0.01;
$cfdef[] = new cassandra_CfDef($record);

// Add keyspace with one columnfamily
$result = $cassandra->AddKeyspace($cfdef);

Adding records and reading back values

// Initialize Cassandra
$cassandra = new CassandraDB("mytest_keyspace");

// Debug on

// Set our key
$key = "Mike at";

// Insert record ("Columns" in Cassandra)
$record = array();
$record["name"] = "Mike Peters";
$record["email"] = "mike at";
if (
$cassandra->InsertRecord('mytable', $key, $record))
"Record (Columns) inserted successfully.\r\n";

// Print record
$record = $cassandra->GetRecordByKey('mytable', $key);

Setup tips

The PHP Wrapper was tested with the 0.7 trunk version, June 30, 2010 build.

Make sure you patch your Thrift TSocket as described here (comments section).


Further Reading

If you're just getting started with Cassandra, you'll enjoy the selection of hand picked Cassandra tutorials listed below. These are the best of the best:

= Tutorials
* Start here: WTF is a SuperColumn
* Cassandra Basics - Indexing
* Introduction to Cassandra
* History of Cassandra (Video)
* The shift to NoSQL

= Case Studies
* Scaling Twitter with Cassandra
* Cassandra at Digg
* Cassandra at RackSpace

= Internals
* How Reads work in Cassandra
* How Writes work in Cassandra
* Cassandra Replication and Consistency
* Random Partitioner vs Order Preserving Partitioner (OPP)
* Cassandra 101
* Do you really need SQL to do it all in Cassandra
* Let's play with Cassandra

Sauce, 07-29-2010
Hi! Yeah, love the code man, keep it up!

Getting this weird error, any help would be appreciated:

Fatal error: Call to undefined method CassandraClient::set_keyspace()

Mike Peters, 07-29-2010

Which version of Cassandra are you running?

Sauce, 07-29-2010
Mike Peters,

It's version 0.63, Thanks for your quick reply!


Sauce, 07-29-2010
This is for 0.7 well that would make sense duh! thanks :)

Mike Peters, 07-29-2010
Hi Sauce,

Ah that explains it then. This wrapper is for Cassandra version 0.7 (not released yet)

If you're using version 0.6, please refer to this PHP wrapper:

Haddad, 10-05-2010

I have problems writing to a super column family (I managed to write to a "normal" column family) with this library accessing the newly released version 0.7 beta 2).

I am using CassandraDB::InsertRecordArray() to insert a new record to a super family, oddly it calls the same method (CassandraClient::batch_mutate()) as CassandraDB::InsertRecord() but with an extra parameter in the beginning ($this->keyspace), I looked at the source of CassandraClient::batch_mutate() and it only takes two parameters, so I removed this extra parameter in the beginning. But now I just get an exception

sPHP Fatal error: Call to a member function write() on a non-object in /....../thrift/Thrift.php on line 682

Could you assist? I am still new to Cassandra and Thrift...


Mike Peters, 10-08-2010

Send your code over to support(at) and we'll gladly help out.

Michael, 01-17-2011
Thank Mike, good work!
I use the wrapper, but have issue:
PHP Fatal error: Class 'cassandra_Clock' not found in /var/www/db_cassandra.php on line 630

How can i fixed this issue? i use cassandra 0.7.0

mod, 01-24-2011
Hi, nice wrapper, it's very helpful for me!
I also have the same issue with Michael.

PHP Fatal error: Class 'cassandra_Clock' not found in /var/www/db_cassandra.php on line 630

cassandra ver: 0.7.0
thrift ver: 0.5.0

I'd like to know how fix it. Thanks.

Thomas Pillmayr, 03-17-2011
@defkrie & mod & ...
i think you should be able to comment out the cassandra_clock lines. for me, it works pretty well ;)
Enjoyed this post?

Subscribe Now to receive new posts via Email as soon as they come out.

Post your comments

Note: No link spamming! If your message contains link/s, it will NOT be published on the site before manually approved by one of our moderators.

About Us  |  Contact us  |  Privacy Policy  |  Terms & Conditions