Data transfer
In YDB, a transfer is an asynchronous mechanism that moves data from a topic
In Managed Service for YDB, transfers are only available in the dedicated database operating mode. An use case of creating a transfer that moves data within the same database is described in the article titled Transfer: quick start
A transfer can read data from topics located either in the same database
To read a topic from another Managed Service for YDB database in Yandex Cloud, there is support for authorization via Identity and Access Management.
Creating a transfer to read data from another Managed Service for YDB database in Yandex Cloud
Prepare an API key to access the topic
Note
You should follow the steps to prepare an API key in the same cloud the source topic database is in.
- Create a service account where the topic is located.
- Assign the following roles to the service account:
- To read from the data stream:
ydb.viewer. - To automatically add a reader
, if applicable:ydb.admin.
- To read from the data stream:
- Create an API key with the
yc.ydb.topics.managescope.
Create a secret to access the topic in the source database
Add the secret in the cloud you are going to create your transfer in:
CREATE OBJECT example_secret (TYPE SECRET) WITH value="ApiKey <your_api_key>";
Where:
<your_api_key>: API key you created earlier.
Create a topic
Create a topic the transfer will be reading data from. You can do it using this SQL query:
CREATE TOPIC `transfer_recipe/source_topic`;
The transfer_recipe/source_topic topic allows transferring any unstructured data.
Create a table
After you create the topic, create a table to receive data from source_topic. You can do it using this SQL query:
CREATE TABLE `transfer_recipe/target_table` (
partition Uint32 NOT NULL,
offset Uint64 NOT NULL,
data String,
PRIMARY KEY (partition, offset)
);
The transfer_recipe/target_table table has three columns:
partition: ID of the topic partition the message was received from.offset: Sequence number that identifies the message within the partition.data: Message body.
Create a transfer
After you create the topic and the table, you need to add a data transfer that will move your messages from the one to the other. You can do it using this SQL query:
$transformation_lambda = ($msg) -> {
return [
<|
partition: $msg._partition,
offset: $msg._offset,
data: $msg._data
|>
];
};
CREATE TRANSFER `transfer_recipe/example_transfer`
FROM `transfer_recipe/source_topic` TO `transfer_recipe/target_table`
USING $transformation_lambda
WITH (
CONNECTION_STRING = '<endpoint>',
TOKEN_SECRET_NAME = 'example_secret'
)
Where:
<endpoint>: Endpoint of connection to the source database the topic is in. It has the following format:grpcs://lb.etn952fh3eo2jd2mrIhK.ydb.mdb.yandexcloud.net:2135/?database=/global/b1gvcqr959dbmi0e5c1B/etn77atb9o1epqUsCGoY. The endpoint appears in the Endpoint field on the Overview tab of the data stream page in the management console .example_secret: Secret you created earlier.
Test the transfer
To test the transfer for correct operation, write several messages into the topic.
Some time after the messages are added to source_topic, the relevant records will appear in transfer_recipe/target_table. To check this, run the following SQL query:
SELECT *
FROM `transfer_recipe/target_table`;