Created
November 23, 2017 07:50
-
-
Save allyraza/b5dc8f17d66c4338872bb3b792b8b58d to your computer and use it in GitHub Desktop.
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
// 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