This topic describes how to create an external table by using SQL statements.
Overview
An external table is a logical table object. Its data is stored in an external storage system rather than in the database.
For more information about external tables, see Overview.
Prerequisites
Before you create an external table, make sure that:
You have connected to OceanBase Database. For more information about how to connect to the database, see Overview of connection methods.
Note
The tenant mode to which the logged-in tenant belongs can be confirmed by the
systenant through a query on theoceanbase.DBA_OB_TENANTSview.You have the
CREATEprivilege. You can view the privileges of the current user by following the steps in View user privileges.
Considerations
An external table can only be used for query operations, and does not support DML operations.
When you query an external table, if the external file accessed by the external table has been deleted, the system does not return an error, but returns an empty result set.
If the external storage system that manages the file accessed by the external table becomes unavailable, an error is returned when you query the external table.
External tables store data in external data sources. Therefore, query operations involve factors such as network and file systems, which may affect query performance. Make sure to select an appropriate data source and optimize strategies when you create external tables to improve query efficiency.
Create an external table syntax
The CREATE EXTERNAL TABLE statement typically has the following syntax:
CREATE EXTERNAL TABLE table_name (column_options [AS (metadata$filecol{N})])
LOCATION = '<string>'
FORMAT = (format_options)
[PATTERN = '<regex_pattern>'];
;
For more information about the CREATE EXTERNAL TABLE statement, see CREATE EXTERNAL TABLE.
Parameter description:
table_name: The name of the external table.
column_options: Column definitions of the external table, including column names and data types.
[AS (metadata$filecol{N})]: This is an optional parameter that allows you to manually define column mappings.Note
By default, data columns in an external file are automatically mapped to the columns defined in the external table in the same order. For example, the first column in the external table corresponds to the first column of data in the external file. If the order of columns in the external file differs from that in the external table, you can use the `metadata$filecol{N}` pseudo column to specify that the Nth column in the external file corresponds to a column in the external table. Note that the columns in the file are numbered starting from 1.
LOCATION: specifies the path where the external file is stored.FORMAT: specifies the format of the external file.PATTERN: specifies a regular pattern string to filter files in theLOCATIONdirectory.
Define the external table name
When you create an external table, you must name it. We recommend that you use specific naming conventions or prefixes to distinguish external tables from regular tables when naming external tables. For example, you can add the suffix _csv to the name of an external table.
Here is an example:
When you create an external table that stores student information, you can name it students_csv.
CREATE EXTERNAL TABLE students_csv external_options
Notice
The preceding SQL statement cannot be executed because no other attributes of the external table are specified.
Define columns
You cannot define constraints, such as DEFAULT, NOT NULL, UNIQUE, CHECK, PRIMARY KEY, and FOREIGN KEY, for columns of an external table.
The column types supported by external tables are the same as those supported by regular tables. For more information about the data types supported in MySQL mode of OceanBase Database, see Data types.
Define LOCATION
The LOCATION option specifies the path where the files of the external table are stored. Generally, the data files of an external table are stored in a dedicated directory, which can contain subdirectories. When you create an external table, the system automatically collects all files in the directory that you specified.
OceanBase Database supports the following two path formats:
Local path:
LOCATION = '[file://] local_file_path'Notice
In a scenario that uses the local path format, you must set the system variable
secure_file_privto specify the path that can be accessed. For more information, see secure_file_priv.Remote path:
LOCATION = '{oss|cos}://$ACCESS_ID:$ACCESS_KEY@$HOST/remote_file_path'You must specify the
$ACCESS_ID,$ACCESS_KEY, and$HOSTparameters for accessing Alibaba Cloud OSS or Tencent Cloud COS. The sensitive access information is encrypted and stored in the system tables of the database.
Notice
When you use an object storage path, separate the parameters of the path with the & character. Make sure that the values of the parameters you entered contain only uppercase and lowercase letters, digits, and the /-_$+= and wildcard characters. If you enter characters other than the aforesaid ones, the setting may fail.
Define FORMAT
The FORMAT option specifies the format of external files. The following parameters are supported:
TYPE: specifies the type of external files. Only the CSV file type is supported.LINE_DELIMITER: specifies the line delimiter for CSV files. The default value isLINE_DELIMITER='\n'.FIELD_DELIMITER: specifies the field delimiter for CSV files. The default value isFIELD_DELIMITER='\t'.ESCAPE: specifies the escape character for CSV files, which can be only 1 byte in length. The default value isESCAPE ='\'.FIELD_OPTIONALLY_ENCLOSED_BY: specifies the characters that enclose field values in CSV files. The default value is an empty string.ENCODING: specifies the character set encoding format of files. For more information about supported character sets in MySQL mode, see Character sets. If this parameter is not specified, the default value UTF8MB4 is used.NULL_IF: specifies the strings that are to be treated asNULL. The default value is an empty string.SKIP_HEADER: specifies the number of lines to skip.SKIP_BLANK_LINES: specifies whether to skip blank lines. The default value isFALSE, which specifies not to skip blank lines.TRIM_SPACE: specifies whether to remove the leading and trailing spaces of fields. The default value isFALSE, which specifies not to remove the leading and trailing spaces of fields.EMPTY_FIELD_AS_NULL: specifies whether to treat empty strings asNULL. The default value isFALSE, which specifies not to treat empty strings asNULL.
(Optional) Define PATTERN
The PATTERN option specifies a regular pattern string for filtering files in the LOCATION directory. For each file path in the LOCATION directory, if the path matches the pattern string, the external table accesses the file. Otherwise, the external table skips the file. If this parameter is not specified, the external table accesses all files in the LOCATION directory by default. The external table stores the list of files that match the PATTERN in the specified LOCATION path in a database system table. During a scan, the external table accesses external files based on this list. The file list can be updated automatically or manually.
Examples
Notice
The commands in the following examples are desensitized. Replace the IP address with your real IP address when you verify the examples.
The following examples show how to create an external table when the external file is located locally and in OceanBase Database in MySQL mode. The procedure is as follows:
Create an external file.
Execute the following command to create a file named
test_tbl1.csvin the/home/admindirectory of the server where the OBServer node resides.[admin@xxx /home/admin]# vi test_tbl1.csvThe content of the file is as follows:
1,'Emma','2021-09-01' 2,'William','2021-09-02' 3,'Olivia','2021-09-03'Set the path of the imported file.
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 the global variable. For more information, see secure_file_priv.Execute the following command to log in to the server where the OBServer node resides.
ssh admin@10.10.10.1Execute the following command to connect to the
mysql001tenant through a local Unix socket.obclient -S /home/admin/oceanbase/run/sql.sock -uroot@mysql001 -p******Execute the following SQL statement to set the import path to
/home/admin.SET GLOBAL secure_file_priv = "/home/admin";
Reconnect to the
mysql001tenant.Here is an example:
obclient -h10.10.10.1 -P2881 -uroot@mysql001 -p****** -A -DtestExecute the following SQL statement to create an external table named
test_tbl1_csv.CREATE EXTERNAL TABLE test_tbl1_csv ( id INT, name VARCHAR(50), c_date DATE ) LOCATION = '/home/admin' FORMAT = ( TYPE = 'CSV' FIELD_DELIMITER = ',' FIELD_OPTIONALLY_ENCLOSED_BY ='\'' ) PATTERN = 'test_tbl1.csv';Execute the following SQL statement to view the data in the external table named
test_tbl1_csv.SELECT * FROM test_tbl1_csv;The return result is as follows:
+------+---------+------------+ | id | name | c_date | +------+---------+------------+ | 1 | Emma | 2021-09-01 | | 2 | William | 2021-09-02 | | 3 | Olivia | 2021-09-03 | +------+---------+------------+ 3 rows in set