3단계: 외부 스키마와 외부 테이블 생성 - Amazon Redshift

3단계: 외부 스키마와 외부 테이블 생성

외부 스키마에서 외부 테이블을 생성합니다. 외부 스키마는 외부 데이터 카탈로그에 있는 데이터베이스를 참조하며, 사용자를 대신하여 Amazon S3에 액세스하도록 클러스터에 권한을 부여하는 IAM 역할 ARN을 제공합니다. 외부 데이터베이스는 Amazon Athena Data Catalog, AWS Glue Data Catalog 또는 Amazon EMR과 같은 Apache Hive 메타스토어에서 생성할 수 있습니다. 이 예제에서는 외부 스키마 Amazon Redshift를 생성할 때 Amazon Athena Data Catalog에 외부 데이터베이스를 생성합니다. 자세한 내용은 Amazon Redshift Spectrum용 외부 스키마 생성 단원을 참조하십시오.

외부 스키마와 외부 테이블을 생성하려면
  1. 외부 스키마를 만들려면 다음 명령에서 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;
  2. 외부 테이블을 만들려면 다음 CREATE EXTERNAL TABLE 명령을 실행합니다.

    참고

    클러스터와 Amazon S3 버킷이 동일한 AWS 리전에 있어야 합니다. 예로 제시된 CREATE EXTERNAL TABLE 명령의 경우 샘플 데이터가 들어 있는 Amazon S3 버킷이 미국 동부(버지니아 북부) AWS 리전에 있습니다. 소스 데이터를 보려면 sales_ts.000 파일을 다운로드합니다.

    이 예를 다른 AWS 리전에서 실행하도록 수정할 수 있습니다. 원하는 AWS 리전에 Amazon S3 버킷을 생성합니다. Amazon S3 copy 명령을 사용하여 판매 데이터를 복사합니다. 그런 다음, 예로 제시된 CREATE EXTERNAL TABLE 명령에서 버킷의 위치를 업데이트합니다.

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

    Amazon S3 copy 명령의 출력은 파일이 원하는 AWS 리전의 bucket-name에 복사되었음을 알려줍니다.

    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');