開始使用 Amazon Redshift Spectrum - Amazon Redshift

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

開始使用 Amazon Redshift Spectrum

在本教學課程中,您將了解如何使用 Amazon Redshift Spectrum 直接從 Amazon S3 上的檔案查詢資料。如果您已經有叢集和SQL用戶端,您可以用最少的設定完成本教學課程。

注意

Redshift Spectrum 查詢會產生額外費用。在本教學中執行範例查詢的成本是名目成本。如需定價的相關資訊,請參閱 Amazon Redshift Spectrum 定價

必要條件

若要使用 Redshift 頻譜,您需要一個 Amazon Redshift 叢集和連線到叢集的用SQL戶端,以便您可以執行命SQL令。Amazon S3 中的叢集和資料檔案必須位於相同 AWS 區域.

如需如何建立 Amazon Redshift 叢集的相關資訊,請參閱亞馬遜 Redshift 入門指南中的開始使用 Amazon Redshift 佈建的資料倉儲。如需連線到叢集的方式的詳細資訊,請參閱亞馬遜 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 複製命令的輸出會確認檔案已複製到 bucket-name 在你想要的 AWS 區域.

aws s3 cp s3://redshift-downloads/tickit/spectrum/ s3://bucket-name/tickit/spectrum/ --copy-props none --recursive

開始使用 Redshift 頻譜使用 AWS CloudFormation

作為以下步驟的替代方法,您可以訪問 Redshift 頻譜 DataLake AWS CloudFormation 用來建立包含可查詢之 Amazon S3 儲存貯體的堆疊的範本。如需詳細資訊,請參閱啟動您的 AWS CloudFormation 在 Amazon S3 中堆疊並查詢您的資料

逐步開始使用 Redshift Spectrum

若要開始使用 Amazon Redshift Spectrum,請遵循這些步驟:

步驟 1. 為 Amazon Redshift 創建一個IAM角色

您的叢集需要授權才能存取您的外部資料目錄 AWS Glue 或 Amazon Athena 和您在 Amazon S3 中的數據文件。若要提供該授權,請參考 AWS Identity and Access Management (IAM) 附加至叢集的角色。如需搭配 Amazon Redshift 使用角色的詳細資訊,請參閱使用IAM角色授權COPY和UNLOAD操作

注意

在某些情況下,您可以將 Athena 資料目錄移轉至 AWS Glue 資料目錄。如果您的叢集位於 AWS 地區在哪裡 AWS Glue 受支援,而且您在 Athena 資料目錄中擁有 Redshift 頻譜外部表格。若要使用 AWS Glue 使用 Redshift 頻譜的資料目錄,您可能需要變更IAM原則。如需詳細資訊,請參閱升級至 AWS Glue《Athena 使用者指南》中的資料目錄。

當您為 Amazon Redshift 建立角色時,請選擇下列其中一個方法:

