1

Connecting to Cassandra with PHP is really a pain. The documentations at Apache and DataStax are extremely poorly written - for Windows users.

I have Thrift installed (I believe!) via Chocolatey. But I am still not able to compile php code for Cassandra using thrift.

If you look at this link ,

  1. now we can compile php code for Cassandra using thrift I used command: d:\cassandra\trift\thrift.exe --gen php d:\cassandra\interface\cassandra.thrift

So what is cassandra.thrift and where does it come from?? WHAT should I put inside it??

If I follow the instruction exactly, I get this error,

Could not open input file: d:\cassandra\interface\cassandra.thrift

enter image description here

So what is going on?

How do I make this work?

I have tried to install DataStax PHP Driver for Apache Cassandra and that documentation even worst.

Why PHP modules do not come with Cassandra like it does for MongoDB? Most of the independent drivers I found are outdated, not supported anymore or abandoned.

EDIT:

From the README,

Install the PHP extension

Installing with pecl

The PHP driver is not published to the official PECL repository yes. You can still install it using pecl by specifying the provided package.xml file path as the argument to pecl install command.

Install the 2.0 version of the C/C++ driver

not published to the official PECL repository yes - is it yes or yet?

Obtaining Build Dependencies

CMake
Git
ActiveState Perl
Python v2.7.x

I have downloaded and installed. Then, what? In Building the Driver,

A batch script has been created to detect installed versions of Visual...

What? Where does A batch script suddenly come from??

Then,

First you will need to open a “Command Prompt” (or Windows SDK Command Prompt) to execute the batch script.

Usage: VC_BUILD.BAT [OPTION...]

--DEBUG                           Enable debug build
--RELEASE                         Enable release build (default)
--DISABLE-CLEAN                   Disable clean build

....

What are these bunch of '--' for?

To build 32-bit shared library:

VC_BUILD.BAT --X86 To build 64-bit shared library:

VC_BUILD.BAT --X64

Where does .BAT come from? What should I put inside it? Where should I run it from??

After all, what are those Build Dependencies for? How do I use them??

Just hope that someone can write a proper guide then the guide above - it is frightening! (if you compare the guides in MongoDB, it is far better and professional)

EDIT 2:

First error when I run the .bat from my desktop,

enter image description here

I have git installed already but I still have this error,

enter image description here

After fixing git issue above, I have a new one - it just frozen there, nothing happens,

enter image description here

Run
  • 54,938
  • 169
  • 450
  • 748
  • 1
    Where exactly are you getting stuck with the DataStax driver? Saying that the documentation is worst is not actually constructive. State your specific concerns and they will be addressed. Or submit a PR. It's open source. – phact Jul 28 '15 at 12:04
  • @phact please see my edit above. thanks. – Run Jul 28 '15 at 14:31

3 Answers3

3

The IDL file cassandra.thrift is usually part of the cassandra package, but you can find it following the link above. The link points to trunk, you may want another version.

After you downloaded the right version of that file or better found it in your downloaded Cassandra package in the interface folder, generate the code as outlined in the documentation you have. The rest should be easy.

Why PHP modules do not come with Cassandra like it does for MongoDB? Most of the independent drivers I found are outdated, not supported anymore or abandoned.

I'm not really sure about that, but my guess is that the fact CQL is promoted heavily for a while now instead of using the raw Thrift API - the latter is a complex task, while CQL is more easy to use - is one of the key factors why it is this way. It more or less eliminates the need for another wrapper.


PS: Just to be sure:

thrift.exe --gen php d:\cassandra\interface\cassandra.thrift

could not open input file cassandra.thrift

Of course you point to the right drives, folders and files, do you?

JensG
  • 13,148
  • 4
  • 45
  • 55
  • thanks for the answer. my problem is not whether I `point to the right drives, folders and files`, but - what **cassandra.thrift** is and what script I should put inside that **.thrift**. – Run Jul 28 '15 at 14:33
  • as phact points out, `thrift is a legacy protocol that will be deprecated in favor of CQL so your best bet is the DataStax driver.` so DataStax driver is actually the biggest pain. I can't understand the installation instructions at all in https://github.com/datastax/php-driver/blob/master/ext/README.md and http://datastax.github.io/cpp-driver/topics/building/. They are just poorly written. – Run Jul 28 '15 at 14:37
  • You don't put anything inside. You need to find the file in the download package and then simply use it to generate PHP code. If it is not in the package (for whatever strange reason) you may pull if from the source tree. – JensG Jul 28 '15 at 14:37
  • 2
    The *Cassandra Thrift API* is considered legacy by the Cassandra people. *Thrift itself is by no means "legacy"*. Very important difference. – JensG Jul 28 '15 at 14:39
1

Update:

The datastax PHP driver is now GA and binaries are available for download (no need to build it yourself):

https://github.com/datastax/php-driver

Regarding the DataStax PHP driver, the instructions are being improved per your feedback as I type.

Because this Driver is in Beta, we do not yet have pre compiled binaries that you can simply download. They will be available once the driver is GA. For now you will have to build them yourself.

The process for building the binaries is very straight forward. 1) Install the dependencies 2) run vc_build.bat.

You can find the vc_build.bat here (just right click save as from your browser):

https://raw.githubusercontent.com/datastax/php-driver/master/ext/vc_build.bat

phact
  • 7,305
  • 23
  • 27
  • Thanks for simplying the instructions. But ` 2) run vc_build.bat.` - where do I run the .bat from? Do I need to run it fro a CMD? – Run Jul 29 '15 at 01:33
  • Well I have errors one after another if I just run the .bat on my desktop by clicking it. It just simply not working! (see my edit2. thanks) – Run Jul 29 '15 at 03:13
1

Forget about Thrift and the 'beta', I found a better solution. It very straight forward and extremely easy!

Example codes,

require_once 'lib/Cassandra/Cassandra.php';

$cassandra = new Cassandra();

$s_server_host     = '127.0.0.1';    // Localhost
$i_server_port     = 9042;
$s_server_username = 'admin';  // We don't use username
$s_server_password = 'password';  // We don't use password
$s_server_keyspace = 'demo';  // We don't have created it yet

$cassandra->connect($s_server_host, $s_server_username, $s_server_password, $s_server_keyspace, $i_server_port);

// Tests if the connection was successful:
if ($cassandra) {

    // Select:
    // Queries a table.
    $cql = "SELECT * FROM users;";

    // Launch the query.
    $results = $cassandra->query($cql);

    // Update:
    // Prepares a statement.
    $stmt = $cassandra->prepare('UPDATE users SET first_name = ?, last_name = ? where id = ?');

    // Executes a prepared statement.
    $values = array('first_name' => 'Fred', 'last_name' => 'Smith', 'id' => '1');
    $result = $cassandra->execute($stmt, $values);

    // Insert:
    // Prepares a statement.
    $stmt = $cassandra->prepare('INSERT INTO users (id, first_name, last_name)
    VALUES (:id, :first_name, :last_name)');

    // Executes a prepared statement.
    $values = array('first_name' => 'John', 'last_name' => 'Robinson', 'id' => '4');
    $result = $cassandra->execute($stmt, $values);

    // Delete:
    // Prepares a statement.
    $stmt = $cassandra->prepare('DELETE FROM users WHERE id = :id');

    // Executes a prepared statement.
    $values = array('id' => '4');
    $result = $cassandra->execute($stmt, $values);

    // Closes the connection.
    $cassandra->close();
}
Run
  • 54,938
  • 169
  • 450
  • 748
  • I think we should only consider the datatax driver when it is part of php official drivers. Before that, I would rather avoiding it at all costs due to its complexity in installation for Windows users. – Run Jul 31 '15 at 01:13