jippi/php-cassandra

This package is abandoned and no longer maintained. No replacement package was suggested.

Cassandra client library for PHP, which support Protocol v3 and asynchronous request

v0.5.2 2014-09-29 08:11 UTC

This package is auto-updated.

Last update: 2024-06-14 06:42:32 UTC


README

68747470733a2f2f636f6465636c696d6174652e636f6d2f6769746875622f64756f7368756f2f7068702d63617373616e6472612e706e67 68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f64756f7368756f2f7068702d63617373616e6472612f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572 68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f64756f7368756f2f7068702d63617373616e6472612f6261646765732f6275696c642e706e673f623d6d6173746572

Cassandra client library for PHP, which support Protocol v3 (Cassandra 2.1) and asynchronous request

Features

  • Using Protocol v3 (Cassandra 2.1)
  • Support asynchronous and synchronous request
  • Support for logged, unlogged and counter batches
  • The ability to specify the consistency, "serial consistency" and all flags defined in the protocol
  • Support Query preparation and execute
  • Support all data types conversion and binding, including collection types, tuple and UDT
  • Support conditional update/insert
  • 5 fetch methods (fetchAll, fetchRow, fetchPairs, fetchCol, fetchOne)
  • 800% performance improvement(async mode) than other php cassandra client libraries

Installation

PHP 5.4+ is required. There is no need for additional libraries.

Append dependency into composer.json

	...
	"require": {
		...
		"duoshuo/php-cassandra": "dev-master"
	}
	...

Basic Using

<?php

$nodes = [
	'127.0.0.1',		// simple way, hostname only
	'192.168.0.2:9160',	// simple way, hostname with port 
	[				// advanced way, array including username, password and socket options
		'host'		=> '10.205.48.70',
		'port'		=> 9042,
		'username'	=> 'admin',
		'password'	=> 'pass',
		'socket'	=> [
			SO_RCVTIMEO => ["sec" => 10, "usec" => 0],
		],
	],
];

// Create a connection.
$connection = new Cassandra\Connection($nodes, 'my_keyspace');

// Run query synchronously.
$response = $connection->querySync('SELECT * FROM "users" WHERE "id" = ?', [new Cassandra\Type\Uuid('c5420d81-499e-4c9c-ac0c-fa6ba3ebc2bc')]);

Fetch Data

// Return a SplFixedArray containing all of the result set.
$rows = $response->fetchAll();		// SplFixedArray

// Return a SplFixedArray containing a specified index column from the result set.
$col = $response->fetchCol();		// SplFixedArray

// Return a assoc array with key-value pairs, the key is the first column, the value is the second column. 
$col = $response->fetchPairs();		// assoc array

// Return the first row of the result set.
$row = $response->fetchRow();		// ArrayObject

// Return the first column of the first row of the result set.
$value = $response->fetchOne();		// mixed

Query Asynchronously

// Return a statement immediately
$statement = $connection->queryAsync($cql);

// Wait until received the response
$response = $statement->getResponse();

$rows = $response->fetchAll();

Using preparation and data binding

$preparedData = $connection->prepare('SELECT * FROM "users" WHERE "id" = :id');

$strictValues = Cassandra\Request\Request::strictTypeValues(
	[
		'id' => 'c5420d81-499e-4c9c-ac0c-fa6ba3ebc2bc',
	],
	$preparedData['metadata']['columns']
);

$response = $connection->executeSync(
	$preparedData['id'],
	$strictValues,
	Cassandra\Request\Request::CONSISTENCY_QUORUM,
	[
		'page_size' => 100,
		'names_for_values' => true,
		'skip_metadata' => true,
	]
);

$response->setMetadata($preparedData['result_metadata']);
$rows = $response->fetchAll();

Using Batch

$batchRequest = new Cassandra\Request\Batch();

// Append a prepared query
$preparedData = $connection->prepare('UPDATE "students" SET "age" = :age WHERE "id" = :id');
$values = [
	'age' => 21,
	'id' => 'c5419d81-499e-4c9c-ac0c-fa6ba3ebc2bc',
];
$batchRequest->appendQueryId($preparedData['id'], Cassandra\Request\Request::strictTypeValues($values, $preparedData['metadata']['columns']));

// Append a query string
$batchRequest->appendQuery(
	'INSERT INTO "students" ("id", "name", "age") VALUES (:id, :name, :age)',
	[
		'id' => new Cassandra\Type\Uuid('c5420d81-499e-4c9c-ac0c-fa6ba3ebc2bc'),
		'name' => new Cassandra\Type\Varchar('Mark'),
		'age' => 20,
	]
);

$response = $connection->syncRequest($batchRequest);
$rows = $response->fetchAll();

Supported datatypes

All types are supported.

//  Ascii
    new Cassandra\Type\Ascii('string');

//  Bigint
    new Cassandra\Type\Bigint(10000000000);

//  Blob
    new Cassandra\Type\Blob('string');

//  Boolean
    new Cassandra\Type\Boolean(true);

//  Counter
    new Cassandra\Type\Counter(1000);

//  Decimal
    new Cassandra\Type\Decimal(123, 4); 	// 0.0123

//  Double
    new Cassandra\Type\Double(2.718281828459);

//  Float
    new Cassandra\Type\Float(2.718);

//  Inet
    new Cassandra\Type\Inet('127.0.0.1');

//  Int
    new Cassandra\Type\Int(1);

//  CollectionList
    new Cassandra\Type\CollectionList([1, 1, 1], Cassandra\Type\Base::INT);

//  CollectionMap
    new Cassandra\Type\CollectionMap(['a' => 1, 'b' => 2], Cassandra\Type\Base::ASCII, Cassandra\Type\Base::INT);

//  CollectionSet
    new Cassandra\Type\CollectionSet([1, 2, 3], Cassandra\Type\Base::INT);

//  Timestamp (unit: microseconds)
    new Cassandra\Type\Timestamp(1409830696263000);

//  Uuid
    new Cassandra\Type\Uuid('62c36092-82a1-3a00-93d1-46196ee77204');

//  Timeuuid
    new Cassandra\Type\Timeuuid('2dc65ebe-300b-11e4-a23b-ab416c39d509');

//  Varchar
    new Cassandra\Type\Varchar('string');

//  Varint
    new Cassandra\Type\Varint(10000000000);

//  Custom
    new Cassandra\Type\Custom('string');

//  Tuple
    new Cassandra\Type\Tuple([1, '2'], [Cassandra\Type\Base::INT, Cassandra\Type\Base::TEXT]);

//  UDT
    new Cassandra\Type\UDT(['intField' => 1, 'textField' => '2'], ['intField' => Cassandra\Type\Base::INT, 'textField' => Cassandra\Type\Base::TEXT]); 	// in the order defined by the type

Recommend Libraries

Inspired by