AWS CloudFormation 스택을 시작한 다음 Amazon S3에서 데이터를 쿼리합니다. - Amazon Redshift

AWS CloudFormation 스택을 시작한 다음 Amazon S3에서 데이터를 쿼리합니다.

Amazon Redshift 클러스터를 생성하고 클러스터에 연결한 후 Redshift Spectrum DataLake AWS CloudFormation 템플릿을 설치한 다음 데이터를 쿼리할 수 있습니다.

CloudFormation은 Redshift Spectrum 시작하기 DataLake 템플릿을 설치하고 다음을 포함하는 스택을 생성합니다.

  • Redshift 클러스터와 연결된 myspectrum_role이라는 역할

  • myspectrum_schema라는 외부 스키마

  • Amazon S3 버킷의 sales라는 외부 테이블

  • 데이터가 로드된 event라는 Redshift 테이블

Redshift Spectrum 시작하기 DataLake CloudFormation 스택을 시작하려면
  1. CFN 스택 시작(Launch CFN stack)을 선택합니다. DataLake.yml 템플릿이 선택된 CloudFormation 콘솔이 열립니다.

    또한 Redshift Spectrum 시작하기 DataLake CloudFormation CFN 템플릿을 다운로드하고 사용자 지정한 다음 CloudFormation 콘솔(https://console.aws.amazon.com/cloudformation)을 열고 사용자 지정한 템플릿으로 스택을 생성할 수 있습니다.

  2. 다음(Next)을 선택합니다.

  3. 파라미터(Parameters)에서 Amazon Redshift 클러스터 이름, 데이터베이스 이름 및 데이터베이스 사용자 이름을 입력합니다.

  4. Next(다음)를 선택합니다.

    스택 옵션이 나타납니다.

  5. 다음(Next)을 선택하여 기본 설정을 적용합니다.

  6. 정보를 검토하고 기능(Capabilities)에서 AWS CloudFormation에서 IAM 리소스를 생성할 수 있음 확인(I acknowledge that might create IAM resources)을 선택합니다.

  7. 스택 생성(Create stack)을 선택합니다.

스택이 생성되는 동안 오류가 발생하면 다음 정보를 확인합니다.

  • 오류 해결에 도움이 되는 정보는 CloudFormation 이벤트(Events) 탭을 참조합니다.

  • 작업을 다시 시도하기 전에 DataLake CloudFormation 스택을 삭제합니다.

  • Amazon Redshift 데이터베이스에 연결되어 있는지 확인합니다.

  • Amazon Redshift 클러스터 이름, 데이터베이스 이름 및 데이터베이스 사용자 이름을 제대로 입력했는지 확인합니다.

Amazon S3에서 데이터 쿼리

다른 Amazon Redshift 테이블을 쿼리할 때와 같은 SELECT 문을 사용하여 외부 테이블을 쿼리합니다. 이 SELECT 문 쿼리에는 테이블 조인, 데이터 집계, 조건자 필터링이 포함됩니다.

다음 쿼리는 myspectrum_schema.sales 외부 테이블의 행 수를 반환합니다.

select count(*) from myspectrum_schema.sales;
count 
------
172462

로컬 테이블과 외부 테이블 조인

다음 예는 상위 10개 이벤트의 총 매출을 찾기 위해 외부 테이블 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 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)