Call us Toll-Free:
1-800-218-1525
Live ChatEmail us

 Sponsors

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
$cassandra->SetDisplayErrors(true);

// 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
$cassandra->SetDisplayErrors(true);

// Set our key
$key = "Mike at softwareprojects.com";

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

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

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
Sauce,

Which version of Cassandra are you running?

Sauce, 07-29-2010
Mike Peters,

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

Sauce

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: http://www.softwareprojects.com/reso...pper-1959.html
Enjoyed this post?

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

 Comments
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