Created
August 17, 2010 16:52
-
-
Save jdmaturen/530803 to your computer and use it in GitHub Desktop.
Cassandra 0.7 thrift example. CC license.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$GLOBALS['THRIFT_ROOT'] = '/path/to/thrift/'; | |
require_once $GLOBALS['THRIFT_ROOT'].'/packages/cassandra/Cassandra.php'; | |
require_once $GLOBALS['THRIFT_ROOT'].'/packages/cassandra/cassandra_types.php'; | |
require_once $GLOBALS['THRIFT_ROOT'].'/transport/TSocket.php'; | |
require_once $GLOBALS['THRIFT_ROOT'].'/protocol/TBinaryProtocol.php'; | |
require_once $GLOBALS['THRIFT_ROOT'].'/transport/TFramedTransport.php'; | |
require_once $GLOBALS['THRIFT_ROOT'].'/transport/TBufferedTransport.php'; | |
function microsecond_timestamp() { | |
list($frag, $seconds) = explode(' ', microtime()); | |
return $seconds . substr($frag, 2, 6); | |
} | |
try { | |
// Make a connection to the Thrift interface to Cassandra | |
$socket = new TSocket('127.0.0.1', 9160); | |
$transport = new TFramedTransport($socket, 1024, 1024); | |
$protocol = new TBinaryProtocolAccelerated($transport); | |
$client = new CassandraClient($protocol); | |
$transport->open(); | |
/* Insert some data into the Stream column family, remove some columns, perform column slice queries */ | |
// "Stream" is analogous to a twitter user timeline | |
$keyspace = 'TwitterSpace'; | |
$client->set_keyspace($keyspace); | |
// $client->truncate('Stream'); | |
// reference to specific User id | |
$keyUserId = "jd2"; | |
// Add Columns for ids [0..1000] | |
$mutations = array(); | |
foreach(range(0,1000) as $x) { | |
$clock = new cassandra_Clock(array('timestamp' => microsecond_timestamp())); | |
$column1 = new cassandra_Column(array('name' => (string)$x, 'value' => '', 'clock' => $clock)); | |
$c_or_sc = new cassandra_ColumnOrSuperColumn(array('column' => $column1)); | |
$mutations[] = new cassandra_Mutation(array('column_or_supercolumn' => $c_or_sc)); | |
} | |
$client->batch_mutate(array($keyUserId => array('Stream' => $mutations)), cassandra_ConsistencyLevel::QUORUM); | |
// Remove id 999 from list | |
$cp = new cassandra_ColumnPath(array('column_family' => 'Stream', 'column' => '999')); | |
$clock = new cassandra_Clock(array('timestamp' => microsecond_timestamp())); | |
$client->remove($keyUserId, $cp, $clock, cassandra_ConsistencyLevel::QUORUM); | |
// Get top 3 ids from list, sorted high to low | |
// Specify what Column Family to query against. | |
$columnParent = new cassandra_ColumnParent(array('column_family' => 'Stream')); | |
$sliceRange = new cassandra_SliceRange(array('reversed' => true, 'start' => '', 'finish' => '', 'count' => 3)); | |
$predicate = new cassandra_SlicePredicate(array('slice_range' => $sliceRange)); | |
$result = $client->get_slice($keyUserId, $columnParent, $predicate, cassandra_ConsistencyLevel::ONE); | |
print_r(array_map(function($obj) { return array($obj->column->name, $obj->column->clock->timestamp); }, $result)); | |
// array(array(1000, {timestamp}), array(998, {timestamp}), array(997, {timestamp})) | |
// Remove entire key and fetch column results again [empty array] | |
$cp = new cassandra_ColumnPath(array('column_family' => 'Stream')); | |
$clock = new cassandra_Clock(array('timestamp' => microsecond_timestamp())); | |
$client->remove($keyUserId, $cp, $clock, cassandra_ConsistencyLevel::QUORUM); | |
$result = $client->get_slice($keyUserId, $columnParent, $predicate, cassandra_ConsistencyLevel::ONE); | |
print_r($result); | |
$transport->close(); | |
} catch (TException $tx) { | |
print $tx; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment