1. Go to this page and download the library: Download mroosz/php-cassandra library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
mroosz / php-cassandra example snippets
$nodes = [
'127.0.0.1', // simple way, hostname only
'192.168.0.2:9042', // simple way, hostname with port
[ // advanced way, array including username, password and socket options
'host' => '10.205.48.70',
'port' => 9042, //default 9042
'username' => 'admin',
'password' => 'pass',
'socket' => [SO_RCVTIMEO => ["sec" => 10, "usec" => 0], //socket transport only
],
],
[ // advanced way, using Connection\Stream, persistent connection
'host' => '10.205.48.70',
'port' => 9042,
'username' => 'admin',
'password' => 'pass',
'class' => 'Cassandra\Connection\Stream',//use stream instead of socket, default socket. Stream may not work in some environment
'connectTimeout' => 10, // connection timeout, default 5, stream transport only
'timeout' => 30, // write/recv timeout, default 30, stream transport only
'persistent' => true, // use persistent PHP connection, default false, stream transport only
],
[ // advanced way, using SSL/TLS
'class' => 'Cassandra\Connection\Stream', // "class" must be defined as "Cassandra\Connection\Stream" for ssl or tls
'host' => 'ssl://10.205.48.70',// or 'tls://10.205.48.70'
'port' => 9042,
'username' => 'admin',
'password' => 'pass',
'ssl' => ['verify_peer' => false, 'verify_peer_name' => false], // disable certificate verification
//'ssl' => ['cafile' => 'cassandra.pem', 'verify_peer_name'=>false] // with SSL certificate validation, no name check
],
];
// Create a connection.
$connection = new \Cassandra\Connection($nodes, 'my_keyspace');
//Connect
try
{
$connection->connect();
}
catch (\Cassandra\Exception $e)
{
echo 'Caught exception: ', $e->getMessage(), "\n";
}
// Set consistency level for farther requests (default is CONSISTENCY_ONE)
$connection->setConsistency(Request::CONSISTENCY_QUORUM);
// Run query synchronously.
try
{
$result = $connection->querySync('SELECT * FROM "users" WHERE "id" = ?', [new \Cassandra\Type\Uuid('c5420d81-499e-4c9c-ac0c-fa6ba3ebc2bc')]);
}
catch (\Cassandra\Exception $e)
{
}
// Return an array containing all of the result set.
$rows = $result->fetchAll(); // array
// Return an array containing a specified index column from the result set.
$col = $result->fetchCol(); // array
// Return a assoc array with key-value pairs, the key is the first column, the value is the second column.
$col = $result->fetchPairs(); // assoc array
// Return the first row of the result set.
$row = $result->fetchRow(); // ArrayObject
// Return the first column of the first row of the result set.
$value = $result->fetchOne(); // mixed
// Print all roles
$result = $connection->querySync("SELECT role FROM system_auth.roles");
foreach($result AS $rowNo => $rowContent)
{
echo $rowContent['role']."\n";
}
// Return a statement immediately
try
{
$statement1 = $connection->queryAsync($cql1);
$statement2 = $connection->queryAsync($cql2);
// Wait until received the result, can be reversed order
$result2 = $statement2->getResult();
$result1 = $statement1->getResult();
$rows1 = $result1->fetchAll();
$rows2 = $result2->fetchAll();
}
catch (\Cassandra\Exception $e)
{
}