-
Notifications
You must be signed in to change notification settings - Fork 11
Examples
mjpearson edited this page Sep 17, 2010
·
29 revisions
// create named connections against PANDRA_PORT_DEFAULT (9160) PandraCore::connect('default', 'host1'); PandraCore::connect('default1', 'host2'); PandraCore::connect('default2', 'host3'); // last connected becomes the active connection // to set active node PandraCore::setActive('default'); // Otherwise, this can all be auto discovered and authenticated (SimpleAuthenticator) $ks = 'Keyspace1'; PandraCore::authKeySpace($ks, 'jsmith', 'havebadpass'); PandraCore::connectSeededKeyspace('localhost', $ks); // ------------ 'mode' and consistency tweaks... // read/write modes. // Available types - PandraCore::MODE_ACTIVE, PandraCore::MODE_ROUND, PandraCore::MODE_RANDOM // default read/write modes are _ACTIVE // where possible, round and random modes will run across APC/Memcached PandraCore::setReadMode(PandraCore::MODE_RANDOM); PandraCore::setWriteMode(PandraCore::MODE_ROUND); // get active CassandraClient (Thrift) $client = PandraCore::getClient(); // disconnections, either named or everything in the cluster PandraCore::disconnect('default'); // closes named node PandraCore::disconnectAll(); // Sets global default write consistency level // Consistency level should be tweaked to replication factor PandraCore::setWriteConsistency(cassandra_ConsistencyLevel::ONE); PandraCore::setReadConsistency(cassandra_ConsistencyLevel::QUORUM);
// Create column without validator $c = new PandraColumn('column1'); // soft attach to keyspace/columnfamily $c->setKeyID('columnTest'); $c->setKeySpace('Keyspace1'); $c->setColumnFamilyName('Standard1'); $c->setValue('new value'); // save $c->save(); // attach to a CF parent (strongly bound, registers the parent in the column and vice-versa) $c->setParent(new PandraColumnFamily($c->getKeyID(), $c->getKeyspace(), $c->getColumnFamilyName() )); // mark it for deletion $c->delete(); // column can either be saved directly via $c->save(); or via it's parent... $cfParent = $c->getParent(); $cfParent->save(); // destroy parent relationship $c->nullParent(); unset($c); // Create a column with a string20 validator, attach to our old parent $c = new PandraColumn('column2', 'string20', $cfParent); if (!$c->setValue('abc123')) { print_r($c->getLastError()); } // push validation errors to firephp during dev $devEnv = TRUE; if ($devEnv) { require_once('../FirePHP/lib/FirePHPCore/fb.php'); PandraCore::addLogger('FirePHP'); ob_start(); $c->setValue('abc123'); ob_end_clean(); // The validation error should appear in the FireBug/FirePHP console } // Validation failed, save shouldn't actually write anything. $cfParent->save();
class MyObject extends PandraColumnFamily { public function init() { $this->setKeySpace('Keyspace1); $this->setName('Standard1'); $this->addColumn('column1'); } } $keyID = 'PandraExample_CF'; PandraCore::auto('localhost'); // Create and save our test object $mo = new MyObject(); $mo->setKeyID($keyID); $mo->setColumn('column1', 'TEST DATA'); $mo->save(); unset($mo); // load ColumnFamily test object $mo = new MyObject($keyID); $mo->load(); echo 'RETRIEVED'.$mo->column_column1.'<BR>';
class Address extends PandraSuperColumn { public function init() { $this->addColumn('city', 'string'); $this->addColumn('street', 'string'); $this->addColumn('zip', 'int'); } } class Addresses extends PandraSuperColumnFamily { public function init() { $this->setKeySpace('Keyspace1'); $this->setName('Super1'); $this->addSuper(new Address('homeAddress')); $this->addSuper(new Address('workAddress')); } } $keyID = 'kenlogin'; $addrs = new Addresses(); $addrs->setKeyID($keyID); // home address $homeAddr = $addrs->getColumn('homeAddress'); $homeAddr->setColumn('city', 'san francisco'); $homeAddr->setColumn('street', '1234 x street'); $homeAddr->setColumn('zip', '94107'); // work address $workAddr = $addrs->getColumn('workAddress'); $workAddr->setColumn('city', 'san jose'); $workAddr->setColumn('street', '9876 y drive'); // custom labelled supercolumn $customAddr = new Address('customAddress'); $customAddr->setColumn('city', 'another city'); $addrs->addSuper($customAddr); // Saving via Column Family $addrs->save(); // Saving via Super Column, $customAddr->setColumn('city', 'another city'); $customAddr->save();
// all set methods will validate from the type definition on the column // be sure to check the column (family) error methods // ((string) getLastError() or public $cf->errors()) // super_ and column_ magic methods $addrs->super_homeAddress->column_city = 'Sydney'; echo "MAGIC PATH : ".$addrs->super_homeAddress->column_city."<br>"; // array syntax $addrs['homeAddress']['city'] = 'San Francisco'; echo "ARRAY PATH : ".$addrs['homeAddress']['city']."<br>"; // get method (for IDE autocompletes) $addrs->getSuper('homeAddress')->getColumn('city')->setValue('Siem Reap'); echo "GET PATH : ".$addrs->getSuper('homeAddress')->getColumn('city')->value."<br>"; // By pluggable 'Clause' $c = new PandraColumnFamily(); $c['username'] = 'myuser'; $c['homeAddress'] = ' MY HOUSE '; $c['phone'] = '987654231'; $c['mobile'] = '011465987'; $c['workAddress'] = ' MY WORK '; // regex extraction column references ending in 'address' // (ie: homeAddress and workAddress) $addresses = $c[new PandraClauseRegex('/address$/i')]; foreach ($addresses as $addressColumn) { echo "QUERIED PATH : ".$addressColumn->value."<br>"; }