Iceberg external tables (Beta)

Updated at:

This topic describes how to create and query Iceberg external tables.

Apache Iceberg is a lake storage format that supports batch-stream integration. It is compatible with common compute engines, such as Spark, Hive, and Flink. MaxCompute lets you use Apache Iceberg to build a data lake storage service on OSS and perform data lake analytics.

Usage notes

  • MaxCompute uses Iceberg SDK version 1.6.1.

  • Read operations: MaxCompute supports reading Iceberg v2 tables. Time-travel queries are not supported.

  • Write operations: INSERT INTO and INSERT OVERWRITE are supported. UPSERT is not supported.

  • Supported data types.

Permission description

  • When you access OSS external tables, data is accessed through the role specified in the odps.properties.rolearn parameter, regardless of whether you use an Alibaba Cloud account, a RAM user, or a RAM role. Therefore, you must create a RAM role and grant it permissions to access the target OSS bucket, then configure the ARN of the role in the odps.properties.rolearn parameter. For more information, see Parameters.

  • You can authorize same-account or cross-account access based on your business requirements. We recommend that you use a custom authorization policy for more fine-grained access control. For more information, see Authorization for external data sources.

Create an Iceberg external table

You can create Iceberg external tables in two modes: a regular external table or a Delegate Mode external table.

Create a regular external table

Syntax

CREATE EXTERNAL TABLE  [if NOT EXISTS] <mc_oss_extable_name>
(
  <col_name> <data_type>,
  ...
)
[COMMENT <table_comment>]
[PARTITIONED BY (<col_name> <data_type>, ...)]
STORED AS ICEBERG
WITH SERDEPROPERTIES (
  'odps.properties.rolearn'='acs:ram::<uid>:role/<role_name>'
)
LOCATION '<oss_location>' 
TBLPROPERTIES ('<tbproperty_name>'='<tbproperty_value>',...);
  • For a regular external table, you must specify the Iceberg data schema, which includes column names and data types. This schema does not update automatically if the schema of the underlying Iceberg data changes.

  • You can create a regular external table that points to an empty directory. When you first write data to the table, MaxCompute creates the Iceberg data schema based on the schema you specified.

Parameters

  • Common parameters

    For a description of common parameters, see Basic syntax parameters.

  • Specific parameters

    If your data files in OSS are in the Iceberg format, add the following TBLPROPERTIES:

    Parameter

    Description

    Value

    iceberg_uuid_fields

    Specifies the columns to be written as the UUID data type. Use a semicolon (;) to separate multiple columns. This property has no default value.

    1. Specify multiple columns, such as col1;col2.

    2. Specify a nested field of a struct as the uuid type: col_struct.field1.

    3. Specify the list elements as the UUID type: col_list.entry.

    4. Specify the key of a map type as the UUID type: col_map.key.

    5. Specify the value of the map type as the UUID type: col_map.val.

    iceberg_time_fields

    Specifies the columns to be written as the TIME data type. Use a semicolon (;) to separate multiple columns. This property has no default value.

    1. Specify multiple columns: col1;col2.

    2. Specify a nested struct field as the time type: col_struct.field1.

    3. Specify the list element type as time: col_list.entry.

    4. Specify the key of a map type as a time type: col_map.key.

    5. Specify the value of a map type as a time type: col_map.val.

    iceberg_write_data_format

    Specifies the format for writing data. The default is Parquet.

    Valid values: orc, parquet, and avro.

Create a Delegate Mode external table

Syntax

Delegate Mode is an external table mode that dynamically generates the table schema and partitions by extracting metadata from the underlying lake storage format, such as Iceberg.

CREATE EXTERNAL TABLE  [if NOT EXISTS] <mc_oss_extable_name>
[COMMENT <table_comment>]
STORED AS ICEBERG
LOCATION '<oss_location>' 
TBLPROPERTIES (
  'iceberg_write_data_format'='[parquet|orc|avro]',
  'odps.properties.rolearn'='acs:ram::<uid>:role/<role_name>'
);
  • You do not need to specify the Iceberg data schema when you create the table. MaxCompute automatically detects the Iceberg data schema from the specified directory.

    This schema is re-evaluated based on the current files for each read operation.

  • You cannot point the table to an empty directory during creation. Doing so results in an error during read and write operations.

Parameters

  • Common parameters

    For a description of common parameters, see Basic syntax parameters.

  • Specific parameters

    If your data files in OSS are in the Iceberg format, add the following TBLPROPERTIES:

    Parameter

    Description

    iceberg_write_data_format

    Specifies the format for writing data.

Write data

  • For more information about the MaxCompute write syntax, see Syntax description.

  • The beta version requires the parameter set odps.service.mode=off;. For example:

    -- # Disable online mode.
    SET odps.service.mode=off; 
    
    INSERT INTO <table-name> VALUES (...);
    INSERT OVERWRITE TABLE <table-name> VALUES (...);

Query and analyze data

  • For more information about the SELECT syntax, see Syntax description.

  • For more information about query plan optimization, see Query optimization.

  • For the beta version, you need to add the parameter set odps.service.mode=off;. For example:

    -- # Disable online mode.
    SET odps.service.mode=off;
    SELECT ... from <table-name> WHERE ...

Partition operations

Regular external tables

The following table describes the supported partition operations on regular external tables.

Operation

Supported

Add a partition

Yes

Update a partition's modification time

Yes

Modify a partition value

No

Merge partitions

No

List all partitions

Yes

View partition information

Yes

Delete a partition

Yes

Truncate partition data

No

If you cannot directly list partitions or read data from a partitioned table, you must run a command to import the partition information. For more information, see Partitioned data syntax.

Delegate mode

Unlike regular Iceberg external tables, you do not need to specify partition fields when creating a Delegate Mode external table. When you query the table, the partition fields appear as the last columns in the result set. For example:

  1. Assume that partitioned files already exist in OSS.

  2. When you create the external table, you do not need to specify the partition key. The creation statement is the same for partitioned and non-partitioned tables.

    CREATE EXTERNAL TABLE ext_tbl_iceberg_pt
    STORED AS ICEBERG
    LOCATION 'oss://oss-cn-REGION-internal.aliyuncs.com/OSS_PATH/'
    TBLPROPERTIES (
        'odps.properties.rolearn'='acs:ram::UID:role/aliyunodpsdefaultrole'
    );
  3. Query: Query the partitioned table directly in MaxCompute. The partition columns appear as the last columns of the query result.

  4. Write: When writing to a partitioned table, you do not need to specify the partition. Specify the partition value at the end of the statement, and the corresponding partition directory is automatically generated.

Other DDL

View CREATE TABLE statement

SHOW CREATE TABLE <table-name>;

View table details

By default, the schema of a Delegate Mode external table is not displayed. To display the schema, enable the following switch:

-- To display the schema of a Delegate Mode table, enable the following switch.
SET odps.console.forward.commands.to.sql=true;
DESC [EXTENDED] <table-name>;

Examples

This example assumes that no Iceberg files exist in the target OSS location.

Regular external table

  1. Create a regular Iceberg external table and insert sample data.

    SET odps.service.mode=off;
    SET odps.sql.type.system.odps2=true;
    SET odps.sql.decimal.odps2=true;
    SET odps.sql.hive.compatible=true;
    
    CREATE EXTERNAL TABLE ext_tbl_iceberg_ordinary
    (
      id bigint COMMENT 'Unique user ID',
      name string COMMENT 'User name',
      age bigint COMMENT 'User age',
      gender string COMMENT 'User gender',
      height float COMMENT 'User height',
      birthday date COMMENT 'User date of birth',
      phone_number string COMMENT 'User phone number',
      email string COMMENT 'User email address',
      address string COMMENT 'User address',
      salary decimal(18, 2) COMMENT 'User salary',
      create_time timestamp COMMENT 'Time the user record was created',
      update_time timestamp COMMENT 'Time the user record was updated',
      is_deleted boolean COMMENT 'A flag indicating whether the user record is deleted'
    )
    STORED AS ICEBERG
    LOCATION 'oss://oss-cn-REGION-internal.aliyuncs.com/OSS_PATH/'
    TBLPROPERTIES (
      'iceberg_uuid_fields'='id',
      'iceberg_write_data_format'='parquet',
      'odps.properties.rolearn'='acs:ram::UID:role/aliyunodpsdefaultrole'
    );
    
    INSERT INTO ext_tbl_iceberg_ordinary VALUES
        (1, 'Zhang San', 18, 'Male', 178.56, '1990-01-01', '13800000000', 'zhangsan@example.com', 'Haidian District, Beijing', 5000.00, '2023-04-19 11:32:00', '2023-04-19 11:32:00', false),
        (2, 'Li Si', 20, 'Female', 162.70, '1992-02-02', '13900000000', 'lisi@example.com', 'Pudong New Area, Shanghai', 6000.00, '2023-04-19 11:32:00', '2023-04-19 11:32:00',false),
        (3, 'Wang Wu', 22, 'Male', 185.21, '1994-03-03', '14000000000', 'wangwu@example.com', 'Nanshan District, Shenzhen', 7000.00, '2023-04-19 11:32:00', '2023-04-19 11:32:00', false)
    ;
  2. Reading data from the regular external table

    SELECT * FROM ext_tbl_iceberg_ordinary;
    
    -- Expected output:
    +------------+------+------------+--------+------------+----------+--------------+----------------------+---------------------------+--------+---------------------+---------------------+------------+
    | id         | name | age        | gender | height     | birthday | phone_number | email                | address                   | salary | create_time         | update_time         | is_deleted |
    +------------+------+------------+--------+------------+----------+--------------+----------------------+---------------------------+--------+---------------------+---------------------+------------+
    | 1          | Zhang San | 18         | Male   | 178.56     | 1990-01-01 | 13800000000  | zhangsan@example.com | Haidian District, Beijing | 5000   | 2023-04-19 11:32:00 | 2023-04-19 11:32:00 | false      |
    | 2          | Li Si     | 20         | Female | 162.7      | 1992-02-02 | 13900000000  | lisi@example.com     | Pudong New Area, Shanghai | 6000   | 2023-04-19 11:32:00 | 2023-04-19 11:32:00 | false      |
    | 3          | Wang Wu   | 22         | Male   | 185.21     | 1994-03-03 | 14000000000  | wangwu@example.com   | Nanshan District, Shenzhen| 7000   | 2023-04-19 11:32:00 | 2023-04-19 11:32:00 | false      |
    +------------+------+------------+--------+------------+----------+--------------+----------------------+---------------------------+--------+---------------------+---------------------+------------+

