Trafodion 的數據加載主要包括兩種方法,即 Trickle Load(持續加載) 和 Bulk Load(批量加載)。
下表介紹了兩種加載方法的區別:html
類型 | 描述 | 方法/工具 |
---|---|---|
Trickle Load | 數據量較小,當即插入 | ODB 工具(性能一般較第三方ETL工具更好); ETL 工具如kettle、Informatica等; 自主開發的ODBC/JDBC應用。 |
Bulk Load | 數據量較大;階段性數據而且是批量加載的方式 | Bulk Loader |
從SQL插入語句實現方式來看,mysql
Trickle Load 又包括如下三種方式:sql
而Bulk Load主要的SQL命令爲: LOAD數據庫
以上4種SQL實現加載方式區別以下表:apache
特徵 | INSERT | UPSERT | UPSERT USING LOAD | LOAD |
---|---|---|---|---|
事務 | 是 | 是 | 不是,使用HBase的WAL來恢復 | 不是,使用快照(SNAPSHOT)來恢復 |
操做方法 | 經過CheckAndPut調用走標準HBase寫路徑 | 經過Put調用走標準HBase寫路徑 | 經過Put調用走標準HBase寫路徑 | 使用HBase bulk load寫路徑並直接建立HFiles |
惟一性約束 | 強制 | 不強制,有相同鍵值的覆蓋以前的行 | 不強制,有相同鍵值的覆蓋以前的行 | 部分強制 |
索引 | 能夠用在有索引的表上 | 能夠用在有索引的表上 | 當用在有索引的表上時,會回到UPSERT | 能夠用在有索引的表,索引會不起做用 |
最大Size | 10k*n 行,n表明節點數 | 10k*n 行,n表明節點數 | 5million*n 行,n表明節點數 | 2 billion*n行,n表明節點數 |
最小Size | 1行 | 1行 | 1行 | 1million*n行 |
速度 | 最慢 | 比INSERT快 | 比UPSERT快 | 最快 |
(注:如下只是初步介紹Trafodion odb工具的幾個主要命令的基本用法,更多用法及適用場景請見下載官方文檔點擊打開連接查看詳情)centos
語法ide
用例
(1)建立測試表工具
(2)建立測試文件並編輯數據oop
(3)Load文件到測試表性能
(4)檢查Load是否成功
語法
用例
(1)繼續使用上述測試表,當前測試表已經加載了5條數據。
(2)Extract上表數據到指定文件
[centos@cent-1 bin]$ ./odb64luo -u trafodion -p traf123 -d traf -l src=test.file:tgt=trafodion.seabase.test_tbl\ > :fs=\,:rows=5:loadcmd=IN:truncate:parallel=2 odb [main(1354)] - Unknow option :fs=,:rows=5:loadcmd=IN:truncate:parallel=2. Ignored odb [2016-09-26 09:15:51]: starting ODBC connection(s)... (1) 1 2 Connected to Trafodion [0.0.0]--- 0 row(s) deleted in 0.687s (prep 0.006s, exec 0.681s, fetch 0.000s/0.000s) [0] 5 records inserted [commit] [0] odb version 1.1.0 Load(2) statistics: [0] Target table: TRAFODION.SEABASE.TEST_TBL [0] Source: test.file [0] Pre-loading time: 0.327 s (00:00:00.327) [0] Loading time: 0.146 s(00:00:00.146) [0] Total records read: 5 [0] Total records inserted: 5 [0] Total number of columns: 2 [0] Total bytes read: 31 [0] Average input row size: 6.2 B [0] ODBC row size: 21 B (data) + 16 B (len ind) [0] Rowset size: 100 [0] Rowset buffer size: 3.61 KiB [0] Load throughput (real data): 0.207 KiB/s [0] Load throughput (ODBC): 0.702 KiB/s odb [2016-09-26 09:15:51]: exiting. Session Elapsed time 0.479 seconds (00:00:00.479)
(3)查詢文件內容
語法
用例
(1)建立一個表和原測試結構一致
(2)利用COPY命令把原測試表數據複製到新表
[centos@cent-1 bin]$ ./odb64luo -u trafodion:trafodion -p traf123:traf123 -d traf:traf -cp src=trafodion.seabase.test_tbl:tgt=trafodion.seabase.test_tbl2:rows=m2:truncate Connected to Trafodion odb [2016-09-26 09:51:51]: starting ODBC connection(s)... 0 >1 >2 Connected to Trafodion [1.0.0]--- 0 row(s) deleted in 0.298s (prep 0.285s, exec 0.013s, fetch 0.000s/0.000s) [1] 5 records copied [commit] [0] odb version 1.1.0 Copy statistics: [0] Source: TRAFODION.SEABASE.TEST_TBL [0] Target: trafodion.seabase.test_tbl2 [0] Total number of columns: 2 [0] ODBC row size: 23 B (data) + 16 B (len ind) [0] Rowset size: 53,773 [0] Rowset buffer size: 2,048.00 KiB [0] Pre-copy time: 0.677 s (00:00:00.677) [0] Copy time: 0.037 s (00:00:00.037) [0] Total records copied: 5 (0.135 krec/s) [0] Copy throughput (ODBC): 0.003 MiB/s (0.010 GiB/h) [0] Total/Wait cycles: 1/0 [0>1] 5 records copied in 0.037 (00:00:00.037 s) [0>2] 0 records copied in 0.037 (00:00:00.037 s) odb [2016-09-26 09:51:52]: exiting. Session Elapsed time 0.733 seconds (00:00:00.733)
(3)檢查數據是否複製成功
語法
Transform是Load的一種,Transform的意思是在Load的時候經過一個mapfile來實現一些功能,好比忽略輸入文件的某些行、生產序列號、插入常量、日期格式轉換、字符串截取和替換、生成隨機數等等。
用例
(1)測試表繼續用上述的test_tbl
(2)建立測試Input文件,內容以下:
(3)建立map文件,內容以下:
(4)根據map文件加載數據
[centos@cent-1 bin]$ ./odb64luo -u trafion -p traf123 -d traf -l src=test_transform.file:tgt=trafodion.seabase.test_tbl:map=m.map:fs=,:truncate odb [2016-09-26 10:28:49]: starting ODBC connection(s)... 0 Connected to Trafodion [0.0.0]--- 5 row(s) deleted in 0.042s (prep 0.001s, exec 0.041s, fetch 0.000s/0.000s) [0] 5 records inserted [commit] [0] odb version 1.1.0 Load statistics: [0] Target table: TRAFODION.SEABASE.TEST_TBL [0] Source: test_transform.file [0] Pre-loading time: 0.165 s (00:00:00.165) [0] Loading time: 0.023 s(00:00:00.023) [0] Total records read: 5 [0] Total records inserted: 5 [0] Total number of columns: 2 [0] Total bytes read: 75 [0] Average input row size: 15.0 B [0] ODBC row size: 21 B (data) + 16 B (len ind) [0] Rowset size: 100 [0] Rowset buffer size: 3.61 KiB [0] Load throughput (real data): 3.184 KiB/s [0] Load throughput (ODBC): 4.458 KiB/s odb [2016-09-26 10:28:49]: exiting. Session Elapsed time 0.195 seconds (00:00:00.195)
(5)檢查數據是否轉換成功
Bulk Load主要是針對大數據量的且通常是批量裝載的方式。Bulk Load一般使用LOAD語句實現。
下面介紹幾種Bulk Load方式,
(1)建立兩個表結構相同的測試表
(2)表1有幾條數據,表2沒有數據
(3)從表1加載數據到表2
(1)建立測試文件並利用scp或者其餘方式上傳到Hadoop Cluster上,內容以下
(2)將測試文件導入HDFS中
(3)在Trafodion中建立測試表
(4)在Hive中建立Hive外部表,指向前面對應的HDFS文件
[hdfs@cent-1 ~]$ hive Logging initialized using configuration in jar:file:/opt/cloudera/parcels/CDH-5.4.8-1.cdh5.4.8.p0.4/jars/hive-common-1.1.0-cdh5.4.8.jar!/hive-log4j.properties WARNING: Hive CLI is deprecated and migration to Beeline is recommended. hive> create external table test_tbl (name string,age int,sex string) row format delimited fields terminated by ',' location '/hive/test/test_tbl'; OK Time taken: 0.085 seconds hive> show tables; OK test_tbl Time taken: 0.364 seconds, Fetched: 1 row(s)
(5)加載數據到Trafodion表
(1)在Hive中建立測試表並插入幾條數據
(2)Trafodion中的表繼續沿用上述測試表,先清空數據
(3)從Hive加載數據到Trafodion
(注:在作從hive到trafodion數據加載的時候有幾個參數能夠配置,用來提高加載性能,如HIVE_MAX_STRING_LENGTH、ALLOW_INCOMPATIBLE_ASSIGNMENT
典型的用法以下:
CQD HIVE_MAX_STRING_LENGTH '1000'; --用於當列的最大長度爲1KB時 CQD ALLOW_INCOMPATIBLE_ASSIGNMENT 'ON'; --用於當有時間相關的列的時候
(1)在集羣上安裝並啓動Sqoop,Sqoop安裝步驟請參照其官方文檔點擊打開連接
(2)下載mysql connector Jar包,並放到/var/lib/sqoop目錄下
(3)在某臺機器上安裝Mysql,安裝Mysql步驟此處不做詳細說明
(4)在mysql數據中建立測試表,並插入幾條數據
(5)使用sqoop命令從mysql往hive導入數據
[hdfs@cent-2 ~]$ sqoop import --connect jdbc:mysql://localhost:3306/test --driver com.mysql.jdbc.Driver --username centos --table test_tbl --split-by name --hive-import --create-hive-table --hive-table test_tabl_hive Warning: /opt/cloudera/parcels/CDH-5.4.8-1.cdh5.4.8.p0.4/bin/../lib/sqoop/../accumulo does not exist! Accumulo imports will fail. ... Logging initialized using configuration in jar:file:/opt/cloudera/parcels/CDH-5.4.8-1.cdh5.4.8.p0.4/jars/hive-common-1.1.0-cdh5.4.8.jar!/hive-log4j.properties OK Time taken: 3.052 seconds Loading data to table default.test_tabl_hive Table default.test_tabl_hive stats: [numFiles=4, totalSize=27] OK Time taken: 0.748 seconds
(6)從Trafodion或Hive Shell中查看數據是否導入成功
做者:Post_yuan,易鯨捷資深交付工程師。