Guest User

Cassandra Client (simple simple)

a guest
Jan 4th, 2011
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.03 KB | None | 0 0
  1. <?php
  2.  
  3. function checkKey($key)
  4. {
  5.     return isset($_POST[$key]) && '' !== $_POST[$key];
  6. }
  7.  
  8. $Host          = checkKey('Host')          ? $_POST['Host'] : '127.0.0.1';
  9. $Keyspace      = checkKey('Keyspace')      ? $_POST['Keyspace'] : null;
  10. $Family        = checkKey('Family')        ? $_POST['Family'] : '';
  11. $SuperColumn   = checkKey('SuperColumn')   ? $_POST['SuperColumn'] : null;
  12. $KeyName       = checkKey('KeyName')       ? $_POST['KeyName'] : '';
  13. $ColumnsNames  = checkKey('ColumnsNames')  ? $_POST['ColumnsNames'] : '';
  14. $ColumnsValues = checkKey('ColumnsValues') ? $_POST['ColumnsValues'] : '';
  15. $insert        = checkKey('insert')        ? $_POST['insert'] : null;
  16.  
  17. $GLOBALS['THRIFT_ROOT'] = '/usr/share/php/Thrift';
  18. require_once $GLOBALS['THRIFT_ROOT'].'/packages/cassandra/Cassandra.php';
  19. require_once $GLOBALS['THRIFT_ROOT'].'/packages/cassandra/cassandra_types.php';
  20. require_once $GLOBALS['THRIFT_ROOT'].'/transport/TSocket.php';
  21. require_once $GLOBALS['THRIFT_ROOT'].'/protocol/TBinaryProtocol.php';
  22. require_once $GLOBALS['THRIFT_ROOT'].'/transport/TFramedTransport.php';
  23. require_once $GLOBALS['THRIFT_ROOT'].'/transport/TBufferedTransport.php';
  24.  
  25. try {
  26.   // Make a connection to the Thrift interface to Cassandra
  27.   $socket = new TSocket($Host, 9160);
  28.   $transport = new TBufferedTransport($socket, 1024, 1024);
  29.   $protocol = new TBinaryProtocolAccelerated($transport);
  30.   $client = new CassandraClient($protocol);
  31.   $transport->open();
  32.  
  33.   // We want the consistency level to be ONE which means to only wait for 1 node
  34.   $consistency_level = cassandra_ConsistencyLevel::ONE;
  35.  
  36.   if(!is_null($insert)
  37.         && !empty($ColumnsValues)
  38.             && !empty($ColumnsNames) ){
  39.            
  40.     if(count($ColumnsNames) > 1 && $ColumnsNames[1] != ''){
  41.       // build columns to insert
  42.       $ptr = 0;
  43.       // build super column containing the columns
  44.       $super_column = new cassandra_SuperColumn();
  45.       $super_column->name = $SuperColumn;
  46.       $cols = array();
  47.       foreach($ColumnsNames as $ColumnName){
  48.         if($ColumnName == '')
  49.             continue;
  50.          
  51.           $cols[$ptr] = new cassandra_Column();
  52.           $cols[$ptr]->name      = $ColumnName;
  53.           $cols[$ptr]->value     = $ColumnsValues[$ptr];
  54.           $cols[$ptr]->timestamp = time();
  55.           $ptr++;
  56.       }
  57.      
  58.       $super_column->columns = $cols;
  59.       // create columnorsupercolumn holder class that batch_insert uses
  60.       $c_or_sc = new cassandra_ColumnOrSuperColumn();
  61.       $c_or_sc->super_column = $super_column;
  62.      
  63.       // create the mutation (a map of ColumnFamily names to lists ColumnsOrSuperColumns objects
  64.       $mutation[$Family] = array($c_or_sc);
  65.        
  66.       //var_dump($Keyspace, $KeyName, $mutation, $consistency_level);
  67.        
  68.        
  69.       $batch = $client->batch_insert($Keyspace, $KeyName, $mutation, $consistency_level);
  70.      
  71.       if($batch)
  72.         echo 'Batch Insert Success! <br />';
  73.        
  74.     }else{
  75.        
  76.         // Constructing the column path that we are adding information into.
  77.         $columnPath = new cassandra_ColumnPath();
  78.         $columnPath->column_family = $Family;
  79.         $columnPath->super_column = $SuperColumn;
  80.         $columnPath->column = $ColumnsNames[0];
  81.  
  82.         // Timestamp for update
  83.         $timestamp = time();
  84.  
  85.         $single = $client->insert($Keyspace, $KeyName, $columnPath,
  86.                                     $ColumnsValues[0], $timestamp, $consistency_level);
  87.        
  88.         if($single)
  89.             echo 'Single Insert Success! <br />';
  90.     }
  91.   }
  92.  
  93.  
  94.    // Specify what Column Family to query against.
  95.   $columnParent = new cassandra_ColumnParent();
  96.   $columnParent->column_family = $Family;
  97.   $columnParent->super_column  = $SuperColumn;
  98.  
  99.  
  100.   $sliceRange = new cassandra_SliceRange();
  101.   $sliceRange->start = "";
  102.   $sliceRange->finish = "";
  103.   $predicate = new cassandra_SlicePredicate();
  104.   list() = $predicate->column_names;
  105.   $predicate->slice_range = $sliceRange;
  106.  
  107.   $result = $client->get_slice($Keyspace, $KeyName, $columnParent, $predicate, $consistency_level);
  108.  
  109.  
  110.   $transport->close();
  111.  
  112. } catch (TException $tx) {
  113.    print 'TException: '.$tx->why. ' Error: '.$tx->getMessage() . "\n";
  114. }
  115.  
  116.  
  117. ?>
  118. <style>
  119. #main{ margin:10px auto; width:650px; text-align:left; }
  120. #form {}
  121.     #form label { float:left; width:400px; margin-top:15px; }
  122.     #form input { border:solid 1px #ccc;float:right; width:180px; padding:2px 4px;}
  123.     .cols input { border:solid 1px #ccc;float:left; width:180px; padding:2px 4px; margin:0px 10px; }
  124.     .cols span  { float:left; width:400px; }
  125.     #form p { width:100%;float:left; margin-top:30px;}
  126.     #form .submit { float:left; margin-left:200px; }
  127. #result { float:left; width:600px; }   
  128. </style>
  129.  
  130. <div id="main">
  131. <form id="form" method="post">
  132. <label>Host<input name="Host" value="<?php echo $Host ?>"  /></label>
  133. <label>Keyspace<input name="Keyspace" value="<?php echo $Keyspace ?>"  /></label>
  134. <label>Family<input name="Family" value="<?php echo $Family ?>"  /></label>
  135. <label>SuperColumn<input name="SuperColumn" value="<?php echo $SuperColumn ?>" /></label>
  136. <label>KeyName<input name="KeyName" value="<?php echo $KeyName ?>" /></label>
  137. <div class="cols">
  138.  
  139.     <span>Column </span>
  140.     <span>
  141.         Name<input name="ColumnsNames[]" value="<?php echo $ColumnsNames[0] ?>" />
  142.     </span>
  143.     <span>
  144.         Value<input name="ColumnsValues[]" value="<?php echo $ColumnsValues[0] ?>" />
  145.     </span>
  146.  
  147.  
  148.     <span>Column </span>
  149.     <span>
  150.         Name<input name="ColumnsNames[]" value="<?php echo $ColumnsNames[1] ?>" />
  151.     </span>
  152.     <span>
  153.         Value<input name="ColumnsValues[]" value="<?php echo $ColumnsValues[1] ?>" />
  154.     </span>
  155.  
  156.  
  157.     <span>Column</span>
  158.     <span>
  159.         Name<input name="ColumnsNames[]" value="<?php echo $ColumnsNames[2] ?>" />
  160.     </span>
  161.     <span>
  162.         value<input name="ColumnsValues[]" value="<?php echo $ColumnsValues[2] ?>" />
  163.     </span>
  164.  
  165.    
  166.     <span>Column</span>
  167.     <span>
  168.         Name<input name="ColumnsNames[]" value="<?php echo $ColumnsNames[3] ?>" />
  169.     </span>
  170.     <span>
  171.         Value<input name="ColumnsValues[]" value="<?php echo $ColumnsValues[3] ?>" />
  172.     </span>
  173.  
  174. </div>
  175. <label>Save<input type="checkbox" name="insert" value="1" <?php echo $insert?'checked':'' ?> /></label>
  176. <p><input type="submit" /></p>
  177. </form>
  178. <fieldset id="result">
  179. <pre>
  180.   <?php print_r($result) ?>
  181. </pre>
  182. </fieldset>
  183. </div>
Advertisement
Add Comment
Please, Sign In to add comment