5
2

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.

AWS GlueでRDSにデータを上書き保存する

Posted at

GlueのジョブでDynamicFrameを使ってデータをRDSに書き込むとき、
追記(Append)で書き込むため、同じジョブを動かすとデータが重複してしまいます。

DynamicFrameをDataFrameに変換すると、上書きモードで書き込むことができます。

自動生成されたジョブのコードに以下を追記します。
JDBCの接続定義が用意されてることが前提です。

#datasink4 = glueContext.write_dynamic_frame.from_jdbc_conf(frame = dropnullfields3, catalog_connection = "MyConnection", connection_options = {"dbtable": "my_table", "database": "my_database"}, transformation_ctx = "datasink4")

# 接続定義からJDBC情報を取得
jdbc_conf = glueContext.extract_jdbc_conf(connection_name='MyConnection')

# DynamicFrameをDataFrameに変換
df = dropnullfields3.toDF()

# DataFrameをテーブルに書き込み(上書きモード)
df.write \
    .format("jdbc") \
    .option("url", jdbc_conf['url']) \
    .option("dbtable", "my_database.my_table") \
    .option("user", jdbc_conf['user']) \
    .option("password", jdbc_conf['password']) \
    .mode("overwrite") \
    .save()

job.commit()

例では、S3のデータをAurora Serverless MySQLに書き込んでいますが上書きできました。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?