Native Cassandra connector for PHP based on the CQL binary protocol (v1), without the need for an external extension.
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
Cassandra
docs
LICENSE
README.md

README.md

Documentation for class Cassandra

Last update : 2014/04/29

Overview :

A native Apache Cassandra connector for PHP based on the CQL binary protocol (v1), without the need for an external extension.

Requires PHP version 5, and Cassandra >1.2.

Important :

Make sure you turn on the native transport for Cassandra by editing your cassandra.yaml file and adding the following line (see External link 1):

start_native_transport: true

Usage :

Available methods:

connect($host, $user = '', $passwd = '', $dbname = '', $port = 9042)

Connects to a Cassandra node.

@param string $host   Host name/IP to connect to.
@param string $user   Username in case authentication is needed.
@param string $passwd Password in case authentication is needed.
@param string $dbname Keyspace to use upon connection.
@param int    $port   Port to connect to.

@return int The socket descriptor used. FALSE if unable to connect.

close()

Closes an opened connection.

@return int 1

query($cql, $consistency = CASSANDRA_CONSISTENCY_ALL)

Queries the database using the given CQL.

@param string $cql         The query to run.
@param int    $consistency Consistency level for the operation.

@return array Result of the query. Might be an array of rows (for SELECT),
              or the operation's result (for USE, CREATE, ALTER, UPDATE).
              NULL on error.

prepare($cql)

Prepares a query.

@param string $cql The query to prepare.

@return array The statement's information to be used with the execute
              method. NULL on error.

execute($stmt, $values, $consistency = CASSANDRA_CONSISTENCY_ALL)

Executes a prepared statement.

@param array $stmt        The prepared statement as returned from the
                          prepare method.
@param array $values      Values to bind in key=>value format where key is
                          the column's name.
@param int   $consistency Consistency level for the operation.

@return array Result of the execution. Might be an array of rows (for
              SELECT), or the operation's result (for USE, CREATE, ALTER,
              UPDATE).
              NULL on error.

In addition, a wrapper has been made for those who prefer to work with procedural programming. To use the wrapper, make sure to include Cassandra_Procedural.php that contains the following methods:

cassandra_connect($host, $user = '', $passwd = '', $dbname = '', $port = 9042)

Same as $Cassandra->connect() above. Returns an object type if connection
was successfull. Otherwise returns NULL.

cassandra_close($obj)

Same as $Cassandra->close() above. Use $obj from cassandra_connect as the
first parameter.

cassandra_query($obj, $cql, $consistency = CASSANDRA_CONSISTENCY_ALL)

Same as $Cassandra->query() above. Use $obj from cassandra_connect as the
first parameter.

cassandra_prepare($obj, $cql)

Same as $Cassandra->prepare() above. Use $obj from cassandra_connect as the
first parameter.

cassandra_execute($obj, $stmt, $values, $consistency = CASSANDRA_CONSISTENCY_ALL)

Same as $Cassandra->execute() above. Use $obj from cassandra_connect as the
first parameter.

Sample usage :

$obj = new Cassandra();

// Connects to the node:
$res = $obj->connect('127.0.0.1', 'my_user', 'my_pass', 'my_keyspace');

// Tests if the connection was successful:
if ($res)
{
    // Queries a table:
    $arr = $obj->query('SELECT col1, col2, col3 FROM my_table');

    // $arr, for example, can contain:
    // Array
    // (
    //     [0] => Array
    //         (
    //             [col1] => first row
    //             [col2] => 1
    //             [col3] => 0x111111
    //         )
    //
    //     [1] => Array
    //         (
    //             [col1] => second row
    //             [col2] => 2
    //             [col3] => 0x222222
    //         )
    //
    // )

    // Prepares a statement:
    $stmt = $obj->prepare('UPDATE my_table SET col2=?,col3=? where col1=?');

    // Executes a prepared statement:
    $values = array('col2' => 5, 'col3' => '0x55', 'col1' => 'five');
    $pResult = $obj->execute($stmt, $values);

    // Upon success, $pResult would be:
    // Array
    // (
    //     [0] => Array
    //         (
    //             [result] => success
    //         )
    //
    // )

    // Closes the connection:
    $obj->close();
}

or, same as above in procedural style:

// Connects to the node:
$handle = cassandra_connect('127.0.0.1', 'my_user', 'my_pass', 'my_keyspace');

// Tests if the connection was successful:
if ($handle)
{
    // Queries a table:
    $arr = cassandra_query($handle, 'SELECT col1, col2, col3 FROM my_table');

    // Prepares a statement:
    $stmt = cassandra_prepare($handle,
        'UPDATE my_table SET col2=?,col3=? where col1=?');

    // Executes a prepared statement:
    $values = array('col2' => 5, 'col3' => '0x55', 'col1' => 'five');
    $pResult = cassandra_execute($handle, $stmt, $values);

    // Closes the connection:
    cassandra_close($handle);
}

External links :

  1. Datastax's blog introducing the binary protocol: http://www.datastax.com/dev/blog/binary-protocol

  2. CQL definitions https://github.com/apache/cassandra/blob/trunk/doc/native_protocol_v1.spec

License :

The MIT License (MIT)

Copyright (c) 2014 Uri Hartmann

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.