Starting from V4.3.5 BP1 (MySQL-compatible mode), OceanBase Database supports creating ODPS external tables using the CREATE EXTERNAL TABLE statement to access table data in Alibaba Cloud MaxCompute (formerly ODPS). By default, the Tunnel API is used.
Unlike file external tables, ODPS external tables do not use a LOCATION clause to specify a file path. Instead, they connect to tables in a remote ODPS project via the MaxCompute API. OceanBase Database stores the external table definition and column mapping locally, and invokes the ODPS Storage API or Tunnel API to interact with MaxCompute during queries or writes.
Note
To access multiple tables within an entire MaxCompute project without creating external tables for each one, you can use ODPS Catalog. This topic describes ODPS external tables at the single-table level.
Overview
MaxCompute provides two types of data access interfaces integrated with OceanBase:
API |
Purpose |
Key features |
|---|---|---|
| Storage API | Data Service API | Supports fine-grained access features such as partition filtering and predicate pushdown |
| Tunnel API | Data Upload/Download API | For batch full-table import and export, no server-side filtering capability |
When creating an ODPS external table, you specify the API to use via the API_MODE clause in the PROPERTIES block. The following table compares the two APIs:
dimension |
Storage API |
Tunnel API |
|---|---|---|
| Data filtering | Supports filtering data based on SQL conditions to transmit only the required data. | Server-side filtering is not supported. The data must be transmitted in its entirety. |
| Sharding Strategy | Automatic Sharding (by Byte or Row Count) | Manual sharding, which is relatively complex to configure. |
| Network requirements | It must be deployed in the same VPC as MaxCompute, and the Storage API permission must be enabled. | No special VPC requirements |
| Application scenarios | Condition queries on partitioned tables, analysis scenarios where data transmission volume needs to be reduced | Storage API not enabled or compatibility scenarios |
Supported operations
Operation |
Supported operations |
|---|---|
SELECTQuery |
Supported |
INSERT INTO / INSERT OVERWRITE |
Supported |
| Non-partitioned tables and partitioned tables | Supported |
| Dynamic Partition Identification | Supported (configuration required)AUTO_REFRESH) |
UPDATE / DELETE |
Not supported |
Prerequisites
- Version and mode: OceanBase Database V4.3.5 BP1 or later (MySQL-compatible mode tenant). For
API_MODE = 'storage_api'orSPLIT, V4.3.5 BP3 or later is required. - Java environment: The MaxCompute SDK is based on Java. You must deploy a Java SDK environment. For more information, see Deploy the OceanBase Database Java SDK environment.
- MaxCompute credentials:
- The AccessKey ID and AccessKey secret of the RAM user (minimum permissions recommended).
- The endpoint of the MaxCompute service, including region information.
- When using the Storage API, ensure the same VPC is used and the Storage API permission has been enabled.
- Table permissions: The RAM user must have the appropriate read and write permissions on the target MaxCompute project and table.
ODPS External Table Access Model
The access chain for an ODPS external table is as follows:
Component |
Function |
|---|---|
| Define an external table in OceanBase Database | Save column mappings, partition definitions, and ODPS connection parameters |
| MaxCompute API | Storage API or Tunnel API, responsible for communicating with the MaxCompute server |
| MaxCompute Table | Table storing data remotely (non-file path external table) |
OceanBase Database does not cache the full data of a MaxCompute table. Data is fetched via the API during queries, and data is written to a MaxCompute table via the API during writes.
Create an ODPS External Table
Non-partitioned table
Automatic column mapping
If no generated column is specified, OceanBase maps them in the order of column definition as external$tablecol1, external$tablecol2, and so on.
CREATE EXTERNAL TABLE t1 (c1 INT, c2 INT)
PROPERTIES = (
TYPE = 'ODPS',
ACCESSID = '*****',
ACCESSKEY = '*****',
ENDPOINT = 'http://service.cn-hangzhou.maxcompute.aliyun.com/api',
PROJECT_NAME = 'odps_project',
SCHEMA_NAME = '',
TABLE_NAME = 't1',
QUOTA_NAME = '',
COMPRESSION_CODE = '',
API_MODE = {"tunnel_api"}
);
Explicit column mapping (recommended)
CREATE EXTERNAL TABLE t1 (
c1 INT AS (external$tablecol1),
c2 INT AS (external$tablecol2)
)
PROPERTIES = (
TYPE = 'ODPS',
ACCESSID = '*****',
ACCESSKEY = '*****',
ENDPOINT = 'http://service.cn-hangzhou.maxcompute.aliyun.com/api',
PROJECT_NAME = 'odps_project',
SCHEMA_NAME = '',
TABLE_NAME = 't1',
QUOTA_NAME = '',
COMPRESSION_CODE = 'lz4',
API_MODE = {"tunnel_api"}
);
Note
AS (external$tablecolx) specifies to map to the x-th ordinary column (non-partitioning column) of the MaxCompute table. The serial number starts from 1. If no generated column is specified, numbers are automatically incremented in the order of column definition.
Partitioned table
Partitioning columns must be explicitly declared using metadata$partition_list_colX, and the PARTITION BY clause must match the partition structure of the MaxCompute table.
CREATE EXTERNAL TABLE t2 (
c1 INT,
c2 INT,
c3 VARCHAR(20) AS (metadata$partition_list_col1),
c4 VARCHAR(20) AS (metadata$partition_list_col2)
)
PROPERTIES = (
TYPE = 'ODPS',
ACCESSID = '*****',
ACCESSKEY = '*****',
ENDPOINT = 'http://service.cn-hangzhou.maxcompute.aliyun.com/api',
PROJECT_NAME = 'odps_project',
SCHEMA_NAME = '',
TABLE_NAME = 't2',
QUOTA_NAME = '',
COMPRESSION_CODE = '',
API_MODE = {"tunnel_api"}
)
PARTITION BY (c3, c4);
Note
metadata$partition_list_colxis used to map to the x-th partitioning column of the MaxCompute table. The serial number starts from 1 and cannot be omitted.- The partitioning columns of the MaxCompute table and the OceanBase external table must correspond one-to-one and have the same number.
Incorrect example: If a partitioning column is not declared with metadata$partition_list_colX, it will be treated as an ordinary column, causing the query to fail.
-- Incorrect syntax
CREATE EXTERNAL TABLE t2 (
c1 INT,
c2 INT,
c3 VARCHAR(20), -- Missing AS (metadata$partition_list_col1)
c4 VARCHAR(20) -- Missing AS (metadata$partition_list_col2)
)
PROPERTIES ( ... )
PARTITION BY (c3, c4);
Key parameter description (PROPERTIES)
Parameter |
Required |
Description |
|---|---|---|
| TYPE | Yes | Fixed as'ODPS' |
| ACCESSID / ACCESSKEY | Yes | RAM User AccessKey (recommended minimum permission) |
| ENDPOINT | Yes | MaxCompute service address (including region) |
| PROJECT_NAME | Yes | MaxCompute project name |
| TABLE_NAME | Yes | MaxCompute Table Name |
| SCHEMA_NAME | No | Required when the table is under a schema. |
| ACCESSTYPE | No | Account type:aliyun(Default) /sts / app |
| STSTOKEN | Condition | OnlyACCESSTYPE = 'sts'Required |
| QUOTA_NAME | No | Specify compute resource quota |
| COMPRESSION_CODE | No | Compression format:zlib / zstd / lz4 / odps_lz4 |
| API_MODE | Yes | {"tunnel_api"}or{"storage_api"} |
| SPLIT | Condition | Usestorage_apiSpecify the sharding method:byteorrow |
For the complete syntax, see CREATE EXTERNAL TABLE.
Query data from MaxCompute
The syntax for querying an ODPS external table is the same as that for querying a regular table.
SELECT * FROM t1;
-- Specify the degree of parallelism.
SELECT /*+ PARALLEL(N) */ * FROM t1;
Write data to MaxCompute
OceanBase supports writing data to an ODPS external table using either INSERT INTO or INSERT OVERWRITE.
-- Append write
INSERT INTO external_table_name
SELECT column_list FROM source_table [WHERE ...];
-- Overwrite
INSERT OVERWRITE external_table_name
SELECT column_list FROM source_table [WHERE ...];
Example of writing to a non-partitioned table
INSERT INTO t1 SELECT * FROM t1_;
INSERT /*+ PARALLEL(N) */ INTO t1 SELECT * FROM t1_;
-- Overwrite
INSERT OVERWRITE t1 SELECT * FROM t1_;
-- Write to Specified Columns
INSERT INTO t1 (c1) SELECT c1 FROM t1_;
Example of writing data to a partitioned table
INSERT INTO t2 PARTITION (c3 = 'abc', c4 = 'def') SELECT * FROM t2_;
INSERT OVERWRITE t2 PARTITION (c3 = 'abc', c4 = 'def') SELECT * FROM t2_;
Note
V4.3.5 BP2 and later versions support automatically creating partitions that do not exist in the MaxCompute table; for earlier versions, you must create the target partitions in MaxCompute in advance.
During writing, the number and order of columns must match those defined in the external table. For more information about syntax, see Insert data.
Partition information synchronization strategy
Use AUTO_REFRESH to control the refresh method for MaxCompute partition metadata:
Policy |
Description |
Application scenarios |
|---|---|---|
IMMEDIATE |
Auto-refresh on each query | Frequent partition changes |
OFF |
Manual Refresh Only | Static partitioned table |
INTERVAL |
Refresh via scheduled task | Medium frequency updates |
Example of enabling immediate refresh
CREATE EXTERNAL TABLE t2 ( ... )
AUTO_REFRESH = IMMEDIATE
PROPERTIES ( ... )
PARTITION BY (c3, c4);
Manual refresh
ALTER EXTERNAL TABLE t2 REFRESH;
Type mapping and limitations
Type mapping
For data type mappings between OceanBase and MaxCompute, see:
Time zone handling
MaxCompute time types (such as DATETIME) do not have explicit time zone information. OceanBase assumes they are consistent with its own session's time zone by default. It is recommended that OceanBase and MaxCompute use the same time zone (for example, Asia/Shanghai).
Limitations
Limitations |
Description |
|---|---|
| Mode | Only MySQL-compatible mode is supported. |
| Complex type | Not supportedARRAY<MAP<STRING, BIGINT>>Nested complex types |
| Write Column Constraints | The number and order of columns during write must strictly match the definition in the external table. |
| Performance | Query speed is affected by the MaxCompute API quota and network bandwidth. |
Considerations
- Difference between ODPS external tables and file external tables: ODPS external tables access remote tables through the MaxCompute API and do not use a
LOCATIONfile path; file external tables are used to access CSV/Parquet/ORC files located in paths such as OSS or HDFS. - Difference between ODPS external tables and ODPS Catalog: External tables map to a single table; Catalog connects to the entire MaxCompute project and synchronizes metadata automatically. See ODPS Catalog.
- API selection: Prefer the Storage API when you need to reduce data transmission and support conditional filtering; use the Tunnel API if the Storage API is not available or the network does not meet VPC requirements.
- Credential security: It is recommended to use a RAM sub-account and follow the principle of least privilege; avoid hardcoding plaintext AccessKeys in SQL.
