ydb-platform / ydb-php-sdk
YDB PHP SDK
Requires
- php: >=7.2
- ext-bcmath: *
- ext-curl: *
- ext-grpc: *
- ext-json: *
- google/protobuf: ~3.15.8
- grpc/grpc: ^1.35
- phpseclib/phpseclib: ^2.0|^3.0
- psr/log: ^1|^2|^3
Requires (Dev)
- overtrue/phplint: ^3.0
- phpunit/phpunit: >= 6.0, <10.0
- dev-main
- v1.15.0
- v1.14.0
- v1.13.2
- v1.13.1
- v1.13.0
- v1.12.0
- v1.11.0
- v1.10.0
- v1.9.0
- v1.8.2
- v1.8.1
- v1.8.0
- v1.7.0
- v1.6.0
- v1.5.6
- v1.5.5
- v1.5.4
- v1.5.3
- v1.5.2
- v1.5.1
- v1.5.0
- v1.4.5
- v1.4.4
- v1.4.3
- v1.4.2
- v1.4.1
- v1.4.0
- v1.3.1
- v1.3.0
- v1.2.0
- v1.1.1
- v1.1.0
- v1.0.14
- v1.0.13
- v1.0.12
- v1.0.11
- v1.0.10
- v1.0.9
- v1.0.8
- v1.0.7
- v1.0.6
- v1.0.5
- v1.0.4
- v1.0.3
- v1.0.2
- v1.0.1
- v1.0
- dev-fixl
- dev-tx-control
- dev-slo-with-query
- dev-slo-with-backet
- dev-test-slo-params
This package is auto-updated.
Last update: 2024-09-18 09:28:12 UTC
README
YDB PHP SDK 提供了从 PHP 代码访问 YDB 的功能。
YDB 是一个开源的分布式容错数据库管理系统,具有高可用性、可扩展性、严格一致性和 ACID。查询使用 SQL 方言 – YQL。
YDB 可在以下几种模式下使用
- 本地安装(本 SDK 尚不支持);
- YC 中的无服务器计算模式(仅执行操作时收费);
- YC 中的专用实例模式(付费使用专用计算资源)。
文档
安装
推荐的安装方法是使用 Composer。
运行以下命令
composer require ydb-platform/ydb-php-sdk
连接
首先,使用 Yandex Cloud 控制台 创建数据库。
YDB 支持以下认证方式
- 访问令牌
- OAuth 令牌
- JWT + 私钥
- JWT + JSON 文件
- 元数据 URL
- 匿名
访问令牌
使用您的访问令牌
<?php use YdbPlatform\Ydb\Ydb; $config = [ // Database path 'database' => '/ru-central1/b1glxxxxxxxxxxxxxxxx/etn0xxxxxxxxxxxxxxxx', // Database endpoint 'endpoint' => 'ydb.serverless.yandexcloud.net:2135', // Auto discovery (dedicated server only) 'discovery' => false, // IAM config 'iam_config' => [ 'root_cert_file' => './CA.pem', // Root CA file (dedicated server only!) // Access token authentication 'access_token' => 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA', ], ]; $ydb = new Ydb($config);
或
<?php use YdbPlatform\Ydb\Ydb; use YdbPlatform\Ydb\Auth\Implement\AccessTokenAuthentication; $config = [ // Database path 'database' => '/ru-central1/b1glxxxxxxxxxxxxxxxx/etn0xxxxxxxxxxxxxxxx', // Database endpoint 'endpoint' => 'ydb.serverless.yandexcloud.net:2135', // Auto discovery (dedicated server only) 'discovery' => false, // IAM config 'iam_config' => [ 'root_cert_file' => './CA.pem', // Root CA file (dedicated server only!) ], 'credentials' => new AccessTokenAuthentication('AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA') ]; $ydb = new Ydb($config);
OAuth 令牌
您需要获取 新的 OAuth 令牌。
使用您的 OAuth 令牌
<?php use YdbPlatform\Ydb\Ydb; $config = [ // Database path 'database' => '/ru-central1/b1glxxxxxxxxxxxxxxxx/etn0xxxxxxxxxxxxxxxx', // Database endpoint 'endpoint' => 'ydb.serverless.yandexcloud.net:2135', // Auto discovery (dedicated server only) 'discovery' => false, // IAM config 'iam_config' => [ 'temp_dir' => './tmp', // Temp directory 'root_cert_file' => './CA.pem', // Root CA file (dedicated server only!) // OAuth token authentication 'oauth_token' => 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA', ], ]; $ydb = new Ydb($config);
或
<?php use YdbPlatform\Ydb\Ydb; use YdbPlatform\Ydb\Auth\Implement\OAuthTokenAuthentication; $config = [ // Database path 'database' => '/ru-central1/b1glxxxxxxxxxxxxxxxx/etn0xxxxxxxxxxxxxxxx', // Database endpoint 'endpoint' => 'ydb.serverless.yandexcloud.net:2135', // Auto discovery (dedicated server only) 'discovery' => false, // IAM config 'iam_config' => [ 'temp_dir' => './tmp', // Temp directory 'root_cert_file' => './CA.pem', // Root CA file (dedicated server only!) ], 'credentials' => new OAuthTokenAuthentication('AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA') ]; $ydb = new Ydb($config);
JWT + 私钥
创建具有 editor
角色的 服务帐户,然后创建私钥。您还需要一个密钥 ID 和服务帐户 ID。
连接到您的数据库
<?php use YdbPlatform\Ydb\Ydb; $config = [ 'database' => '/ru-central1/b1glxxxxxxxxxxxxxxxx/etn0xxxxxxxxxxxxxxxx', 'endpoint' => 'ydb.serverless.yandexcloud.net:2135', 'discovery' => false, 'iam_config' => [ 'temp_dir' => './tmp', // Temp directory 'root_cert_file' => './CA.pem', // Root CA file (dedicated server only!) // Private key authentication 'key_id' => 'ajexxxxxxxxx', 'service_account_id' => 'ajeyyyyyyyyy', 'private_key_file' => './private.key', ], ]; $ydb = new Ydb($config);
或
<?php use YdbPlatform\Ydb\Ydb; use YdbPlatform\Ydb\Auth\Implement\JwtWithPrivateKeyAuthentication; $config = [ 'database' => '/ru-central1/b1glxxxxxxxxxxxxxxxx/etn0xxxxxxxxxxxxxxxx', 'endpoint' => 'ydb.serverless.yandexcloud.net:2135', 'discovery' => false, 'iam_config' => [ 'temp_dir' => './tmp', // Temp directory 'root_cert_file' => './CA.pem', // Root CA file (dedicated server only!) // Private key authentication 'key_id' => 'ajexxxxxxxxx', 'service_account_id' => 'ajeyyyyyyyyy', 'private_key_file' => './private.key', ], 'credentials' => new JwtWithPrivateKeyAuthentication( "ajexxxxxxxxx","ajeyyyyyyyyy",'./private.key') ]; $ydb = new Ydb($config);
JWT + JSON 文件
创建具有 editor
角色的 服务帐户。
创建一个服务帐户 JSON 文件,将其保存到您的项目中,命名为 sa_name.json
。
连接到您的数据库
<?php use YdbPlatform\Ydb\Ydb; $config = [ 'database' => '/ru-central1/b1glxxxxxxxxxxxxxxxx/etn0xxxxxxxxxxxxxxxx', 'endpoint' => 'ydb.serverless.yandexcloud.net:2135', 'discovery' => false, 'iam_config' => [ 'temp_dir' => './tmp', // Temp directory 'root_cert_file' => './CA.pem', // Root CA file (dedicated server only!) // Service account JSON file authentication 'service_file' => './sa_name.json', ], ]; $ydb = new Ydb($config);
或
<?php use YdbPlatform\Ydb\Ydb; use YdbPlatform\Ydb\Auth\Implement\JwtWithJsonAuthentication; $config = [ 'database' => '/ru-central1/b1glxxxxxxxxxxxxxxxx/etn0xxxxxxxxxxxxxxxx', 'endpoint' => 'ydb.serverless.yandexcloud.net:2135', 'discovery' => false, 'iam_config' => [ 'temp_dir' => './tmp', // Temp directory 'root_cert_file' => './CA.pem', // Root CA file (dedicated server only!) ], 'credentials' => new JwtWithJsonAuthentication('./jwtjson.json') ]; $ydb = new Ydb($config);
元数据 URL
当您将项目部署到 Yandex.Cloud 的 VM 或函数时,您可以使用 元数据 URL 连接到数据库。在开始之前,您应该将服务帐户链接到现有的或新的 VM 或函数。
<?php use YdbPlatform\Ydb\Ydb; $config = [ // Database path 'database' => '/ru-central1/b1glxxxxxxxxxxxxxxxx/etn0xxxxxxxxxxxxxxxx', // Database endpoint 'endpoint' => 'ydb.serverless.yandexcloud.net:2135', // Auto discovery (dedicated server only) 'discovery' => false, // IAM config 'iam_config' => [ 'temp_dir' => './tmp', // Temp directory 'use_metadata' => true, ], ]; $ydb = new Ydb($config);
或
<?php use YdbPlatform\Ydb\Ydb; use YdbPlatform\Ydb\Auth\Implement\MetadataAuthentication; $config = [ // Database path 'database' => '/ru-central1/b1glxxxxxxxxxxxxxxxx/etn0xxxxxxxxxxxxxxxx', // Database endpoint 'endpoint' => 'ydb.serverless.yandexcloud.net:2135', // Auto discovery (dedicated server only) 'discovery' => false, // IAM config 'iam_config' => [ 'temp_dir' => './tmp', // Temp directory ], 'credentials' => new MetadataAuthentication() ]; $ydb = new Ydb($config);
匿名
<?php use YdbPlatform\Ydb\Ydb; $config = [ // Database path 'database' => '/local', // Database endpoint 'endpoint' => 'localhost:2136', // Auto discovery (dedicated server only) 'discovery' => false, // IAM config 'iam_config' => [ 'anonymous' => true, 'insecure' => true, ], ]; $ydb = new Ydb($config);
或
<?php use YdbPlatform\Ydb\Ydb; use YdbPlatform\Ydb\Auth\Implement\AnonymousAuthentication; $config = [ // Database path 'database' => '/local', // Database endpoint 'endpoint' => 'localhost:2136', // Auto discovery (dedicated server only) 'discovery' => false, // IAM config 'iam_config' => [ 'insecure' => true, ], 'credentials' => new AnonymousAuthentication() ]; $ydb = new Ydb($config);
由环境变量决定
<?php use YdbPlatform\Ydb\Ydb; use YdbPlatform\Ydb\Auth\Implement\EnvironCredentials; $config = [ // Database path 'database' => '/local', // Database endpoint 'endpoint' => 'localhost:2136', // Auto discovery (dedicated server only) 'discovery' => false, // IAM config 'iam_config' => [ 'insecure' => true, ], 'credentials' => new EnvironCredentials() ]; $ydb = new Ydb($config);
以下算法适用于 YDB-PHP-SDK
- 如果设置了
YDB_SERVICE_ACCOUNT_KEY_FILE_CREDENTIALS
环境变量的值,则使用 系统帐户密钥 认证模式,并从该变量指定的文件中获取密钥。 - 否则,如果将
YDB_ANONYMOUS_CREDENTIALS
环境变量的值设置为 1,则使用匿名认证模式。 - 否则,如果将
YDB_METADATA_CREDENTIALS
环境变量的值设置为 1,则使用 元数据 认证模式。 - 否则,如果设置了
YDB_ACCESS_TOKEN_CREDENTIALS
环境变量的值,则使用 访问令牌 认证模式,其中此变量的值被传递。 - 否则,使用 元数据 认证模式。
静态凭证
<?php use YdbPlatform\Ydb\Ydb; use YdbPlatform\Ydb\Auth\Implement\StaticAuthentication; $config = [ // Database path 'database' => '/local', // Database endpoint 'endpoint' => 'localhost:2136', // Auto discovery (dedicated server only) 'discovery' => false, // IAM config 'iam_config' => [ 'insecure' => true, ], 'credentials' => new StaticAuthentication($user, $password) ]; $ydb = new Ydb($config);
从文本文件读取
<?php use YdbPlatform\Ydb\Ydb; use YdbPlatform\Ydb\Auth\ReadTokenFromFile; $config = [ // Database path 'database' => '/local', // Database endpoint 'endpoint' => 'localhost:2136', // Auto discovery (dedicated server only) 'discovery' => false, // IAM config 'iam_config' => [ 'insecure' => true, ], 'credentials' => new ReadTokenFromFile($tokenPath, $refreshTime) ]; $ydb = new Ydb($config);
用法
您应从表服务初始化一个会话以开始带有重试的查询。
<?php use YdbPlatform\Ydb\Ydb; $config = [ // ... ]; $ydb = new Ydb($config); // obtaining the Table service $table = $ydb->table(); $result = $table->retryTransaction(function(Session $session){ // making a query return $session->query('select * from `users` limit 10;'); }, true); $users_count = $result->rowCount(); $users = $result->rows(); $columns = $result->columns();
一旦您的脚本完成,会话将被销毁。
定制查询
通常,通过query()
方法进行常规查询就足够了,但在异常情况下,您可能需要调整查询设置。您可以使用查询构建器来完成。
<?php $result = $table->retryTransaction(function(Session $session){ // creating a new query builder instance $query = $session->newQuery('select * from `users` limit 10;'); // a setting to keep in cache $query->keepInCache(); // a setting to begin a transaction with the given mode $query->beginTx('stale'); return $query->execute(); }, true);
查询构建器的方法
keepInCache(bool $value)
- 保持缓存(默认:true
)collectStats(int $value)
- 收集统计信息(默认:1)parameters(array $parameters)
- 参数operationParams(\Ydb\Operations\OperationParams $operation_params)
- 操作参数beginTx(string $mode)
- 以给定的模式开始事务- stale
- online
- online_inconsistent
- serializable
- snapshot
txControl(\Ydb\Table\TransactionControl $tx_control)
- 使用自定义设置的交易控制
您可以通过链式调用这些方法来提高方便性。
日志记录
为了日志记录目的,您需要使用实现\Psr\Log\LoggerInterface
的类。YDB-PHP-SDK在YdbPlatform\Ydb\Logger
命名空间中具有内置的日志记录器
NullLogger
- 默认SimpleStdLogger($level)
- 日志记录器,将日志推送到STDERR。
使用示例
$config = [ 'logger' => new \YdbPlatform\Ydb\Logger\SimpleStdLogger(\YdbPlatform\Ydb\Logger\SimpleStdLogger::INFO) ] $ydb = new \YdbPlatform\Ydb\Ydb($config);
gRPC
gRPC客户端的选项
您可以通过在配置数组中设置选项来自定义gRPC客户端的行为
使用示例
$config = [ // ... 'grpc' => [ 'opts' => [ 'grpc.max_receive_message_length' => 8*1024*1024, 'grpc.default_compression_algorithm' => 2, 'grpc.default_compression_level' => 2, ], ], ]; $ydb = new \YdbPlatform\Ydb\Ydb($config);