uri2x / php-cql
基于CQL二进制协议的Apache Cassandra和ScyllaDB的PHP连接器,支持持久连接
README
最后更新:2023/07/08
基于CQL二进制协议(v3)的Native Apache Cassandra和ScyllaDB连接器,无需外部扩展。
需要PHP版本 >5,Cassandra >1.2,以及任何ScyllaDB版本。
安装
或者
- 通过Composer
$ composer require uri2x/php-cql
或者
- 将
Cassandra.php复制到您的项目并包含它。
重要
确保您已通过编辑cassandra.yaml文件并添加以下行来启用Cassandra的本地传输
start_native_transport: true
使用方法
可用方法
connect($host, $user = '', $passwd = '', $dbname = '', $port = 9042)
Connects to a Cassandra node.
@param string $host Host name/IP to connect to use 'p:' as prefix for persistent connections.
@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.
@param int $retries: Number of connection retries (default: 3, useful for persistent connections in case of timeouts).
@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, $values = [])
Queries the database using the given CQL.
@param string $cql The query to run.
@param int $consistency Consistency level for the operation.
@param array $values Values to bind in a sequential or key=>value format,
where key is the column's name.
@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.
bind_param($value, $column_type) 返回一个用于查询方法的绑定参数(静态方法)
@param mixed $value Value to bind The query to run.
@param int $type Value type out of one of the Cassandra::COLUMNTYPE_* constants
@return array value to be used as part of the $values parameter of the query method
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.
过程式
此外,还为您提供了过程式编程的包装器。要使用包装器,请确保包含包含以下方法的Cassandra_Procedural.php
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, $values = [])
Same as $Cassandra->query() above. Use $obj from cassandra_connect as the
first parameter.
cassandra_bind_param($value, $column_type)
Same as $Cassandra->bind_param() above
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.
示例用法
<?php require_once('vendor/autoload.php'); use CassandraNative\Cassandra; $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 WHERE id=?', Cassandra::CONSISTENCY_ONE, [Cassandra::bind_param(1001, Cassandra::COLUMNTYPE_BIGINT]); // $arr, for example, may 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 = ['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(); }
或者,与上面相同的过程式风格
// 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 = ['col2' => 5, 'col3' => '0x55', 'col1' => 'five']; $pResult = cassandra_execute($handle, $stmt, $values); // Closes the connection: cassandra_close($handle); }
外部链接
-
Datastax的博客介绍二进制协议:http://www.datastax.com/dev/blog/binary-protocol
许可
The MIT License (MIT)
Copyright (c) 2023 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.