全部產品
Search
文件中心

Hologres:通過建立外部表格加速查詢MaxCompute資料

更新時間:Jul 24, 2024

Hologres支援通過建立外部表格(包括手動建立外部表格方式和外部表格自動載入功能)來加速MaxCompute資料的查詢,此方法允許您直接在Hologres環境中訪問和分析儲存在MaxCompute中的資料,從而提高查詢效率並簡化資料處理流程。

前提條件

注意事項

通過建立外部表格加速查詢MaxCompute資料時,您需要注意如下內容:

  • 由於跨地區查詢存在較多網路不可靠因素,查詢穩定性無法保障,請確保Hologres和MaxCompute處於同一地區。

  • Hologres只能加速查詢MaxCompute的內部表,不能查詢MaxCompute的外部表格和VIEW。

  • MaxCompute的分區與Hologres無強映射關係,映射至Hologres之後均為普通欄位,但可通過分區條件過濾查詢。

  • 通過外部表格方式加速查詢MaxCompute資料,一次Query命中的資料量大小不超過200 GB,一次Query命中的分區數不超過512個。通過匯入資料至Hologres內部表的方式則沒有此限制。

  • 暫不支援讀取MaxCompute中Map、Struct類型的資料。

  • 暫不支援讀取MaxCompute側使用DATETIME、TIMESTAMP、DECIMAL類型作為Cluster列的Range Cluster表資料。

資料類型映射

MaxCompute與Hologres資料類型一一映射,建表時您可以查看映射關係,詳情請參見MaxCompute與Hologres的資料類型映射

使用說明

您可通過手動建立外部表格方式或外部表格自動載入功能實現MaxCompute資料的加速查詢。

  • 手動建立外部表格方式:適用於需要加速的外部表格數量較少、且MaxCompute源表結構變更不頻繁的情境。

  • 外部表格自動載入功能:適用於需要加速的外部表格數量較多或外部表格結構變更比較頻繁的情境,您可根據需要選擇按需自動載入或全量自動載入。

方案一:通過手動建立外部表格實現MaxCompute資料的加速查詢

Hologres支援使用IMPORT FOREIGN SCHEMA命令大量建立MaxCompute外部表格,也支援使用CREATE FOREIGN TABLE方式靈活建立MaxCompute外部表格(可自訂表格名稱、自由選擇列、自訂comments資訊等),此處以CREATE FOREIGN TABLE方式為例為您介紹通過Hologres查詢MaxCompute非分區表和分區表資料的操作步驟。

說明

大量建立MaxCompute外部表格詳情請參見IMPORT FOREIGN SCHEMA。同時,您也可以使用HoloWeb可視化建表,詳情請參見建立Hologres外部表格(映射到MaxCompute)

