📜 ⬆️ ⬇️

Asynchronous Php extension to work with Cassandra without Thrift

Greetings, habrasoobschestvo!
I think many people who worked with the Cassandra database from php know that all existing drivers use the Thrift interface in themselves, which is declared as deprecated in version 0.8.
Instead, the developers recommend using the new CQL database access interface (Cassandra Query Language), but there is 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 with the official asynchronous C ++ driver with Php. Who cares what came out of it - I ask for cat.

How to make friends with the plus code with Php is described in sufficient detail on the devzon zone . Probably many, this link has already come across, if you are somehow interested in the development of extensions for Php. Attention should be paid 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 quite trivial 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 a library libcql.so.0.7.0 on which you 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 will see an 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 in the comments :)

The interface of the module repeats the interface of the original driver and contains the classes: CqlBuilder, CqlCluster, CqlError, CqlFutureResult, CqlQuery, CqlSession, CqlResult.

Example of module interaction:

 // 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(); 

Source: https://habr.com/ru/post/221521/


All Articles