外部模式、文件与表格式

Redshift 入门

Jason Myers

Principal Architect

外部模式

数据库组件 数据库组件

  • 当元数据目录和存储不在集群内时,视为外部

Redshift Spectrum

使用 Redshift Spectrum 的外部模式

  • Redshift 是引擎
  • 默认使用 AWS Glue Data Catalog 和 AWS S3 存储
Redshift 入门

S3 数据文件格式

文件格式 列式 支持并行读取
Parquet
ORC
TextFile
OpenCSV
JSON
Redshift 入门

创建 CSV 外部表

CREATE TABLE spectrumdb.IDAHO_SITE_ID
(
    'pk_siteid' INTEGER PRIMARY KEY,
    -- 为节省空间省略其余列
)

-- CSV 行以逗号分隔 ROW FORMAT DELIMITED
-- CSV 字段以逗号终止 FIELDS TERMINATED BY ','
-- CSV 属于文本文件类型 STORED AS TEXTFILE
-- 数据在 AWS S3 中的位置 LOCATION 's3://spectrum-id/idaho_sites/'
-- 文件包含表头,需要跳过 TABLE PROPERTIES ('skip.header.line.count'='1');
Redshift 入门

查询 Spectrum 表

  • 查询方式与内部表相同
  • EXPLAIN 输出不同
  • 无需考虑 DISTKEYSORTKEY
  • 伪列
    • $path:显示该行文件存储路径
    • $size:显示该行文件大小
Redshift 入门

使用伪列

SELECT "$path", 
       "$size",
       pk_siteid
  FROM spectrumdb.idaho_site_id;
$path                           | $size | pk_siteid
================================|=======|==========
's3://spectrum-id/idaho_sites/' | 1616  | 1 
's3://spectrum-id/idaho_sites/' | 1616  | 2 
's3://spectrum-id/idaho_sites/' | 1616  | 3 
Redshift 入门

表格式

  • 常见格式:

    • Hive
    • Iceberg
    • Hudi
    • Deltalake
  • 仅支持读取

  • 某些如 Hive 需除 AWS Glue 外的外部目录
Redshift 入门

查看外部模式

  • SVV_ALL_SCHEMAS - internalexternal
SELECT schema_name, 
       schema_type
  FROM SVV_ALL_SCHEMAS
 ORDER BY SCHEMA_NAME;
schema_name           | schema_type
======================|=============
public_intro_redshift | internal
spectrumdb            | external
Redshift 入门

查看外部表

  • SVV_ALL_TABLES - TABLEEXTERNAL TABLE
SELECT table_name, 
       table_type
  FROM SVV_ALL_TABLES
 WHERE schema_name = 'public_intro_redshift';
table_name                | table_type
==========================|================
coffee_county_weather     | TABLE
idaho_monitoring_location | TABLE
idaho_samples             | TABLE
ecommerce_sales           | EXTERNAL_TABLE
Redshift 入门

Passons à la pratique !

Redshift 入门

Preparing Video For Download...