この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。
Amazon Redshiftにて、2014/06/30付けで新しい機能がリリースされたようなので早速試してみます。
当エントリではその中の1つ、『Cross-region COPY』に関して見てみたいと思います。
異なるリージョン間でのCOPY処理を試してみる
従来、Amazon Redshiftに取り込むS3データについては、『Amazon Redshiftクラスタと同じリージョンに作成されたバケット』にアップロードしていなければなりませんでした。それが、今回の対応でリージョンを跨いでCOPYが行えるようになります。
まずはこちらのRedshiftクラスタ。us-west-2aとあるので、オレゴンリージョンに配置されている事になります。
以前のエントリでも何度か用いている、AWSの入門編ドキュメントで紹介されているテーブル及びファイルを使ってみましょう。
Redshiftにログインし、スキーマ[cm_demo]にlistingテーブルを作成します。
create table cm_demo.listing(
listid integer not null distkey,
sellerid integer not null,
eventid integer not null,
dateid smallint not null sortkey,
numtickets smallint not null,
priceperticket decimal(8,2),
totalprice decimal(8,2),
listtime timestamp);
以下は確認内容。
# \d listing;
テーブル "cm_demo.listing"
列 | 型 | 修飾語
----------------+-----------------------------+----------
listid | integer | not null
sellerid | integer | not null
eventid | integer | not null
dateid | smallint | not null
numtickets | smallint | not null
priceperticket | numeric(8,2) |
totalprice | numeric(8,2) |
listtime | timestamp without time zone |
この後は、本来であれば、クラスタと同じオレゴンリージョンにバケットを作成し、データをアップロード・COPYしないと行けなかった事になります。上記公式ドキュメントであれば、awssampledbuswest2バケットのものでないとダメでした。
まずはこれまで通りの仕様確認として、クラスタと同一リージョンのバケット(awssampledbuswest2)内にあるファイルをCOPYしてみます。問題無いですね。
# copy listing
# from 's3://awssampledbuswest2/tickit/listings_pipe.txt'
# CREDENTIALS 'aws_access_key_id=XXXXXXXXXX;aws_secret_access_key=YYYYYYYYYY'
# delimiter '|';
INFO: Load into table 'listing' completed, 192497 record(s) loaded successfully.
COPY
#
次いで、単純にCOPY処理時のバケットを別のものに変えて試してみましょう。東京リージョンのバケット(awssampledbapnortheast1)にあるファイルで試してみます。するとエラーが出てしまいました。この動作も想定通りですね。
# truncate cm_demo.listing;
TRUNCATE TABLE and COMMIT TRANSACTION
# SELECT COUNT(*) FROM cm_demo.listing;
count
-------
0
(1 行)
# copy listing
# from 's3://awssampledbapnortheast1/tickit/listings_pipe.txt'
# CREDENTIALS 'aws_access_key_id=XXXXXXXXXX;aws_secret_access_key=YYYYYYYYYY'
# delimiter '|';
ERROR: S3ServiceException:
The bucket you are attempting to access must be addressed using the specified endpoint.
Please send all future requests to this endpoint.,Status 301,Error PermanentRedirect,
Rid XXXXXXXXXXXXXX,ExtRid XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
DETAIL:
-----------------------------------------------
error:
S3ServiceException:The bucket you are attempting to access must be addressed
using the specified endpoint.
Please send all future requests to this endpoint.,Status 301,Error PermanentRedirect,
Rid XXXXXXXXXXXXXX,ExtRid XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
code: 8001
context: Listing bucket=awssampledbapnortheast1 prefix=tickit/listings_pipe.txt
query: 103331
location: s3_utility.cpp:525
process: padbmaster [pid=2243]
-----------------------------------------------
#
公式ドキュメントに記載されている指定方法に従い、REGIONオプションを付与してみます。REGIONオプションを付与する事で、問題無くインポート処理が行えました!(※ちなみに、同一リージョン(オレゴン同士)と異なるリージョン(クラスタ:オレゴン、S3:東京)では処理時間に開きがありました。この辺はやはり、異なるリージョン間でのデータ移動なので止むを得ない部分なのでしょうか。)
# truncate cm_demo.listing;
TRUNCATE TABLE and COMMIT TRANSACTION
# SELECT COUNT(*) FROM cm_demo.listing;
count
-------
0
(1 行)
# copy cm_demo.listing
# from 's3://awssampledbapnortheast1/tickit/listings_pipe.txt'
# CREDENTIALS 'aws_access_key_id=XXXXXXXXXX;aws_secret_access_key=YYYYYYYYYY'
# delimiter '|'
# REGION AS 'ap-northeast-1';
INFO: Load into table 'listing' completed, 192497 record(s) loaded successfully.
COPY
#
# SELECT COUNT(*) FROM cm_demo.listing;
count
--------
192497
(1 行)
まとめ
以上、COPY処理のクロスリージョン対応について試してみました。リージョンを跨ぐ様な環境でRedshiftを用いている場合はデータ移動が事前に必要でしたが、これでその必要もなくなりましたね。その他色々な局面でこの恩恵に預かる事が出来そうです。私からは以上です。