dimajolkin/ydb-php-sdk

Yandex Database SDK (PHP)

v1.3.2 2022-11-06 17:07 UTC

This package is auto-updated.

Last update: 2024-09-23 12:33:51 UTC


README

由于在云服务中似乎忽略了该库的支持,因此临时将该分支迁移到此处以使用安装所需的变化

  composer require dimajolkin/ydb-php-sdk "^1.3"
  1. https://github.com/bashkarev/ydb-php-sdk/tree/anonymous - 允许匿名授权
  2. ydb-platform#22

YDB PHP SDK 提供了从 PHP 代码访问 Yandex Database 云服务的能力。

Yandex Database 是一个分布式容错数据库管理系统,具有高可用性和可伸缩性,严格的一致性和 ACID。查询使用 SQL 语言的方言 - YQL。

Yandex Database 可在两种模式下使用

  • 无服务器计算模式(仅执行的操作收费);
  • 专用实例模式(专用计算资源收费)。

文档

https://cloud.yandex.ru/docs/ydb/

安装

推荐的安装方法是 Composer。

运行以下命令

composer require yandex-cloud/ydb-php-sdk

连接

首先,使用 Yandex Cloud 控制台 创建数据库。

Yandex Database 支持以下身份验证方法

  • OAuth 令牌
  • JWT + 私钥
  • JWT + JSON 文件
  • 元数据 URL
  • 匿名

OAuth 令牌

您应该获取一个新的 OAuth 令牌

使用您的 OAuth 令牌

<?php

use YandexCloud\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);

JWT + 私钥

创建具有 editor 角色的 服务帐户,然后创建私钥。您还需要密钥 ID 和服务帐户 ID。

连接到您的数据库

<?php

use YandexCloud\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);

JWT + JSON 文件

创建具有 editor 角色的 服务帐户

创建服务帐户 JSON 文件,将其保存到您的项目中作为 sa_name.json

连接到您的数据库

<?php

use YandexCloud\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);

元数据 URL

当您将项目部署到 Yandex.Cloud 的 VM 或函数时,您可以使用 元数据 URL 连接到数据库。在开始之前,您应将服务帐户链接到现有或新的 VM 或函数。

<?php

use YandexCloud\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 YandexCloud\Ydb\Ydb;
$config = [
    // Database path
    'database'    => '/local',
    // Database endpoint
    'endpoint'    => 'localhost:2135',
    // Auto discovery (dedicated server only)
    'discovery'   => false,
    // IAM config
    'iam_config'  => [
        'anonymous' => true,
        // Allow insecure grpc connection, default false
        'insecure' => false,
    ],
];
$ydb = new Ydb($config);

用法

您应该从 Table 服务初始化一个会话以开始查询。

<?php

use YandexCloud\Ydb\Ydb;

$config = [
    // ...
];

$ydb = new Ydb($config);

// obtaining the Table service
$table = $ydb->table();

// obtaining a session
$session = $table->session();

// making a query
$result = $session->query('select * from `users` limit 10;');

$users_count = $result->rowCount();
$users = $result->rows();

$columns = $result->columns();

您还可以直接在 Table 服务上调用 query() 方法。在这种情况下,后台将创建一个会话,并将您的查询代理到该会话。

<?php

$table = $ydb->table();

// making a query
$result = $table->query('select * from `users` limit 10;');

一旦您的脚本完成,会话将被销毁。

自定义查询

通常,通过 query() 方法进行常规查询就足够了,但在异常情况下,您可能需要调整查询设置。您可以使用查询构建器来完成此操作

<?php

$session = $table->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');

$result = $query->execute();

查询构建器的方法

  • 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
  • txControl(\Ydb\Table\TransactionControl $tx_control) - 使用自定义设置的事务控制

您可以通过链式调用这些方法来提高方便性。