This topic describes how to create and query file-based external tables in MySQL-compatible mode. For complete syntax and descriptions of parameters such as LOCATION, FORMAT, and PROPERTIES, see CREATE EXTERNAL TABLE.
If you only need to temporarily read from an external file without creating a table object, use the FILES table function.
Prerequisites
- You have deployed a cluster, created a MySQL-compatible tenant, and connected to the database.
- You have created a database.
- The current user has the
CREATEprivilege. For more information, see View user privileges. - Local files: The data is placed in a path accessible to the OBServer, and secure_file_priv is configured (modifying this variable usually requires a local Unix Socket connection).
- HDFS / ODPS: The OceanBase Database Java SDK environment has been deployed and configured.
Examples
The following examples fall into two categories: local file-based external tables (accessing CSV files on the OBServer via LOCATION + FORMAT) and ODPS external tables (accessing MaxCompute tables via PROPERTIES, which requires the Java SDK). After either type of external table is created, you can run SHOW CREATE TABLE to view its definition and SELECT to query data.
Create a local CSV file-based external table
Assume a data.csv file exists under /home/admin/oceanbase/ on the local machine, containing the following content.
1,"lin",98
2,"hei",90
3,"ali",95
On the OBServer node, the tenant administrator connects to the MySQL-compatible tenant of the cluster over a local Unix Socket.
A sample connection is as follows:
obclient -S /home/admin/oceanbase/run/sql.sock -uroot@sys -p********For details on how to connect to OceanBase Database over a local Unix Socket, see secure_file_priv.
Configure the path
/home/admin/oceanbase/accessible by the database.SET GLOBAL secure_file_priv = "/home/admin/oceanbase/";After the command executes successfully, you need to restart the session for the changes to take effect.
After reconnecting to the database, create the external table
ext_t3.CREATE EXTERNAL TABLE ext_t3(id int, name char(10),score int) LOCATION = '/home/admin/oceanbase/' FORMAT = ( TYPE = 'CSV' FIELD_DELIMITER = ',' FIELD_OPTIONALLY_ENCLOSED_BY ='"' ) PATTERN = 'data.csv';
After the external table is successfully created, you can use the SHOW CREATE TABLE statement to view its definition, just like a regular table.
SHOW CREATE TABLE ext_t3;
The query result is as follows:
+--------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+--------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| ext_t3 | CREATE EXTERNAL TABLE `ext_t3` (
`id` int(11) GENERATED ALWAYS AS (metadata$filecol1),
`name` char(10) GENERATED ALWAYS AS (metadata$filecol2),
`score` int(11) GENERATED ALWAYS AS (metadata$filecol3)
)
LOCATION='file:///home/admin/oceanbase/'
PATTERN='data.csv'
FORMAT (
TYPE = 'CSV',
FIELD_DELIMITER = ',',
FIELD_OPTIONALLY_ENCLOSED_BY = '"',
ENCODING = 'utf8mb4'
)DEFAULT CHARSET = utf8mb4 ROW_FORMAT = DYNAMIC COMPRESSION = 'zstd_1.3.8' REPLICA_NUM = 1 BLOCK_SIZE = 16384 USE_BLOOM_FILTER = FALSE TABLET_SIZE = 134217728 PCTFREE = 0 |
+--------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set
An external table can also be accessed like a regular table. When querying an external table, the system reads the external file directly through the driver layer of the external table, parses it according to the file format, converts it into internal data types of OceanBase Database, and then returns the data rows. The following example shows how to query the newly created external table ext_t3.
SELECT * FROM ext_t3;
The query result is as follows:
+----+------+-------+
| id | name | score |
+----+------+-------+
| 1 | lin | 98 |
| 2 | hei | 90 |
| 3 | ali | 95 |
+----+------+-------+
3 rows in set
In addition, external tables can be queried together with regular tables. Suppose the current database contains a regular table info with the following data:
+------+--------+------+
| name | sex | age |
+------+--------+------+
| lin | male | 8 |
| hei | male | 9 |
| li | female | 8 |
+------+--------+------+
3 rows in set
The following example shows how to perform a combined query on the external table ext_t3 and the regular table info.
SELECT info.* FROM info, ext_t3 WHERE info.name = ext_t3.name AND ext_t3.score > 90;
The query result is as follows:
+------+--------+------+
| name | sex | age |
+------+--------+------+
| lin | male | 8 |
| li | female | 8 |
+------+--------+------+
2 rows in set
For more information about queries, see Read data.
Create an ODPS Java SDK external table
This example maps the TPC-H table lineitem under the schema tpch_10g of the MaxCompute public dataset bigdata_public_dataset (for the table structure and sample data description, see Query and read/write MaxCompute (ODPS) data in OceanBase Database). Before creating the ODPS external table, you must have deployed the OceanBase Database Java SDK environment on all OBServer nodes and prepared the ACCESSID / ACCESSKEY with MaxCompute read permissions.
Note
If API_MODE is not explicitly specified, tunnel_api is used by default. This applies to public network access or when OceanBase Database and MaxCompute are not in the same VPC. If OceanBase Database and MaxCompute are in the same region's VPC and the Storage API is enabled, you can set API_MODE = 'storage_api' and SPLIT = 'byte' to improve throughput. For parameter descriptions, see ODPS External Table and properties_type_options in CREATE EXTERNAL TABLE.
Connect to a MySQL-compatible tenant as a user with the
CREATEprivilege, and switch to the target database (the example database name istest).USE test;Create the ODPS external table
ext_lineitem. The column definitions must match those of the MaxCompute source table.CREATE EXTERNAL TABLE ext_lineitem ( l_orderkey BIGINT, l_partkey BIGINT, l_suppkey BIGINT, l_linenumber BIGINT, l_quantity DECIMAL(15,2), l_extendedprice DECIMAL(15,2), l_discount DECIMAL(15,2), l_tax DECIMAL(15,2), l_returnflag CHAR(1), l_linestatus CHAR(1), l_shipdate DATE, l_commitdate DATE, l_receiptdate DATE, l_shipinstruct CHAR(25), l_shipmode CHAR(10), l_comment VARCHAR(44) ) PROPERTIES = ( TYPE = 'ODPS', ACCESSID = '***********', ACCESSKEY = '***********', ENDPOINT = 'https://service.cn-hangzhou.maxcompute.aliyun.com/api', PROJECT_NAME = 'bigdata_public_dataset', SCHEMA_NAME = 'tpch_10g', TABLE_NAME = 'lineitem', QUOTA_NAME = '', COMPRESSION_CODE = 'zstd' );If you use the Storage API (in a VPC where the MaxCompute Storage API is enabled), you can add
API_MODE = 'storage_api'andSPLIT = 'byte'toPROPERTIES, and replaceENDPOINTwith the VPC endpoint in the same region (for example,https://service.cn-hangzhou-vpc.maxcompute.aliyun-inc.com/api).View the external table definition to verify that
PROPERTIESand the column mapping have taken effect.SHOW CREATE TABLE ext_lineitem;Query the ODPS external table. The system accesses MaxCompute via the Java SDK and returns the results according to the external table column definitions.
SELECT * FROM ext_lineitem LIMIT 5;The query result is as follows (the first five rows match those of the MaxCompute source table):
+------------+-----------+-----------+--------------+------------+------------------+------------+-------+--------------+--------------+------------+------------+-------------+--------------------+------------+----------------------------------+ | l_orderkey | l_partkey | l_suppkey | l_linenumber | l_quantity | l_extendedprice | l_discount | l_tax | l_returnflag | l_linestatus | l_shipdate | l_commitdate | l_receiptdate | l_shipinstruct | l_shipmode | l_comment | +------------+-----------+-----------+--------------+------------+------------------+------------+-------+--------------+--------------+------------+------------+-------------+--------------------+------------+----------------------------------+ | 1 | 1551894 | 76910 | 1 | 17.00 | 33078.94 | 0.04 | 0.02 | N | O | 1996-03-13 | 1996-02-12 | 1996-03-22 | DELIVER IN PERSON | TRUCK | egular courts above the | | 1 | 673091 | 73092 | 2 | 36.00 | 38306.16 | 0.09 | 0.06 | N | O | 1996-04-12 | 1996-02-28 | 1996-04-20 | TAKE BACK RETURN | MAIL | ly final dependencies: slyly bold| | 1 | 636998 | 36999 | 3 | 8.00 | 15479.68 | 0.10 | 0.02 | N | O | 1996-01-29 | 1996-03-05 | 1996-01-31 | TAKE BACK RETURN | REG AIR | riously. regular, express dep | | 1 | 21315 | 46316 | 4 | 28.00 | 34616.68 | 0.09 | 0.06 | N | O | 1996-04-21 | 1996-03-30 | 1996-05-16 | NONE | AIR | lites. fluffily even de | | 1 | 240267 | 15274 | 5 | 24.00 | 28974.00 | 0.10 | 0.04 | N | O | 1996-03-30 | 1996-03-14 | 1996-04-01 | NONE | FOB | pending foxes. slyly re | +------------+-----------+-----------+--------------+------------+------------------+------------+-------+--------------+--------------+------------+------------+-------------+--------------------+------------+----------------------------------+ 5 rows in setCombine an ODPS external table with a local regular table in a query (using the same JOIN approach as for
ext_t3andinfoin the previous example). Assume that the database has a regular tableorders_summarythat records the order keys to focus on. Example:SELECT o.order_id, l.l_quantity, l.l_extendedprice FROM orders_summary o JOIN ext_lineitem l ON o.order_id = l.l_orderkey WHERE l.l_shipdate >= '1996-03-01' LIMIT 10;
What to do next
- After adding an external file: Execute
ALTER EXTERNAL TABLE table_name REFRESH;, or refer to Manage external files. - Manage partitions by directory: Refer to Create an external table partition.
- Column types: Refer to Data type mappings.
- Drop an external table: Use
DROP TABLE, which is the same as for a regular table. Refer to Drop a table.