duoshuo / php-cassandra
PHP 的 Cassandra 客户端库,支持协议 v3 和异步请求
v0.5.2
2014-09-29 08:11 UTC
Requires
- php: >=5.4.0
This package is auto-updated.
Last update: 2024-09-23 19:37:33 UTC
README
PHP 的 Cassandra 客户端库,支持协议 v3 (Cassandra 2.1) 和异步请求
功能
- 使用协议 v3 (Cassandra 2.1)
- 支持 ssl/tls 通过流传输层
- 支持异步和同步请求
- 支持日志记录、非日志和计数批次
- 可以指定一致性,"序列一致性" 和协议中定义的所有标志
- 支持查询准备和执行
- 支持所有数据类型转换和绑定,包括集合类型、元组和 UDT
- 支持条件更新/插入
- 5 种检索方法(fetchAll, fetchRow, fetchPairs, fetchCol, fetchOne)
- 两个传输层 - 套接字和流。
- 使用异常来报告错误
- 比其他 PHP Cassandra 客户端库有 800% 的性能提升(异步模式)
安装
需要 PHP 5.4+,不需要额外的库。
如果想要使用 Bigint 或 Timestamp 类型,需要 64 位系统。
将依赖项添加到 composer.json 中
...
"require": {
...
"duoshuo/php-cassandra": "dev-master"
}
...
也可以直接从 Github 获取项目并将其包含到您的代码中
require 'php-cassandra-folder-on-your-computer/php-cassandra.php';
基本使用
<?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) { }
检索数据
// 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
异步查询
// 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) { }
使用准备和数据绑定
$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();
使用批处理
$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();
支持的数据类型
支持所有类型。
// 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
使用嵌套数据类型
// 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 ] ] ] ] ] ]);
推荐库
- shen2/fluent-cql:在流畅接口中编写 CQL
- duoshuo/uuid:生成 UUID 和 TimeUUID
- shen2/crest:Cassandra 的 Restful 网络API
- shen2/cadmin:Cassandra 的 Web 管理面板,类似于 phpmyadmin