intermaterium/cassandra-native

Apache Cassandra 和 ScyllaDB 的 PHP 原生连接器,基于 CQL 二进制协议,支持持久连接

2.1.3 2024-08-20 21:08 UTC

This package is auto-updated.

Last update: 2024-09-27 14:49:00 UTC


README

License: MIT

PHP 应用程序的 Apache Cassandra 和 ScyllaDB 原生连接器,使用 CQL 二进制协议(v4),无需外部扩展。

需要 PHP 版本 >=8,Cassandra >1.2,以及任何 ScyllaDB 版本。

大部分 API 是为了模仿 Datastax PHP Driver。由 Uri Hartmann 创作的原作。

安装

$ composer require intermaterium/cassandra-native

功能

  • 简单和预编译语句
  • SSL 加密
  • 持久连接
  • 通过 LZ4 和 Snappy 进行压缩。

缺少的功能

  • 批语句
  • 异步查询
  • 结果分页
  • 元组和用户定义类型

使用方法

集群

可以通过 ClusterBuilder 类构建 Cassandra 集群。默认情况下,集群将尝试连接到 localhost。

$clusterBuilder = new \CassandraNative\Cluster\ClusterBuilder();
$cassandra = $clusterBuilder->build();

您可以使用 withContactPoints 方法指定要连接的 IP 地址/主机名集。与 Datastax Driver 不同,当连接时,客户端将随机选择一个接触主机并尝试连接到它。

$clusterBuilder = new \CassandraNative\Cluster\ClusterBuilder();
$clusterBuilder->withContactPoints(['1.0.0.0', '2.0.0.0']);
$cassandra = $clusterBuilder->build();

在连接时,创建的 Cassandra 实例不会连接到特定的键空间。在创建的 Cassandra 实例上调用 connect 并指定键空间名称将执行 USE $keyspace 查询。

$cassandra->connect('system');

SSL

您可以通过 SSLBuilder 类启用 SSL 加密,并将对 build 方法的调用结果传递给集群构建实例的 withSSL 方法。

$sslBuilder = new \CassandraNative\SSL\SSLBuilder();
$sslBuilder
    ->withClientCert(__DIR__ . '/certs/localhost.cer')
    ->withPrivateKey(__DIR__ . '/certs/localhost.key.pem');
    ->withTrustedCerts(__DIR__ . '/certs/localhost.cer.pem');

$clusterBuilder->withSSL($sslBuilder->build());

压缩

可以通过在集群构建器上调用 withCompression 方法来启用压缩。

$clusterBuilder->withCompression(true);

客户端将检查是否已安装 snappy 或 LZ4 扩展,并选择可用的一个。如果两者都可用,它将选择 LZ4 而不是 Snappy。如果两者都不可用,构建器将在您尝试构建集群时抛出异常。

语句

客户端当前仅支持两种类型的语句,SimplePrepared。这两种类型的语句都是通过 Cassandra 实例上的 execute 方法执行的。执行方法接受语句、绑定到参数的可选数组值以及可选的一致性级别,该级别覆盖默认一致性。

execute 方法返回一个实现 ArrayAccessIterator 接口的 Rows 类。

简单语句

简单语句使用 SimpleStatement 类。

$stmt = new \CassandraNative\Statement\SimpleStatement('DESCRIBE TABLES');
$rows = $cassandra->execute($stmt);

简单语句支持参数化值。

$stmt = new \CassandraNative\Statement\SimpleStatement('SELECT col1, col2, col3 FROM my_table WHERE id=?')
$rows = $cassandra->execute(
    $stmt,
    [
        [1001, Cassandra::COLUMNTYPE_BIGINT]
    ]
);

// Or

$stmt = new \CassandraNative\Statement\SimpleStatement('SELECT col1, col2, col3 FROM my_table WHERE id=:id')
$rows = $cassandra->execute(
    $stmt,
    [
        'id' => [1001, Cassandra::COLUMNTYPE_BIGINT]
    ]
);

在使用简单语句时,您必须指定绑定参数的类型

预编译语句

预编译语句是通过 Cassandra 实例上的 prepare 方法创建的。

$stmt = $cassandra->prepare('UPDATE my_table SET col2=?,col3=? WHERE col1=?');
$values = ['col2' => 5, 'col3' => '0x55', 'col1' => 'five'];
$rows = $cassandra->execute($stmt, $values);

与简单语句不同,您不需要指定绑定值的类型。

外部链接

  1. Datastax 的博客介绍二进制协议:http://www.datastax.com/dev/blog/binary-protocol

  2. CQL 定义 https://cassandra.apache.ac.cn/_/native_protocol.html

许可证

The MIT License (MIT)

Copyright (c) 2023 Uri Hartmann
Copyright (c) 2024 Christopher Birmingham

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.