Typically, table data in a database is stored within the database's own storage space, while data in an external table is stored in an external storage service. OceanBase Database introduces the feature of external tables, allowing users to directly query data stored in external systems and quickly import external data into OceanBase Database. With the external table feature, OceanBase can connect to and access data in file systems such as HDFS, OSS, and S3, supporting various data file formats including CSV, ORC, and Parquet.
If data is already stored in external locations such as local paths, object storage, or HDFS, and you want to import it with a single SQL statement, you can use the FILES table function to access the external files. For more information, see INSERT INTO SELECT FROM FILES and LOAD DATA FROM FILES.
Considerations
When an external data file directory has a well-structured hierarchy, such as by time, province, or city, consider specifying it as an external partitioned table to enable partition pruning for faster queries. For the syntax to create an external partitioned table, see Create an external table partition in MySQL-compatible mode and Create an external table partition in Oracle-compatible mode.
Before creating an external table, ensure the network between the external data source and the OBServer is connected.
Application scenarios
External tables are a database feature commonly used in analytical business scenarios. Some typical application scenarios include:
- Log analysis: Enterprises generate large volumes of log information daily, including access logs and error logs. These logs are often first stored in object storage. By creating external tables that reference these log files, you can perform complex query operations on these logs quickly, such as counting visits within a specific period or identifying the module with the highest error frequency.
- Historical data analysis: For historical data that needs long-term storage but is not frequently accessed, consider storing it in lower-cost object storage or HDFS. By establishing external tables, you can directly query this data when needed, saving storage space while ensuring data access efficiency.
- Multi-source data integration: Modern enterprise business processes often involve multiple different information systems, each potentially generating data in its own format. Using external tables, you can load data files of various formats (CSV, Parquet, ORC, etc.) from different sources, then execute cross-system joint queries on a unified platform, helping enterprises gain a more comprehensive data insight.
- Data lake analytics: As the data lake architecture becomes increasingly popular, more and more enterprises are building their own data lakes to centrally manage and analyze raw or semi-structured data from various channels. In this case, by creating external tables that reference specific locations in the data lake, you can directly participate in data exploration activities in the data lake, thereby accelerating the decision-making process.
- Lakehouse acceleration: The offline data warehouse in a data lake can provide non-real-time query analysis, but in some scenarios, it may not meet real-time business requirements. By quickly importing data into internal tables using external tables, you can provide query analysis capabilities with better timeliness and performance.
Example
The following example simulates reading and analyzing local log files using external tables.
There is a folder in the local directory containing CSV format files. You can use a partitioned external table to organize these files. The file organization is as follows.
external_table_mock_log ├── 2023-06-01 │ ├── server_log1.csv │ └── server_log2.csv ├── 2023-06-02 │ └── server_log1.csv ├── 2023-06-03 │ ├── server_log1.csv │ ├── server_log2.csv │ └── server_log3.csv └── 2023-07-01 └── server_log1.csvTaking the server_log1.csv file as an example, its content is as follows:
2023-06-01 14:42:37.568624, INTERNAL ERROR, -4007, Not supported 2023-06-01 14:42:38.861356, ITER END, -4008, traverse map failed 2023-06-01 14:42:39.931161, NEED WAIT, -4076, query and update last id fail 2023-06-01 14:42:39.931161, SUCCESS, 0, do flush cache successSet the import file path.
Configure the system variable
secure_file_privto specify the accessible paths for import or export files. For more settings, see secure_file_priv.Notice
For security reasons, when setting the system variable
secure_file_priv, you must connect to the database via a local Unix Socket to execute the SQL statement that modifies this global variable.After connecting to the database, create an auto-partitioned external table to access the directory files. The SQL command is as follows.
obclient> create external table ex_t1 ( time date, errstate varchar(30), errcode int, errcontent varchar(100), date_key date as (substr(substr(metadata$fileurl, instr(metadata$fileurl, '%') + 1), 1, 10)) ) location='/home/admin/external_table_mock_log' FORMAT ( type = 'csv', field_delimiter = ',', SKIP_BLANK_LINES = TRUE ) partition by (date_key) ;The
METADATA$FILEURLcolumn records the path and filename information. For a local file, it returnsip:port%2023-06-01/server_log2.csv.Query the data.
Query external table data.
Specify the value or range of the partitioning key for the query. Partition pruning is performed, and the external table reads only the files in that partition.
obclient> select * from ex_t1 where date_key = '2023-06-01';Query the file list of an external table.
After creating an external table, you can also view its file list through the
ALL_OB_EXTERNAL_TABLE_FILESview.SELECT * FROM oceanbase.ALL_OB_EXTERNAL_TABLE_FILES WHERE TABLE_NAME='ex_t1';
Import data from an external table to an internal table.
You can modify the
default_load_modeparameter to control the data import behavior. For more information, see default_load_mode.Create an internal table.
obclient> create table t1 ( time date, errstate varchar(30), errcode int, errcontent varchar(100), date_key date ) partition by list columns(date_key) (partition p20230601 values in ('2023-06-01'), partition p20230602 values in ('2023-06-02'), partition p20230603 values in ('2023-06-03'), partition p20230701 values in ('2023-07-01') );Import external data into the internal table.
obclient> insert into t1 select * from ex_t1;
- For more information about URL external tables, see URL external tables.
Link to the Data Lake chapter
Capacities such as file external tables, Catalog, and ODPS complement the Data Lake documentation. You can refer to the following documents based on your scenario: