1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Snowflake ハンズオン|S3 と連携し、データの読込をやってみた

Posted at

はじめに

Snowflake から S3 のデータを安全に読み込むため、
Storage Integration + External Stage を構築してみました。

全体像

S3
↑(IAM Role)
Storage Integration
↑
External Stage

1. File Format を作成する(事前にルールを定義)

CREATE OR REPLACE FILE FORMAT FF_CSV
  TYPE= CSV
  SKIP_HEADER=1
  FIELD_OPTIONALLY_ENCLOSED_BY='"'
  COMPRESSION= AUTO;

File Format は
👉 「データの読み取りルール」

として、Stage や COPY で再利用できます。

2. AWS 側の準備(概要)

  • IAM Policy 作成
  • IAM Role 作成
  • Snowflake から AssumeRole できるよう設定

詳細は公式ドキュメント参照
https://docs.snowflake.com/ja/user-guide/data-load-s3-config-storage-integration

3. Storage Integration 作成

CREATE OR REPLACE STORAGE INTEGRATION s3_snowflake_ryu
  TYPE= EXTERNAL_STAGE
  STORAGE_PROVIDER= S3
  ENABLED=TRUE
  STORAGE_AWS_ROLE_ARN='arn:aws:iam::xxxxxxxxxxxx:role/xxxxxxxxxxxx'
  STORAGE_ALLOWED_LOCATIONS= (
's3://xxx-xxxx-xxxx/'
  );

👉 STORAGE_ALLOWED_LOCATIONS は必ず制限するのが推奨です。

4. DESC INTEGRATION の確認

DESC INTEGRATION xxxxxxxxxx;

ここで表示される

  • STORAGE_AWS_IAM_USER_ARN
  • STORAGE_AWS_EXTERNAL_ID

IAM Role の Trust Policy に設定します。

👉 最も詰まりやすいポイントです。

5. Snowflake 側の権限付与

GRANTCREATE STAGEON SCHEMA public TO ROLE ACCOUNTADMIN;
GRANT USAGEON INTEGRATION xxxxxxxxxx TO ROLE ACCOUNTADMIN;

6. External Stage 作成

CREATE STAGE ryu_s3_stage
  STORAGE_INTEGRATION= xxxxxxxxxx
  URL='s3://xxxxxxxxxxxxxx/xxxx/'
  FILE_FORMAT= FF_CSV;

External Stage は

  • S3 の場所
  • 認証(Integration)
  • 読み取りルール(File Format)
    をまとめたオブジェクトです。

7. ここまででできること

  • LIST @xxxxxxxxxxxx
  • COPY INTO table FROM @xxx_s3_stage
  • Task / Pipe への拡張

まとめ

  • S3 連携は File Format / Integration / Stage の3点セット
  • Access Key を使わない構成は本番必須
  • 一度作ると再利用性が高い
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?