Skip to content

Instantly share code, notes, and snippets.

@allyraza
Created November 23, 2017 07:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save allyraza/b5dc8f17d66c4338872bb3b792b8b58d to your computer and use it in GitHub Desktop.
Save allyraza/b5dc8f17d66c4338872bb3b792b8b58d to your computer and use it in GitHub Desktop.
// create a file under your app/components/Cassandra.php and put the following code in it.
<?php
namespace app\components;
use yii\base\Component;
/**
* Cassandra component.
*/
class Cassandra extends Component
{
protected $session = null;
public $keyspace = null;
public $servers = null;
public $result = null;
public function init()
{
$cluster = \Cassandra::cluster()->build();
$this->session = $cluster->connect($this->keyspace);
}
public function query($cql)
{
$this->result = $this->session->execute($cql);
return $this;
}
public function all()
{
return iterator_to_array($this->result, true);
}
}
?>
// configure your newly created cassandra components in config/web.php
<?php
[
//...
'cassandra' => [
'class' => 'app\components\Cassandra',
'keyspace' => 'adstream',
'servers' => ['127.0.0.1:9042'],
],
// ...
]
// once all that is in place you can query your cassandra tables anywhere in your app
$data = Yii::$app->cassandra->query('SELECT * FROM transactions')->all();
foreach($data as $row) {
echo $row['user_agent'];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment