My favorites | Sign in
Project Home Downloads Wiki Issues Source
Search
for
SimpleCassie  

Featured
Updated Dec 22, 2010 by 33Concep...@gmail.com

Apache Cassandra PHP Client

Introduction

SimpleCassie is entirely stand-alone package which wrap itself around Thrift libs (licensed to Apache Software Foundation) already built into this class.

SimpleCassie supports all features of TimeUUIDType.

The latest version is compatible only with Cassandra 7.0.x and higher.

Please report all bugs under: http://code.google.com/p/simpletools-php/issues/list so will be able to help and comment on them easier.

User Notice: as from SimpleCassie 0.7.1.4 defaults to i32 timestamps due to compatibility between 32 and 64 bits systems when using native thrift protocol. However for backward-compatibility with previous versions which default to i64 SimpleCassie can be forced to defaults to i64 long timestamps by calling ->useI64Timestamps(); method

User Notice: as from SimpleCassie 0.7.1.3 path is being reset after issuing command so you need to remember to always specify full path ->cf()->key()->.... with every command. The only exception of that is keyspace as this is being set globally per latest Cassandra API so all command are sharing lastly set keyspace

Usage

  <?php

  require_once('SimpleCassie.php');

  $cassie = new SimpleCassie(HOST, PORT, TIMEOUT_MS);


  //failover support added since SimpleCassie 0.7.1.2 by:
  $cassie->addNode(HOST2, PORT2, TIMEOUT_MS);
  $cassie->addNode(HOST3, PORT3, TIMEOUT_MS);

  if(!$cassie->isConnected())
    throw new Exception('Couldn\'t connect to server');

  //checking active node - since SimpleCassie 0.7.1.2
  $activeNode = $cassie->getActiveNode();

  /*
  * setting working keyspace
  * @return - (false) on failure true on success
  * working keyspace can be change at any point by running ->keyspace() method
  */
   $cassie->keyspace('Keyspace1');


  /*
  * setting new column (and key if not exist)
  * @return - (false) on failure
  */
   $cassie->keyspace('MyApp')->cf('Users')->key('user1')->column('name')->set('Marcin');
   $cassie->cf('Users')->key('user1')->column('surname')->set('Rosinski');
   
  /*
  * setting in batches
  * @return - number of batches or false on failure
  */
   $cassie->cf('Users')->key('user1')->column('name')->batch('Marcin');
   $cassie->cf('Users')->key('user1')->column('surname')->batch('Rosinski');

   //batching deletion
   $cassie->cf('Users')->key('user1')->column('columntodrop')->batch();

   //commiting all above batches
   $cassie->batchCommit();

  /*
  * delete column or row/key
  * @return - (false) on failure
  */

  //deleting column
  $cassie->cf('Users')->key('user1')->column('name')->remove();

  //deleting row
  $cassie->cf('Users')->key('user1')->remove();

   /*
  * count number of columns in row/key
  * @return - (int) on succes, false on failure
  */
  $count = $cassie->cf('Users')->key('user2')->count();

  /*
  * count number of columns with predicate: from column to column
  * @return - (int) on succes, false on failure
  */
  $count = $cassie->cf('Users')->key('user2')->column('fromColumn','toColumn')->count();

/*
  * count number of columns with predicate: from column to column for multiple keys
  * @return - (int) on succes, false on failure
  */
  $count = $cassie->cf('Users')->key('user2','user3')->column('fromColumn','toColumn')->count();

  /*
  * getting single column
  * @return - object on succes,  null on failure
  */
  $name = $cassie->keyspace('Keyspace1')->cf('Standard1')->key('user1')->column('name')->get();

  /*
  * getting multiple columns
  * @return - array of objects on success, null on failure
  */
  $user = $cassie->cf('Standard1')->key('user1')->column('name','surname')->get();

