Asynchronous Php extension for working with Cassandra without Thrift
I think many who worked with the Cassandra database from php know that all existing drivers use the Thrift interface, which is declared as deprecated in version 0.8.
Instead, developers recommend using the new CQL (Cassandra Query Language) access interface, but there has been no driver for php for the new protocol for a very long time. There are drivers for C ++, Java, C # and Python in the official Datastax repository . As you know, Php itself is written in C, which means that by rolling up our sleeves we can make friends the official C ++ asynchronous driver with Php. Who cares what came of it - I ask for a cat.
How to make friends with a positive code with Php is described in detail on the zenda devzona. Most likely this link has already come across to many if you are at least somehow interested in developing extensions for Php. You should pay attention to the PHP_REQUIRE_CXX () macro in config.m4, as well as the need to manually add the stdc ++ library, if, of course, you used it when developing your module.
Building the Datastax C ++ library is trivial enough and all you need to do is download the official driver
git clone https://github.com/datastax/cpp-driver.git
Install Boost, Openssl and Cmake to build, if you haven’t already installed them and compile the driver
cd cpp-driver
cmake . && make && make install
Hint: make install is not necessary to do, because all we need is the libcql.so.0.7.0 library for which we can make a symlink
ln -s libcql.so.0.7.0 /usr/lib/libcql.so.0
ln -s /usr/lib/libcql.so.0 /usr/lib/libcql.so
After installing the official driver, we can use our wrapper:
git clone https://github.com/aparkhomenko/php-cassandra.git
cd php-cassandra
phpize && ./configure && make
If there are no errors in the modules folder, you can see the extension for Php cassandra.so
We can check that it works correctly for us:
php -d="extension=modules/cassandra.so" -m
In the list of modules should be the inscription cassandra. If everything worked out - congratulations; if not, please comment :)
The interface of the module repeats the interface of the original driver and contains the classes: CqlBuilder, CqlCluster, CqlError, CqlFutureResult, CqlQuery, CqlSession, CqlResult.
Module interaction example:
// Suppose you have the Cassandra cluster at 127.0.0.1,
// listening at default port (9042).
$builder = new CqlBuilder();
$builder->addContactPoint("127.0.0.1");
// Now build a model of cluster and connect it to DB.
$cluster = $builder->build();
$session = $cluster->connect();
// Write a query, switch keyspaces.
$query = new CqlQuery('SELECT * FROM system.schema_keyspaces');
// Send the query.
$future = $session->query($query);
// Wait for the query to execute; retrieve the result.
$future->wait();
$result = $future->getResult();
if (null === $future->getError()) {
echo "rowCount: {$result->getRowCount()}\n";
while ($result->next()) {
echo "strategy_options: " . $result->get("strategy_options") . "\n";
}
}
// Boilerplate: close the connection session and perform the cleanup.
$session->close();
$cluster->shutdown();