This topic describes how to use a SELECT INTO OUTFILE statement to export data. The SELECT INTO OUTFILE statement allows you to specify the fields to be exported, and is preferred when primary key fields do not need to be exported. You can use this statement in combination with the LOAD DATA INFILE statement to facilitate data import and export.
Background
OceanBase Database is compatible with the SELECT INTO OUTFILE statement.
| Mode | Recommended OceanBase Database version | Recommended client |
|---|---|---|
| MySQL mode | V2.2.40 and later | MySQL client and OBClient |
| Oracle mode | V2.2.40 and later | OBClient |
Notice
To import data into and export data from OceanBase Database, the client must be connected to the OceanBase Database instance directly.
Syntax
SELECT column_list_option
INTO OUTFILE file_route_option
[format_of_field_option]
[start_and_end_option]
FROM table_name_list
[WHERE where_conditions]
[GROUP BY group_by_list [HAVING having_search_conditions]]
[ORDER BY order_expression_list]
column_list_option:
column_name[,column_name]...
file_route_option:
'/path/file'
| 'oss://$PATH/$FILENAME/?host=$HOST&access_id=$ACCESS_ID&access_key=$ACCESSKEY'
format_of_field_option:
{FIELDS | COLUMNS}
[TERMINATED BY 'string']
[[OPTIONALLY] ENCLOSED BY 'char']
[ESCAPED BY 'char']
start_and_end_option:
LINES
[STARTING BY 'string']
[TERMINATED BY 'string']
| Parameter | Required | Description | Example |
|---|---|---|---|
| column_list_option | Yes | Column options for the export. To select all data, use an asterisk (*). column_name: The name of the column to be exported. |
SELECT col1,col2,col3 ... |
| file_route_option | Yes | The path of the exported file. Alibaba Cloud Object Storage Service (OSS) is supported.
NoteAlibaba Cloud OSS does not support files larger than 5 GB. When you export a file larger than 5 GB to Alibaba Cloud OSS, it is split into multiple smaller files. |
... INTO OUTFILE '/home/admin/student.sql' ... |
| format_of_field_option | No | Field format options for the export. You can use the FIELDS or COLUMNS clause to specify the format of each field in the output file.
|
... TERMINATED BY ',' ENCLOSED BY '"' ... |
| start_and_end_option | No | Start and end character options for exported data rows. LINES specifies the characters to enclose each line in the output file.
|
... LINES TERMINATED BY '\n' ... specifies that a row ends with a line feed. |
| FROM table_name_list | Yes | The object from which data is selected. | ... FROM tbl1,tbl2 ... |
| WHERE where_conditions | No | The filter condition. Only data that meets the condition is returned in the query results. | ... WHERE col1 > 100 ... |
| GROUP BY group_by_list | No | The grouping field, which is usually used together with aggregate functions.
NoteIf no column following the |
... GROUP BY col1,col2 ... |
| HAVING having_search_conditions | No | Filters the grouped data. The HAVING clause is similar to the WHERE clause, but the HAVING clause can reference an aggregate function such as SUM and AVG. |
... HAVING SUM(col1) < 160 ... |
| ORDER BY order_expression_list | No | Sorts the query results by one or multiple columns in ascending (ASC) or descending (DESC) order. If you do not specify ASC or DESC, the default value ASC is used.
|
... ORDER BY col1,col2 DESC ... |
Examples
The following takes exporting data to a local device as an example to describe how to export data.
Create a table named
tbl1in thetestdatabase in themysql001tenant and insert data into the table.obclient [test]> CREATE TABLE tbl1(col1 INT PRIMARY KEY,col2 varchar(128),col3 INT); Query OK, 0 rows affected obclient [test]> INSERT INTO tbl1 VALUES(1,'one',80),(2,'two',90),(3,'three',100); Query OK, 3 rows affected Records: 3 Duplicates: 0 Warnings: 0 obclient [test]> SELECT * FROM tbl1; +------+-------+------+ | col1 | col2 | col3 | +------+-------+------+ | 1 | one | 80 | | 2 | two | 90 | | 3 | three | 100 | +------+-------+------+ 3 rows in setSet a path for the exported file.
Log on to the tenant as an administrator (
rootfor a MySQL tenant andSYSfor an Oracle tenant), and set the system variablesecure_file_privto specify the path to be accessed during file import or export.Notice
For security reasons, when you set the system variable
secure_file_priv, you can use only the local client to execute the SQL statement that modifies the global variable. For more information, see secure_file_priv.Log on to the OBServer node to connect to.
[xxx@xxx /home/admin]# ssh admin@xxx.xxx.xxx.xxxUse the OBClient to connect to the
mysql001tenant in direct connection mode.obclient -hxxx.xxx.xxx.xxx -P2881 -uroot@mysql001 -p****** -AAlternatively, run the following command to connect to the tenant
mysql001by using a local Unix Socket connection.obclient -S /home/admin/oceanbase/run/sql.sock -uroot@mysql001 -p******Set the export path to
/home/admin.obclient [(none)]> SET GLOBAL secure_file_priv = "/home/admin"; Query OK, 0 rows affected
After you reconnect to the database, execute the
SELECT INTO OUTFILEstatement to export data from thetbl1table. The name of the exported file istbl1.sql. The field values are separated with a comma (,). The values of the string type are enclosed within a pair of double quotations marks ("). The end symbol is a line feed.obclient [test]> SELECT * INTO OUTFILE '/home/admin/tbl1.sql' FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY '\n' FROM tbl1; Query OK, 3 rows affectedLog on to the OBServer node and check the information about the exported file in the
/home/admindirectory on your local device.[xxx@xxx /home/admin]# cat tbl1.sql 1,"one",80 2,"two",90 3,"three",100
More information
You can use the LOAD DATA statement to import the files that are exported by using SELECT INTO OUTFILE. For more information, see Import data by using the LOAD DATA statement.