偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

利用SQOOP將數(shù)據(jù)從數(shù)據(jù)庫(kù)導(dǎo)入到HDFS

企業(yè)動(dòng)態(tài)
本文程序?qū)氲紿DFS中的數(shù)據(jù)是文本格式,所以在創(chuàng)建Hive外部表的時(shí)候,不需要指定文件的格式為RCFile,而使用默認(rèn)的TextFile即可。數(shù)據(jù)間的分隔符為'\001'.如果多次導(dǎo)入同一個(gè)表中的數(shù)據(jù),數(shù)據(jù)以append的形式插入到HDFS目錄中。

基本使用

如下面這個(gè)shell腳本:

  1. #Oracle的連接字符串,其中包含了Oracle的地址,SID,和端口號(hào) 
  2. CONNECTURL=jdbc:oracle:thin:@20.135.60.21:1521:DWRAC2 
  3. #使用的用戶名 
  4. ORACLENAME=kkaa 
  5. #使用的密碼 
  6. ORACLEPASSWORD=kkaa123 
  7. #需要從Oracle中導(dǎo)入的表名 
  8. oralceTableName=tt 
  9. #需要從Oracle中導(dǎo)入的表中的字段名 
  10. columns=AREA_ID,TEAM_NAME 
  11. #將Oracle中的數(shù)據(jù)導(dǎo)入到HDFS后的存放路徑 
  12. hdfsPath=apps/as/hive/$oralceTableName 
  13. #執(zhí)行導(dǎo)入邏輯。將Oracle中的數(shù)據(jù)導(dǎo)入到HDFS中 
  14. sqoop import --append --connect $CONNECTURL --username $ORACLENAME --password $ORACLEPASSWORD --target-dir $hdfsPath  --num-mappers 1 --table $oralceTableName --columns $columns --fields-terminated-by '\001' 

 

執(zhí)行這個(gè)腳本之后,導(dǎo)入程序就完成了。

接下來(lái),用戶可以自己創(chuàng)建外部表,將外部表的路徑和HDFS中存放Oracle數(shù)據(jù)的路徑對(duì)應(yīng)上即可。

注意:這個(gè)程序?qū)氲紿DFS中的數(shù)據(jù)是文本格式,所以在創(chuàng)建Hive外部表的時(shí)候,不需要指定文件的格式為RCFile,而使用默認(rèn)的TextFile即可。數(shù)據(jù)間的分隔符為'\001'.如果多次導(dǎo)入同一個(gè)表中的數(shù)據(jù),數(shù)據(jù)以append的形式插入到HDFS目錄中。

并行導(dǎo)入

假設(shè)有這樣這個(gè)sqoop命令,需要將Oracle中的數(shù)據(jù)導(dǎo)入到HDFS中:

  1. sqoop import --append --connect $CONNECTURL --username $ORACLENAME --password $ORACLEPASSWORD --target-dir $hdfsPath  --m 1 --table $oralceTableName --columns $columns --fields-terminated-by '\001'  --where "data_desc='2011-02-26'" 

請(qǐng)注意,在這個(gè)命令中,有一個(gè)參數(shù)"-m",代表的含義是使用多少個(gè)并行,這個(gè)參數(shù)的值是1,說(shuō)明沒(méi)有開(kāi)啟并行功能。

現(xiàn)在,我們可以將"-m"參數(shù)的值調(diào)大,使用并行導(dǎo)入的功能,如下面這個(gè)命令:

  1. sqoop import --append --connect $CONNECTURL --username $ORACLENAME --password $ORACLEPASSWORD --target-dir $hdfsPath  --m 4 --table $oralceTableName --columns $columns --fields-terminated-by '\001'  --where "data_desc='2011-02-26'" 

一般來(lái)說(shuō),Sqoop就會(huì)開(kāi)啟4個(gè)進(jìn)程,同時(shí)進(jìn)行數(shù)據(jù)的導(dǎo)入操作。

但是,如果從Oracle中導(dǎo)入的表沒(méi)有主鍵,那么會(huì)出現(xiàn)如下的錯(cuò)誤提示:

  1. ERROR tool.ImportTool: Error during import: No primary key could be found for table creater_user.popt_cas_redirect_his. Please specify one with --split-by or perform a sequential import with '-m 1'. 

在這種情況下,為了更好的使用Sqoop的并行導(dǎo)入功能,我們就需要從原理上理解Sqoop并行導(dǎo)入的實(shí)現(xiàn)機(jī)制。

如果需要并行導(dǎo)入的Oracle表的主鍵是id,并行的數(shù)量是4,那么Sqoop首先會(huì)執(zhí)行如下一個(gè)查詢:

  1. select max(id) as maxselect min(id) as min from table [where 如果指定了where子句]; 

通過(guò)這個(gè)查詢,獲取到需要拆分字段(id)的***值和最小值,假設(shè)分別是1和1000.

