Skip to content
mjpearson edited this page Sep 13, 2010 · 29 revisions

Connection Handling

// create named connections against PANDRA_PORT_DEFAULT (9160)
Pandra::Connect('default', 'host1'); 
Pandra::Connect('default1', 'host2');
Pandra::Connect('default2', 'host3'); // last connected becomes the active connection

// to set active node
Pandra::setActive('default');

// read/write modes.  
// Available types - PANDRA_MODE_ACTIVE, PANDRA_MODE_ROUND, PANDRA_MODE_RANDOM
// default read/write modes are _ACTIVE
Pandra::setReadMode(PANDRA_MODE_RANDOM);
Pandra::setWriteMode(PANDRA_MODE_ROUND); 

// get active CassandraClient (Thrift)
$client = Pandra::getClient();

// disconnections, either named or everything in the cluster
Pandra::disconnect('default'); // closes named node
Pandra::disconnectAll();

// Sets global default consistency level for reads and writes
// Consistency level should be tweaked to replication factor
Pandra::setConsistency(cassandra_ConsistencyLevel::ONE);

Column Family, definition and usage

class MyObject extends PandraColumnFamily {

    var $keySpace = 'Keyspace1';
    var $name = 'Standard1';

    public function init() {
        $this->addColumn('column1');
    }
}

$keyID = 'PandraExample_CF';

Pandra::connect('default', 'localhost');

// Create and save our test object
$mo = new MyObject();
$mo->keyID = $keyID;

$mo->setColumn('column1', 'TEST DATA');
$mo->save();

unset($mo);

// load ColumnFamily test object
$mo = new MyObject($keyID);
echo 'RETRIEVED'.$mo->column_column1.'<BR>';

Super Columns, definition and usage

class Address extends PandraSuperColumn {
    public function init() {
        $this->addColumn('city', 'string');
        $this->addColumn('street', 'string');
        $this->addColumn('zip', 'int');
    }
}

class Addresses extends PandraSuperColumnFamily {

    var $keySpace = 'Keyspace1';
    var $name = 'Super1';

    public function init() {
        $this->addSuper(new Address('homeAddress'));
        $this->addSuper(new Address('workAddress'));
    }
}

$keyID = 'kenlogin';

$addrs = new Addresses();
$addrs->keyID = $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);

$addrs->save();

Column Retrieval Syntax

// 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>";

// all methods will validate from the type definition on the column 
// be sure to check the column (family) error methods
Clone this wiki locally