Example #1
0
$sys->create_keyspace('Keyspace1', array("strategy_class" => StrategyClass::SIMPLE_STRATEGY, "strategy_options" => array('replication_factor' => '1')));
$sys->create_column_family('Keyspace1', 'Users');
// Start a connection pool, create our ColumnFamily instance
$pool = new ConnectionPool('Keyspace1', array('127.0.0.1'));
$users = new ColumnFamily($pool, 'Users');
// Insert a few records
$users->insert('user0', array("name" => "joe", "state" => "TX"));
$users->insert('user1', array("name" => "bob", "state" => "CA"));
// Fetch a user record
$user = $users->get('user0');
$name = $user["name"];
echo "Fetched user {$name}\n";
// Fetch both at once
$both = $users->multiget(array('user0', 'user1'));
foreach ($both as $user_id => $columns) {
    echo "{$user_id} state: " . $columns["state"] . "\n";
}
// Only fetch the name of user1
$columns = $users->get('user1', $column_slice = null, $column_names = array("name"));
echo "Name is " . $columns["name"] . "\n";
// Insert two more records at once
$users->batch_insert(array("user3" => array("name" => "kat"), "user4" => array("name" => "tom")));
// Remove the last row
$users->remove("user4");
// Clear out the column family
$users->truncate();
// Destroy our schema
$sys->drop_keyspace("Keyspace1");
// Close our connections
$pool->close();
$sys->close();
$pool = new ConnectionPool('Keyspace1', array('127.0.0.1'));
$user_logs = new ColumnFamily($pool, 'UserLogs');
// Don't use dictionary-style arrays for data
$user_logs->insert_format = ColumnFamily::ARRAY_FORMAT;
$user_logs->return_format = ColumnFamily::ARRAY_FORMAT;
// Make a couple of user IDs with non-Time UUIDs
$user1_id = UUID::uuid4();
$user2_id = UUID::uuid4();
// Insert some log messages
$user1_logs = array();
$user2_logs = array();
for ($i = 0; $i < 5; $i++) {
    $user1_logs[] = array(UUID::uuid1(), "message{$i}");
    $user2_logs[] = array(UUID::uuid1(), "message{$i}");
}
$user_logs->batch_insert(array(array($user1_id, $user1_logs), array($user2_id, $user2_logs)));
echo "Using ColumnFamily::ARRAY_FORMAT\n";
echo "================================\n\n";
// Pull the first two logs back out
$slice = new ColumnSlice('', '', $count = 2);
$logs = $user_logs->get($user1_id, $slice);
echo "First two logs for user1: \n";
list($uuid, $message) = $logs[0];
echo "    " . $uuid->time . ", " . $message . "\n";
list($uuid, $message) = $logs[1];
echo "    " . $uuid->time . ", " . $message . "\n\n";
// Fetch the last log for both users at once
$slice = new ColumnSlice('', '', $count = 1, $reversed = true);
$rows = $user_logs->multiget(array($user1_id, $user2_id), $slice);
foreach ($rows as $row) {
    list($user_id, $logs) = $row;
Example #3
0
// ColumnFamily::add() is the easiest way to increment a counter
$count_cf->add("key1", "col1");
$results = $count_cf->get("key1");
$current_count = $results["col1"];
echo "After one add(): {$current_count}\n";
// You can specify a value other than 1 to adjust the counter by
$count_cf->add("key1", "col1", 10);
$results = $count_cf->get("key1");
$current_count = $results["col1"];
echo "After add(10): {$current_count}\n";
// ColumnFamily::insert() will also increment values, but you can
// adjust multiple columns at the same time
$count_cf->insert("key1", array("col1" => 3, "col2" => -1));
$results = $count_cf->get("key1");
$current_count = $results["col1"];
echo "After insert(3): {$current_count}\n\n";
// And ColumnFamily::batch_insert() lets you increment counters
// in multiple rows at the same time
$count_cf->batch_insert(array("key1" => array("col1" => 1, "col2" => -1), "key2" => array("col1" => 16)));
echo "After batch_insert:\n";
print_r($count_cf->multiget(array("key1", "key2")));
// ColumnFamily::remove_counter() should basically only be used when you
// will *never* use a counter again
$count_cf->remove_counter("key1", "col1");
echo "\nRow key1 after remove_counter(key1, col1):\n";
print_r($count_cf->get("key1"));
// Destroy our schema
$sys->drop_keyspace("Keyspace1");
// Close our connections
$pool->close();
$sys->close();
 public function test_get_indexed_slices()
 {
     $cf = new ColumnFamily($this->pool, 'Indexed1');
     $cf->insert_format = ColumnFamily::ARRAY_FORMAT;
     $cf->return_format = ColumnFamily::ARRAY_FORMAT;
     $cols = array(array('birthdate', 1), array('col', 'val'));
     $rows = array(array(self::$KEYS[0], $cols), array(self::$KEYS[1], $cols), array(self::$KEYS[2], $cols));
     $cf->batch_insert($rows);
     $expr = new IndexExpression('birthdate', 1);
     $clause = new IndexClause(array($expr));
     $result = iterator_to_array($cf->get_indexed_slices($clause));
     // usort($rows, array("ArrayFormatCFTest", "sort_rows"));
     usort($result, array("ArrayFormatCFTest", "sort_rows"));
     $this->assertEquals($rows, $result);
 }