External Schemas, File, and Table Formats

Introduction to Redshift

Jason Myers

Principal Architect

External schemas

องค์ประกอบของฐานข้อมูล องค์ประกอบของฐานข้อมูล

  • เมื่อ metadata catalog และที่เก็บข้อมูลไม่ได้อยู่ใน cluster จะถือว่าเป็นแบบ external

Redshift Spectrum

External Schemas กับ Redshift Spectrum

  • Redshift ทำหน้าที่เป็น engine
  • ใช้ AWS Glue Data Catalog และ AWS S3 เป็นที่เก็บข้อมูลโดยค่าเริ่มต้น
Introduction to Redshift

รูปแบบไฟล์ข้อมูลใน S3

รูปแบบไฟล์ แบบ Columnar รองรับการอ่านแบบขนาน
Parquet Yes Yes
ORC Yes Yes
TextFile No No
OpenCSV No Yes
JSON No No
Introduction to Redshift

สร้าง External Table แบบ 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');
Introduction to Redshift

การ query ตาราง Spectrum

  • ใช้งานเหมือนการ query ตารางภายในทั่วไป
  • EXPLAIN จะแสดงผลต่างออกไป
  • ไม่ต้องกังวลเรื่อง DISTKEY หรือ SORTKEY
  • Pseudocolumns
    • $path - แสดง path ของไฟล์ที่เก็บข้อมูลของแถวนั้น
    • $size - แสดงขนาดไฟล์ของแถวนั้น
Introduction to Redshift

การใช้ pseudocolumns

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 
Introduction to Redshift

Table formats

  • รูปแบบที่นิยม:

    • Hive
    • Iceberg
    • Hudi
    • Deltalake
  • อ่านได้อย่างเดียว

  • บางรูปแบบ เช่น Hive ต้องใช้ external catalog อื่นที่ไม่ใช่ AWS Glue
Introduction to Redshift

ดู external schemas

  • SVV_ALL_SCHEMAS - internal หรือ external
SELECT schema_name, 
       schema_type
  FROM SVV_ALL_SCHEMAS
 ORDER BY SCHEMA_NAME;
schema_name           | schema_type
======================|=============
public_intro_redshift | internal
spectrumdb            | external
Introduction to Redshift

ดู external tables

  • SVV_ALL_TABLES - TABLE หรือ EXTERNAL 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
Introduction to Redshift

มาฝึกกันเถอะ!

Introduction to Redshift

Preparing Video For Download...