Bulk data load from files
The following page describes the steps to import data in CSV or TEXT format from flat files on your local disk or in cloud storage, including AWS S3, GCS buckets, and Azure Blob.
Prerequisite
- Before you perform a bulk load, in your target YugabyteDB database, create the schema of the tables into which the data in the flat files will be imported.
Prepare the target database
Prepare your target YugabyteDB database cluster by creating a user.
Create a user
Create a user with SUPERUSER
role.
-
For a local YugabyteDB cluster or YugabyteDB Anywhere, create a user and role with the superuser privileges using the following command:
CREATE USER ybvoyager SUPERUSER PASSWORD 'password';
-
For YugabyteDB Aeon, create a user with
yb_superuser
role using the following command:CREATE USER ybvoyager PASSWORD 'password'; GRANT yb_superuser TO ybvoyager;
If you want yb-voyager to connect to the target YugabyteDB database over SSL, refer to SSL Connectivity.
Deleting the ybvoyager user
After migration, all the migrated objects (tables, views, and so on) are owned by the ybvoyager
user. You should transfer the ownership of the objects to some other user (for example, yugabyte
) and then delete the ybvoyager
user. Example steps to delete the user are:
REASSIGN OWNED BY ybvoyager TO yugabyte;
DROP OWNED BY ybvoyager;
DROP USER ybvoyager;
Import data files from the local disk
If your data files are in CSV or TEXT format and are present on the local disk, and you have already created a schema in your target YugabyteDB database, you can use the following yb-voyager import data file
command with required arguments to load the data into the target table directly from the flat file(s).
# Replace the argument values with those applicable to your migration.
yb-voyager import data file --export-dir <EXPORT_DIR> \
--target-db-host <TARGET_DB_HOST> \
--target-db-user <TARGET_DB_USER> \
--target-db-password <TARGET_DB_PASSWORD> \ # Enclose the password in single quotes if it contains special characters.
--target-db-name <TARGET_DB_NAME> \
--target-db-schema <TARGET_DB_SCHEMA> \
--data-dir </path/to/files/dir/> \
--file-table-map <filename1>:<table1>,<filename2>:<table2> \
--format csv|text \ # default csv
# Optional arguments as per data format
--delimiter <DELIMITER> \ # default ',' for csv and '\t' for text
--escape-char <ESCAPE_CHAR> \ # for csv format only. default: '"'
--quote-char <QUOTE_CHAR> \ # for csv format only. default: '"'
--has-header \ # for csv format only. default: false
--null-string <NULL_STRING> # default '' (empty string) for csv and '\N' for text
Refer to import data file for details about the arguments.
Migrating data files with large size rows
For CSV file format, the import data file has a default row size limit of 32MB. If a row exceeds this limit but is smaller than the batch-size * max-row-size
, you can increase the limit for the import data file process by setting the following environment variable:
export CSV_READER_MAX_BUFFER_SIZE_BYTES = <MAX_ROW_SIZE_IN_BYTES>
Import data status
Run the yb-voyager import data status --export-dir <EXPORT_DIR>
command to get an overall progress of the data import operation.
Refer to import data status for details about the arguments.
Load multiple files into the same table
The import data file command also supports importing multiple files to the same table by providing the --file-table-map
flag with a <fileName>:<tableName>
entry for each file, or by passing a glob expression in place of the file name. For example, fileName1:tableName,fileName2:tableName
or fileName*:tableName
.
Incremental data loading
You can also import files to the same table across multiple runs. For example, you could import orders1.csv
as follows:
yb-voyager import data file --file-table-map 'orders1.csv:orders' ...
And then subsequently import orders2.csv
to the same table as follows:
yb-voyager import data file --file-table-map 'orders2.csv:orders' ...
To import an updated version of the same file (that is, having the same file name and data-dir), use the --start-clean
flag and proceed without truncating the table. yb-voyager ingests the data present in the file in upsert mode. For example:
yb-voyager import data file --data-dir /dir/data-dir --file-table-map 'orders.csv:orders' ...
After new rows are added to orders.csv
, use the following command to load them with the flags --start-clean
and --enable-upsert
.
Note
Ensure that tables on the target YugabyteDB database do not have secondary indexes. If a table has secondary indexes, using--enable-upsert true
may lead to corruption of the indexes.
yb-voyager import data file --data-dir /dir/data-dir \
--file-table-map 'orders.csv:orders' \
--start-clean true \
--enable-upsert true
For details about the argument, refer to the arguments table.
Import data files from cloud storage
Using the import data file
command, you can import data from files in cloud storage, including AWS S3, GCS buckets, and Azure blob. Importing from cloud storage reduces local disk requirements for storing the data while it is imported to your YugabyteDB database.
To import data from AWS S3, provide the S3 bucket URI in the data-dir
flag as follows:
yb-voyager import data file .... \
--data-dir s3://voyager-data
The authentication mechanism for accessing an S3 bucket using yb-voyager is the same as that used by the AWS CLI. Refer to Configure the AWS CLI for additional details on setting up your S3 bucket.
To import data from GCS buckets, provide the GCS bucket URI in the data-dir
flag as follows:
yb-voyager import data file .... \
--data-dir gs://voyager-data
The authentication mechanism for accessing a GCS bucket using yb-voyager is the Application Default Credentials (ADC) strategy for GCS. Refer to Set up Application Default Credentials for additional details on setting up your GCS buckets.
To import data from Azure blob storage containers, provide the Azure container URI in the data-dir
flag as follows:
yb-voyager import data file .... \
--data-dir https://<account_name>.blob.core.windows.net/<container_name>...
The authentication mechanism for accessing blobs using yb-voyager is the same as that used by the Azure CLI. The Azure storage account used for the import should at least have the Storage Blob Data Reader role assigned to it. Refer to Sign in with Azure CLI for additional details on setting up your Azure blobs.
Verify migration
After the data import is complete, manually run validation queries on the target YugabyteDB database to ensure that the data is correctly imported. For example, run queries to check the row count of each table.
Row count reported by import data status
Suppose you have the following scenario:
- The import data file command fails.
- To resolve this issue, you delete some of the rows from the split files.
- After retrying, the import data to target command completes successfully.
In this scenario, the import data status command reports an incorrect imported row count because it doesn't take into account the deleted rows.
For more details, refer to the GitHub issue #360.
End migration
To complete the migration, you need to clean up the export directory (export-dir) and Voyager state (Voyager-related metadata) stored in the target YugabyteDB database.
Run the yb-voyager end migration
command to perform the clean up, and to back up the migration report, and the log files by providing the backup related flags (mandatory) as follows:
# Replace the argument values with those applicable for your migration.
yb-voyager end migration --export-dir <EXPORT_DIR> \
--backup-log-files <true, false, yes, no, 1, 0> \
--backup-data-files false \
--backup-schema-files false \
--save-migration-reports <true, false, yes, no, 1, 0> \
# Set optional argument to store a back up of any of the above arguments.
--backup-dir <BACKUP_DIR>
Note
-
After performing end migration, you can't continue import data file operations using the specified export directory (export-dir).
-
Because import data file only imports data (and doesn't import or export the schema or export data), set the
--backup-data-files
andbackup-schema-files
arguments to false.
If you want to back up the log file and import data status output for future reference, use the --backup-dir
argument, and provide the path of the directory where you want to save the backup content (based on what you choose to back up).
Refer to end migration for more details on the arguments.