Purpose
This statement is used to import data from an external source.
OceanBase Database supports the following input file types for the
LOAD DATAstatement:Server-side files (on OBServer nodes): Files stored on the OBServer nodes of OceanBase Database. You can use the
LOAD DATA INFILEorLOAD DATA FROM URLstatement to load data from these server-side files into a database table.Client-side files (local files): Files stored on the local file system of the client. You can use the
LOAD DATA LOCAL INFILEorLOAD DATA FROM URLstatement to load data from these client-side files into a database table.Note
When you execute the
LOAD DATA LOCAL INFILEstatement, the system automatically adds theIGNOREoption.OSS files: Files stored in the OSS file system. You can use the
LOAD DATA REMOTE_OSS INFILEstatement to load data from these OSS files into a database table.
The
LOAD DATAstatement currently supports importing data from CSV text files. The import process can be divided into the following steps:File parsing: OceanBase Database reads the data from the file based on the file name provided by the user and parses the data in parallel or sequentially based on the specified parallelism.
Data distribution: Since OceanBase Database is a distributed database, data in different partitions may be distributed across different OBServer nodes. The
LOAD DATAstatement calculates the parsed data and determines which OBServer node the data should be sent to.Data insertion: Once the target OBServer node receives the data, it executes the
INSERToperation locally to insert the data into the corresponding partition.
Considerations
Tables with triggers cannot use the
LOAD DATAstatement.To import data from an external file, you must have the
FILEprivilege and the following settings:- When loading a server-side file, you must set the system variable secure_file_priv in advance to specify the path for accessing files during import or export.
- When loading a client-side local file, you must add the
--local-infile[=1]option when starting the MySQL/OBClient client to enable the feature of loading data from the local file system.
When using partitioned table bypass import, the target table cannot be a replicated table, and it cannot contain auto-increment columns, identity columns, or global indexes.
Syntax
-- Load data from a file
LOAD DATA
[/*+ PARALLEL(N) [load_batch_size(M)] [APPEND | direct(bool, int, [load_mode])] | NO_DIRECT */]
[REMOTE_OSS | LOCAL] INFILE 'file_name'
[REPLACE | IGNORE]
INTO TABLE table_name [PARTITION(PARTITION_OPTION)]
[COMPRESSION [=] {AUTO|NONE|GZIP|DEFLATE|ZSTD}]
[{FIELDS | COLUMNS}
[TERMINATED BY 'string']
[[OPTIONALLY] ENCLOSED BY 'char']
[ESCAPED BY 'char']
]
[LINES
[STARTING BY 'string']
[TERMINATED BY 'string']
]
[IGNORE number {LINES | ROWS}]
[(column_name_var
[, column_name_var] ...)]
load_mode:
'full'
| 'inc_replace'
PARTITION_OPTION:
partition_option_list
| subpartition_option_list
-- Load data from a URL
LOAD DATA
[/*+ PARALLEL(N) [load_batch_size(M)] [APPEND | direct(bool, int, [load_mode])] | NO_DIRECT */]
[REPLACE | IGNORE]
FROM { url_table_function_expr |
( SELECT expression_list FROM url_table_function_expr ) }
INTO TABLE table_name
[PARTITION(PARTITION_OPTION)]
[(column_name_var [, column_name_var] ...)]
[LOG ERRORS [REJECT LIMIT {integer | UNLIMITED}]]
load_mode:
'full'
| 'inc_replace'
url_table_function_expr:
FILES (
LOCATION = '<string>',
{
FORMAT = (
TYPE = 'CSV',
LINE_DELIMITER = '<string>' | <expr>,
FIELD_DELIMITER = '<string>' | <expr>,
PARSE_HEADER = { TRUE | FALSE },
ESCAPE = '<character>' | <expr>,
FIELD_OPTIONALLY_ENCLOSED_BY = '<character>' | <expr>,
ENCODING = 'charset',
NULL_IF = ('<string>' | <expr>, '<string>' | <expr> ...),
SKIP_HEADER = <int>,
SKIP_BLANK_LINES = { TRUE | FALSE },
TRIM_SPACE = { TRUE | FALSE },
EMPTY_FIELD_AS_NULL = { TRUE | FALSE }
)
| FORMAT = ( TYPE = 'PARQUET' | 'ORC' )
},
[PATTERN = '<regex_pattern>']
)
| SOURCE (
TYPE = 'ODPS',
ACCESSID = '<string>',
ACCESSKEY = '<string>',
ENDPOINT = '<string>',
TUNNEL_ENDPOINT = '<string>',
PROJECT_NAME = '<string>',
SCHEMA_NAME = '<string>',
TABLE_NAME = '<string>',
QUOTA_NAME = '<string>',
COMPRESSION_CODE = '<string>'
)
PARTITION_OPTION:
partition_option_list
| subpartition_option_list
Parameters
| Parameter | Description |
|---|---|
| parallel(N) | The parallelism for data loading, defaulting to 4. |
| load_batch_size(M) | Specifies the batch size for each insert, with M defaulting to 100. Recommended range is [100, 1000]. |
| APPEND | direct() |NO_DIRECT | Use the hint to enable direct load.
NoticeDuring a direct load task, it is not recommended to perform an upgrade of OceanBase Database, as this may result in the failure of the direct load task.
LOAD DATA for direct load, see Use the LOAD DATA statement to directly load data or files. |
| REMOTE_OSS | LOCAL | Optional,
|
| file_name | Specifies the path and name of the input file. The format of file_name is as follows:
NoteWhen importing files from OSS, ensure the following:
|
| REPLACE | IGNORE | If a unique key conflict occurs, REPLACE indicates that the conflicting row will be overwritten, and IGNORE indicates that the conflicting row will be ignored. LOAD DATA determines whether data is duplicated based on the table's primary key. If the table does not have a primary key, the REPLACE and IGNORE options have no effect. By default, when duplicate data is encountered, LOAD DATA records the erroneous data in the log file.
Notice
|
| url_table_function_expr | Optional. Specifies the data source from which data is read. This parameter can be FILES or SOURCE. |
| table_name | The name of the table to which data is imported.
|
| PARTITION_OPTION | The name of the partition when you perform a direct load.
NoteSpecifying a partition is supported only for direct load. It is not supported for the normal LOAD DATA statement. That is, if you do not add the direct load hint or set the direct load configuration item, specifying a partition in the LOAD DATA statement will not take effect. |
| COMPRESSION | The compression format of the file.
|
| FIELDS | COLUMNS | The format of the fields.
|
| LINES STARTING BY | The starting character of a line. |
| LINES TERMINATED BY | The ending character of a line. |
| IGNORE number { LINES | ROWS } | The number of lines to be ignored. LINES specifies the number of lines at the beginning of the file, and ROWS specifies the number of rows at the beginning of the file based on the field delimiter. By default, each field in the input file is matched with a column in the table. If the input file does not contain all the columns, the missing columns are filled with the following values:
NoteWhen you import data from multiple files, the behavior is the same as when you import data from a single file. |
| column_name_var | Optional. The name of the imported column. |
| LOG ERRORS [REJECT LIMIT {integer | UNLIMITED}] | Optional. Specifies whether to enable error diagnostics during the import of a URL table. For more information, see log_errors below.
NoteFor OceanBase Database V4.3.5, the LOAD DATA statement supports specifying error diagnostics for the import of a URL table starting from V4.3.5 BP2. |
FILES
The FILES keyword is composed of the LOCATION, FORMAT, and PATTERN clauses.
The
LOCATIONclause specifies the path where the external table files are stored. Typically, the data files of an external table are stored in a separate directory, which can contain subdirectories. When a table is created, the external table automatically collects all files in this directory.For a local location, the format is
LOCATION = '[file://] local_file_path', wherelocal_file_pathcan be a relative or absolute path. If a relative path is specified, the current directory must be the installation directory of OceanBase Database. Thesecure_file_privparameter specifies the file paths that OBServer nodes are allowed to access.local_file_pathmust be a subpath of thesecure_file_privpath.For a remote location, the format is as follows:
LOCATION = '{oss|cos|S3}://$ACCESS_ID:$ACCESS_KEY@$HOST:s3_region/remote_file_path', where$ACCESS_ID,$ACCESS_KEY, and$HOSTare the access information required for accessing OSS, COS, and S3, respectively, ands3_regionis the region information selected when using S3. These sensitive access information are stored in the system tables of the database in an encrypted manner.LOCATION = 'hdfs://$ {hdfs_namenode_address}:${port}/PATH.localhost', whereportis the port number of HDFS, andPATHis the directory path in HDFS.- For Kerberos authentication:
LOCATION = 'hdfs://localhost:port/user?principal=xxx&keytab=xxx&krb5conf=xxx&configs=xxx'. The parameters are described as follows:principal: the user for login authentication.keytab: the path of the user authentication key file.krb5conf: the path of the Kerberos environment description file.configs: additional HDFS configuration items. The default value is empty. However, in a Kerberos environment, this configuration item usually has a value and needs to be configured. For example,dfs.data.transfer.protection=authentication,privacyspecifies the data transfer protection level asauthenticationandprivacy.
- For Kerberos authentication:
Notice
When using an object storage path, the parameters of the object storage path are separated by the
&symbol. Make sure that the parameter values you enter contain only uppercase and lowercase letters, numbers,\/-_$+=, and wildcards. If you enter other characters, the settings may fail.
The
FORMATclause specifies properties related to the file reading format. It supports CSV, PARQUET, and ORC file formats.When TYPE = 'CSV', the following fields are included:
LINE_DELIMITER: specifies the line delimiter of the CSV file. The default value isLINE_DELIMITER='\n'.FIELD_DELIMITER: an optional parameter that specifies the column delimiter of the CSV file. The default value isFIELD_DELIMITER='\t'.PARSE_HEADER: an optional parameter that specifies whether the first line of the CSV file is the column name for each column. The default value isFALSE, indicating that the first line of the CSV file is not specified as the column name for each column.ESCAPE: specifies the escape character of the CSV file. It can only be one byte, with the default value ofESCAPE ='\'.FIELD_OPTIONALLY_ENCLOSED_BY: an optional parameter that specifies the symbol used to enclose field values in the CSV file. The default value is empty. Using this option indicates that only fields of certain types (such as CHAR, VARCHAR, TEXT, and JSON) are enclosed.ENCODING: specifies the character set encoding format of the file. If not specified, the default value isUTF8MB4.NULL_IF: specifies the string that is treated asNULL. The default value is empty.SKIP_HEADER: skips the file header and specifies the number of skipped rows.SKIP_BLANK_LINES: specifies whether to skip blank lines. The default value isFALSE, indicating that blank lines are not skipped.TRIM_SPACE: specifies whether to remove leading and trailing spaces from fields in the file. The default value isFALSE, indicating that leading and trailing spaces are not removed from fields in the file.EMPTY_FIELD_AS_NULL: specifies whether to treat empty strings asNULL. The default value isFALSE, indicating that empty strings are not treated asNULL.
When TYPE = 'PARQUET/ORC', there are no additional fields.
The
PATTERNclause specifies a regular expression pattern used to filter files in theLOCATIONdirectory. For each file path in theLOCATIONdirectory, if it matches the pattern, the external table accesses the file; otherwise, it skips the file. If this parameter is not specified, the external table can access all files in theLOCATIONdirectory by default.
SOURCE
The SOURCE keyword does not include other clauses. In this case, TYPE = 'ODPS' and the following fields are included:
ACCESSID: specifies the ID of the ODPS user.ACCESSKEY: specifies the password of the ODPS user.ENDPOINT: specifies the connection address of the ODPS service.TUNNEL_ENDPOINT: specifies the connection address of the Tunnel data transmission service.PROJECT_NAME: specifies the project where the table to be queried is located.SCHEMA_NAME: an optional parameter that specifies the schema of the table to be queried.TABLE_NAME: specifies the name of the table to be queried.QUOTA_NAME: an optional parameter that specifies whether to use the specified quota.COMPRESSION_CODE: an optional parameter that specifies the compression format of the data source. It supportsZLIB,ZSTD,LZ4, andODPS_LZ4. If not specified, compression is not enabled.
log_errors
LOG ERRORS: enables error diagnostics during the import process, allowing failed rows to be recorded (currently stored in thewarning buffer, which can be viewed usingshow warnings, with a maximum of 64 rows), instead of terminating the entire operation due to the first error. When used with theREJECT LIMITclause, it allows control over the number of allowed error rows.REJECT LIMIT: an optional parameter that specifies the maximum number of error rows allowed:- The default value is 0, indicating that no error rows are allowed, and the operation will fail upon encountering the first error.
integer: the maximum number of error rows allowed on a single machine. For example, 10 indicates that up to 10 error rows can be encountered on a single machine.UNLIMITED: allows an unlimited number of error rows.
Notice
- If the
LOG ERRORSclause is not specified, the default behavior is to immediately report an error upon encountering the first error. - If the
LOG ERRORSclause is specified but theREJECT LIMITclause is not, it is equivalent to specifying a diagnostic limit of 0. In this case, the operation will fail upon encountering the first error, but the first error will be recorded, and the error code will be related to the diagnostic error, such as "reject limit reached".Rules for using wildcards in multi-file direct load
To facilitate multi-file import, the wildcard feature is introduced for server-side and OSS file imports, but not for client-side file imports.
Server-side wildcard usage
Matching rules:
Match a file:
load data /*+ parallel(20) direct(true, 0) */ infile '/xxx/test.*.csv' replace into table t1 fields terminated by '|';Match a directory:
load data /*+ parallel(20) direct(true, 0) */ infile '/aaa*bb/test.1.csv' replace into table t1 fields terminated by '|';Match both a directory and a file:
load data /*+ parallel(20) direct(true, 0) */ infile '/aaa*bb/test.*.csv' replace into table t1 fields terminated by '|';
Considerations:
At least one matching file must exist. Otherwise, an error code 4027 is returned.
For the input
load data /*+ parallel(20) direct(true, 0) */ infile '/xxx/test.1*.csv,/xxx/test.6*.csv' replace into table t1 fields terminated by '|';, the pattern/xxx/test.1*.csv,/xxx/test.6*.csvis considered as a whole. If no files match, an error code 4027 is returned.Only POSIX-compatible wildcards supported by the GLOB function are supported, such as
test.6*(6|0).csvandtest.6*({0.csv,6.csv}|.csv). Although these patterns can be found using thelscommand, the GLOB function cannot match them, resulting in an error code 4027.
Wildcard usage in Cloud Object Storage Service (
OSS)Matching rules:
Match a file:
load data /*+ parallel(20) direct(true, 0) */ remote_oss infile 'oss://xxx/test.*.csv?host=xxx&access_id=xxx&access_key=xxx' replace into table t1 fields terminated by '|';Considerations:
Directory matching is not supported. For example, the input
load data /*+ parallel(20) direct(true, 0) */ remote_oss infile 'oss://aa*bb/test.*.csv?host=xxx&access_id=xxx&access_key=xxx' replace into table t1 fields terminated by '|';will returnOB_NOT_SUPPORTED.Only the
*and?wildcards are supported for filenames. Other wildcards, although allowed in input, cannot match any results.
Examples
Note
When you use the
LOAD DATAstatement, you can use\Nto representNULL.Import data from a server-side file
Example 1: Import data from a server-side file.
Set the global secure file path.
obclient> SET GLOBAL secure_file_priv = "/" Query OK, 0 rows affected obclinet> \q ByeNote
Since
secure_file_privis aGLOBALvariable, you must execute\qto make it effective.After you reconnect to the database, import data from an external file.
obclient> LOAD DATA INFILE 'test.sql' INTO TABLE t1; Query OK, 0 rows affected
Example 2: Enable direct load by using the
APPENDhint.LOAD DATA /*+ PARALLEL(4) APPEND */ INFILE '/home/admin/a.csv' INTO TABLE t;Example 3: Import a CSV file.
Import all columns of the
test1.csvfile.load data /*+ direct(true,0) parallel(2)*/ from files( location = "data/csv", format = ( type = 'csv', field_delimiter = ',', parse_header = true, skip_blank_lines = true ), pattern = 'test1.csv') into table t1;Read the
c1andc2columns from thetest1.csvfile in thedata/csvdirectory and import them to thecol1andcol2columns of thet1table.load data /*+ direct(true,0) parallel(2)*/ from ( select c1, c2 from files( location = 'data/csv' format = ( type = 'csv', field_delimiter = ',', parse_header = true, skip_blank_lines = true ), pattern = 'test1.csv')) into table t1 (col1, col2);
Example 4: Import a PARQUET file.
load data /*+ direct(true,0) parallel(2)*/ from files( location = "data/parquet", format = ( type = 'PARQUET'), pattern = 'test1.parquet') into table t1;Example 5: Import an ORC file.
load data /*+ direct(true,0) parallel(2)*/ from files( location = "data/orc", format = ( type = 'ORC'), pattern = 'test1.orc') into table t1;Example 5: Import an ODPS file.
load data /*+ direct(true,0) parallel(2)*/ from source ( type = 'ODPS', accessid = '$ODPS_ACCESSID', accesskey = '******', endpoint= '$ODPS_ENDPOINT', project_name = 'example_project', schema_name = '', table_name = 'example_table', quota_name = '', compression_code = '') into table t1;Import data from a local file
Example 1: Import data from a local file to a table in OceanBase Database.
Open a terminal or command prompt window and enter the following command to start the client.
obclient --local-infile -hxxx.xxx.xxx.xxx -P2881 -uroot@mysql001 -p****** -A -DtestThe return result is as follows:
Welcome to the OceanBase. Commands end with ; or \g. Your OceanBase connection id is 3221719526 Server version: OceanBase 4.2.2.0 (r100000022023121309-f536833402c6efe9364d5a4b61830a858ef24d82) (Built Dec 13 2023 09:58:18) Copyright (c) 2000, 2018, OceanBase and/or its affiliates. All rights reserved. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. obclient [test]>Notice
To use the
LOAD DATA LOCAL INFILEfeature, you must use OBClient V2.2.4 or later. If you do not have OBClient of a specific version, you can also use MySQL client to connect to the database.In the client, execute the
LOAD DATA LOCAL INFILEstatement to load the local data file.obclient [test]> LOAD DATA LOCAL INFILE '/home/admin/test_data/tbl1.csv' INTO TABLE tbl1 FIELDS TERMINATED BY ',';The return result is as follows:
Query OK, 3 rows affected Records: 3 Deleted: 0 Skipped: 0 Warnings: 0
Example 2: Directly import a compressed file by setting COMPRESSION.
LOAD DATA LOCAL INFILE '/your/file/lineitem.tbl.gz' INTO TABLE lineitem COMPRESSION GZIP FIELDS TERMINATED BY '|';Example 3: Specify a partition for direct load by using PARTITION.
- Specify a partition for direct load
load data /*+ direct(true,0) parallel(2) load_batch_size(100) */ infile "$FILE_PATH" into table t1 partition(p0, p1) fields terminated by '|' enclosed by '' lines starting by '' terminated by '\n';- Specify a subpartition for direct load
load data /*+ direct(true,0) parallel(2) load_batch_size(100) */ infile "$FILE_PATH" into table t1 partition(p0sp0, p1sp1) fields terminated by '|' enclosed by '' lines starting by '' terminated by '\n';Import data from an OSS file
Example 1: Enable direct load by using the
direct(bool, int)hint. The direct load file is stored in OSS.load data /*+ parallel(1) direct(false,0)*/ remote_oss infile 'oss://antsys-oceanbasebackup/backup_rd/xiaotao.ht/lineitem2.tbl?host=***.oss-cdn.***&access_id=***&access_key=***' into table lineitem fields terminated by '|' enclosed by '' lines starting by '' terminated by '\n';Import data from a server-side file as a URL external table
Notice
The commands involving IP addresses in the examples have been desensitized. When verifying, replace them with the actual IP address of your machine.
The following example describes how to create an external table in the MySQL mode of OceanBase Database when the external file is stored locally or in the database. The steps are as follows:
Prepare an external file.
Run the following command to create a file named
column_conv.csvin the/home/admin/test_csvdirectory on the machine where the OBServer node is located.[admin@xxx /home/admin/test_csv]# vi column_conv.csvThe content of the file is as follows:
1,short,short 2,long_text,long_text 3,long_text,long_textSet the import file path.
Notice
For security reasons, you can only modify the
secure_file_privsystem variable by executing an SQL statement through a local Unix socket. For more information, see secure_file_priv.Run the following command to log in to the machine where the OBServer node is located.
ssh admin@10.10.10.1Run the following command to connect to the
mysql001tenant through a local Unix socket.obclient -S /home/admin/oceanbase/run/sql.sock -uroot@mysql001 -p******Run the following SQL statement to set the import path to
/home/admin/test_csv.SET GLOBAL secure_file_priv = "/home/admin/test_csv";
Reconnect to the
mysql001tenant.Here is an example:
obclient -h10.10.10.1 -P2881 -uroot@mysql001 -p****** -A -Ddb_testCreate a table named
test_tbl1.CREATE TABLE test_tbl1(col1 VARCHAR(5), col2 VARCHAR(5), col3 VARCHAR(5));Use the
LOAD DATAstatement to import data into thetest_tbl1table as a URL external table and specify error diagnostics.LOAD DATA FROM FILES( LOCATION = '/home/admin/test_csv', FORMAT = ( TYPE = 'csv', FIELD_DELIMITER = ','), PATTERN = 'column_conv.csv') INTO TABLE test_tbl1 LOG ERRORS REJECT LIMIT UNLIMITED;The return result is as follows:
Query OK, 1 row affected, 2 warnings Records: 1 Deleted: 0 Skipped: 0 Warnings: 2View the Warnings.
SHOW warnings;The return result is as follows:
+---------+------+----------------------------------------------------------------------------------------------------------------------+ | Level | Code | Message | +---------+------+----------------------------------------------------------------------------------------------------------------------+ | Warning | 1406 | fail to scan file column_conv.csv at line 2 for column "db_test"."test_tbl1"."col2", error: Data too long for column | | Warning | 1406 | fail to scan file column_conv.csv at line 3 for column "db_test"."test_tbl1"."col2", error: Data too long for column | +---------+------+----------------------------------------------------------------------------------------------------------------------+ 2 rows in setView the data in the
test_tbl1table.SELECT * FROM test_tbl1;The return result is as follows:
+------+-------+-------+ | col1 | col2 | col3 | +------+-------+-------+ | 1 | short | short | +------+-------+-------+ 1 row in set
References
- For more information about connecting to OceanBase Database, see Overview of connection methods.
- For more information about using the
LOAD DATAstatement, see Import data by using the LOAD DATA statement - For more information about using the
LOAD DATAstatement for direct load, see Import data by using the LOAD DATA statement for direct load