• Stars
    star
    152
  • Rank 244,685 (Top 5 %)
  • Language
    PHP
  • License
    MIT License
  • Created about 10 years ago
  • Updated 6 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

php Cassandra driver which support Protocol v3 (Cassandra 2.1) and asynchronous requests

Cassandra client library for PHP

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

Features

  • Using Protocol v3 (Cassandra 2.1)
  • Support ssl/tls with stream transport layer
  • 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)
  • Two transport layers - socket and stream.
  • Using exceptions to report errors
  • 800% performance improvement(async mode) than other php cassandra client libraries

Installation

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

If you want to use Bigint or Timestamp type, 64-bit system is required.

Append dependency into composer.json

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

Also you can just fetch project from Github and include in your code:

require 'php-cassandra-folder-on-your-computer/php-cassandra.php';

Basic Using

<?php

$nodes = [
	'127.0.0.1',		// simple way, hostname only
	'192.168.0.2:9042',	// simple way, hostname with port 
	[				// advanced way, array including username, password and socket options
		'host'		=> '10.205.48.70',
		'port'		=> 9042, //default 9042
		'username'	=> 'admin',
		'password'	=> 'pass',
		'socket'	=> [SO_RCVTIMEO => ["sec" => 10, "usec" => 0], //socket transport only
		],
	],
	[				// advanced way, using Connection\Stream, persistent connection
		'host'		=> '10.205.48.70',
		'port'		=> 9042,
		'username'	=> 'admin',
		'password'	=> 'pass',
		'class'		=> 'Cassandra\Connection\Stream',//use stream instead of socket, default socket. Stream may not work in some environment
		'connectTimeout' => 10, // connection timeout, default 5,  stream transport only
		'timeout'	=> 30, // write/recv timeout, default 30, stream transport only
		'persistent'	=> true, // use persistent PHP connection, default false,  stream transport only  
	],
	[				// advanced way, using SSL
		'class'		=> 'Cassandra\Connection\Stream', // "class" must be defined as "Cassandra\Connection\Stream" for ssl or tls
		'host'		=> 'ssl://10.205.48.70',// or 'tls://10.205.48.70'
		'port'		=> 9042,
		'username'	=> 'admin',
		'password'	=> 'pass',
	],
];

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

//Connect
try
{
	$connection->connect();
}
catch (Cassandra\Exception $e)
{
	echo 'Caught exception: ',  $e->getMessage(), "\n";
	exit;//if connect failed it may be good idea not to continue
}


// Set consistency level for farther requests (default is CONSISTENCY_ONE)
$connection->setConsistency(Request::CONSISTENCY_QUORUM);

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

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
try
{
	$statement1 = $connection->queryAsync($cql1);
	$statement2 = $connection->queryAsync($cql2);

	// Wait until received the response, can be reversed order
	$response2 = $statement2->getResponse();
	$response1 = $statement1->getResponse();


	$rows1 = $response1->fetchAll();
	$rows2 = $response2->fetchAll();
}
catch (Cassandra\Exception $e)
{
}

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('0.0123');

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

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

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

//  Int
    new Cassandra\Type\PhpInt(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: millisecond)
    new Cassandra\Type\Timestamp((int) (microtime(true) * 1000));
    new Cassandra\Type\Timestamp(1409830696263);

//  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', 'var_name');

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

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

Using nested datatypes

// CollectionSet<UDT>, where UDT contains: Int, Text, Boolean, CollectionList<Text>, CollectionList<UDT>
new Cassandra\Type\CollectionSet([
	[
		'id' => 1,
		'name' => 'string',
		'active' => true,
		'friends' => ['string1', 'string2', 'string3'],
		'drinks' => [['qty' => 5, 'brand' => 'Pepsi'], ['qty' => 3, 'brand' => 'Coke']]
	],[
		'id' => 2,
		'name' => 'string',
		'active' => false,
		'friends' => ['string4', 'string5', 'string6'],
		'drinks' => []
	]
], [
	[
	'type' => Cassandra\Type\Base::UDT,
	'definition' => [
		'id' => Cassandra\Type\Base::INT,
		'name' => Cassandra\Type\Base::VARCHAR,
		'active' => Cassandra\Type\Base::BOOLEAN,
		'friends' => [
			'type' => Cassandra\Type\Base::COLLECTION_LIST,
			'value' => Cassandra\Type\Base::VARCHAR
		],
		'drinks' => [
			'type' => Cassandra\Type\Base::COLLECTION_LIST,
			'value' => [
				'type' => Cassandra\Type\Base::UDT,
				'typeMap' => [
					'qty' => Cassandra\Type\Base::INT,
					'brand' => Cassandra\Type\Base::VARCHAR
				]
			]
		]
	]
]
]);

Recommend Libraries

Inspired by

More Repositories

1

airpub

a pure front-end blog engine powered by duoshuo API
JavaScript
362
star
2

node-duoshuo

a duoshuo SDK in Node.js
JavaScript
98
star
3

duoshuo-python-sdk

A Python library for using the duoshuo API
Python
87
star
4

duoshuo-embed.css

多说评论框 CSS 样式源代码
CSS
78
star
5

duoshuo-wordpress

多说评论插件 for WordPress
PHP
73
star
6

easy-http

EasyHttp是一个帮助你忽略不同的php环境情况,无差别地发送http请求的php类。 你不再需要关注当前php环境是否支持curl/fsockopen/fopen,EasyHttp会自动选择一个最合适的方式去发出http请求。 EasyHttp源于WordPress中的WP_Http类,去除了所有对WordPress其他函数的依赖,将其拆分到不同的文件中,并做了少量简化。
PHP
61
star
7

duoshuo-php-sdk

多说的php SDK
PHP
31
star
8

duoshuo-mediawiki

多说 extension for mediawiki
PHP
19
star
9

angular-duoshuo

duoshuo SDK for angular.js
JavaScript
13
star
10

uuid

A PHP 5.4+ library for generating RFC 4122 version 1, 3, 4, and 5 universally unique identifiers (UUID).
PHP
10
star
11

duoshuo-docs

API document of duoshuo.com
CSS
9
star
12

react-native-duoshuo

a duoshuo directive for React Native
JavaScript
8
star
13

duoshuo-typecho

TYPECHO多说实时同步插件
PHP
8
star
14

duoshuo-plugins-dedecms

多说评论插件 for DedeCMS(织梦)
PHP
7
star
15

documentation

duoshuo API and Widgets documentation repo
6
star
16

php-boilerplate

使用php5.4+(依赖mysqlnd、curl)和多说实现的一个具有基本用户登录绑定功能的网站模板,开发者可以快速复制这个模板开发自己的php网站。
5
star
17

duoshuo-joomla

PHP
4
star
18

express-boilerplate

duoshuo-node-boilerplate使用express.js和duoshuo实现的一个具有基本用户登录绑定功能的网站模板,开发者可以快速复制这个模板开发自己的网站。
4
star
19

duoshuo-phpcms

PHP
3
star
20

duoshuo-emlog

多说评论插件 for emlog
PHP
2
star
21

ionic-duoshuo

a ionic duoshuo directive based on angular.js
JavaScript
2
star
22

theme-IconMoon

a duoshuo-embed comments theme IconMoon by JJYing.
CSS
2
star
23

express-static-server

a simple static server based on express
JavaScript
1
star