Export data by using OUTFILE statements

2023-08-28 09:28:41  Updated

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.

Note

Alibaba 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: specifies the separator between field values. For example, TERMINATED BY ',' specifies that field values are separated with commas (,).
  • ENCLOSED BY: specifies the symbol for enclosing field values. For example, ENCLOSED BY '"' specifies that character values are enclosed within a pair of double quotation marks (""). If the OPTIONALLY keyword is used, only values of the string type are enclosed within the specified symbols.
  • ESCAPED BY: specifies the escape character. For example, ESCAPED BY '*' indicates that the asterisk (*) but not the backslash (\) is used as the escape character. The backslash (\) is the default escape character.
... 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.
  • STARTING BY: specifies the character to start each line.
  • TERMINATED BY: specifies the character to end each line.
... 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.

Note

If no column following the SELECT clause uses any aggregate function, the columns following the SELECT clause must be specified after the GROUP BY clause.

... 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.
  • ASC: indicates an ascending order.
  • DESC: indicates a descending order.
... ORDER BY col1,col2 DESC ...

Examples

The following takes exporting data to a local device as an example to describe how to export data.

  1. Create a table named tbl1 in the test database in the mysql001 tenant 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 set
    
  2. Set a path for the exported file.

    Log on to the tenant as an administrator (root for a MySQL tenant and SYS for an Oracle tenant), and set the system variable secure_file_priv to 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.

    1. Log on to the OBServer node to connect to.

      [xxx@xxx /home/admin]# ssh admin@xxx.xxx.xxx.xxx
      
    2. Use the OBClient to connect to the mysql001 tenant in direct connection mode.

      obclient -hxxx.xxx.xxx.xxx -P2881 -uroot@mysql001 -p****** -A
      

      Alternatively, run the following command to connect to the tenant mysql001 by using a local Unix Socket connection.

      obclient -S /home/admin/oceanbase/run/sql.sock -uroot@mysql001 -p******
      
    3. Set the export path to /home/admin.

      obclient [(none)]> SET GLOBAL secure_file_priv = "/home/admin";
      Query OK, 0 rows affected
      
  3. After you reconnect to the database, execute the SELECT INTO OUTFILE statement to export data from the tbl1 table. The name of the exported file is tbl1.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 affected
    
  4. Log on to the OBServer node and check the information about the exported file in the /home/admin directory 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.

Contact Us