All Products
Search
Document Center

Hologres:CREATE FOREIGN TABLE

Last Updated:Aug 14, 2023

You can execute the CREATE FOREIGN TABLE statement to create a foreign table. Hologres allows you to create a foreign table to read data from MaxCompute, Object Storage Service (OSS), and Data Lake Formation (DLF), and across Hologres databases. This topic describes how to use the CREATE FOREIGN TABLE statement to create a foreign table.

Limits

Only Hologres V1.3 and later support the three-layer model of MaxCompute. This model allows you to create schemas in MaxCompute projects and use these schemas to classify objects such as tables. For more information, see Schema-related operations. If you want to create a foreign table in a Hologres instance to map a table in a MaxCompute project that uses the three-layer model and the version of your Hologres instance is earlier than V1.3, manually upgrade your Hologres instance in the Hologres console or join a DingTalk group to apply for an instance upgrade. For more information about how to manually upgrade a Hologres instance, see Instance upgrades. For more information about how to join a Hologres DingTalk group, see Obtain online support for Hologres.

Syntax

CREATE FOREIGN TABLE [ IF NOT EXISTS ] table_name ( [
  { column_name data_type }
  [, ... ]
  ] )
SERVER odps_server
[ OPTIONS ( option 'value' [, ... ] ) ]

The following table describes the parameters in the syntax.

Parameter

Description

SERVER

The server on which the MaxCompute table is stored.

You can use the odps_server server that is created at the underlying layer of Hologres. For more information, see postgres_fdw.

OPTIONS

The options to be specified. You must set the project_name and table_name fields. If your MaxCompute project uses the three-layer model but you specify only the name of the MaxCompute project, an error message is returned. Example:

failed to import foreign schema:Table not found - table_xxx
  • MaxCompute two-layer model

    • project_name: the name of the MaxCompute project to which the MaxCompute table belongs.

    • table_name: the name of the MaxCompute table that you want to query.

  • MaxCompute three-layer model

    • project_name: the name of the MaxCompute project and the name of the schema to which the MaxCompute table belongs, in the format of odps_project_name#odps_schema_name.

    • table_name: the name of the MaxCompute table that you want to query.

Note

The data types in the Hologres foreign table must match those in the MaxCompute table.

Examples

  • Directly query MaxCompute data

    You can create a foreign table in Hologres to query the data mapped from the MaxCompute table. For example, you can execute the following SQL statements:

    -- MaxCompute two-layer model
    CREATE FOREIGN TABLE src_pt(
      id text, 
      pt text) 
    SERVER odps_server 
    OPTIONS(project_name '<odps_project>', table_name '<odps_table>');
    
    -- MaxCompute three-layer model
    CREATE FOREIGN TABLE src_pt(
      id text,
      pt text)
    SERVER odps_server
    OPTIONS(project_name '<odps_project>#<odps_schema>', table_name '<odps_table>');
    
    SELECT * FROM src_pt;

    For more information, see Create a foreign table in Hologres to accelerate queries of MaxCompute data.

  • Import MaxCompute data to Hologres and query the data

    You can also import MaxCompute data to Hologres and query the data. For example, you can execute the following statements:

    CREATE FOREIGN TABLE src_pt_odps(
      id text,
      pt text) 
    SERVER odps_server 
    OPTIONS (project_name'<odps_project>', table_name'<odps_table>');
    
    BEGIN;
    CREATE TABLE src_pt(
     id text, 
      pt text);
    COMMIT;
    
    INSERT INTO src_pt SELECT * FROM src_pt_odps;

    For more information, see Import data from MaxCompute to Hologres by executing SQL statements.

Data type mappings between MaxCompute and Hologres when you create a foreign table

For information about the data type mappings between MaxCompute and Hologres when you create a foreign table, see the "Data type mappings between MaxCompute and Hologres" section of the Data types topic.

Note
  • In MaxCompute, DATETIME uses UTC+8 as the standard system time zone. The time ranges from 0000-01-01 to 9999-12-31 and is accurate to milliseconds.

  • Hologres supports the TIMESTAMPTZ type of PostgreSQL, including the time zone. The time ranges from 4713 BC to 294276 AD and is accurate to microseconds.

  • If the MaxCompute table contains fields whose data types are not supported by Hologres, you can query other fields.