3.2. Миграция данных #
При миграции данных важен порядок полей в исходной и целевой схеме. Порядок полей и их тип в таблицах нераспределённой и распределённой БД должен быть одинаковым.
Утилита миграции делает ровно то, что необходимо пользователю, никакими действиями не вторгаясь в процессы миграции данных, кроме, возможно, распределения данных при миграции сразу в тот сегмент, где они должны храниться.
Shardman предоставляет удобные инструменты для миграции данных. При уже созданной схеме и выбранном ключе сегментирования остаётся определить правила миграции данных. Источником данных могут быть, как файлы экспорта данных в формате CSV, так и одиночный сервер СУБД.
Далеко не всегда удобно использовать CSV-файлы, так как они могут значительно увеличиваться в объёме и требовать дополнительных ресурсов для хранения и передачи.
Гораздо удобнее использовать прямую миграцию данных из БД в БД без этапа промежуточного хранения.
При миграции данных следует учитывать порядок загрузки данных. Таблицы могут быть связаны друг с другом внешним ключом, поэтому сначала нужно загружать данные таблиц, на которые будут ссылаться другие таблицы. Для этого в файле миграции следует задать приоритет, определяющий порядок загрузки данных из таблиц. Чем больше значение параметра priority, тем выше приоритет. Например, если заданы приоритеты 1, 2 и 3, то сначала будут загружены данные из таблиц с приоритетом 3, потом с приоритетом 2, последними — с приоритетом 1.
Команда shardmanctl load позволяет определить порядок миграции таблиц, который можно указать в конфигурационном файле в формате YML.
3.2.1. Наивный подход #
Ниже представлен пример содержимого файла migrate.yml:
version: "1.0"
migrate:
connstr: "dbname=demo host=single-pg-instance port=5432 user=postgres password=******"
jobs: 8
batch: 2000
options:
schemas:
- name: bookings
# параметр all со значением false отключает автоматическое создание таблиц
# таблицы уже созданы ранее, на этапе миграции схемы
all: false
tables:
- name: airports
# определяем глобальную таблицу
type: global
# так как таблицы связаны, нужно задать порядок миграции данных,
# выставляем наивысший приоритет таблицам, данные которых
# должны быть скопированы в первую очередь
priority: 3
- name: aircrafts
type: global
priority: 3
- name: seats
type: global
priority: 3
- name: bookings
type: global
priority: 3
- name: flights
type: global
priority: 3
- name: tickets
type: sharded
# определяем сегментированную таблицу
# указываем ключ распределения
distributedby: ticket_no
partitions: 4
priority: 2
- name: ticket_flights
type: sharded
distributedby: ticket_no
# определяем сегментированную и совместно размещённую таблицу
# указываем имя таблицы с которой будет совместно размещена таблица tickets_flights
colocatewith: tickets
partitions: 4
priority: 2
- name: boarding_passes
type: sharded
distributedby: ticket_no
colocatewith: tickets
partitions: 4
priority: 1В данном файле определён источник данных — узел с именем single-pg-instance, его порт для подключения, имя пользователя и пароль, а также имя БД источника данных. Определены также и некоторые параметры работы утилиты (их может быть довольно много, как показано Подразделе «Загрузка данных из схемы PostgreSQL»). В файле также задано количество потоков — 8, то есть размер порции обрабатываемых данных в строках при миграции, а также приоритеты для последовательной обработки таблиц. Сначала мигрируют данные в глобальные таблицы, следом данные в распределённые таблицы tickets и ticket_flights, затем данные в таблицу boarding_passes. Значение priority задаёт приоритет загрузки данных, чем больше значение ключа, тем раньше будут загружены данные соответствующих таблиц. Миграция выполняется с помощью следующей команды:
shardmanctl load --schema migrate.yml
Если в конце работы утилиты выводится сообщение: «data loading completed successfully» (загрузка данных успешно завершена), то миграция данных прошла успешно.
3.2.2. Комплексный подход #
При данном подходе, запуск и работа утилиты shardmanctl в режиме load не будет отличаться от работы при наивном подходе. Однако, немного изменится файл, определяющий порядок загрузки таблиц, так как изменился ключ сегментирования:
---
version: "1.0"
migrate:
connstr: "dbname=demo host=single-pg-instance port=5432 user=postgres password=postgres"
jobs: 8
batch: 2000
options:
schemas:
- name: bookings
all: false
tables:
- name: airports
type: global
priority: 5
- name: aircrafts
type: global
priority: 5
- name: seats
type: global
priority: 5
- name: flights
type: global
priority: 5
- name: bookings
type: sharded
priority: 4
partitions: 4
distributedby: book_ref
- name: tickets
type: sharded
distributedby: book_ref
colocatewith: bookings
partitions: 4
priority: 3
- name: ticket_flights
type: sharded
distributedby: book_ref
colocatewith: bookings
partitions: 4
priority: 2
- name: boarding_passes
type: sharded
distributedby: book_ref
colocatewith: bookings
partitions: 4
priority: 13.2. Data Migration #
When migrating data, the order of fields in the source and target schema is important. The order and types of fields in the non-distributed and distributed databases must be the same.
The migration utility does exactly what is requested by the user, who does not interfere with data migration processes except, maybe, distributing the data directly to the shard where it must be stored.
Shardman provides convenient migration tools. Once the distributed schema is created and the sharding key chosen, it is now needed to define the data migration rules. The data source can be either export CSV data files or a single DBMS server.
It is not always convenient to use CSV files as they can reach a pretty large size and require additional resources for storage and transfer.
Migrating data directly from DB to DB without an intermediate storage phase is much more convenient.
The order of loading data during migration must be taken into account. Tables can be linked with a foreign key, so the data in tables that other tables will reference must be loaded first. To follow such an order, in the migration file, you should establish the priority that defines tables whose data must be loaded first. The higher the value of the priority parameter, the higher the priority. For example, if the priorities 1, 2 and 3 are defined, tables with the priority 3 will be loaded first, then those with the priority 2, and last with the priority 1.
The shardmanctl load command lets you define the order of migrating tables, which can be specified in the configuration YML file.
3.2.1. Naive Approach #
The following is an example of the migrate.yml file:
version: "1.0"
migrate:
connstr: "dbname=demo host=single-pg-instance port=5432 user=postgres password=******"
jobs: 8
batch: 2000
options:
schemas:
- name: bookings
# the all parameter set to false turns off automatic creation of pages
# tables are already created, at the Schema Migration phase
all: false
tables:
- name: airports
# defining a global table
type: global
# as tables are linked, data migration priority must be defined
# setting highest priority to tables whose data
# must be copied first
priority: 3
- name: aircrafts
type: global
priority: 3
- name: seats
type: global
priority: 3
- name: bookings
type: global
priority: 3
- name: flights
type: global
priority: 3
- name: tickets
type: sharded
# defining a sharded table
# specifying the sharding key
distributedby: ticket_no
partitions: 4
priority: 2
- name: ticket_flights
type: sharded
distributedby: ticket_no
# defining a sharded and colocated table
# specifying the name of the table that ticket_flights table will be colocated with
colocatewith: tickets
partitions: 4
priority: 2
- name: boarding_passes
type: sharded
distributedby: ticket_no
colocatewith: tickets
partitions: 4
priority: 1
This file defines the data source, that is, the single-pg-instance node, its connection port, user name and password, and data source DB name. Some parameters of the migration utility operation are also defined (there can be quite a few of them, as explained in the section called “Loading Data with a Schema from PostgreSQL”). The file also defines the number of threads — 8, batch size, that is, the number of rows organized into batches for processing during migration, as well as table processing priorities. The data for the global tables is migrated first, then the data for the sharded tables tickets and ticket_flights, and migration of the boarding_passes table completes the migration. The value of priority defines the priority of data loading, data for tables with higher value will be loaded earlier than with the lower value. The following command performs the migration:
shardmanctl load --schema migrate.yml
If the utility completes with the message “data loading completed successfully”, it means that the migration was a success.
3.2.2. Complex Approach #
With this approach, the launch and operation of the shardmanctl utility in the load mode is the same as with the naive approach. However, the file that defines the order of loading tables will slightly differ as the sharding key has changed:
---
version: "1.0"
migrate:
connstr: "dbname=demo host=single-pg-instance port=5432 user=postgres password=postgres"
jobs: 8
batch: 2000
options:
schemas:
- name: bookings
all: false
tables:
- name: airports
type: global
priority: 5
- name: aircrafts
type: global
priority: 5
- name: seats
type: global
priority: 5
- name: flights
type: global
priority: 5
- name: bookings
type: sharded
priority: 4
partitions: 4
distributedby: book_ref
- name: tickets
type: sharded
distributedby: book_ref
colocatewith: bookings
partitions: 4
priority: 3
- name: ticket_flights
type: sharded
distributedby: book_ref
colocatewith: bookings
partitions: 4
priority: 2
- name: boarding_passes
type: sharded
distributedby: book_ref
colocatewith: bookings
partitions: 4
priority: 1