LOAD DATA
Description
LOAD DATA imports data from a text file into a table. V1.4.0 supports two file locations:
LOAD DATA INFILE: Reads a file from a filesystem accessible to the seekdb server.LOAD DATA LOCAL INFILE: Reads a local file through the client, which sends its contents to seekdb.
Syntax
LOAD DATA
[LOCAL] INFILE 'file_name'
[REPLACE | IGNORE]
INTO TABLE table_name
[PARTITION (partition_name [, partition_name] ...)]
[COMPRESSION [=] {AUTO | NONE | GZIP | DEFLATE | ZSTD}]
[CHARACTER SET {binary | utf8mb4}]
[{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_or_user_variable
[, column_name_or_user_variable] ...)]
[SET column_name = {expression | DEFAULT}
[, column_name = {expression | DEFAULT}] ...]
[LOGFILE [=] 'log_file_name']
[REJECT LIMIT [=] integer]
[BADFILE [=] 'bad_file_name'];
Parameters
| Parameter | Description |
|---|---|
LOCAL | Reads a file from the client's local filesystem. If omitted, seekdb reads from the server filesystem. Specifying LOCAL has the same duplicate-key behavior as specifying IGNORE. |
file_name | The input file path and name. Server-side imports are restricted by the secure_file_priv system variable. |
REPLACE | When a primary-key or unique-key conflict occurs, replaces the existing row with the row from the input file. |
IGNORE | When a primary-key or unique-key conflict occurs, ignores the conflicting row from the input file. |
table_name | The target table. It cannot have an INSERT or UPDATE trigger. |
PARTITION | Imports data only into the listed partitions or subpartitions. Separate multiple names with commas. |
COMPRESSION | The compression format of the input file. AUTO detects the format from the filename extension. You can also specify NONE, GZIP, DEFLATE, or ZSTD. |
CHARACTER SET | The input file character set. Valid values are binary and utf8mb4. |
FIELDS, COLUMNS | Specifies field delimiters, enclosure characters, and escape characters. FIELDS and COLUMNS are equivalent. |
LINES | Specifies the line prefix and line terminator. |
IGNORE number LINES | Skips a number of lines at the beginning of the file, commonly to skip a header row. ROWS and LINES are equivalent. |
| Column or user-variable list | Maps input fields, in order, to target columns or user variables. |
SET | Assigns an expression, user variable, or DEFAULT to a target column. |
LOGFILE | Specifies the import log file. |
REJECT LIMIT | Specifies the number of data-error rows allowed. |
BADFILE | Specifies a file in which to record rejected rows. |
Considerations
- A server-side import requires the
FILEprivilege and theINSERTprivilege on the target table. The file path must satisfysecure_file_priv. - A client-side import requires the
INSERTprivilege on the target table, and the client connection must enable local file loading. For example, start the MySQL client with--local-infile=1. LOAD DATA LOCAL INFILEhandles duplicate keys withIGNOREsemantics.\NrepresentsNULL.- Field and line delimiters currently support only single-byte ASCII characters.
Examples
Import a server-side file
The following statement imports /data/student.csv on the server into student and skips its header row:
LOAD DATA INFILE '/data/student.csv'
INTO TABLE student
CHARACTER SET utf8mb4
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 LINES;
Import a client-side file
Start a client that supports local file loading:
mysql --local-infile=1 -h127.0.0.1 -P2881 -uroot -p
Run the import:
LOAD DATA LOCAL INFILE '/home/user/student.csv'
INTO TABLE student
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\n';
Transform data with a user variable
LOAD DATA LOCAL INFILE '/home/user/student.csv'
INTO TABLE student
FIELDS TERMINATED BY ','
(id, name, @score)
SET score = CAST(@score AS SIGNED);