Quantcast
Channel: web pakun
Viewing all articles
Browse latest Browse all 247

Amazon Redshiftを使ってみよう その③「S3からCOPYコマンドでロード」

$
0
0

S3はAmazonのストレージサービスです。大量のデータを置いておくことができます。

前回の記事の最後に、サンプルデータをRedshiftにロードしてみましょうといいましたが、

Amazonが用意してくれたサンプルデータのファイルも、S3で公開されています。

 

これを利用しようと思いますが、

S3にアクセスするには、「アクセスキーID」と「シークレットアクセスキー」が必要です。

今後、ツールを使ってファイルをS3にアップロードしてデータロードを行う際にも必要ですので、今回取得しておきましょう。

 

Redshiftへのデータの登録、更新は、JDBCドライバを使用してローカルからでも行えます。
ただ、Redshiftの使用目的として、分析目的での大量データのストックがあります。JDBC経由で登録、更新はできますが、データベースの特性上、この処理が非常に遅いです。
そのため、、Redshiftでは、更新対象のデータ・ファイルをS3ストレージにアップロードし、そこからCOPYコマンドで高速ロードするのです。
そして、API経由でS3にファイルのアップロード、ダウンロードを行うには上記の2つのキーが必要です。

 

キーの取得

「アクセスキーID」と「シークレットアクセスキー」を取得するには、管理コンソールの「IAM」サービスに行きます。

IAMは「Identity and Access Management」の略で、

AWS内のサービスを利用する際に必要なユーザの管理を行います。

 

 

初期状態では上記のようにユーザがいませんので、

左のナビゲーションから、「グループ」を選択し、「新しいグループの作成」を押下します。

 

 

 

グループ名を入力し、次に進みます。

 

 

 

 

ポリシーのリストが開きますので、「AdministratorAccess」 ポリシーのチェックボックスを選択します。

作成するグループに、どのような権限を与えるのかの指定になります。

 

 

次にユーザを作成しましょう。

左から「ユーザ」→「新規ユーザの作成」を選択します。

 

 

ユーザ名を入力し、「ユーザごとにアクセスキーを生成」にチェックを入れてから、「生成」ボタンを押します。

 

 

ユーザが作成され、下記のように画面になります。

「ユーザのセキュリティ情報を表示」させると、「アクセスキーID」と「シークレットアクセスキー」が表示されます。

これは重要な情報ですので、必ず控えておきましょう!!

 

 

最後に、作成したユーザを作成したグループに追加しておきます。

 

サンプルデータのロード

さて、いよいよデータをロードします。

前回設定した「SQL Workbench/J」を使ってRedshiftに接続します。

その後、以下のコマンドでテーブルを作成しましょう。

 

 

create table users(		
	userid integer not null distkey sortkey,	
	username char(8),	
	firstname varchar(30),	
	lastname varchar(30),	
	city varchar(30),	
	state char(2),	
	email varchar(100),	
	phone char(14),	
	likesports boolean,	
	liketheatre boolean,	
	likeconcerts boolean,	
	likejazz boolean,	
	likeclassical boolean,	
	likeopera boolean,	
	likerock boolean,	
	likevegas boolean,	
	likebroadway boolean,	
	likemusicals boolean);	
		
create table venue(		
	venueid smallint not null distkey sortkey,	
	venuename varchar(100),	
	venuecity varchar(30),	
	venuestate char(2),	
	venueseats integer);	
		
create table category(		
	catid smallint not null distkey sortkey,	
	catgroup varchar(10),	
	catname varchar(10),	
	catdesc varchar(50));	
		
create table date(		
	dateid smallint not null distkey sortkey,	
	caldate date not null,	
	day character(3) not null,	
	week smallint not null,	
	month character(5) not null,	
	qtr character(5) not null,	
	year smallint not null,	
	holiday boolean default('N'));	
		
create table event(		
	eventid integer not null distkey,	
	venueid smallint not null,	
	catid smallint not null,	
	dateid smallint not null sortkey,	
	eventname varchar(200),	
	starttime timestamp);	
		
create table 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);	
		
create table sales(		
	salesid integer not null,	
	listid integer not null distkey,	
	sellerid integer not null,	
	buyerid integer not null,	
	eventid integer not null,	
	dateid smallint not null sortkey,	
	qtysold smallint not null,	
	pricepaid decimal(8,2),	
	commission decimal(8,2),	
	saletime timestamp);	

 

成功したら、Amazonが用意してくれているサンプルのS3からデータをロードするために、

「SQL Workbench/J」で以下のクエリを投げます。

との部分を上記で取得したキーに変更してクエリを投げてください。

 

copy users from 's3://awssampledbuswest2/tickit/allusers_pipe.txt' 
credentials 'aws_access_key_id=;aws_secret_access_key=' 
delimiter '|' region 'us-west-2';

copy venue from 's3://awssampledbuswest2/tickit/venue_pipe.txt' 
credentials 'aws_access_key_id=;aws_secret_access_key=' 
delimiter '|' region 'us-west-2';

copy category from 's3://awssampledbuswest2/tickit/category_pipe.txt' 
credentials 'aws_access_key_id=;aws_secret_access_key=' 
delimiter '|' region 'us-west-2';

copy date from 's3://awssampledbuswest2/tickit/date2008_pipe.txt' 
credentials 'aws_access_key_id=;aws_secret_access_key=' 
delimiter '|' region 'us-west-2';

copy event from 's3://awssampledbuswest2/tickit/allevents_pipe.txt' 
credentials 'aws_access_key_id=;aws_secret_access_key=' 
delimiter '|' timeformat 'YYYY-MM-DD HH:MI:SS' region 'us-west-2';

copy listing from 's3://awssampledbuswest2/tickit/listings_pipe.txt' 
credentials 'aws_access_key_id=;aws_secret_access_key=' 
delimiter '|' region 'us-west-2';

copy sales from 's3://awssampledbuswest2/tickit/sales_tab.txt'
credentials 'aws_access_key_id=;aws_secret_access_key='
delimiter '\t' timeformat 'MM/DD/YYYY HH:MI:SS' region 'us-west-2';


ロードできたら、以下のようなクエリを投げて、データが入っているかどうか、確認してみましょう。

 

select count(*) from sales;

エラーが起きてしまったら、管理コンソールから確認することができます。

クラスタの「load」タブをクリックするとロードした履歴を参照することができます。

Load番号のリンクをクリックして詳細を表示すると、

 

 

下記のようにエラー理由が出力されていますので、エラー修正の参考にしてください。

 

 

このときは、日時型のカラムのフォーマット指定がうまくできてなかったようですね。。


Viewing all articles
Browse latest Browse all 247

Trending Articles