外部綱要、檔案與資料表格式

Redshift 入門

Jason Myers

Principal Architect

外部綱要

資料庫元件 資料庫元件

  • 當中繼資料目錄與儲存不屬於叢集時,視為外部

Redshift Spectrum

使用 Redshift Spectrum 的外部綱要

  • Redshift 是引擎
  • 預設使用 AWS Glue Data Catalog 與 AWS S3 儲存
Redshift 入門

S3 資料檔案格式

檔案格式 欄式 支援平行讀取
Parquet Yes Yes
ORC Yes Yes
TextFile No No
OpenCSV No Yes
JSON No No
Redshift 入門

建立 CSV 外部資料表

CREATE TABLE spectrumdb.IDAHO_SITE_ID
(
    'pk_siteid' INTEGER PRIMARY KEY,
    -- Cutting the rest of columns for space
)

-- CSV rows are comma delimited ROW FORMAT DELIMITED
-- CSV fields are terminated by a comma FIELDS TERMINATED BY ','
-- CSVs are a type of text file STORED AS TEXTFILE
-- This is where the data is in AWS S3 LOCATION 's3://spectrum-id/idaho_sites/'
-- This file has headers that we want to skip 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 入門

一起來練習吧!

Redshift 入門

Preparing Video For Download...