1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

AuroraServerlessへcsv形式で一括データ挿入する

Posted at

S3に置いたcsvファイルのデータをAuroraServerlessにdataAPIを介して挿入するlambda。

import json
import csv
import boto3
import os

rdsData = boto3.client('rds-data')
s3 = boto3.resource('s3')
bucket = s3.Bucket('バケット名')
tableName = "テーブル名"
clusterArn = 'クラスターARN'
secretArn = 'シークレットARN'

def lambda_handler(event, context):

    # S3からファイル取得
    bucket.download_file('S3上のファイル名.csv', '/tmp/保存時のファイル名.csv')
    
    # 最終的にAuroraServerlessに投げるSQL文
    allSqlstr = ""

    # csv → json変換
    with open('/tmp/names.csv') as csvfile:
        reader = csv.DictReader(csvfile, quotechar="'") #デフォルトだとダブルクオート

        for row in reader:
            # 辞書の全てのvalueを取得し、SQL文を組み立て
            sqlstr = "INSERT INTO " + tableName + " VALUES ( "
            for val in row.values():
                sqlstr += "'" + str(val) + "',"
            allSqlstr += sqlstr[:-1] + " );"
            
        print("requestSQL:", allSqlstr)
            
    # AuroraServerless更新
    res = rdsData.execute_statement(
            resourceArn = clusterArn,
            secretArn = secretArn,
            database = 'DB名', 
            sql = allSqlstr)
    print (res)
    return 0
1
1
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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?