LoginSignup
2
0

More than 1 year has passed since last update.

Redshift Data APIを使ってS3のJSONデータを取り込む

Last updated at Posted at 2022-04-05

Redshiftへ取り込みたいデータがWeb APIでしか提供されていない場合にLambdaでAPIを叩いて取得したデータをRedshiftに取り込むということになると思います。今回はそのような実装したので紹介します。細い説明は省きます。

1. Redshiftのデータベースとテーブルを作成する

2. LambdaでRedshift Data APIを使ってS3にあるJSONデータをRedshiftに取り込む

今回のアーキテクチャは以下の通りです

IAM周りのセットアップ

1. LambdaのIAMロールに「AmazonRedshiftFullAccess」ポリシーをアタッチする

2. Redshift用のIAMロールを作成 & 「AmazonS3ReadOnlyAccess」をポリシーをアタッチする

スクリーンショット 2022-04-05 22.12.15.png

スクリーンショット 2022-04-05 22.07.27.png

スクリーンショット 2022-04-05 22.10.53.png

3. 作成したIAMロールを割り当てる

スクリーンショット 2022-04-05 22.16.44.png

コードの実装

Redshift Data APIを使ってRedshiftのデータに簡単にアクセスできます。COPYコマンドを実行し、S3のJSONファイルに記述されているデータをRedshiftに取り込みます。転送元のファイルの書き方をコツがあって以下の記事を参考にしました ↓↓↓

import json
import boto3
import time

CLUSTER_NAME='redshift-cluster-1'
DATABASE_NAME='json_database'
DB_USER='awsuser'

sql="COPY public.users FROM 's3://s3-backet-redshift/test_2022-04-04-16-24-56.json' " + \
        "iam_role 'arn:aws:iam::009554248005:role/RedshiftAccessS3' " + \
        "FORMAT AS JSON 'auto' " + \
        "REGION AS 'ap-northeast-1';"

client = boto3.client('redshift-data')

def lambda_handler(event, context):
    result = client.execute_statement(
        ClusterIdentifier=CLUSTER_NAME,
        Database=DATABASE_NAME,
        DbUser=DB_USER,
        Sql=sql,
    )
    # 実行IDを取得
    id = result['Id']
    # クエリが終わるのを待つ & フラグ管理
    statement = ''
    status = ''
    # ステータスが変わるまで無限ループ
    while status != 'FINISHED' and status != 'FAILED' and status != 'ABORTED':
        statement = client.describe_statement(Id=id)
        status = statement['Status']
        print("Status:", status)
        time.sleep(1)
    # 成功したら Status: FINISHED と出力される
    print("Status:", status)

参考文献

2
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
2
0