2016-04-25 16 views

回答

4

在Redshift中,您可以通過搜索svl_qlog中任何創建表sql的開始和結束時間來獲取表的創建時間。還有其他表格可以查看以獲取類似的數據,但這種方式的問題在於它只保存了幾天(3-5)。儘管每個人都希望元數據與表本身一起存儲來查詢。亞馬遜建議保留此數據以將數據從要保留到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語句將輸出創建的表名和它創建的日期時間。

使用要導出到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中獲取表的創建時間戳。一種解決方法是使用STL_DDLTEXT表,其中記錄了DDL的歷史記錄,包括CREATE TABLE

下面是一個例子(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) 

在上述情況下,starttimeendtime將是test_table表創建的時間戳。

注:

  • 紅移不保留STL_DDLTEXT很長一段時間,所以你不能永久使用這種方式。
  • 如果通過重命名錶名稱等其他方式創建表,則不能使用此方式。
+2

感謝您的建議。但是,紅移不會很長時間(最多3到5天)將信息存儲在stl。*表中。因此,不太可能從中獲得所有表的創建時間戳。儘管我們可以每天/每週將這些表格轉儲到另一張表格中,並確保我們隨時都可以隨時獲取這些信息。我正在尋找一些更具體的東西,像這裏提到的一樣.. http://stackoverflow.com/a/2577388/4330205 ..但我找不到pg_ls_dir在redshift中的替代方案。 –

+1

你說得對。 Redshift不會長時間存儲stl_ *數據,所以這可以在有限的情況下使用。 –

相關問題