然后,Sqoop會(huì)根據(jù)需要并行導(dǎo)入的數(shù)量,進(jìn)行拆分查詢,比如上面的這個(gè)例子,并行導(dǎo)入將拆分為如下4條SQL同時(shí)執(zhí)行:

  1. select * from table where 0 <= id < 250; 
  2. select * from table where 250 <= id < 500; 
  3. select * from table where 500 <= id < 750; 
  4. select * from table where 750 <= id < 1000;    

 

注意,這個(gè)拆分的字段需要是整數(shù)。

從上面的例子可以看出,如果需要導(dǎo)入的表沒(méi)有主鍵,我們應(yīng)該如何手動(dòng)選取一個(gè)合適的拆分字段,以及選擇合適的并行數(shù)。

再舉一個(gè)實(shí)際的例子來(lái)說(shuō)明:

我們要從Oracle中導(dǎo)入creater_user.popt_cas_redirect_his.

這個(gè)表沒(méi)有主鍵,所以我們需要手動(dòng)選取一個(gè)合適的拆分字段。

首先看看這個(gè)表都有哪些字段:

然后,我假設(shè)ds_name字段是一個(gè)可以選取的拆分字段,然后執(zhí)行下面的sql去驗(yàn)證我的想法:

  1. select min(ds_name), max(ds_name) from creater_user.popt_cas_redirect_his where data_desc='2011-02-26' 

發(fā)現(xiàn)結(jié)果不理想,min和max的值都是相等的。所以這個(gè)字段不合適作為拆分字段。

再測(cè)試一下另一個(gè)字段:CLIENTIP

  1. select min(CLIENTIP), max(CLIENTIP) from creater_user.popt_cas_redirect_his where data_desc='2011-02-26'  

這個(gè)結(jié)果還是不錯(cuò)的。所以我們使用CLIENTIP字段作為拆分字段。

所以,我們使用如下命令并行導(dǎo)入:

  1. sqoop import --append --connect $CONNECTURL --username $ORACLENAME --password $ORACLEPASSWORD --target-dir $hdfsPath  --m 12 --split-by CLIENTIP --table $oralceTableName --columns $columns --fields-terminated-by '\001'  --where "data_desc='2011-02-26'" 

這次執(zhí)行這個(gè)命令,可以看到,消耗的時(shí)間為:20mins, 35sec,導(dǎo)入了33,222,896條數(shù)據(jù)。

另外,如果覺(jué)得這種拆分不能很好滿足我們的需求,可以同時(shí)執(zhí)行多個(gè)Sqoop命令,然后在where的參數(shù)后面指定拆分的規(guī)則。如:

 

  1. sqoop import --append --connect $CONNECTURL --username $ORACLENAME --password $ORACLEPASSWORD --target-dir $hdfsPath  --m 1 --table $oralceTableName --columns $columns --fields-terminated-by '\001'  --where "data_desc='2011-02-26' logtime<10:00:00" 
  2. sqoop import --append --connect $CONNECTURL --username $ORACLENAME --password $ORACLEPASSWORD --target-dir $hdfsPath  --m 1 --table $oralceTableName --columns $columns --fields-terminated-by '\001'  --where "data_desc='2011-02-26' logtime>=10:00:00" 

 

從而達(dá)到并行導(dǎo)入的目的。

【本文為51CTO專欄作者“王森豐”的原創(chuàng)稿件,轉(zhuǎn)載請(qǐng)注明出處】

責(zé)任編輯:龐桂玉 來(lái)源: 神算子
相關(guān)推薦

2010-10-22 11:22:33

SQL Server數(shù)

2024-04-09 13:20:00

Excel數(shù)據(jù)庫(kù)數(shù)據(jù)

2020-11-13 11:12:59

Navicat

2010-06-01 13:47:19

2010-04-22 11:58:00

Oracle數(shù)據(jù)庫(kù)

2010-10-28 11:48:38

ORACLE數(shù)據(jù)導(dǎo)入

2021-09-09 17:41:54

MySQLNavicat工具

2011-04-08 10:43:08

mysql數(shù)據(jù)access數(shù)據(jù)庫(kù)

2009-04-10 09:06:16

Windows Emb

2025-03-31 08:20:00

SQL 查詢數(shù)據(jù)庫(kù)dsq

2021-06-01 21:55:33

物聯(lián)網(wǎng) IoTDB數(shù)據(jù)庫(kù)

2010-10-20 14:56:18

2021-05-07 05:54:43

數(shù)據(jù)庫(kù)數(shù)據(jù)湖數(shù)據(jù)

2012-02-21 10:10:16

2023-11-29 09:53:29

數(shù)據(jù)庫(kù)遷移SQL Server

2011-05-13 09:42:21

2010-05-19 15:01:14

MySQL數(shù)據(jù)導(dǎo)入

2018-10-15 13:57:38

Hadoop數(shù)據(jù)庫(kù)MySQL

2012-06-20 11:17:02

MemSQL

2021-04-16 07:19:04

Hive數(shù)據(jù)類型Hql
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)