Description
The statement is used to import data from an external source.
Notice
- Tables with triggers are not supported.
- To import data from an external file, you need to have the
FILEprivilege and meet the following requirements:- When you import a file from the server, you must specify the system variable secure_file_priv in advance to indicate the path that is accessible for importing or exporting files.
- When you import a file from the client, you must add the
--local-infile[=1]option when you start the MySQL client or OBClient to enable the data loading feature from the local file system.
The LOAD DATA statement in OceanBase Database supports the following input files:
Server (OBServer) node files: files stored on the OBServer nodes of OceanBase Database. You can use the
LOAD DATA INFILEstatement to load data from server node files to database tables.Client (local) files: files stored in the local file system of the client. You can use the
LOAD DATA LOCAL INFILEstatement to load data from client local files to database tables.OSS files: files stored in the OSS file system. You can use the
LOAD DATA REMOTE_OSS INFILEstatement to load data from OSS files to database tables.
The LOAD DATA statement currently supports importing CSV files. The entire import process can be divided into the following phases:
Parse the file: OceanBase Database reads data from the file based on the specified file name, and reads data from the file in parallel or in series based on the specified parallelism.
Distribute data: As OceanBase Database is a distributed database, data in different partitions may be stored on different OBServer nodes. The
LOAD DATAstatement calculates the destination OBServer node for each parsed data.Insert data: After the data is received by the target OBServer node, an
INSERTstatement is executed locally to insert the data into the corresponding partition.
To import data from an external file, you must have the FILE privilege. You can execute the GRANT FILE ON *.* TO $user_name; statement to grant the FILE privilege to the user specified by $user_name. For example, to grant the privilege to the user named admin, you would execute the GRANT FILE ON *.* TO admin; statement.
Syntax
LOAD DATA
[/*+ PARALLEL(N) [load_batch_size(M)] [APPEND | direct(bool, int, [load_mode])] */]
[REMOTE_OSS | LOCAL] INFILE 'file_name'
INTO TABLE table_name
[{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'
Parameters
| Parameter | Description |
|---|---|
| parallel(N) | The degree of parallelism for loading data. The default value of N is 4. |
| load_batch_size(M) | The batch size for inserting data. The default value of M is 100. We recommend that you set this parameter to a value within the range of [100,1000]. |
| APPEND | direct() | The hint to enable direct load.
LOAD DATA statement to enable direct load, see Import data or files by using the LOAD DATA statement. |
| REMOTE_OSS | LOCAL | Optional.
|
| file_name | The path and name of the input file. The format of file_name is described as follows:
NoteBefore you import a file from Alibaba Cloud OSS, make sure that the following conditions are met:
|
| table_name | The name of the table to which the imported data is to be loaded. The table can be a partitioned or non-partitioned table. |
| FIELDS | COLUMNS | The format of fields.
|
| LINES STARTING BY | The starting delimiter of lines. |
| LINES TERMINATED BY | The ending delimiter of lines. |
| IGNORE number { LINES | ROWS } | The number of rows to be ignored at the beginning of the file. LINES indicates rows in the file, and ROWS indicates rows separated by field separators. By default, the input file is parsed to match the columns in the table. If the input file does not contain all the columns in the table, the missing columns are filled with default values, which are generated based on the following rules:
|
| column_name_var | The name of the column to be imported. |
Examples
Example 1: Import data from a file on the server (OBServer node)
Set the global security path.
Notice
For security reasons, when you set the system variable
secure_file_priv, you can connect to the database only through a local socket to execute the SQL statement that modifies this global variable. For more information, see secure_file_priv.obclient> SET GLOBAL secure_file_priv = "/";Log out.
Note
After you set the
secure_file_privvariable, you must execute\qto log out for the setting to take effect.obclinet> \qThe return result is as follows:
ByeReconnect to the database and use the
LOAD DATAstatement to import data.Perform a regular import.
obclient> LOAD DATA INFILE '/home/admin/test.csv' INTO TABLE t1;Use the
APPENDhint to enable direct load.LOAD DATA /*+ PARALLEL(4) APPEND */ INFILE '/home/admin/test.csv' INTO TABLE t1;
Example 2: Import data from an OSS file
Notice
When you use an object storage path, parameters in the object storage path are separated by & characters. Make sure that the values of the parameters you enter contain only uppercase and lowercase letters, numbers, and the following characters: \/-_$+=. If you enter characters other than the preceding ones, the setting may fail.
Use the direct(bool, int) hint to enable direct load, and import the file from Alibaba Cloud OSS.
LOAD DATA /*+ direct(true,1024) parallel(16) */ REMOTE_OSS INFILE 'oss://antsys-oceanbasebackup/backup_rd/xiaotao.ht/lineitem2.tbl?host=***.oss-cdn.***&access_id=***&access_key=***' INTO TABLE tbl1 FIELDS TERMINATED BY ',';
Example 3: Import data from a local client file
Execute the following statements to import data from a local file to a table in OceanBase Database.
Open the terminal or command prompt window, and enter the following command to start the client.
obclient --local-infile -hxxx.xxx.xxx.xxx -P2881 -usys@oracle001 -p******The return result is as follows:
Welcome to the OceanBase. Commands end with ; or \g. Your OceanBase connection id is 3221548006 Server version: OceanBase 4.2.2.0 (r100000032024010510-75c47d4be18a399e13c5309de1a81da5caf4e7c0) (Built Jan 5 2024 10:17:55) 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 [SYS]>Notice
To use the
LOAD DATA LOCAL INFILEfeature, use OBClient V2.2.4 or later.In the client, execute the
LOAD DATA LOCAL INFILEstatement to load the local data file.obclient [SYS]> 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
References
- For more information about how to use the
LOAD DATAstatement, see Import data by using the LOAD DATA statement. - For more information about direct load using the
LOAD DATAstatement, see Import data by using the LOAD DATA statement.