為 Amazon Redshift 創建IAM角色
  1. 開啟主IAM控台

  2. 在導覽窗格中,選擇 Roles (角色)。

  3. 選擇 Create Role (建立角色)。

  4. 選擇 AWS 服務做為受信任的實體,然後選擇 Redshift 做為使用案例。

  5. 在其他使用案例下 AWS 服務,選擇 [Redshift]-[自訂],然後選擇 [下一步]。

  6. 連接許可政策頁面隨即出現。選擇 AmazonS3ReadOnlyAccessAWSGlueConsoleFullAccess,如果您正在使用 AWS Glue 資料目錄。或者,如果您使用的是 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/*" } ] }
  7. Role name (角色名稱) 中輸入角色名稱,例如 myspectrum_role

  8. 檢閱資訊,然後選擇 Create role (建立角色)

  9. 在導覽窗格中,選擇角色。選擇新角色的名稱以檢視摘要,然後將「角色」複製ARN到剪貼簿。此值是您剛建立之角色的 Amazon 資源名稱 (ARN)。您可以在建立外部資料表時使用該值,以便在 Amazon S3 上參考資料檔案。

若要建立 Amazon Redshift 使用的IAM角色 AWS Glue Data Catalog 已啟用 AWS Lake Formation
  1. 在開啟IAM主控台https://console.aws.amazon.com/iam/

  2. 在導覽窗格中,選擇政策

    如果這是您第一次選擇 Policies (政策),將會顯示 Welcome to Managed Policies (歡迎使用受管政策) 頁面。選擇 Get Started (開始使用)

  3. 選擇 Create policy (建立政策)。

  4. 選擇此選項可在JSON索引標籤上建立策略。

  5. 貼上下列JSON原則文件,此文件會授與資料目錄的存取權,但拒絕 Lake Formation 的管理員權限。

    { "Version": "2012-10-17", "Statement": [ { "Sid": "RedshiftPolicyForLF", "Effect": "Allow", "Action": [ "glue:*", "lakeformation:GetDataAccess" ], "Resource": "*" } ] }
  6. 完成時,選擇 Review (檢閱) 以檢閱該政策。政策驗證程式會回報任何語法錯誤。

  7. 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 資料庫中進行查詢
  1. 打開 Lake Formation 控制台的位置https://console.aws.amazon.com/lakeformation/

  2. 在導覽窗格中,選擇資料湖許可,然後選擇授予

  3. 遵循使用指定資源方法授與資料表權限中的指AWS Lake Formation 開發人員指南。請提供下列資訊:

    • 對於IAM角色,請選擇您建立的IAM角色myspectrum_role。當您執行 Amazon Redshift 查詢編輯器時,它會使用此IAM角色取得資料的權限。

      注意

      若要授與 Lake 格式中資料表的SELECT權限 — 啟用要查詢的資料目錄,請執行下列動作:

      • 在 Lake Formation 中註冊資料的路徑。

      • 在 Lake Formation 中授予使用者該路徑的許可。

      • 建立的資料表會位於 Lake Formation 中註冊的路徑。

  4. 選擇 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角色與叢集建立關聯
  1. 登入 AWS Management Console 並在https://console.aws.amazon.com/redshiftv2/打開 Amazon Redshift 控制台。

  2. 在導覽選單上,選擇叢集,然後選擇您要更新的叢集名稱。

  3. 對於動作,選擇管理IAM角色。便會顯示「IAM角色」頁面。

  4. 選擇 [輸入],ARN然後輸入ARN或IAM角色,或從清單中選擇IAM角色。然後選擇添加IAM角色將其添加到附加IAM角色列表中。

  5. 選擇 [完成],將IAM角色與叢集產生關聯。叢集會進行修改以完成變更。

步驟 3:建立外部結構描述與外部資料表

在外部結構描述中建立外部資料表。外部結構描述參考外部資料目錄中的資料庫ARN,並提供授權叢集代表您存取 Amazon S3 的IAM角色。您可以在 Amazon Athena 資料目錄中建立外部資料庫, AWS Glue Data Catalog,或者一個 Apache 蜂巢中繼存儲,如 Amazon EMR。在此範例中,當您建立外部結構描述 Amazon Redshift 時,在 Amazon Athena 資料目錄中建立外部資料庫。如需詳細資訊,請參閱Amazon Redshift Spectrum 中的外部架構

建立外部結構描述與外部資料表
  1. 若要建立外部結構描述,請將下列命令ARN中的IAM角色取代為ARN您在步驟 1 中建立的角色。然後在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;
  2. 若要建立外部資料表,請執行下列CREATEEXTERNALTABLE命令。

    注意

    您的叢集和 Amazon S3 儲存貯體必須位於相同 AWS 區域。 對於此範例CREATEEXTERNALTABLE命令,包含範例資料的 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 --recursive

    Amazon S3 複製命令的輸出會確認檔案已複製到 bucket-name 在你想要的 AWS 區域.

    copy: s3://redshift-downloads/tickit/spectrum/sales/sales_ts.000 to s3://bucket-name/tickit/spectrum/sales/sales_ts.000
    create 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 中查詢您的資料
  1. 獲取 MYSPECTRUM _ 中的行數SCHEMA。 SALES表。

    select count(*) from myspectrum_schema.sales;
    count 
    ------
    172462
  2. 為符合最佳實務,請將較大的事實資料表存放於 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);
  3. 以您在EVENT中建立的IAM角色取代下列COPY命令ARN中的角色來載ARN入表格步驟 1. 為 Amazon Redshift 創建一個IAM角色。您可以選擇allevents_pipe.txt從中的 Amazon S3 儲存貯體下載和檢視來源資料 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到前 10 名活動的總銷售額。

    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
  4. 檢視先前查詢的查詢計畫。請注意針對 Amazon S3 上的資料執行的 S3 Seq ScanS3 HashAggregateS3 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 叢集並連線到叢集之後,您可以安裝紅移頻譜 DataLake AWS CloudFormation 模板,然後查詢您的數據。

CloudFormation 安裝 Redshift 頻譜入門 DataLake 範本,並建立包含下列項目的堆疊:

  • 名為 myspectrum_role 且與您的 Redshift 叢集相關聯的角色

  • 名為 myspectrum_schema 的外部結構描述

  • Amazon S3 儲存貯體中名為 sales 的外部資料表

  • 名為 event 且已載入資料的 Redshift 資料表

若要啟動您的 Redshift 頻譜入門堆疊 DataLake CloudFormation
  1. 選擇啟動CFN堆疊。主 CloudFormation 控台會開啟,並選取 DataLake .yml 範本。

    您也可以下載並自訂 Redshift 頻譜入門 DataLake CloudFormation CFN範本,然後開啟 CloudFormation 主控台 (https://console.aws.amazon.com/cloud formation),並使用自訂範本建立堆疊。

  2. 選擇 Next (下一步)

  3. 參數下,輸入 Amazon Redshift 叢集名稱、資料庫名稱和您的資料庫使用者名稱。

  4. 選擇 Next (下一步)

    堆疊選項隨即出現。

  5. 選擇下一步以接受預設設定。

  6. 查看信息並在「功能」下,然後選擇「我確認」 AWS CloudFormation 可能會創建IAM資源

  7. 選擇建立堆疊

如果在建立堆疊時發生錯誤,請參閱下列資訊:

  • 檢視 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 ScanS3 HashAggregateS3 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)