Starting from OceanBase Database V4.3.5 BP1 (MySQL-compatible mode/Oracle-compatible mode), you can directly read data from CSV, Parquet, and ORC files by using the SELECT or LOAD DATA statement. Starting from V4.3.5 BP1, you can also read data from an ODPS data source by using the SOURCE() or LOAD DATA statement. This feature is suitable for the following typical AP scenarios:
- Ad-hoc data exploration
- Rapid data loading
A URL external table is a metadata-free external data access capability provided by OceanBase Database. You can directly specify the data file path and format in the SELECT or LOAD DATA statement, and the system dynamically parses the data and returns the result. You do not need to create an external table in advance.
Supported data sources and formats
Data source type |
Access method |
Supported format |
Description |
|---|---|---|---|
| Local / object storage file | FILES() |
CSV / Parquet / ORC | Supports HDFS, OSS, S3, object storage systems compatible with the S3 protocol, and local files |
| MaxCompute (ODPS) | SOURCE() |
ODPS table | Directly connects to the ODPS Tunnel without requiring export |
Notice
- CSV query results are of the
VARCHARtype by default (you can use theCASTfunction to convert the data type later). - Parquet / ORC automatically infers the schema, including nested structures.
- The amount of data that can be read in one operation is subject to the cluster resources, external storage, and network conditions. The documentation does not define a fixed upper limit on the number of files or the total size.
- ODPS does not support the
FILES()function. You must use theSOURCE()function (currently, the syntax is mainly based on theLOAD DATAstatement in MySQL-compatible mode). - URL external tables (
FILES()/Location URL) are supported in both MySQL-compatible mode and Oracle-compatible mode. For specific syntax, see the SQL reference of each mode.
Query external data
General syntax
Method 1: Use the location URL format (concise)
SELECT * FROM 'outfiles/'
(
FORMAT (TYPE = 'format_type', FIELD_DELIMITER = ','),
PATTERN = 'regex'
);
Method 2: Use the table function format (flexible and recommended)
SELECT * FROM FILES(
LOCATION = 'outfiles/',
FORMAT (TYPE = 'format_type', FIELD_DELIMITER = ','),
PATTERN = 'regex'
);
'outfiles/': Specifies the path where the external files are stored. Subdirectories can be scanned recursively.FORMAT: Specifies the format and parsing options for the external files.PATTERN: Specifies a regular expression pattern to filter files in theLOCATIONdirectory. If not specified, all files are read by default.
Query examples in different formats
CSV files
General syntax
The URL external table supports two equivalent syntax forms for reading CSV files. TYPE = 'CSV' must be specified.
Location URL form (concise)
SELECT * FROM 'outfiles/'
(
FORMAT (
TYPE = 'CSV',
FIELD_DELIMITER = ','
),
PATTERN = 'data$'
);
Table function form (flexible and recommended)
SELECT * FROM FILES(
LOCATION = 'outfiles/',
FORMAT (
TYPE = 'CSV',
FIELD_DELIMITER = ','
),
PATTERN = 'data$'
);
Note
outfiles/specifies the path where the external table files are stored, and subdirectories are scanned recursively.
FORMATspecifies the external file format as CSV and the related parsing options.
PATTERNspecifies the regular expression pattern for filtering files in theLOCATIONdirectory; if not specified, all files are read.
- Important: When you use a URL external table to read CSV files, the data type of all columns is VARCHAR by default (you can use CAST to convert them to other types later).
The LOCATION parameter can be a path in string form (for example, 'oss://bucket/path/' or 'outfiles/'), or the name of an existing Location object (referenced by using @location_name; for the syntax, see the SQL reference of the current version).
Example 1: Create a persistent external table and reference a Location object
This form is suitable for external tables that need to be used for a long time. The table metadata is stored in the database.
-- First, define a Location object named my_hdfs_loc.
CREATE LOCATION my_hdfs_loc URL = 'hdfs://namenode:8020/data/';
-- Reference this Location object when creating the external table.
CREATE EXTERNAL TABLE ex_t1 (
c1 INT,
c2 INT,
c3 INT
)
LOCATION = @my_hdfs_loc
FORMAT (
TYPE = 'csv',
FIELD_DELIMITER = ',',
LINE_DELIMITER = '\n'
);
Example 2: Reference a Location object in a temporary query by using FILES()
This form is suitable for one-off analysis of external files. No persistent table is required.
-- Assume that a Location object named my_hdfs_loc already exists.
CREATE LOCATION my_hdfs_loc URL = 'hdfs://namenode:8020/data/';
-- Reference the Location object directly in the FILES table function.
SELECT * FROM FILES(
LOCATION = @my_hdfs_loc, -- Use the @ prefix to reference a defined Location.
FORMAT (TYPE = 'PARQUET'),
PATTERN = '.*\\.parquet$' -- Match all .parquet files.
);
Example 3: Specify the path directly without a Location object
This form is suitable for simple or temporary scenarios where the path does not need to be reused. You can write the URL or path directly in the query, and two equivalent syntax forms are supported.
-- Method 1: Use the path as the table name (concise).
SELECT * FROM '/data/'
(
FORMAT (TYPE = 'CSV', FIELD_DELIMITER = ',', SKIP_BLANK_LINES = TRUE),
PATTERN = '^datafiles.*\\.csv$' -- Note: PATTERN is a regular expression.
);
-- Method 2: Use the FILES table function (explicit, recommended for complex configurations).
SELECT * FROM FILES(
LOCATION = '/data/',
FORMAT (TYPE = 'CSV', FIELD_DELIMITER = ',', SKIP_BLANK_LINES = TRUE),
PATTERN = '^datafiles.*\\.csv$'
);
Support for header parsing
When you access a CSV file by using the FILES() function, if the table structure (column names and column count) is not explicitly defined, the system automatically infers the table structure:
- Column count: The first line of the CSV file in the specified directory is sampled.
- Column names: By default, columns are named
'c1','c2', and so on in order.
Because the first line of many CSV files contains column names (the header) and the subsequent lines contain data, OceanBase Database provides the PARSE_HEADER configuration parameter to control whether the first line is parsed as column names.
- If the first line of the CSV file contains column names: Set
PARSE_HEADER = TRUE. - If the first line of the CSV file contains actual data: Keep
PARSE_HEADER = FALSE(default).
-- Basic query. The first line of the CSV file contains data (no header), so the default column names c1, c2... are used.
SELECT * FROM FILES(
LOCATION = 'oss://my-bucket/logs/',
FORMAT (
TYPE = 'CSV',
FIELD_DELIMITER = ',',
SKIP_BLANK_LINES = TRUE
),
PATTERN = 'user_log_202504.*\\.csv$'
);
-- The first line of the CSV file contains column names (such as name, age, and city), so enable PARSE_HEADER.
SELECT * FROM FILES(
LOCATION = '/data/sales.csv',
FORMAT (
TYPE = 'CSV',
FIELD_DELIMITER = ',',
PARSE_HEADER = TRUE -- Parse the first line as column names.
)
);
Limitations: PARSE_HEADER and SKIP_HEADER are mutually exclusive and cannot be used together.
Parquet files (automatic schema inference)
-- Location URL form
SELECT * FROM 'outfiles/'
(
FORMAT (TYPE = 'PARQUET'),
PATTERN = 'data$'
);
-- Table function form
SELECT * FROM FILES(
LOCATION = 'outfiles/',
FORMAT (TYPE = 'PARQUET'),
PATTERN = 'data$'
);
'outfiles/'specifies the path where the external files are stored, and subdirectories are scanned recursively.FORMATspecifies the file format asPARQUET.PATTERNis used to filter files (regular expression match).
-- Practical example: Read Parquet partition files from S3.
SELECT * FROM FILES(
LOCATION = 's3://analytics-bucket/events/',
FORMAT (TYPE = 'PARQUET'),
PATTERN = 'part-.*\\.parquet$'
);
-- Read all Parquet files whose names start with "datafiles" under the '/data/' path.
SELECT * FROM '/data/' (
FORMAT = (TYPE = 'PARQUET'),
PATTERN = 'datafiles$'
);
SELECT * FROM FILES(
LOCATION = '/data/',
FORMAT = (TYPE = 'PARQUET'),
PATTERN = 'datafiles$'
);
ORC files
-- URL Location form
SELECT * FROM 'outfiles/'
(
FORMAT (TYPE = 'ORC'),
PATTERN = 'data$'
);
-- Table function form
SELECT * FROM FILES(
LOCATION = 'outfiles/',
FORMAT (TYPE = 'ORC'),
PATTERN = 'data$'
);
'outfiles/'specifies the path, and subdirectories are supported.FORMATspecifies the format asORC.PATTERNis used to filter files.
-- Example: Read an ORC file from HDFS.
SELECT * FROM 'hdfs://mycluster/data/orc/'
(
FORMAT (TYPE = 'ORC'),
PATTERN = 'clicks_202504.*'
);
ODPS tables (directly connected to MaxCompute)
An ODPS data source is not in file format and does not support FILES(). Only SOURCE() is supported:
SELECT * FROM SOURCE(
TYPE = 'ODPS',
ACCESSID = 'LTAI5tXXXXXX',
ACCESSKEY = 'xxxxxxxxxxxxxx',
ENDPOINT = 'http://service.cn-hangzhou.maxcompute.aliyun.com/api',
TUNNEL_ENDPOINT = 'http://dt.cn-hangzhou.maxcompute.aliyun.com',
PROJECT_NAME = 'sales_analytics',
TABLE_NAME = 'user_behavior'
);
Import external data (LOAD DATA)
You can efficiently import external data into an internal table of OceanBase Database. Parallel import, direct write, and other optimizations are supported.
Syntax
LOAD DATA
[/*+ INSERT HINT */]
[REPLACE | IGNORE]
FROM {
<url_table_function_expr> |
( SELECT expression_list FROM <url_table_function_expr> )
}
INTO TABLE table_name
[PARTITION (partition_name1, [partition_name2 ...])]
[(column_name_var [, column_name_var] ...)]
<url_table_function_expr> is as follows:
FILES (
LOCATION = '<string>',
{
FORMAT = (
TYPE = 'CSV',
LINE_DELIMITER = '<string>' | <expr>,
FIELD_DELIMITER = '<string>' | <expr>,
ESCAPE = '<character>' | <expr>,
FIELD_OPTIONALLY_ENCLOSED_BY = '<character>' | <expr>,
ENCODING = 'charset',
NULL_IF = ('<string>' | <expr>, '<string>' | <expr> ...),
SKIP_HEADER = <int>,
SKIP_BLANK_LINES = { TRUE | FALSE },
TRIM_SPACE = { TRUE | FALSE },
EMPTY_FIELD_AS_NULL = { TRUE | FALSE }
)
| FORMAT = ( TYPE = 'PARQUET' | 'ORC' )
},
[PATTERN = '<regex_pattern>']
)
| SOURCE (
TYPE = 'ODPS',
ACCESSID = '<string>',
ACCESSKEY = '<string>',
ENDPOINT = '<string>',
TUNNEL_ENDPOINT = '<string>',
PROJECT_NAME = '<string>',
SCHEMA_NAME = '<string>',
TABLE_NAME = '<string>',
QUOTA_NAME = '<string>',
COMPRESSION_CODE = '<string>'
)
LOCATIONspecifies the file path, and subdirectories are scanned recursively.Local path: The format is
[file:// | sfile://] local_file_path, and the path must be a subdirectory of the path configured forsecure_file_priv.file://: With this protocol header, files under the same logical path on multiple OBServer nodes are treated as different files and processed separately. Each node independently reads the files under its own local path.sfile://: With this protocol header, when the same logical path is encountered on multiple OBServer nodes, the system deduplicates the path and reads the data only once (which supports load balancing across servers).Notice
- When
sfile://is used, the system checks the consistency of file sizes. If the files under the same logical path have different sizes on different OBServer nodes, the system returns theOB_INVALID_EXTERNAL_FILEerror, to prevent incorrect results caused by inconsistent data. - For V4.4.2, support for the
sfile://protocol header in the local Location path format of theLOAD DATAsyntax is available starting from V4.4.2 BP2.
- When
Remote path: For example,
oss://bucket/path/. Sensitive information such as AccessKey is encrypted and stored in system tables by OceanBase Database.
Note
Sensitive credentials such as AccessKey are encrypted and stored in system tables by OceanBase Database. You can reuse a preconfigured Location object to avoid repeatedly writing them in plaintext in SQL statements (for the syntax of the Location object, see the SQL reference).
Import examples
Import CSV files (all columns or specified columns)
-- Import all columns from data1.csv.
LOAD DATA /*+ DIRECT(TRUE) PARALLEL(2) */
FROM FILES(
LOCATION = 'data/csv',
FORMAT = (
TYPE = 'CSV',
FIELD_DELIMITER = ',',
SKIP_BLANK_LINES = TRUE
),
PATTERN = 'data1.csv'
)
INTO TABLE csv_ex_t1;
-- Import only columns c1 and c2 into the target columns col1 and col2.
LOAD DATA /*+ DIRECT(TRUE) PARALLEL(2) */
FROM (
SELECT c1, c2 FROM FILES(
LOCATION = 'data/csv',
FORMAT = (
TYPE = 'CSV',
FIELD_DELIMITER = ',',
SKIP_BLANK_LINES = TRUE
),
PATTERN = 'data1.csv'
)
)
INTO TABLE csv_ex_t1 (col1, col2);
Import Parquet files
LOAD DATA /*+ DIRECT(TRUE) PARALLEL(2) */
FROM FILES(
LOCATION = 'data/parquet',
FORMAT = (TYPE = 'PARQUET'),
PATTERN = 'data1.parquet'
)
INTO TABLE parquet_ex_t1;
Import ORC files
LOAD DATA /*+ DIRECT(TRUE) PARALLEL(2) */
FROM FILES(
LOCATION = 'data/orc',
FORMAT = (TYPE = 'ORC'),
PATTERN = 'data1.orc'
)
INTO TABLE orc_ex_t1;
Import from ODPS
LOAD DATA
FROM SOURCE (
TYPE = 'ODPS',
ACCESSID = '$ODPS_ACCESSID',
ACCESSKEY = '$ODPS_ACCESSKEY',
ENDPOINT = '$ODPS_ENDPOINT',
PROJECT_NAME = 'example_project',
SCHEMA_NAME = '',
TABLE_NAME = 'example_table',
QUOTA_NAME = '',
COMPRESSION_CODE = ''
)
INTO TABLE odps_ex_t1;
Parameter description
LOCATION path formats
Type |
Format example |
Description |
|---|---|---|
| Local file | file://data/ or /data/ |
secure_file_priv must be configured, and the path must be its subdirectory |
| OSS | oss://bucket/path/ |
AccessKey must be configured on the OBServer node |
| S3 | s3://bucket/path/ |
IAM Role and AccessKey are supported |
| HDFS | hdfs://namenode:8020/path/ |
The Java SDK must be deployed, and Kerberos if it is enabled |
CSV format options (common)
Parameter |
Default value |
Description |
|---|---|---|
FIELD_DELIMITER |
, |
Field delimiter |
PARSE_HEADER |
FALSE |
Whether to use the first line as column names |
SKIP_BLANK_LINES |
FALSE |
Whether to skip blank lines |
EMPTY_FIELD_AS_NULL |
FALSE |
Whether to convert empty fields to NULL |
ENCODING |
utf8 |
File encoding |
Performance hints (LOAD DATA only)
/*+ DIRECT(TRUE) PARALLEL(8) */
DIRECT(TRUE): Bypasses the transaction layer and writes directly to the storage engine (fast, but the data cannot be rolled back).PARALLEL(N): Reads files in parallel (N ≤ the number of files).