-
Notifications
You must be signed in to change notification settings - Fork 11
Examples
mjpearson edited this page Sep 13, 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'); // read/write modes. // Available types - PANDRA_MODE_ACTIVE, PANDRA_MODE_ROUND, PANDRA_MODE_RANDOM // default read/write modes are _ACTIVE PandraCore::setReadMode(PANDRA_MODE_RANDOM); PandraCore::setWriteMode(PANDRA_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 consistency level for reads and writes // Consistency level should be tweaked to replication factor PandraCore::setConsistency(cassandra_ConsistencyLevel::ONE);
// Create column without validator $c = new PandraColumn('column1'); // soft attach to keyspace/columnfamily $c->setKeyID('columnTest'); $c->setKeySpace('Keyspace1); $c->setColumnFamily('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->getColumnFamily('Standard1') )); // 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()); } // 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::connect('default', '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); 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) $q = new PandraQuery(); $addresses = $c[$q->Regex('/address$/i')]->get()); foreach ($addresses as $addressColumn) { echo "QUERIED PATH : ".$addressColumn->value."<br>"; }