接上例:http://blog.csdn.net/kunshan_shenbin/article/details/7193289
官方主页:http://code.google.com/p/simpletools-php/
API介绍:http://code.google.com/p/simpletools-php/wiki/SimpleCassie
和上例一样,依然尝试将SimpleCassie继承到CodeIgniter中运行。
1. 下载源码:http://code.google.com/p/simpletools-php/downloads/list,解压后把SimpleCassie.php拷贝至application/libraries下。
2. 修改autoload.php如下:
$autoload['libraries'] = array('SimpleCassie', 'smarty');
注意,如果和上例是同一个工程,则不用让SimpleCassie和上例中的db同时自动加载,因为他们都是cassandra的client类库,很多的class同名的。
3. 为了能够被加载进codeigniter,SimpleCassie的构造函数需要修改一下:
public function __construct() {}
// public function __construct($host,$port=9160,$timeout=null)
// {
// $this->__nodes[$host] = array($port,$timeout);
// }
由于对CodeIgniter不是很熟,这一步是不是必须目前不敢确定。
4. 在controller里可以直接使用SimpleCassie类库了:
$cassie = $this->simplecassie;
$cassie->addNode("192.168.11.124", 9160);
$cassie->addNode("192.168.11.185", 9160);
if(!$cassie->isConnected())
throw new Exception('Couldn\'t connect to server');
$activeNode = $cassie->getActiveNode();
$cassie->keyspace('LiftDNA_DB');
$cassie->keyspace('MyApp')->cf('LiftDNA_Table')->key('shenyi')->column('age')->set('27');
$cassie->cf('LiftDNA_Table')->key('yangye')->column('age')->set('28');
//$cassie->cf('LiftDNA_Table')->key('yangye')->column('name')->batch('28');
//$cassie->cf('LiftDNA_Table')->key('yangye')->column('age')->batch('29');
//$cassie->batchCommit();
$age = $cassie->cf('LiftDNA_Table')->key('shenyi')->column('age')->value();
$this->load->view('cassandra', $age);
<?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);
?>
<?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;
?>