MPP 二、Greenplum数据加载
Loading external data into greenplum database table using different ways...
Greenplum 有常規的COPY加載方法,有基于分布式的并行的gpfdist加載方法;COPY方式適合用于加載小數據;gpfdist適合大數據量加載;下文中將討論這兩種數據加載方式。
gp_sydb=# select current_database(),current_user,current_schema(),session_user,current_timestamp,version();current_database | gp_sydb current_user | gpadmin current_schema | faa session_user | gpadmin now | 2017-06-04 15:33:01.000678+08 version | PostgreSQL 8.3.23 (Greenplum Database 5.0.0-alpha.5 build commit:2e87c5aa435c779b2f3837fa8c7273876497f6ba) on x86_64-pc-linux-gnu, compiled by GCC gcc (GCC) 6.2.0 compiled on May 19 2017 18:14:121 COPY方式加載數據
使用COPY方式加載外部文件,可以指定文件類型、文件格式、日志信息,greenplum便會自動解析,將數據加載到目標表,這種方式比單純的insert語句效率高,但它不是并行的,適合于加載少量數據。比如有如下包含兩列的csv數據(csv數據和表結構可以在gpdb-sandbox-tutorials上獲取到);
[gpadmin@gp-master faa]$ more L_AIRLINE_ID.csv Code,Description "19031","Mackey International Inc.: MAC" "19032","Munz Northern Airlines Inc.: XY" "19033","Cochise Airlines Inc.: COC" "19034","Golden Gate Airlines Inc.: GSA" "19035","Aeromech Inc.: RZZ" "19036","Golden West Airlines Co.: GLW" "19037","Puerto Rico Intl Airlines: PRN" "19038","Air America Inc.: STZ" "19039","Swift Aire Lines Inc.: SWT" "19040","American Central Airlines: TSF" "19041","Valdez Airlines: VEZ" "19042","Southeast Alaska Airlines: WEB" "19043","Altair Airlines Inc.: AAR" "19044","Chitina Air Service: CHI" "19045","Marco Island Airways Inc.: MRC" "19046","Caribbean Air Services Inc.: OHZ" "19047","Sundance Airlines: PRO" "19048","Seair Alaska Airlines Inc.: SAI"在數據庫中創建相同結構的分布表;
gp_sydb=# \d faa.d_airlinesTable "faa.d_airlines"Column | Type | Modifiers --------------+---------+-----------airlineid | integer | airline_desc | text | Distributed by: (airlineid)將外部文件的數據加載到分布表中;
\COPY faa.d_airlines FROM 'L_AIRLINE_ID.csv' CSV HEADER LOG ERRORS SEGMENT REJECT LIMIT 500 ROWS;LOG ERRORS 表示將有問題無法正常導出的數據記錄到系統表中,數據加載完成后可以通過如下的語句查詢到有問題無法正常導入的數據。
gp_sydb=# SELECT gp_read_error_log('D_AIRLINES');gp_read_error_log ------------------------------------------------------------------------------------------------------------("2017-06-04 12:07:13.151372+08",d_airlines,<stdin>,1517,,"unterminated CSV quoted field","""21395,""VirginBlue International Airlines t/a V Australia: VA""\r這種方式導入數據和在oracle中通過sqlload,在mysql中通過load導入數據很相似,對于錯誤的監控greenplum也做得比較完善。對于csv文件,greenplum要求首行指定數據列名;其它的格式的文件,我們可能要指定分割符,結束符,這些都是統一的,比如|分隔換行符結束的text文件的導入;
\COPY country FROM '/data/gpdb/data01.txt' WITH DELIMITER '|' LOG ERRORSSEGMENT REJECT LIMIT 10 ROWS;2 GPFDIST方式加載數據
gpfdist程序運行在數據存放的節點上,它將數據均勻分布到每個節點上,它是并行工作的,文件可以是按照特定格式存儲后壓縮的gzip文件,也可以是未壓縮的原文件;這種方式適用于大數據加載。假設要加載如下的已經壓縮的csv數據;
[gpadmin@gp-master faa]$ ls -ltr --block-size m otp*.gz -rwxrwxrwx 1 root root 31M Aug 6 2012 otp200912.gz -rwxrwxrwx 1 root root 30M Aug 6 2012 otp201001.gz要將這些數據加載到faa.faa_otp_load表,我們需要先創建gpfdist進程;
[gpadmin@gp-master faa]$ gpfdist -d /mnt/vbox/greenplum-master/test_data/faa -p 8081 > /tmp/gpfdist.log 2>&1 & [1] 19533gpfdist類似文件服務器,需要指定端口,文件目錄信息;gpfdist創建以后需要創建一張external table;
CREATE EXTERNAL TABLE faa.ext_load_otp (LIKE faa.faa_otp_load) LOCATION ('gpfdist://192.168.56.10:8081/otp*.gz') FORMAT 'csv' (header) LOG ERRORS SEGMENT REJECT LIMIT 50000 rows;因為gpfdist要將數據均勻的分布到每個節點上,所以創建EXTERNAL TABLE時LOCATION中指定的地址要是集群內的節點能夠訪問的地址,如果指定為127.0.0.1僅僅是當前服務器可以訪問,其它節點訪問不了,系統會報錯拒絕連接。
gp_sydb=# INSERT INTO faa.faa_otp_load SELECT * FROM faa.ext_load_otp; ERROR: connection with gpfdist failed for gpfdist://localhost:8081/otp*.gz. effective url: http://127.0.0.1:8081/otp*.gz. error code = 111 (Connection refused) (seg2 slice1 192.168.56.12:40002 pid=3546)從external table加載數據到表中;
gp_sydb=# INSERT INTO faa.faa_otp_load SELECT * FROM faa.ext_load_otp; NOTICE: Found 26526 data formatting errors (26526 or more input rows). Rejected related input data. INSERT 0 1024552 gp_sydb=# select count(*) from faa.faa_otp_load;count ---------1024552 (1 row)Greenplum也支持通過external table對外部文件的訪問,但數據不能存放到內存中,每次操作成本很高。
gp_sydb=# select count(*) from faa.ext_load_otp; NOTICE: Found 26526 data formatting errors (26526 or more input rows). Rejected related input data.count ---------1024552 (1 row)查看加載錯誤的數據(在external table表上查看);
SELECT gp_read_error_log('faa.ext_load_otp');最后停止gpfdist進程;
[gpadmin@gp-master faa]$ killall gpfdist3 GPLOAD
gpfdist的操作需要我們一步步的配置和執行,Greenplum提供了一個封裝好的依賴配置的工具GPLOAD;首先我們創建所需的配置文件gpload01.yaml和操作日志表faa.load_audit;
create table faa.load_audit(tname varchar(100),tnode varchar(300),tdate timestamp);vi gpload01.yaml--- VERSION: 1.0.0.1 DATABASE: gp_sydb # 數據庫名稱 USER: gpadmin # 用戶名 HOST: 192.168.56.10 PORT: 5432 GPLOAD:INPUT:- SOURCE:LOCAL_HOSTNAME:- 192.168.56.10PORT: 8081FILE: # 文件位置- /mnt/vbox/greenplum-master/test_data/faa/otp*.gz- FORMAT: csv- QUOTE: '"'- ERROR_LIMIT: 50000- LOG_ERRORS: trueOUTPUT:- TABLE: faa.faa_otp_load- MODE: INSERTPRELOAD:- REUSE_TABLES: trueSQL:- BEFORE: "INSERT INTO faa.load_audit VALUES('faa.faa_otp_load','start', current_timestamp)"- AFTER: "INSERT INTO faa.load_audit VALUES('faa.faa_otp_load','end', current_timestamp)"注意檢查端口是否被占用,文件路徑是否正確;最后執行加載;
[gpadmin@gp-master faa]$ gpload -f gpload01.yaml -l gpload01.log 2017-06-04 14:05:58|INFO|gpload session started 2017-06-04 14:05:58 2017-06-04 14:05:58|INFO|started gpfdist -p 8081 -P 8082 -f "/mnt/vbox/greenplum-master/test_data/faa/otp*.gz" -t 30 2017-06-04 14:05:58|INFO|did not find an external table to reuse. creating ext_gpload_reusable_e0c13d44_48eb_11e7_868b_0800279a5c02 2017-06-04 14:06:32|WARN|2084 bad rows 2017-06-04 14:06:32|WARN|Please use following query to access the detailed error 2017-06-04 14:06:32|WARN|select * from gp_read_error_log('ext_gpload_reusable_e0c13d44_48eb_11e7_868b_0800279a5c02') where cmdtime = '2017-06-04 14:05:58.88747+08' 2017-06-04 14:06:32|INFO|running time: 34.07 seconds 2017-06-04 14:06:32|INFO|rows Inserted = 1024552 2017-06-04 14:06:32|INFO|rows Updated = 0 2017-06-04 14:06:32|INFO|data formatting errors = 2084 2017-06-04 14:06:32|INFO|gpload succeeded with warnings日志提示臨時創建的external table,數據插入、更新、錯誤信息,還提示怎么查看錯誤數據。
gp_sydb=# select count(*) from faa.faa_otp_load;count ---------1024552 (1 row)GPLOAD提供更多的支持和自動化操作,也更方便定制某些特殊的操作,比如監控和統計。
轉載于:https://www.cnblogs.com/lanston/p/greenplum_data_loading.html
總結
以上是生活随笔為你收集整理的MPP 二、Greenplum数据加载的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [转]解读Unity中的CG编写Shad
- 下一篇: 动态密码卡TOTP算法