Delegate mode external table

You cannot point a Delegate Mode external table to an empty directory, as this will cause an error during read and write operations. Ensure that readable Iceberg data exists in the OSS path.

  1. Create a Delegate Mode external table.

    SET odps.service.mode=off;
    
    CREATE EXTERNAL TABLE ext_tbl_iceberg_delegate
    STORED AS ICEBERG
    LOCATION 'oss://oss-cn-REGION-internal.aliyuncs.com/OSS_PATH/'
    TBLPROPERTIES (
        'odps.properties.rolearn'='acs:ram::UID:role/aliyunodpsdefaultrole'
    );
  2. Read data from the table.

    SELECT * FROM ext_tbl_iceberg_delegate;
    
    -- Expected output:
    +------------+------+------------+--------+--------+----------+--------------+----------------------+---------------------------+--------+---------------------+---------------------+------------+
    | id         | name | age        | gender | height | birthday | phone_number | email                | address                   | salary | create_time         | update_time         | is_deleted |
    +------------+------+------------+--------+--------+----------+--------------+----------------------+---------------------------+--------+---------------------+---------------------+------------+
    | 1          | Zhang San | 18         | Male   | 178.56 | 1990-01-01 | 13800000000  | zhangsan@example.com | Haidian District, Beijing | 5000   | 2023-04-19 11:32:00 | 2023-04-19 11:32:00 | false      |
    | 2          | Li Si     | 20         | Female | 162.7  | 1992-02-02 | 13900000000  | lisi@example.com     | Pudong New Area, Shanghai | 6000   | 2023-04-19 11:32:00 | 2023-04-19 11:32:00 | false      |
    | 3          | Wang Wu   | 22         | Male   | 185.21 | 1994-03-03 | 14000000000  | wangwu@example.com   | Nanshan District, Shenzhen| 7000   | 2023-04-19 11:32:00 | 2023-04-19 11:32:00 | false      |
    +------------+------+------------+--------+--------+----------+--------------+----------------------+---------------------------+--------+---------------------+---------------------+------------+

Supported data types

For more information about Iceberg data types, see Iceberg primitive types.

Iceberg type

MaxCompute type

Types.BooleanType

BOOLEAN

Types.IntegerType

INT

Types.LongType

BIGINT

Types.FloatType

FLOAT

Types.DoubleType

DOUBLE

Types.DecimalType

DECIMAL(precision, scale)

  • 0 <= precision <= 38

  • 0 <= scale <= 18

Types.DateType

DATE

Types.TimeType

BIGINT

  • MaxCompute writes data as the Iceberg Time type with microsecond precision.

  • When you write data to an Iceberg external table, you must specify the time-type columns in the iceberg_time_fields TBLPROPERTIES parameter.

    • col1;col2

    • List type: col1.entry

    • Map type: col1.key;col1.value

    • Struct type: col1.field1;col2.field2

Types.TimestampType

TIMESTAMP_NTZ

Types.TimestampType_z

TIMESTAMP

Types.StringType

STRING

Types.UUIDType

BINARY

  • The UUID length cannot exceed 16 bytes.

  • For more information about UUID conversion, see Conversion link.

  • When you write data to an Iceberg external table, you must specify the UUID-type columns in the iceberg_uuid_fields TBLPROPERTIES parameter.

    • col1;col2

    • List type: col1.entry

    • Map type: col1.key;col1.val

    • Struct type: col1.field1;col2.field2

Types.FixedType

BINARY

Types.BinaryType

BINARY

TypeID.STRUCT

STRUCT

TypeID.LIST

ARRAY

TypeID.MAP

MAP

N/A

TINYINT, SMALLINT, VARCHAR(n), CHAR(n), DATETIME, JSON