本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
開始使用 Amazon Redshift Spectrum
在本教學課程中,您將了解如何使用 Amazon Redshift Spectrum 直接從 Amazon S3 上的檔案查詢資料。如果您已有叢集和 SQL 用戶端,則可透過最少的設定完成本教學課程。
注意
Redshift Spectrum 查詢會產生額外費用。在本教學中執行範例查詢的成本是名目成本。如需定價的相關資訊,請參閱 Amazon Redshift Spectrum 定價
先決條件
若要使用 Redshift Spectrum,您需要一個 Amazon Redshift 叢集和一個連接到您叢集的 SQL 用戶端,以便您執行 SQL 命令。叢集與 Amazon S3 中的資料檔案必須在相同的 AWS 區域。
如需有關如何建立 Amazon Redshift 叢集的資訊,請參閱《Amazon Redshift 入門指南》中的 Amazon Redshift 佈建資料倉儲入門。如需有關如何連線至叢集的資訊,請參閱《Amazon Redshift 入門指南》中的連線至 Amazon Redshift 資料倉儲。
在下面的一些範例中,範例資料位於美國東部 (維吉尼亞北部) 區域 (us-east-1
),因此您需要一個同樣位於 us-east-1
中的叢集。或者,您可以使用 Amazon S3 將資料物件從下列儲存貯體和資料夾複製到 AWS 區域 叢集所在的 儲存貯體:
s3://redshift-downloads/tickit/spectrum/customers/*
s3://redshift-downloads/tickit/spectrum/sales_partition/*
s3://redshift-downloads/tickit/spectrum/sales/*
s3://redshift-downloads/tickit/spectrum/salesevent/*
執行類似下列的 Amazon S3 命令,將位於美國東部 (維吉尼亞北部) 的範例資料複製到您的 AWS 區域。執行命令之前,請在儲存貯體中建立儲存貯體和資料夾,以符合 Amazon S3 複製命令。Amazon S3 複製命令的輸出會確認檔案已複製到所需 AWS 區域的 bucket-name
。
aws s3 cp s3://redshift-downloads/tickit/spectrum/ s3://
bucket-name
/tickit/spectrum/ --copy-props none --recursive
使用 開始使用 Redshift Spectrum AWS CloudFormation
除了下列步驟之外,您也可以存取 Redshift Spectrum DataLake AWS CloudFormation 範本,來建立具有可查詢的 Amazon S3 儲存貯體的堆疊。如需詳細資訊,請參閱啟動您的 AWS CloudFormation 堆疊,然後在 Amazon S3 中查詢您的資料。
逐步開始使用 Redshift Spectrum
若要開始使用 Amazon Redshift Spectrum,請遵循這些步驟:
步驟 1. 為 Amazon Redshift 建立 IAM 角色
您的叢集需要授權才能存取 中的外部 Data Catalog AWS Glue 或 Amazon Athena,以及 Amazon S3 中的資料檔案。若要提供該授權,請參考連接至叢集的 AWS Identity and Access Management (IAM) 角色。如需將角色與 Amazon Redshift 搭配使用的相關資訊,請參閱使用 IAM 角色授權 COPY 與 UNLOAD 操作。
注意
在某些情況下,您可以將 Athena Data Catalog 遷移至 AWS Glue Data Catalog。如果您的叢集位於 AWS Glue 支援 AWS 的區域,而且您在 Athena Data Catalog 中有 Redshift Spectrum 外部資料表,則可以執行此操作。若要搭配 Redshift Spectrum 使用 AWS Glue Data Catalog,您可能需要變更 IAM 政策。如需詳細資訊,請參閱《Athena 使用者指南》中的升級至 AWS Glue 資料目錄。
當您為 Amazon Redshift 建立角色時,請選擇下列其中一個方法:
如果您將 Redshift Spectrum 與 Athena Data Catalog 或 AWS Glue Data Catalog 搭配使用,請遵循 中概述的步驟為 Amazon Redshift 建立 IAM 角色。
如果您使用 Redshift Spectrum 搭配已啟用 AWS Glue Data Catalog 的 AWS Lake Formation,請遵循下列程序中所述的步驟:
為 Amazon Redshift 建立 IAM 角色
-
開啟 IAM 主控台
。 -
在導覽窗格中,選擇 Roles (角色)。
-
選擇 Create Role (建立角色)。
-
選擇AWS 服務做為信任的實體,然後選擇Redshift做為使用案例。
-
在其他 的使用案例 AWS 服務下,選擇 Redshift - 可自訂,然後選擇下一步。
-
連接許可政策頁面隨即出現。如果您使用的是 AWS Glue Data Catalog
AWSGlueConsoleFullAccess
,請選擇AmazonS3ReadOnlyAccess
和 。或者,如果您使用的是 Athena 資料目錄,則選擇AmazonAthenaFullAccess
。選擇 Next (下一步)。注意
AmazonS3ReadOnlyAccess
政策會授予您的叢集對所有 Amazon S3 儲存貯體的唯讀存取權限。若要僅授予對 AWS 範例資料儲存貯體的存取權,請建立新的政策並新增下列許可。{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "s3:Get*", "s3:List*" ], "Resource": "arn:aws:s3:::redshift-downloads/*" } ] }
-
在 Role name (角色名稱) 中輸入角色名稱,例如
myspectrum_role
。 -
檢閱資訊,然後選擇 Create role (建立角色)。
-
在導覽窗格中,選擇角色。選擇新角色的名稱以檢視摘要,然後複製 Role ARN (角色 ARN) 至您的剪貼簿。此值是您剛剛建立的角色 Amazon Resource Name (ARN)。您可以在建立外部資料表時使用該值,以便在 Amazon S3 上參考資料檔案。
使用 AWS Glue Data Catalog 啟用的 為 Amazon Redshift 建立 IAM 角色 AWS Lake Formation
-
開啟位於 https://console.aws.amazon.com/iam/
的 IAM 主控台。 -
在導覽窗格中,選擇政策。
如果這是您第一次選擇 Policies (政策),將會顯示 Welcome to Managed Policies (歡迎使用受管政策) 頁面。選擇 Get Started (開始使用)。
-
選擇 Create policy (建立政策)。
-
在 JSON 索引標籤上選擇建立政策。
-
貼上下列 JSON 政策文件,這能授予資料目錄的存取權限,但會拒絕 Lake Formation 的管理員許可。
{ "Version": "2012-10-17", "Statement": [ { "Sid": "RedshiftPolicyForLF", "Effect": "Allow", "Action": [ "glue:*", "lakeformation:GetDataAccess" ], "Resource": "*" } ] }
-
完成時,選擇 Review (檢閱) 以檢閱該政策。政策驗證程式會回報任何語法錯誤。
-
在 Review policy (檢閱政策) 頁面的 Name (名稱) 中,輸入
myspectrum_policy
來為您所建立的政策命名。輸入 Description (說明) (選用)。檢閱政策 Summary (摘要) 來查看您的政策所授予的許可。然後選擇 Create policy (建立政策) 來儲存您的工作。建立政策之後,您可以提供存取權給使用者。
若要提供存取權,請新增權限至您的使用者、群組或角色:
-
中的使用者和群組 AWS IAM Identity Center:
建立權限合集。請按照 AWS IAM Identity Center 使用者指南 中的 建立權限合集 說明進行操作。
-
透過身分提供者在 IAM 中管理的使用者:
建立聯合身分的角色。遵循「IAM 使用者指南」的為第三方身分提供者 (聯合) 建立角色中的指示。
-
IAM 使用者:
-
建立您的使用者可擔任的角色。請按照「IAM 使用者指南」的為 IAM 使用者建立角色中的指示。
-
(不建議) 將政策直接附加至使用者,或將使用者新增至使用者群組。請遵循《IAM 使用者指南》的新增許可到使用者 (主控台) 中的指示。
-
授予資料表的 SELECT 許可,以在 Lake Formation 資料庫中進行查詢
-
開啟 Lake Formation 主控台,網址為 https://console.aws.amazon.com/lakeformation/
。 -
在導覽窗格中,選擇資料湖許可,然後選擇授予。
-
遵循AWS Lake Formation 開發人員指南的使用具名資源方法授予資料表權限中的指示。請提供下列資訊:
-
在IAM 角色中,選擇您所建立的 IAM 角色
myspectrum_role
。當您執行 Amazon Redshift 查詢編輯器時,其會使用此 IAM 角色來取得資料的許可。注意
若要授予啟用 Lake Formation 之資料目錄中的資料表進行查詢的 SELECT 許可,請執行下列操作:
在 Lake Formation 中註冊資料的路徑。
在 Lake Formation 中授予使用者該路徑的許可。
建立的資料表會位於 Lake Formation 中註冊的路徑。
-
-
選擇 Grant (授予)。
重要
根據最佳實務,請僅允許透過 Lake Formation 許可存取基礎 Amazon S3 物件。若要防止未經授權的存取,則可移除授予 Lake Formation 外部 Amazon S3 物件的任何許可。如果您先前在設定 Lake Formation 之前就已存取 Amazon S3 物件,則請移除先前設定的所有 IAM 政策或儲存貯體許可。如需詳細資訊,請參閱將 AWS Glue 資料許可升級至 AWS Lake Formation 模型和 Lake Formation 許可。
步驟 2:建立 IAM 角色與叢集的關聯
現在您已擁有授權 Amazon Redshift 為您存取外部資料目錄和 Amazon S3 的 IAM 角色。此時,您必須將該角色與您的 Amazon Redshift 叢集建立關聯。
將 IAM 角色與叢集建立關聯
-
登入 AWS Management Console ,並在 https://console.aws.amazon.com/redshiftv2/
:// 開啟 Amazon Redshift 主控台。 -
在導覽選單上,選擇叢集,然後選擇您要更新的叢集名稱。
-
針對動作,選擇管理 IAM 角色。IAM roles (IAM 角色) 頁面隨即出現。
-
選擇輸入 ARN,然後輸入 ARN 或 IAM 角色,或是從清單中選擇 IAM 角色。然後選擇 Add IAM role (新增 IAM 角色) 來將其新增至 Attached IAM roles (已連接的 IAM 角色) 清單。
-
選擇 Done (完成) 來將 IAM 角色與叢集建立關聯。叢集會進行修改以完成變更。
步驟 3:建立外部結構描述與外部資料表
在外部結構描述中建立外部資料表。外部結構描述參考外部資料目錄中的資料庫,並提供授權您的叢集代表您存取 Amazon S3 的 IAM 角色 ARN。您可以在 Amazon Athena Data Catalog AWS Glue Data Catalog或 Apache Hive 中繼存放區中建立外部資料庫,例如 Amazon EMR。在此範例中,當您建立外部結構描述 Amazon Redshift 時,在 Amazon Athena 資料目錄中建立外部資料庫。如需詳細資訊,請參閱Amazon Redshift Spectrum 中的外部結構描述。
建立外部結構描述與外部資料表
-
如要建立外部結構描述,請將以下命令中的 IAM 角色 ARN 替換為您在步驟 1 中建立的角色 ARN。然後,在您的 SQL 用戶端中執行命令。
create external schema myspectrum_schema from data catalog database 'myspectrum_db' iam_role 'arn:aws:iam::123456789012:role/myspectrum_role' create external database if not exists;
-
若要建立外部資料表,請執行以下 CREATE EXTERNAL TABLE 命令。
注意
您的叢集和 Amazon S3 儲存貯體必須位於相同的 AWS 區域。在此範例 CREATE EXTERNAL TABLE 命令中,Amazon S3 儲存貯體與範例資料位於美國東部 (維吉尼亞北部) AWS 區域。若要查看來源資料,請下載
sales_ts.000
檔案。 您可以修改此範例以在不同 中執行 AWS 區域。在您想要的 中建立 Amazon S3 儲存貯體 AWS 區域。使用 Amazon S3 複製命令複製銷售資料。然後將範例
CREATE EXTERNAL TABLE
命令中的位置選項更新到您的儲存貯體。aws s3 cp s3://redshift-downloads/tickit/spectrum/sales/ s3://
bucket-name
/tickit/spectrum/sales/ --copy-props none --recursiveAmazon S3 複製命令的輸出會確認檔案已複製到所需 AWS 區域的
bucket-name
。copy: s3://redshift-downloads/tickit/spectrum/sales/sales_ts.000 to s3://
bucket-name
/tickit/spectrum/sales/sales_ts.000create external table myspectrum_schema.sales( salesid integer, listid integer, sellerid integer, buyerid integer, eventid integer, dateid smallint, qtysold smallint, pricepaid decimal(8,2), commission decimal(8,2), saletime timestamp) row format delimited fields terminated by '\t' stored as textfile location 's3://redshift-downloads/tickit/spectrum/sales/' table properties ('numRows'='172000');
步驟 4:在 Amazon S3 中查詢您的資料
建立外部資料表後,您可以透過用於查詢其他 Amazon Redshift 資料表的相同 SELECT 陳述式來查詢它們。這些 SELECT 陳述式查詢包括聯結資料表、彙總資料和述詞篩選。
在 Amazon S3 中查詢您的資料
-
取得 MYSPECTRUM_SCHEMA.SALES 資料表中的列數。
select count(*) from myspectrum_schema.sales;
count ------ 172462
-
為符合最佳實務,請將較大的事實資料表存放於 Amazon S3,並將較小的維度資料表存放於 Amazon Redshift。如果您在載入資料中載入範例資料,您的資料庫中會有名為 EVENT 的資料表。如果沒有,請使用以下命令來建立 EVENT 資料表。
create table event( eventid integer not null distkey, venueid smallint not null, catid smallint not null, dateid smallint not null sortkey, eventname varchar(200), starttime timestamp);
-
透過將以下 COPY 命令中的 IAM 角色 ARN 替換為您在 步驟 1. 為 Amazon Redshift 建立 IAM 角色 中建立的角色 ARN 來載入 EVENT 資料表。您可以選擇從 Amazon S3 儲存貯體下載和檢視 的來源資料
allevents_pipe.txt
AWS 區域 us-east-1
。copy event from 's3://redshift-downloads/tickit/allevents_pipe.txt' iam_role 'arn:aws:iam::123456789012:role/myspectrum_role' delimiter '|' timeformat 'YYYY-MM-DD HH:MI:SS' region 'us-east-1';
以下範例會結合使用外部 Amazon S3 資料表 MYSPECTRUM_SCHEMA.SALES 與本機 Amazon Redshift 資料表 EVENT,藉此尋找排名前十名事件的總銷售額。
select top 10 myspectrum_schema.sales.eventid, sum(myspectrum_schema.sales.pricepaid) from myspectrum_schema.sales, event where myspectrum_schema.sales.eventid = event.eventid and myspectrum_schema.sales.pricepaid > 30 group by myspectrum_schema.sales.eventid order by 2 desc;
eventid | sum --------+--------- 289 | 51846.00 7895 | 51049.00 1602 | 50301.00 851 | 49956.00 7315 | 49823.00 6471 | 47997.00 2118 | 47863.00 984 | 46780.00 7851 | 46661.00 5638 | 46280.00
-
檢視先前查詢的查詢計畫。請注意針對 Amazon S3 上的資料執行的
S3 Seq Scan
、S3 HashAggregate
和S3 Query Scan
步驟。explain select top 10 myspectrum_schema.sales.eventid, sum(myspectrum_schema.sales.pricepaid) from myspectrum_schema.sales, event where myspectrum_schema.sales.eventid = event.eventid and myspectrum_schema.sales.pricepaid > 30 group by myspectrum_schema.sales.eventid order by 2 desc;
QUERY PLAN ----------------------------------------------------------------------------- XN Limit (cost=1001055770628.63..1001055770628.65 rows=10 width=31) -> XN Merge (cost=1001055770628.63..1001055770629.13 rows=200 width=31) Merge Key: sum(sales.derived_col2) -> XN Network (cost=1001055770628.63..1001055770629.13 rows=200 width=31) Send to leader -> XN Sort (cost=1001055770628.63..1001055770629.13 rows=200 width=31) Sort Key: sum(sales.derived_col2) -> XN HashAggregate (cost=1055770620.49..1055770620.99 rows=200 width=31) -> XN Hash Join DS_BCAST_INNER (cost=3119.97..1055769620.49 rows=200000 width=31) Hash Cond: ("outer".derived_col1 = "inner".eventid) -> XN S3 Query Scan sales (cost=3010.00..5010.50 rows=200000 width=31) -> S3 HashAggregate (cost=3010.00..3010.50 rows=200000 width=16) -> S3 Seq Scan myspectrum_schema.sales location:"s3://redshift-downloads/tickit/spectrum/sales" format:TEXT (cost=0.00..2150.00 rows=172000 width=16) Filter: (pricepaid > 30.00) -> XN Hash (cost=87.98..87.98 rows=8798 width=4) -> XN Seq Scan on event (cost=0.00..87.98 rows=8798 width=4)
啟動您的 AWS CloudFormation 堆疊,然後在 Amazon S3 中查詢您的資料
建立 Amazon Redshift 叢集並連線至叢集後,您可以安裝 Redshift Spectrum DataLake AWS CloudFormation 範本,然後查詢您的資料。
CloudFormation 會安裝 Redshift Spectrum 入門 DataLake 範本,並建立包含下列項目的堆疊:
名為
myspectrum_role
且與您的 Redshift 叢集相關聯的角色名為
myspectrum_schema
的外部結構描述Amazon S3 儲存貯體中名為
sales
的外部資料表名為
event
且已載入資料的 Redshift 資料表
啟動您的 Redshift Spectrum 入門 DataLake CloudFormation 堆疊
選擇啟動 CFN 堆疊
。CloudFormation 主控台會開啟,並選取 DataLake.yml 範本。 您也可以下載並自訂 Redshift Spectrum 入門 DataLake CloudFormation CFN 範本
,然後開啟 CloudFormation 主控台 (https://console.aws.amazon.com/cloudformation ),並使用自訂範本建立堆疊。 選擇 Next (下一步)。
在參數下,輸入 Amazon Redshift 叢集名稱、資料庫名稱和您的資料庫使用者名稱。
選擇 Next (下一步)。
堆疊選項隨即出現。
選擇下一步以接受預設設定。
檢閱 和 功能下的資訊,然後選擇我確認 AWS CloudFormation 可能會建立 IAM 資源。
選擇建立堆疊。
如果在建立堆疊時發生錯誤,請參閱下列資訊:
檢視 CloudFormation 事件索引標籤,以取得有助於您解決錯誤的資訊。
請先刪除 DataLake CloudFormation 堆疊,然後再次嘗試此操作。
請確定您已連線到 Amazon Redshift 資料庫。
請確定您為 Amazon Redshift 叢集名稱、資料庫名稱和資料庫使用者名稱輸入正確的資訊。
在 Amazon S3 中查詢您的資料
您可以使用用於查詢其他 Amazon Redshift 資料表的相同 SELECT 陳述式,來查詢外部資料表。這些 SELECT 陳述式查詢包括聯結資料表、彙總資料和述詞篩選。
下列查詢會傳回 myspectrum_schema.sales
外部資料表中的列數。
select count(*) from myspectrum_schema.sales;
count ------ 172462
使用本機資料表聯結外部資料表
以下範例會結合使用外部資料表 myspectrum_schema.sales
與本機資料表 event
,以尋找前十名事件的總銷售額。
select top 10 myspectrum_schema.sales.eventid, sum(myspectrum_schema.sales.pricepaid) from myspectrum_schema.sales, event
where myspectrum_schema.sales.eventid = event.eventid
and myspectrum_schema.sales.pricepaid > 30
group by myspectrum_schema.sales.eventid
order by 2 desc;
eventid | sum --------+--------- 289 | 51846.00 7895 | 51049.00 1602 | 50301.00 851 | 49956.00 7315 | 49823.00 6471 | 47997.00 2118 | 47863.00 984 | 46780.00 7851 | 46661.00 5638 | 46280.00
檢視查詢計劃
檢視先前查詢的查詢計畫。請注意在 Amazon S3 的資料上執行的 S3 Seq Scan
、S3 HashAggregate
和 S3 Query Scan
步驟。
explain
select top 10 myspectrum_schema.sales.eventid, sum(myspectrum_schema.sales.pricepaid)
from myspectrum_schema.sales, event
where myspectrum_schema.sales.eventid = event.eventid
and myspectrum_schema.sales.pricepaid > 30
group by myspectrum_schema.sales.eventid
order by 2 desc;
QUERY PLAN
-----------------------------------------------------------------------------
XN Limit (cost=1001055770628.63..1001055770628.65 rows=10 width=31)
-> XN Merge (cost=1001055770628.63..1001055770629.13 rows=200 width=31)
Merge Key: sum(sales.derived_col2)
-> XN Network (cost=1001055770628.63..1001055770629.13 rows=200 width=31)
Send to leader
-> XN Sort (cost=1001055770628.63..1001055770629.13 rows=200 width=31)
Sort Key: sum(sales.derived_col2)
-> XN HashAggregate (cost=1055770620.49..1055770620.99 rows=200 width=31)
-> XN Hash Join DS_BCAST_INNER (cost=3119.97..1055769620.49 rows=200000 width=31)
Hash Cond: ("outer".derived_col1 = "inner".eventid)
-> XN S3 Query Scan sales (cost=3010.00..5010.50 rows=200000 width=31)
-> S3 HashAggregate (cost=3010.00..3010.50 rows=200000 width=16)
-> S3 Seq Scan spectrum.sales location:"s3://redshift-downloads/tickit/spectrum/sales" format:TEXT (cost=0.00..2150.00 rows=172000 width=16)
Filter: (pricepaid > 30.00)
-> XN Hash (cost=87.98..87.98 rows=8798 width=4)
-> XN Seq Scan on event (cost=0.00..87.98 rows=8798 width=4)