/*
  * getting multiple column values
  * @return - array of objects on success, null on failure
  */
  $user = $cassie->cf('Standard1')->key('user1')->column('name','surname')->value();

  /*
  * getting single column from multiple rows/keys
  * @return - array of objects on succes,  null on failure
  */
  $users = $cassie->cf('Standard1')->key('user1','user2')->column('name')->get();

  /*
  * getting multiple columns from multiple rows/keys
  * @return - array of objects on succes,  null on failure
  */
  $users = $cassie->cf('Standard1')->key('user1','user2')->column('name','username')->get();

  /*
  * getting slice of columns from single row/key
  * @return - array of objects on succes,  null on failure
  */
  $limit = 10;
  $reversed = false;
  $from_name = 'Puma';
  $to_name = 'Tiger';
  $friends = $cassie->cf('Standard1')->key('user1friends')->column($from_name,$to_name)->slice($limit,$reversed);

  /*
  * getting slice of columns from single supercolumn row
  * @return - array of objects on succes,  null on failure
  */
  $limit = 10;
  $reversed = false;
  $friends = $cassie->cf('Standard1')->key('user1')->supercolumn('friends')->column($from_name,$to_name)->slice($limit,$reversed);

  //resetting supercolumn for future use - deprecated since SimpleCassie 0.7.1.3 - this process has been automated
  $cassie->key('user1')->supercolumn(null);

  /*
  * getting slice of columns from multiple rows/keys
  * @return - array of objects on succes,  null on failure
  */
  $limit = 10;
  $reversed = true;
  $friends = $cassie->cf('Standard1')->key('user1friends','user2friends')->column($from_name,$to_name)->slice($limit,$reversed);

  /*
  * increment column value
  * @return - (int) new value on succes, false on failure
  */
  $new_value = $cassie->cf('Standard1')->key('user1')->column('friends')->increment();

  /*
  * decrement column value
  * @return - (int) new value on succes, false on failure
  */
  $new_value = $cassie->cf('Standard1')->key('user1')->column('friends')->decrement();


  /*
  * Getting active keyspace
  */
  echo $cassie->keyspace();

  /*
   * Range support - since SimpleCassie 0.7.1.2
   */
  $range = $cassie->cf('MyColumnFamily')->key('fromKey','toKey')->column('fromColumn','toColumn')->range($keyCount,$columnCount);

  ?>

UUID Usage Examples

  <?php 

  //setting new uuid column
  $cassie->keyspace('MyApp')->cf('BlogPosts')->key('post')->column($cassie->uuid())->set('I like raw food.');

  //getting latest added post (assuming TimeUUIDType)
  $post = $cassie->cf('BlogPosts')->key('post')->slice(1);

  //getting post column uuid name in canonical form
  $uuid = $cassie->uuid($post->column->name);

  $string_form = (string) $uuid; //canonical form
  $binary_form = $uuid->uuid;

  ?>

Using the Native PHP Extension

The PHP Native extension is giving thrift real performance boost so its highly recommend to install it.

Please apply patch if getting timeouts, more info here: https://issues.apache.org/jira/browse/THRIFT-867 - packed patch can be downloaded from http://simpletools-php.googlecode.com/files/thrift_protocol_patch.zip

Installation on Centos

sudo yum install php-devel

cd PATH-TO-THRIFT/lib/php/src/ext/thrift_protocol
phpize
./configure --enable-thrift_protocol
make

sudo cp modules/thrift_protocol.so /usr/lib64/php/modules/

# enable module: /etc/php.d/thrift_protocol.ini
extension=thrift_protocol.so

Development Roadmap

  • Connection Load Balancing
  • ->remove() method for multiple rows/keys - possible in batch() method, look above
  • ->increment() method for multiple rows/keys
  • ->decrement() method for multiple rows/keys
  • all describe methods

Download Link

SimpleCassie.php download - http://simpletools-php.googlecode.com/files/SimpleCassie.zip

Discussion Group

SimpleCassie related discussion

Contributes

33Concept Ltd
WorkDigital Ltd
Shopperhive Ltd

Comment by amalgam....@gmail.com, Mar 28, 2010

ok now, it's the browser's cache, i downloaded the older version. i download it with wget again and the new version works. Thanks

Comment by goo...@fuzzster.com, Mar 28, 2010

The supercolumn->set is working excellently, thank you for the update.

When slicing a supercolumn at the key level is there a way to limit the column results in the sub-array returned?

ie. $cassie->keyspace('Keyspace1')->cf('Super1')->key('test')->slice(5);

