Broker Load
Broker Load provides an asynchronous interface for bulk data import into ApsaraDB for SelectDB. It efficiently imports large-scale data, up to hundreds of gigabytes per job, from distributed storage systems such as HDFS, S3, and OSS. This topic describes how to use Broker Load to import data into an ApsaraDB for SelectDB instance.
Benefits
Broker Load provides the following benefits:
High capacity: A single import job can handle up to hundreds of gigabytes of data, which is ideal for migrating large offline datasets.
Asynchronous and high concurrency: Non-blocking, asynchronous writes improve cluster resource utilization.
High compatibility: Reads data from remote storage systems such as HDFS and S3.
Easy to use:
Create import jobs using the standard MySQL protocol.
Use the
SHOW LOADcommand to monitor import progress and results in real time.
Use cases
Broker Load efficiently imports large-scale data from distributed storage systems such as OSS, HDFS, and S3.
For small batches of data (for example, 100 MB), an import job typically completes within 10 seconds.
For large batches of data (for example, 100 GB), an import job typically completes within 10 minutes.
Prerequisites
If the remote storage system, such as S3 or HDFS, is deployed in a public network environment, your ApsaraDB for SelectDB instance must have access to the public network. For more information, see Resolve network connectivity issues with data sources.
Create an import job
You can use Broker Load to read data from a remote storage system, such as HDFS or S3, and import it into a table in ApsaraDB for SelectDB.
Syntax
LOAD LABEL load_label
(
data_desc1[, data_desc2, ...]
)
WITH broker_type
[broker_properties]
[load_properties];Parameters
Parameter | Description |
| The unique identifier for the import job. A label is a custom name specified in the import statement. You can use the label to check the execution status of the corresponding import job. Labels also prevent duplicate data imports. A label can be reused if its import job status is Format: Note We recommend using the same label for the same data batch. This practice provides at-most-once semantics by ensuring that each batch is processed only once. |
| Describes a set of files to be imported. For more information, see Parameters for data_desc1. |
| Specifies the broker type to use. Valid values are HDFS and S3. A Broker Load job of the S3 type is also known as OSS Load. For more information, see OSS Load. |
| The properties that the broker requires to access the remote storage system, such as BOS or HDFS. Syntax: |
| Specifies the properties for the import job. For more information, see Parameters for load_properties. |
Parameters for data_desc1
[MERGE|APPEND|DELETE]
DATA INFILE
(
"file_path1"[, file_path2, ...]
)
[NEGATIVE]
INTO TABLE `table_name`
[PARTITION (p1, p2, ...)]
[COLUMNS TERMINATED BY "column_separator"]
[FORMAT AS "file_type"]
[(column_list)]
[COLUMNS FROM PATH AS (c1, c2, ...)]
[PRECEDING FILTER predicate]
[SET (column_mapping)]
[WHERE predicate]
[DELETE ON expr]
[ORDER BY source_sequence]
[PROPERTIES ("key1"="value1", ...)]Parameter | Description |
| The merge mode. The default value is APPEND, which indicates a standard append operation. The MERGE and DELETE modes apply only to tables that use the Unique Key model. The MERGE mode must be used with the |
| Specifies the paths of the files to import. You can specify multiple file paths and use a wildcard. The paths must resolve to files. The import job fails if a path resolves only to a directory. |
| Indicates that this is a "negative" import. This option applies only to aggregate tables with integer columns that use the SUM aggregation type. It negates the integer values in the SUM columns of the imported data. Use this option to offset previously imported incorrect data. |
| Specifies the partitions of the table into which you want to import data. Data that does not belong to the specified partitions is ignored. |
| Specifies the column separator. This parameter is valid only for the CSV format and supports only single-byte separators. |
| Specifies the file format. The default value is CSV. Supported formats are CSV, PARQUET, and ORC. |
| Specifies the order of columns in the source file. |
| Specifies the columns to extract from the file path. |
| Specify the pre-filtering conditions. Data is first concatenated into raw data rows in sequence based on |
| Specifies the column transformation functions. |
| Specifies the filtering condition for the data. |
| Must be used with the MERGE mode and applies only to tables that use the Unique Key model. Use this to specify an expression that identifies which imported rows to treat as delete operations. |
| Applies only to tables that use the Unique Key model. Use this to specify the sequence column in the source data to ensure data order during the import. |
| Specifies parameters for the import format. For example, if the imported files are in JSON format, you can specify parameters such as json_root, jsonpaths, and fuzzy_parse here. |
Parameters for load_properties
Parameter | Description |
| The timeout for the import job, in seconds. Default value: 14400 (4 hours). |
| The maximum allowed ratio of rows to be filtered due to data quality issues. The default value is 0, which means zero tolerance. The value must be between 0 and 1. |
| The memory limit for the import job, in bytes. Default value: 2147483648 (2 GB). |
| Specifies whether to enable strict mode for the import job. Default value: false. |
| Specifies the time zone for certain time zone-sensitive functions, and the default is |
| The import parallelism. The default value is 1. Increasing this value speeds up the import process by running the job with multiple parallel execution plans. |
| The parallelism for sending data batches. If the value of this parameter exceeds the |
| Specifies whether to import data to only one tablet of the corresponding partition. The default value is false. This parameter can be set only when you import data into a table that uses the Duplicate Key model with random bucketing. |
Examples
Create the destination tables in ApsaraDB for SelectDB. The following is an example.
CREATE TABLE test_table ( id int, name varchar(50), age int, address varchar(50) ) UNIQUE KEY(`id`) DISTRIBUTED BY HASH(id) BUCKETS 4 PROPERTIES("replication_num" = "1"); CREATE TABLE test_table2 ( id int, name varchar(50), age int, address varchar(50) ) DISTRIBUTED BY HASH(id) BUCKETS 4 PROPERTIES("replication_num" = "1");Create the source files to be imported.
The file
file1.txtcontains the following content.1,tomori,32,shanghai 2,anon,22,beijing 3,taki,23,shenzhen 4,rana,45,hangzhou 5,soyo,14,shanghai 6,saki,25,hangzhou 7,mutsumi,45,shanghai 8,uika,26,shanghai 9,umiri,27,shenzhen 10,nyamu,37,shanghaiThe file
file2.csvhas the following content.1,saki,25,hangzhou 2,mutsumi,45,shanghai 3,uika,26,shanghai 4,umiri,27,shenzhen 5,nyamu,37,shanghai
Import the data from the files into the tables.
The following is an example of importing the text file
file1.txtfrom HDFS.LOAD LABEL example_db.label1 ( DATA INFILE("hdfs://hdfs_host:hdfs_port/example/file1.txt") INTO TABLE `my_table` COLUMNS TERMINATED BY "," ) WITH HDFS ( "fs.defaultFS" = "hdfs://hdfs_host:hdfs_port" );This job imports the
file1.txtfile into thetest_tabletable, with columns separated by commas. When you import data from HDFS, you must specify thefs.defaultFSproperty inbroker_propertiesto ensure a proper connection to the HDFS cluster and to locate the data file.Import data from HDFS by loading two files into two tables simultaneously. The following is an example.
LOAD LABEL test_db.test_02 ( DATA INFILE("hdfs://hdfs_host:hdfs_port/example/file2.csv") INTO TABLE `test_table` COLUMNS TERMINATED BY "," (id,name,temp_age,address) SET ( age = temp_age + 1 ), DATA INFILE("hdfs://hdfs_host:hdfs_port/example/file1.txt") INTO TABLE `test_table2` COLUMNS TERMINATED BY "," ) WITH HDFS ( "fs.defaultFS" = "hdfs://hdfs_host:hdfs_port" );Import the files
file1.txtandfile2.csvinto thetest_tableandtest_table2tables respectively, and when importingfile2.csv, increment the values in theagecolumn by 1.Import a batch of data from an HDFS cluster that is deployed in HA mode. The following is an example.
LOAD LABEL test_db.test_03 ( DATA INFILE("hdfs://hdfs_host:hdfs_port/example/*") INTO TABLE `test_table` COLUMNS TERMINATED BY "\\x01" ) WITH HDFS ( "hadoop.username" = "hive", "fs.defaultFS" = "hdfs://my_ha", "dfs.nameservices" = "my_ha", "dfs.ha.namenodes.my_ha" = "my_namenode1, my_namenode2", "dfs.namenode.rpc-address.my_ha.my_namenode1" = "nn1_host:rpc_port", "dfs.namenode.rpc-address.my_ha.my_namenode2" = "nn2_host:rpc_port", "dfs.client.failover.proxy.provider.my_ha" = "org.apache.hadoop.hdfs.server.namenode.ha.ConfiguredFailoverProxyProvider" );Specify the delimiter as the default Hive delimiter,
\\x01, and use the wildcard*to specify all files in thedatadirectory and its subdirectories.Filter the imported data from file1.txt so that only data that meets the specified condition is imported. The following is an example.
LOAD LABEL test_db.test_04 ( DATA INFILE("hdfs://host:port/example/file1.txt") INTO TABLE `test_table2` COLUMNS TERMINATED BY "," (id,name,age,address) WHERE age < 20 ) WITH HDFS ( "fs.defaultFS" = "hdfs://hdfs_host:hdfs_port" );Only rows from the source data where
age < 20are imported.The following example imports a batch of data from the file1.txt file in HDFS with a specified timeout and filtering ratio, deletes rows from the existing data that match imported rows where
age<20, and imports all other rows.LOAD LABEL test_db.test_05 ( MERGE DATA INFILE("hdfs://hdfs_host:hdfs_port/example/file1.txt") INTO TABLE `test_table` COLUMNS TERMINATED BY "," (id,name,age,address) DELETE ON age < 20 ) WITH HDFS ( "fs.defaultFS" = "hdfs://hdfs_host:hdfs_port" ) PROPERTIES ( "timeout" = "3600", "max_filter_ratio" = "0.1" );Import data by using the MERGE method.
test_tablemust be a table with a unique key. If the value in theagecolumn of the imported data is less than20, the row is considered a delete row. The timeout for the import job is 3600 seconds, and an error rate of up to 10% is allowed.
Cancel an import job
You can manually cancel a Broker Load job if its status is not CANCELLED or FINISHED. When you cancel a job, you must specify its label. After the import job is canceled, any data it has written is rolled back.
Syntax
CANCEL LOAD
[FROM db_name]
WHERE [LABEL = "load_label" | LABEL like "label_pattern"];Parameters
Parameter | Description |
| The name of the database. If this parameter is not specified, the current default database is used. |
| The label of the import job for an exact match. If you use |
Examples
Cancel the import job on the database
example_dbwith the labelexample_db_test_load_label.CANCEL LOAD FROM example_db WHERE LABEL = "example_db_test_load_label";Cancel all import jobs on the database
example_dbthat containexample_.CANCEL LOAD FROM example_db WHERE LABEL like "example_";
View import jobs
Broker Load is an asynchronous import process. A successful LOAD LABEL statement only confirms that the job was submitted, not that the data import is complete. You must use the SHOW LOAD command to monitor the job's status.
Syntax
SHOW LOAD
[FROM db_name]
[
WHERE
[LABEL [ = "your_label" | LIKE "label_matcher"]]
[STATE = ["PENDING"|"ETL"|"LOADING"|"FINISHED"|"CANCELLED"|]]
]
[ORDER BY ...]
[LIMIT limit][OFFSET offset];Parameters
Parameter | Description |
| The name of the database. If this parameter is not specified, the current default database is used. |
| The label of the import job for an exact match. If you use LABEL LIKE, the command matches import jobs whose labels contain the label_matcher. |
| The import status. Filters the results to show only jobs with a specific status. |
| Specifies the sorting criteria. |
| Limits the number of records returned. If unspecified, all matching records are returned. |
| Specifies the starting point for returning results. The default offset is 0. |
Examples
Displays the 10 oldest import jobs for the database
example_dbwhose label contains the string2014_01_02.SHOW LOAD FROM example_db WHERE LABEL LIKE "2014_01_02" LIMIT 10;Displays the import jobs for the database
example_dbwith the labelload_example_db_20140102, sorted in descending order byLoadStartTime.SHOW LOAD FROM example_db WHERE LABEL = "load_example_db_20140102" ORDER BY LoadStartTime DESC;Show the import job for the database
example_dbwith the labelload_example_db_20140102and a state ofloading.SHOW LOAD FROM example_db WHERE LABEL = "load_example_db_20140102" AND STATE = "loading";Displays the import jobs for the
example_dbdatabase, sorted in descending order byLoadStartTime, and returns 10 results starting from an offset of 5.SHOW LOAD FROM example_db ORDER BY LoadStartTime DESC limit 5,10; SHOW LOAD FROM example_db ORDER BY LoadStartTime DESC limit 10 offset 5;
Best practices
Check the status of an import job
Broker Load is an asynchronous process. A successful
LOAD LABELstatement only confirms that the job was submitted, not that the data import is complete. You must monitor the job's status by using theSHOW LOADcommand.Cancel an import job
Import jobs that have been submitted but have not yet finished can be canceled by using the
CANCEL LOADcommand. After cancellation, any data that has already been written is also rolled back and will not take effect.Labels, import transactions, and multi-table atomicity
All import jobs in ApsaraDB for SelectDB are atomic, even when a single job imports data into multiple tables. ApsaraDB for SelectDB also uses labels to ensure that data is neither lost nor duplicated during an import.
Column mapping, derived columns, and filtering
ApsaraDB for SelectDB supports a rich set of column transformation and filtering operations in import statements, including most built-in functions and UDFs. For more information, see the Data transformation documentation.
Filter incorrect data
SelectDB import jobs can tolerate some data with format errors. This tolerance is configured by using the
max_filter_ratiosetting. The default value is 0, which means that if a single row contains a format error, the entire import job fails. If you want to ignore rows with issues, you can set this parameter to a value between 0 and 1. SelectDB will then automatically skip any rows that have incorrect data formats.For details on how the filter ratio is calculated, see the Data transformation documentation.
Strict mode
The
strict_modeproperty specifies whether an import job runs in strict mode. This mode affects the results of column mapping, transformation, and filtering.Timeout
The default timeout for a Broker Load job is 4 hours, starting from when the job is submitted. The job fails if it does not complete within this time.
Limits on data volume and number of jobs
We recommend importing no more than 100 GB of data per Broker Load job. Although there is theoretically no upper limit on the data volume in one job, overly large jobs can increase execution time and make retries more costly.
Due to cluster size limitations, the maximum import data volume is limited to the number of nodes multiplied by 3 GB to ensure reasonable resource utilization. For larger datasets, split the data into multiple, smaller import jobs.
ApsaraDB for SelectDB limits the number of concurrently running import jobs in a cluster, typically to a value between 3 and 10. Subsequent import jobs are queued. The maximum queue length is 100. Submissions beyond this limit are immediately rejected.
NoteQueued time is included in the job's total runtime. A job is canceled if it times out while in the queue. Therefore, we recommend that you monitor the status of jobs to control the submission frequency.