12
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?

インポートしたCSVの行番号

Posted at
目的

AWS S3からAurora PostgreSQLテーブルへのインポートした際に
データがCSVの何行目だったのかを確認したい

確認手段

今回はインポートしたデータをチェックや加工して処理したかったので
一時テーブルを準備し IDENTITY で自動採番することにしました

インポートしたいcsvサンプル

import.csv
jan_code,trade_name
4912345678901,ミネラルウォーター
4998765432104,オーガニックコーヒー豆
4901234567898,オリジナルブレンド紅茶
4955555555554,高級チョコレート
4988888888889,食パン

一時テーブル作成

CREATE TEMPORARY TABLE IF NOT EXISTS hoge_temp
(
    row_num BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
    jan_code BIGINT NOT NULL,
    trade_name VARCHAR(50) NOT NULL
)
ON COMMIT DROP;

インポートSQL
パラメータの2つ目でCSVのヘッダとテーブルのカラムを指定します

SELECT aws_s3.table_import_from_s3(
    'hoge_temp'
    ,'jan_code,trade_name'
    ,'(format csv, header true)'
    ,aws_commons.create_s3_uri(
        'amzn-s3-demo-bucket'
        ,'import.csv'
        ,'ap-northeast-1'
    )
);
結果

row_numに IDENTITY で自動採番された値が入りCSVの行番号として利用できそうです

> select * from hoge_temp order by row_num;
row_num|jan_code     |trade_name |
-------+-------------+-----------+
      1|4912345678901|ミネラルウォーター  |
      2|4998765432104|オーガニックコーヒー豆|
      3|4901234567898|オリジナルブレンド紅茶|
      4|4955555555554|高級チョコレート   |
      5|4988888888889|食パン        |

参考リンク
GENERATED IDENTITY
aws_s3.table_import_from_s3

12
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
12
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?