will return five supercolumn arrays of unlimited 'columns'. Is it possible to limit the number of 'columns' returned?

I'm not sure if it's a limitation of Cassandra itself?

Comment by project member mr.rosin...@gmail.com, Mar 29, 2010

I don't think that Cassandra allow for it but I will make sure and add this option if possible and let you know. Thanks for the feedback btw.

Comment by project member mr.rosin...@gmail.com, Apr 2, 2010

yup, this is what I will be working on following week, let you know once ready but should be soon. cheers

Comment by QiH...@gmail.com, May 17, 2010

Does this lib support Authentication?

Comment by platon.d...@gmail.com, May 24, 2010

How to get all keys in column family? Is there a possibility of including a method for get_key_range or get_range_slice?

Comment by platon.d...@gmail.com, May 25, 2010

And what is about get_range_slices for 0.6. I was looking and couldn't finde any methods.

Comment by mariosh...@gmail.com, Jul 5, 2010

get() does not work! I'm trying to get and print a column's data. I get the error: Object of class cassandra_ColumnOrSuperColumn? could not be converted to string.

What I'm I doing wrong?

the code I use is: $email = $cassie->keyspace('Test')->cf('Users')->key('marios')->column('email')->get(); echo $email;

Thanks.

Comment by project member mr.rosin...@gmail.com, Nov 21, 2010

ok guys, going to crack on on that now, will be ready soon, support fro 0.7.x

Comment by project member 33Concep...@gmail.com, Nov 21, 2010

will keep you updated

Comment by project member 33Concep...@gmail.com, Nov 21, 2010

please report all bugs into issues tab: http://code.google.com/p/simpletools-php/issues/list

Comment by project member 33Concep...@gmail.com, Nov 21, 2010

the latest version will be fully compatible with cassandra 0.7.x and won't be backward compatible with lover versions becase of Cassandra API changes

Comment by project member 33Concep...@gmail.com, Nov 21, 2010

ok, so latest class is almost ready, as wrote above, will be fully ported to Cassandra 0.7.x which make it not compatible with lower Cassandra builds, however SimpleCassie syntax won't change so there won't be need to rewrite your applications, just Cassandra update will sort all.

Comment by project member 33Concep...@gmail.com, Nov 22, 2010

ok, the latest class is ready to download, with most of the goods from Cassandra 0.7.x. - missing methods coming really soon...

Comment by MrMichae...@gmail.com, Feb 9, 2011

Did you update simpleCassie with Increment counters (issues.apache.org/jira/browse/CASSANDRA-1072) ?

Comment by project member gilhilde...@gmail.com, Apr 8, 2011

Note there has been a change in the count() function between Simplecassie 0.6 and Simplecassie 0.7.

Originally the count() function only accepted consistency level as a parameter. In 0.7 version, that is the third parameter, while the first two are $limit and $reversed.

Comment by douglas....@bigbrer.com, Apr 28, 2011

A little help here. I successfully connnect to an Amazon ec2 instance with cassandra running.

I created the 'xxx' keyspace with column family 'Locations' ...Here is the definition:

Keyspace: 'xxx': Replication Strategy: org.apache.cassandra.locator.SimpleStrategy?

Replication Factor: 1
Column Families:
ColumnFamily?: Locations (Super)
Columns sorted by: org.apache.cassandra.db.marshal.UTF8Type/org.apache.cassandra.db.marshal.BytesType? Row cache size / save period: 0.0/0 Key cache size / save period: 200000.0/14400 Memtable thresholds: 1.0875/232/1440 GC grace seconds: 864000 Compaction min/max thresholds: 4/32 Read repair chance: 1.0 Built indexes: Column Metadata:
Column Name: name (6e616d65)
Validation Class: org.apache.cassandra.db.marshal.UTF8Type

$activeNode = $cassie->getActiveNode(); $cassie->keyspace("xxx");

//This gives no output $cassie->supercolumn("Locations")->key("first")->column("name")->set("Dougie Fresh"); echo $cassie->supercolumn("Locations")->key("first")->column("name")->get();


Sign in to add a comment
Powered by Google Project Hosting