I recently released a PHP client that implements the CQL binary protocol. I wanted to explain how to get started using the client and provide a walk through for getting this setup.
Installing
Not to much to install, just copy the code to your server and you should be ready to go. You do need to make sure that your PHP installation does meet the following requirements:
- 64 bit install of PHP
- iconv PHP module is installed (usually it is installed by default)
- mbstring PHP module is installed (usually it is installed by default)
- bcmath PHP module is installed (usually it is installed by default)
- snappy PHP module installed if you want to use compression
Walk through of simple_query.php
In the examples folder there is a simple_example.php, we’ll walk through that now:
Make sure we grab the autoloader.
require '../vendor/autoload.php';
Register the namespace with the class loader.
Now we are ready to instantiate the client, we are passing in the host and the port for the Cassandra server. CQL native transport port: 9042
$pbc = new \McFrazier\PhpBinaryCql\CqlClient('192.168.2.240', '9042');
Here we are configuring the connection, by passing in the version of CQL we will be using. This has to be done before you send a query to the server.
You can get a list of the available options by calling the getSupportedStartupOptions method.
You can configure compression by passing this option when configuring the connection. You need to have the PHP snappy module installed to use compression.
Ok, now that we have the connection configured lets create a query. We have to enter two parameters, the actual valid CQL text for the query, and the query consistency.
$response = $pbc->query($queryText, \McFrazier\PhpBinaryCql\CqlConstants::QUERY_CONSISTENCY_ONE);
You will get a response object back, you can view the data payload of the response object by calling the getData() method. I’ll make a separate blog post about the response object, and the information that is being returned.
object(stdClass)#24 (3) {
["rowMetadata"]=>
object(stdClass)#14 (4) {
["flags"]=>
int(1)
["columnsCount"]=>
int(4)
["globalTableSpec"]=>
object(stdClass)#15 (2) {
["keyspace"]=>
string(6) "system"
["tableName"]=>
string(16) "schema_keyspaces"
}
["columnSpec"]=>
array(4) {
[0]=>
object(stdClass)#10 (2) {
["columnName"]=>
string(13) "keyspace_name"
["optionId"]=>
int(13)
}
[1]=>
object(stdClass)#11 (2) {
["columnName"]=>
string(14) "durable_writes"
["optionId"]=>
int(4)
}
[2]=>
object(stdClass)#12 (2) {
["columnName"]=>
string(14) "strategy_class"
["optionId"]=>
int(13)
}
[3]=>
object(stdClass)#13 (2) {
["columnName"]=>
string(16) "strategy_options"
["optionId"]=>
int(13)
}
}
}
["rowsCount"]=>
int(1)
["rowsContent"]=>
array(1) {
[0]=>
object(stdClass)#9 (4) {
["keyspace_name"]=>
string(8) "ks_34512"
["durable_writes"]=>
bool(true)
["strategy_class"]=>
string(43) "org.apache.cassandra.locator.SimpleStrategy"
["strategy_options"]=>
string(26) "{"replication_factor":"1"}"
}
}
}
Let me know if you have any questions.