YDB change data capture and delivery to YDS
A Data Streams stream can get data from Managed Service for YDB databases in real time using the Change Data Capture (CDC) technology.
Note
In YDB, CDC mode is supported starting from version 22.5.
To set up CDC using Data Transfer:
- Prepare the Managed Service for YDB source database.
- Create a target stream in Data Streams.
- Set up and activate the transfer.
- Test your transfer.
If you no longer need the resources you created, delete them.
Required paid resources
-
Managed Service for YDB databases (see Managed Service for YDB pricing). The cost depends on deployment mode:
- In serverless mode, you pay for data operations and storage volume, including stored backups.
- In dedicated instance mode, you pay for the use of computing resources allocated to the database, storage size, and backups.
-
Data Streams (see Data Streams pricing). The cost depends on the pricing model:
- Based on allocated resources: You pay a fixed hourly rate for the established throughput limit and message retention period, and additionally for the number of units of actually written data.
- On-demand: You pay for the performed read/write operations, the amount of read or written data, and the actual storage used for messages that are still within their retention period.
Getting started
Set up the infrastructure:
-
Create a Managed Service for YDB source database in any suitable configuration.
-
If you selected Dedicated DB mode for the source, create and configure a security group in the network hosting the DB.
-
Create a Managed Service for YDB database in any suitable configuration for the Data Streams target stream.
-
If you do not have Terraform yet, install it.
-
Get the authentication credentials. You can add them to environment variables or specify them later in the provider configuration file.
-
Configure and initialize a provider. There is no need to create a provider configuration file manually, you can download it
. -
Place the configuration file in a separate working directory and specify the parameter values. If you did not add the authentication credentials to environment variables, specify them in the configuration file.
-
Download the data-transfer-ydb-yds.tf
configuration file to the same working directory.This file describes:
- Network.
- Subnet.
- Security group and the rule required to connect to the Managed Service for YDB database.
- Managed Service for YDB source database.
- Managed Service for YDB database for the target stream.
- Transfer.
-
In the
data-transfer-ydb-yds.tffile, specify the following variables:source_db_name: Managed Service for YDB source database name.target_db_name: Name of the Managed Service for YDB database for the target data stream.transfer_enabled:0to ensure that no transfer is created before you create the endpoints.
-
Validate your Terraform configuration files using this command:
terraform validateTerraform will display any configuration errors detected in your files.
-
Create the required infrastructure:
-
Run this command to view the planned changes:
terraform planIf you described the configuration correctly, the terminal will display a list of the resources to update and their parameters. This is a verification step that does not apply changes to your resources.
-
If everything looks correct, apply the changes:
-
Run this command:
terraform apply -
Confirm updating the resources.
-
Wait for the operation to complete.
-
All the required resources will be created in the specified folder. You can check resource availability and their settings in the management console
. -
Prepare the Managed Service for YDB source database
-
Get ready for running SQL queries in the Managed Service for YDB source database:
Management consoleYDB CLI- In the management console
, select the folder containing your database. - From the list of services, select Managed Service for YDB.
- Select the database from the list and go to the Navigation tab.
- Click New SQL query.
-
Set up a connection to the Managed Service for YDB database.
-
Make sure you can run queries using the YDB CLI with the selected authentication mode. For example, for an OAuth token, run the following query:
ydb \ --endpoint <endpoint> \ --database <DB_name> \ --yc-token-file <path_to_OAuth_token> \ yql -s "SELECT 1;"Result:
┌─────────┐ | column0 | ├─────────┤ | 1 | └─────────┘
- In the management console
-
Create a table for test data:
CREATE TABLE test ( id Uint64, text Utf8, PRIMARY KEY (id) );
Create a target stream in Data Streams
Create a stream for the Data Streams.
Set up and activate the transfer
-
Create an
YDBsource endpoint:-
Connection settings:
- Database: Select the Managed Service for YDB database from the list.
- Service account ID: Select an existing service account or create a new one with the
ydb.editorrole.
-
Included paths list:
- Path 1:
test.
- Path 1:
-
-
Create an endpoint for the
Yandex Data Streamstarget:-
Connection settings:
- Database: Select the Managed Service for YDB database for the target stream from the list.
- Stream: Specify the name of the stream in Data Streams.
- Service account: Select an existing service account or create a new one with the
yds.editorrole.
-
Serializing settings:
Auto.
-
-
Create a transfer:
ManuallyTerraform-
In the
data-transfer-ydb-yds.tffile, specify the following variables:source_endpoint_id: ID of the source endpoint.target_endpoint_id: ID of the target endpoint.transfer_enabled:1to create a transfer.
-
Validate your Terraform configuration files using this command:
terraform validateTerraform will display any configuration errors detected in your files.
-
Create the required infrastructure:
-
Run this command to view the planned changes:
terraform planIf you described the configuration correctly, the terminal will display a list of the resources to update and their parameters. This is a verification step that does not apply changes to your resources.
-
If everything looks correct, apply the changes:
-
Run this command:
terraform apply -
Confirm updating the resources.
-
Wait for the operation to complete.
-
The transfer will activate automatically upon creation.
-
-
Test the transfer
-
Wait for the transfer status to change to Replicating.
-
Add the test data to the
testtable in the Managed Service for YDB source database:INSERT INTO test ( id, text ) VALUES ( 1, 'text 1' ), ( 2, 'text 2' ), ( 3, 'text 3' );
Make sure the data from the source has been moved to the Data Streams stream:
- In the management console
, select Data Streams. - Select the target stream from the list and navigate to
Data viewer. - Make sure
shard-000000now contains messages with the source table rows. For a closer look at the messages, click .
-
Install the AWS CLI
. -
Configure the environment for Data Streams.
-
Read the stream data using:
Delete the resources you created
Note
Before deleting the created resources, deactivate the transfer.
To reduce the consumption of resources you do not need, delete them:
-
If you created service accounts together with the endpoints, delete them.
-
Delete other resources using the same method used for their creation:
ManuallyTerraform- Delete the Managed Service for YDB source database.
- Delete the Managed Service for YDB database used for the target stream.
-
In the terminal window, go to the directory containing the infrastructure plan.
Warning
Make sure the directory has no Terraform manifests with the resources you want to keep. Terraform deletes all resources that were created using the manifests in the current directory.
-
Delete resources:
-
Run this command:
terraform destroy -
Confirm deleting the resources and wait for the operation to complete.
All the resources described in the Terraform manifests will be deleted.
-