jippi / 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-06-14 06:42:32 UTC
README
PHP的Cassandra客户端库,支持协议v3(Cassandra 2.1)和异步请求
特性
- 使用协议v3(Cassandra 2.1)
- 支持异步和同步请求
- 支持日志记录、未记录和计数批次
- 可以指定一致性、"串行一致性"和协议中定义的所有标志
- 支持查询准备和执行
- 支持所有数据类型转换和绑定,包括集合类型、元组和UDT
- 支持条件更新/插入
- 5种获取方法(fetchAll、fetchRow、fetchPairs、fetchCol、fetchOne)
- 比其他PHP Cassandra客户端库性能提升800%(异步模式)
安装
需要PHP 5.4+,无需额外的库。
将依赖项添加到composer.json中
...
"require": {
...
"duoshuo/php-cassandra": "dev-master"
}
...
基本用法
<?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')]);
获取数据
// 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 $statement = $connection->queryAsync($cql); // Wait until received the response $response = $statement->getResponse(); $rows = $response->fetchAll();
使用准备和数据绑定
$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(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
推荐库
- shen2/fluent-cql:使用流畅接口编写CQL
- duoshuo/uuid:生成UUID和时间UUID