2016-04-25 295 views

答えて

4

Redshiftでは、svl_qlogで実行されるcreate table sqlの開始時刻と終了時刻を検索することで、テーブルの作成時間を取得する他の方法があります。同様のデータを得るために見ることができる他のテーブルがありますが、この方法での問題は数日(3〜5)しか保持されないということです。誰もが、テーブル自体とともに格納されているメタデータを照会することを望んでいますが。 AmazonはS3に保存するログからS3にデータをエクスポートするようにこのデータを保存することを推奨します。それでは、私の意見では、これらのs3ファイルをaws_table_historyなどと呼ばれる永続的なテーブルにインポートして戻すことができます。

select * from svl_qlog where substring ilike 'create table%' order by starttime desc limit 100; 

select * from stl_query a, stl_querytext b where a.query = b.query and b.text ilike 'create table%' order by a.starttime desc limit 100; 

またはこのような単なる表の名前と日付を取得:

select split_part(split_part(b.text,'table ', 2), ' ', 1) as tablename, 
starttime as createdate 
from stl_query a, stl_querytext b 
where a.query = b.query and b.text ilike 'create table%' order by a.starttime desc; 

エクスポートを作成して表データ履歴、あなたのキーを使用して作成したS3バケットにしたいです。以下のselectステートメントは、作成されたテーブル名と作成されたdatetimeを出力します。

S3にエクスポートするデータを含む一時テーブルを作成します。

create table temp_history as 
(select split_part(split_part(b.text,'table ', 2), ' ', 1) as tablename, starttime as createdate 
from stl_query a, stl_querytext b 
where a.query = b.query 
and b.text ilike 'create table%' order by a.starttime desc); 

このテーブルをS3にアップロードします。

unload ('select * from temp_history') 
to 's3://tablehistory' credentials 'aws_access_key_id=myaccesskey;aws_secret_access_key=mysecretkey' 
DELIMITER '|' NULL AS '' ESCAPE ALLOWOVERWRITE; 

AWS Redshiftで新しい表を作成します。

CREATE TABLE aws_table_history 
(
tablename VARCHAR(150), 
createdate DATETIME 
); 

次に、カスタムテーブルにインポートします。

copy aws_table_history from 's3://tablehistory' credentials 'aws_access_key_id=MYKEY;aws_secret_access_key=MYID' 
emptyasnull 
blanksasnull 
removequotes 
escape 
dateformat 'YYYY-MM-DD' 
timeformat 'YYYY-MM-DD HH:MI:SS' 
maxerror 20; 
delimiter '|'; 

私はこれをすべてテストしたところ、私たちのために働いていました。これが何人かの人々に役立つことを願っています 最後に、より簡単な方法は、Talend Big Data Open Studioを使用して、新しいジョブを作成してコンポーネントtRedshiftRowを取得し、それに次のSQLを貼り付けることです。その後、ジョブをビルドし、必要な環境で.bat(Windows)または.sh(unix)を実行するようにスケジュールすることができます。

INSERT INTO temp_history 
(select split_part(split_part(b.text,'table ', 2), ' ', 1) as tablename, starttime as createdate 
from stl_query a, stl_querytext b 
where a.query = b.query 
and b.text ilike 'create table%' order by a.starttime desc); 
COMMIT; 
insert into historytable 
select distinct s.* 
from temp_history s; 
COMMIT; 
--remove duplicates 
DELETE FROM historytable USING historytable a2 
WHERE historytable.tablename = a2.tablename AND 
historytable.createdate < a2.createdate; 
COMMIT; 
---clear everything from prestage 
TRUNCATE temp_history; 
COMMIT; 
+0

Talend Big Dataジョブを作成し、その環境で実行するBATジョブまたはSHジョブをスケジュールすることができます。 tRedShiftRowコンポーネントを作成し、次のSQLを貼り付けると、テーブルの永続的な作成を維持できます。 –

2

Redshiftでテーブルの作成タイムスタンプを取得できないようです。 1つの回避策は、CREATE TABLEを含むDDLの履歴を記録するSTL_DDLTEXTテーブルを使用することです。ここ

は、例えば(test_tableテーブル名)である:上記の場合

dev=> select starttime, endtime, trim(text) as ddl from stl_ddltext where text ilike '%create%table%test_table%' order by endtime desc limit 1; 
     starttime   |   endtime   |                ddl 
----------------------------+----------------------------+---------------------------------------------------------------------------------------------------------------------------------- 
2016-04-25 05:38:11.666338 | 2016-04-25 05:38:11.674947 | CREATE TABLE "test_table" (id int primary key, value varchar(24)); 
(1 row) 

starttimeまたはendtimetest_tableテーブル作成のタイムスタンプであろう。

注:永久にこの方法を使用することはできませんので

  • 赤方偏移は、長い時間ためSTL_DDLTEXTを保持しません。
  • テーブルがテーブル名の名前を変更するなどの他の方法で作成されている場合、この方法は使用できません。
+2

ご意見ありがとうございます。しかし、redshiftは情報をstl。*テーブルに長期間保存しません(最大3〜5日間)。だから、そこからすべてのテーブルの作成タイムスタンプを得ることはできません。毎日/毎週これらのテーブルを別の透過テーブルにダンプすることができますので、いつでも私たちの意思でこの情報を確認することができます。私はここに記載されているようなより具体的なものを探していました.. http://stackoverflow.com/a/2577388/4330205 ...しかし、私はredshiftでpg_ls_dirの代替案を見つけることができませんでした。 –

+1

あなたは正しいです。 Redshiftはstl_ *データを長期間保存しないため、限られた状況で使用することができます。 –

関連する問題