樣本一:查詢MaxCompute非分區表資料

  1. 準備MaxCompute非分區表資料。

    在MaxCompute中建立一張非分區表並匯入資料。樣本資料選用MaxCompute公開資料集BIGDATA_PUBLIC_DATASET.tpcds_10t下的customer表,其表DDL及部分資料如下:

    --MaxCompute公用資料集的表DDL
    CREATE TABLE IF NOT EXISTS public_data.customer(
      c_customer_sk BIGINT,
      c_customer_id STRING,
      c_current_cdemo_sk BIGINT,
      c_current_hdemo_sk BIGINT,
      c_current_addr_sk BIGINT,
      c_first_shipto_date_sk BIGINT,
      c_first_sales_date_sk BIGINT,
      c_salutation STRING,
      c_first_name STRING,
      c_last_name STRING,
      c_preferred_cust_flag STRING,
      c_birth_day BIGINT,
      c_birth_month BIGINT,
      c_birth_year BIGINT,
      c_birth_country STRING,
      c_login STRING,
      c_email_address STRING,
      c_last_review_date_sk STRING);
    
    --在MaxCompute中查詢表是否有資料
    SELECT * FROM BIGDATA_PUBLIC_DATASET.tpcds_10t.customer;

    部分資料如下:image

  2. 在Hologres中建立外部表格。

    在Hologres中建立一張用於映射MaxCompute資料的外部表格。樣本語句如下。

    SET hg_enable_convert_type_for_foreign_table = true;
    CREATE FOREIGN TABLE customer (
        "c_customer_sk" int8,
        "c_customer_id" text,
        "c_current_cdemo_sk" int8,
        "c_current_hdemo_sk" int8,
        "c_current_addr_sk" int8,
        "c_first_shipto_date_sk" int8,
        "c_first_sales_date_sk" int8,
        "c_salutation" text,
        "c_first_name" text,
        "c_last_name" text,
        "c_preferred_cust_flag" text,
        "c_birth_day" int8,
        "c_birth_month" int8,
        "c_birth_year" int8,
        "c_birth_country" text,
        "c_login" text,
        "c_email_address" text,
        "c_last_review_date_sk" text)
    SERVER odps_server
    OPTIONS (project_name 'BIGDATA_PUBLIC_DATASET.tpcds_10t', table_name 'customer');
    

    參數說明如下表所示。

    參數

    描述

    SERVER

    外部表格伺服器。

    您可以直接調用Hologres底層已建立的名為odps_server的外部表格伺服器。詳細原理請參見Postgres FDW

    project_name

    • 如果您MaxCompute的Project是三層模型模式:project_name為MaxCompute的專案名稱和Schema名稱,格式為odps_project_name#odps_schema_name

    • 如果您MaxCompute的Project是兩層模型模式:project_name為MaxCompute的專案名稱。

    三層模型詳情請參見Schema操作

    table_name

    需要查詢的MaxCompute表名稱。

  3. 通過Hologres加速查詢MaxCompute表資料。

    外部表格建立成功後,直接在Hologres中查詢外部表格,即可查詢到MaxCompute的資料。樣本語句如下。

    SELECT * FROM customer limit 10;

樣本2:查詢MaxCompute分區表資料

  1. 準備MaxCompute分區表資料

    在MaxCompute中準備一張分區表並匯入資料,樣本資料選用MaxCompute公開資料集BIGDATA_PUBLIC_DATASET.finance下的ods_enterprise_share_trade_h表,其DDL及部分資料如下:

    --公用資料集下表的DDL
    CREATE TABLE IF NOT EXISTS public_data.ods_enterprise_share_trade_h(
      code STRING COMMENT '代碼',
      name STRING COMMENT '名稱',
      industry STRING COMMENT '所屬行業',
      area STRING COMMENT '地區',
      pe STRING COMMENT '市盈率',
      outstanding STRING COMMENT '流通股本',
      totals STRING COMMENT '總股本(萬)',
      totalassets STRING COMMENT '總資產(萬)',
      liquidassets STRING COMMENT '流動資產',
      fixedassets STRING COMMENT '固定資產',
      reserved STRING COMMENT '公積金',
      reservedpershare STRING COMMENT '每股公積金',
      eps STRING COMMENT '每股盈餘',
      bvps STRING COMMENT '每股淨資',
      pb STRING COMMENT '市淨率',
      timetomarket STRING COMMENT '上市日期',
      undp STRING COMMENT '未分利潤',
      perundp STRING COMMENT '每股未分配',
      rev STRING COMMENT '收入同比(%)',
      profit STRING COMMENT '利潤同比(%)',
      gpr STRING COMMENT '毛利率(%)',
      npr STRING COMMENT '淨利潤率(%)',
      holders_num STRING COMMENT '股東人數') 
      PARTITIONED BY (ds STRING) STORED AS ALIORC TBLPROPERTIES ('comment'='資料匯入日期');
      
    --在MaxCompute中查詢某個分區的資料
     SELECT * FROM BIGDATA_PUBLIC_DATASET.finance.ods_enterprise_share_trade_h WHERE ds = '20170113';

    部分資料如下:image

  2. 在Hologres中建立外部表格。

    在Hologres中建立一張用於映射MaxCompute資料的外部表格。樣本語句如下。

    CREATE FOREIGN TABLE public.foreign_ods_enterprise_share_trade_h (
        "code" text,
        "name" text,
        "industry" text,
        "area" text,
        "pe" text,
        "outstanding" text,
        "totals" text,
        "totalassets" text,
        "liquidassets" text,
        "fixedassets" text,
        "reserved" text,
        "reservedpershare" text,
        "eps" text,
        "bvps" text,
        "pb" text,
        "timetomarket" text,
        "undp" text,
        "perundp" text,
        "rev" text,
        "profit" text,
        "gpr" text,
        "npr" text,
        "holders_num" text,
        "ds" text
    )
    SERVER odps_server
    OPTIONS (project_name 'BIGDATA_PUBLIC_DATASET#finance', table_name 'ods_enterprise_share_trade_h');
    comment on foreign table public.foreign_ods_enterprise_share_trade_h is '股票歷史交易資訊';
    comment on column public.foreign_ods_enterprise_share_trade_h."code" is '代碼';
    comment on column public.foreign_ods_enterprise_share_trade_h."name" is '名稱';
    comment on column public.foreign_ods_enterprise_share_trade_h."industry" is '所屬行業';
    comment on column public.foreign_ods_enterprise_share_trade_h."area" is '地區';
    comment on column public.foreign_ods_enterprise_share_trade_h."pe" is '市盈率';
    comment on column public.foreign_ods_enterprise_share_trade_h."outstanding" is '流通股本';
    comment on column public.foreign_ods_enterprise_share_trade_h."totals" is '總股本(萬)';
    comment on column public.foreign_ods_enterprise_share_trade_h."totalassets" is '總資產(萬)';
    comment on column public.foreign_ods_enterprise_share_trade_h."liquidassets" is '流動資產';
    comment on column public.foreign_ods_enterprise_share_trade_h."fixedassets" is '固定資產';
    comment on column public.foreign_ods_enterprise_share_trade_h."reserved" is '公積金';
    comment on column public.foreign_ods_enterprise_share_trade_h."reservedpershare" is '每股公積金';
    comment on column public.foreign_ods_enterprise_share_trade_h."eps" is '每股盈餘';
    comment on column public.foreign_ods_enterprise_share_trade_h."bvps" is '每股淨資';
    comment on column public.foreign_ods_enterprise_share_trade_h."pb" is '市淨率';
    comment on column public.foreign_ods_enterprise_share_trade_h."timetomarket" is '上市日期';
    comment on column public.foreign_ods_enterprise_share_trade_h."undp" is '未分利潤';
    comment on column public.foreign_ods_enterprise_share_trade_h."perundp" is '每股未分配';
    comment on column public.foreign_ods_enterprise_share_trade_h."rev" is '收入同比(%)';
    comment on column public.foreign_ods_enterprise_share_trade_h."profit" is '利潤同比(%)';
    comment on column public.foreign_ods_enterprise_share_trade_h."gpr" is '毛利率(%)';
    comment on column public.foreign_ods_enterprise_share_trade_h."npr" is '淨利潤率(%)';
    comment on column public.foreign_ods_enterprise_share_trade_h."holders_num" is '股東人數';
    
  3. 通過Hologres查詢MaxCompute分區表資料。

    • 查詢前10條資料,SQL語句如下:

      SELECT * FROM foreign_ods_enterprise_share_trade_h limit 10;
    • 查詢分區資料,樣本SQL如下:

      SELECT * FROM foreign_ods_enterprise_share_trade_h 
      WHERE ds = '20170113';

方案二:通過外部表格自動載入功能實現MaxCompute資料的加速查詢

當執行個體中需要加速的外部表格較多或外部表格結構變更比較頻繁(如在MaxCompute側執行過刪除列、修改列順序、修改列類型等操作的表)時,您可以直接使用外部表格自動載入(Auto Load)功能實現MaxCompute資料的按需自動載入以及全量自動載入,而無需手動改變外部表格的結構,從而提高查詢效率。詳情請參見外部表格自動載入