使用SQL导入MaxCompute的数据至Hologres

文章目录


一、简介

当MaxCompute业务数据规模超过200 GB,且查询复杂度较高、对响应时间要求达到秒级时,Hologres支持将这些数据直接导入内部表进行查询,相较于通过外部表查询方式,该方式可以设置索引,且数据查询效率更高。本文为您介绍不同场景的数据导入操作以及常见问题。

注意事项

通过SQL导入MaxCompute数据至Hologres时,您需要注意如下内容:

  • MaxCompute的分区与Hologres无强映射关系,MaxCompute的分区字段映射至Hologres为普通字段,因此支持MaxCompute分区数据导入Hologres非分区或者分区。
  • Hologres仅支持一级分区,MaxCompute多级分区导入Hologres分区表时,只需要映射一个分区,其余分区映射成Hologres的普通字段。
  • 如果导入数据时需要更新覆盖原有数据,您需要使用INSERT ON CONFLICT(UPSERT)语法。
  • MaxCompute与Hologres的数据类型映射,请参见数据类型汇总
  • MaxCompute的表数据更新之后,在Hologres存在缓存延迟(一般为10分钟内),建议在导入数据前使用IMPORT FOREIGN SCHEMA语法更新外部表以获取最新数据。
  • 导入MaxCompute数据至Hologres时,建议使用SQL导入,不建议使用数据集成导入,因为使用SQL导入性能表现更优。

二、导入MaxCompute的非分区表数据至Hologres并查询

1、准备MaxCompute的非分区表数据。

在MaxCompute中创建一张非分区源数据表,或直接选用已创建的非分区表。

示例选取MaxCompute公共数据集public_data的customer表,其表DDL以及数据如下。

sql 复制代码
-- 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 STRING,
  useless STRING);

-- 在MaxCompute中查询表是否有数据
SELECT * FROM public_data.customer;

部分数据内容显示如下图所示。

2、在Hologres中创建外部表。

在Hologres中创建一张用于映射MaxCompute源数据表的外部表。

sql 复制代码
CREATE FOREIGN TABLE foreign_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" text,
    "useless" text
)
SERVER odps_server
OPTIONS (project_name 'public_data', table_name 'customer');
参数 描述
Server 您可以直接调用Hologres底层已创建的名为odps_server 的外部表服务器。详细原理请参见Postgres FDW
Project_Name MaxCompute表所在的项目名称。
Table_Name 需要查询的MaxCompute表名称。

外部表字段的数据类型与MaxCompute表字段的数据类型保持一致。

3、在Hologres中创建存储表。

在Hologres中创建一张用于接收MaxCompute源表数据的存储表。

sql 复制代码
-- 示例建一张列存表
BEGIN;
CREATE TABLE public.holo_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" text,
 "useless" text
);
CALL SET_TABLE_PROPERTY('public.holo_customer', 'orientation', 'column');
CALL SET_TABLE_PROPERTY('public.holo_customer', 'bitmap_columns', 'c_customer_id,c_salutation,c_first_name,c_last_name,c_preferred_cust_flag,c_birth_country,c_login,c_email_address,c_last_review_date,useless');
CALL SET_TABLE_PROPERTY('public.holo_customer', 'dictionary_encoding_columns', 'c_customer_id:auto,c_salutation:auto,c_first_name:auto,c_last_name:auto,c_preferred_cust_flag:auto,c_birth_country:auto,c_login:auto,c_email_address:auto,c_last_review_date:auto,useless:auto');
CALL SET_TABLE_PROPERTY('public.holo_customer', 'time_to_live_in_seconds', '3153600000');
CALL SET_TABLE_PROPERTY('public.holo_customer', 'storage_format', 'segment');
COMMIT;

4、导入数据至Hologres。

使用INSERT语句将MaxCompute源头表中的数据导入至Hologres,您可以选择部分字段导入(字段顺序需要一一对应),也可以选择全部字段导入,示例DDL如下。

sql 复制代码
-- (可选)推荐使用Serverless Computing执行大数据量离线导入和ETL作业
SET hg_computing_resource = 'serverless';

--部分字段数据导入
INSERT INTO holo_customer (c_customer_sk,c_customer_id,c_email_address,c_last_review_date,useless)
SELECT 
    c_customer_sk,
    c_customer_id,
    c_email_address,
    c_last_review_date,
    useless
FROM foreign_customer;

--全部字段数据导入
INSERT INTO holo_customer
SELECT * FROM foreign_customer;

-- 重置配置,保证非必要的SQL不会使用serverless资源。
RESET hg_computing_resource;

5、Hologres查询MaxCompute表数据。

在Hologres中查询导入的MaxCompute表数据。示例SQL语句如下。

sql 复制代码
SELECT * FROM holo_customer;
相关推荐
Rosanci4 小时前
谷歌浏览器插件开发实战指南:从 Hello World 到上架发布
大数据·人工智能·chrome·程序人生
m0_466525294 小时前
云从科技上线云起ModelHub:AI团队时代的模型算力基础设施
大数据·人工智能·科技
蓝速科技6 小时前
会议室门牌公告通知发布选型与落地指南丨蓝速科技
大数据·运维·数据库·人工智能·科技
SelectDB9 小时前
同等资源下 Apache Doris 4.2 vs StarRocks 4.1.1:1TB SSB 与 TPC-H 性能实测
大数据·数据库·数据分析
SelectDB9 小时前
Doris vs ClickHouse:企业 OLAP 走向下一阶段,两种技术路线如何选择
大数据·数据库·数据分析
当下新鲜事9 小时前
脱硫脱硝塔动力系统科普:四方DL500变频器工作原理解析
大数据·运维·物联网·业界资讯
珠海西格电力10 小时前
零碳园区管理系统“智慧大脑”功能对园区运营成本的影响有哪些?
大数据·人工智能·安全·系统架构·能源
发量惊人的中年网工10 小时前
2026年DDoS防护方案怎么选?从攻击响应、清洗位置到成本账单,解析全球高防方案
大数据·网络·安全·ddos
知识的搬运工旺仔12 小时前
唯一索引与 NULL 值:PostgreSQL 主键约束与 NULLS NOT DISTINCT
数据库·后端·sql·postgresql
科创致远12 小时前
科创致远 ESD 静电监控系统全场景落地指南
大数据·人工智能·制造·精益工程