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 stream for the Data Streams target.
- Prepare and activate the transfer.
- Test the transfer.
If you no longer need the resources you created, delete them.
Getting started
Prepare 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.tf
file, specify these 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
:0
to ensure that no transfer is created before you create the endpoints.
-
Make sure the Terraform configuration files are correct using this command:
terraform validate
If there are any errors in the configuration files, Terraform will point them out.
-
Create the required infrastructure:
-
Run the command to view planned changes:
terraform plan
If the resource configuration descriptions are correct, the terminal will display a list of the resources to modify and their parameters. This is a test step. No resources are updated.
-
If you are happy with the planned changes, apply them:
-
Run the command:
terraform apply
-
Confirm the update of 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 with the DB you need. - In 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 stream for the Data Streams target
Create a stream for the Data Streams.
Prepare and activate the transfer
-
Create an endpoint for the
YDB
source:-
Connection settings:
- Database: Select the Managed Service for YDB database from the list.
- Service account ID: Select or create a service account with the
ydb.editor
role.
-
Included paths list:
- Path 1:
test
.
- Path 1:
-
-
Create an endpoint for the
Yandex Data Streams
target:-
Connection settings:
- Database: Select the Managed Service for YDB database for the target stream from the list.
- Stream: Specify the name of the Data Streams data stream.
- Service account: Select or create a service account with the
yds.editor
role.
-
Serializing settings:
Auto
.
-
-
Create a transfer:
ManuallyTerraform- Create a transfer of the Replication type that will use the created endpoints.
- Activate your transfer.
-
In the
data-transfer-ydb-yds.tf
file, specify these variables:source_endpoint_id
: ID of the source endpoint.target_endpoint_id
: Target endpoint ID.transfer_enabled
:1
to create a transfer.
-
Make sure the Terraform configuration files are correct using this command:
terraform validate
If there are any errors in the configuration files, Terraform will point them out.
-
Create the required infrastructure:
-
Run the command to view planned changes:
terraform plan
If the resource configuration descriptions are correct, the terminal will display a list of the resources to modify and their parameters. This is a test step. No resources are updated.
-
If you are happy with the planned changes, apply them:
-
Run the command:
terraform apply
-
Confirm the update of resources.
-
Wait for the operation to complete.
-
Once created, your transfer will be activated automatically.
-
Test the transfer
-
Wait for the transfer status to change to Replicating.
-
Add the test data to the
test
table 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 go to
Data viewer. - Make sure
shard-000000
contains messages with the source table rows. To view message details, click .
-
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.
Some resources are not free of charge. To avoid paying for them, delete the resources you no longer need:
- Delete the transfer.
- Delete endpoints for both the source and target.
- If you created service accounts together with the endpoints, delete them.
Delete the other resources depending on how they were created:
- 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.
-
Delete the
data-transfer-ydb-yds.tf
configuration file. -
Make sure the Terraform configuration files are correct using this command:
terraform validate
If there are any errors in the configuration files, Terraform will point them out.
-
Confirm updating the resources.
-
Run the command to view planned changes:
terraform plan
If the resource configuration descriptions are correct, the terminal will display a list of the resources to modify and their parameters. This is a test step. No resources are updated.
-
If you are happy with the planned changes, apply them:
-
Run the command:
terraform apply
-
Confirm the update of resources.
-
Wait for the operation to complete.
-
All the resources described in the
data-transfer-ydb-yds.tf
configuration file will be deleted. -