Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Examples to retrieve data #42

Open
loretoparisi opened this Issue Jul 29, 2010 · 1 comment

Comments

Projects
None yet
1 participant

I'm a newbie to Pandra.
How about data retrieval?

I'm using these techniques by now:

Suppose to have this array

$myArrayKeyValues = array(
'key1' => 'val1',
'key2' => 'val2',
);

// 1. Clause plugins extensions

    class PandraClauseInAddon extends PandraClauseIn {
   public function match($value) {
          // you can implement here your matching logic
          return ( in_array($value, $this->getValueIn()) );
        }
    }

And then apply the plugin to a key name array to retrieve all keys

    $matches=$someSuperColumn->getColumn( 
   new PandraClauseInAddon(
    array_keys( $myArrayKeyValues ) )
    );

// 2. Slicing - you'love it! Look at Pandra Examples

$result = PandraCore::getCFSlice($ks,
    $keyID,
    new cassandra_ColumnParent(array(
            'column_family' => $cfName,
    )),
    new PandraSlicePredicate(
      PandraSlicePredicate::TYPE_RANGE,
       array('start' => '0',
            'finish' => '10',
            'count' => 5,
            'reversed' => true))
    );

NOTE: Due to a bug in previous 0.6.3 Cassandra binaries , you've got an error if you don't specifiy 'start' and 'finish' values when comparing UUID types (CompareWith="TimeUUIDType"). That (fixed) bug described here: https://issues.apache.org/jira/browse/CASSANDRA-377

// 3. Iterating over columns to match inner values. I really hate this!

    $subColumns=$col->getColumns(); // get sub - columns
foreach($subColumns as $sub) {
    if (  in_array($sub->getValue(),$myArrayKeyValues) ) {
               // found some item
           }
}

// Best ways to match values / retrieve keys using great Pandra functionalities?

I recently added a new technique to retrieve values from Cassandra.

Here are you:

// Perform a RANGE KEYS QUERY

            $maxRows=500;
    $res=PandraCore::getRangeKeys(
        'myNS',
        array('start'=>'','finish'=>''),
        new cassandra_ColumnParent(array(
            'column_family' => 'myColumnFamily',
        )),
    new PandraSlicePredicate(
        PandraSlicePredicate::TYPE_RANGE,
        array('start' => '',
            'finish' => '',
            'count' => $maxRows,
            'reversed' => true)),
            $maxRows
        );

// Then apply a MULTI SLICE on those KEYS:

    foreach($res as $k => $v) { // columns

                         foreach($v->columns as $col) {
            if(!empty($col->column->value))
                $keys[]=$col->column->value;
        }

                         $preResult = PandraCore::getCFSliceMulti(
                'myNS', 
                $keys, 
                new cassandra_ColumnParent(array(
                    'column_family' => 'myCF',
                )),
                new PandraSlicePredicate(
                    PandraSlicePredicate::TYPE_RANGE,
                    array('start' => '',
                        'finish' => '',
                        'count' => $maxRows,
                        'reversed' => true))
            );
                    } // end cols

In this way you are doing a query based on keys slice,
then having a list of keys, you can perform a multi_slice query,
much better than a simple slice query.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment