sqoop操作之ORACLE匯入到HIVE

來源:互聯網
上載者:User

標籤:des   style   blog   color   java   使用   os   io   

匯入表的所有欄位

sqoop import --connect jdbc:oracle:thin:@192.168.1.107:1521:ORCL \--username SCOTT --password tiger \--table EMP \--hive-import  --create-hive-table --hive-table emp  -m 1;

 

如果報類似的錯:

ERROR tool.ImportTool: Encountered IOException running import job: org.apache.hadoop.mapred.FileAlreadyExistsException: Output directory EMP already exists

 

先去HDFS系統中刪除該檔案: hadoop fs -rmr /user/hadoop/EMP

如果報類似的錯:

FAILED: Error in metadata: AlreadyExistsException(message:Table emp already exists)

如果報類似的錯:

hive.HiveImport: Exception in thread "main" java.lang.NoSuchMethodError: org.apache.thrift.EncodingUtils.setBit(BIZ)B

這是因為在同路徑下安裝了hive和hbase,而hbase和hive的lib目錄下的thrift版本不同。
hbase下的為libthrift-0.x.0.jar,hive下的為libthrift-0.x.0.jar。將Hbase下的0.x.0版的刪除,換為0.x.0的即可。
ps:不知為什麼Sqoop向Hive中匯入資料還有Hbase的事

 

說明:hive表已經存在,需要先刪除

查看:

desc emp;empno   doubleename   stringjob     stringmgr     doublehiredate        stringsal     doublecomm    doubledeptno  double

select * from emp;7369.0 SMITH CLERK 7902.0 1980-12-17 00:00:00.0 800.0 NULL 20.07499.0 ALLEN SALESMAN 7698.0 1981-02-20 00:00:00.0 1600.0 300.0 30.07521.0 WARD SALESMAN 7698.0 1981-02-22 00:00:00.0 1250.0 500.0 30.07566.0 JONES MANAGER 7839.0 1981-04-02 00:00:00.0 2975.0 NULL 20.07654.0 MARTIN SALESMAN 7698.0 1981-09-28 00:00:00.0 1250.0 1400.0 30.0……

 

註:一般情況下不使用--create-hive-table去建立表的,因為它建立的表的欄位格式,不符合我們的要求。

 

匯入表的指定欄位

手工建立hive表:

create table emp_column(empno int,ename string,job string,mgr int,hiredate string,sal double,comm double,deptno int)row format delimited fields terminated by ‘\t‘ lines terminated by ‘\n‘ stored as textfile;
sqoop import --connect jdbc:oracle:thin:@192.168.1.107:1521:ORCL \--username SCOTT --password tiger \--table EMP --columns "EMPNO,ENAME,JOB,SAL,COMM" \--fields-terminated-by ‘\t‘ --lines-terminated-by ‘\n‘ \--hive-drop-import-delims --hive-import  --hive-table emp_column \-m 3;

 

說明:重新再執行,每重複匯入一次,hive中的資料會重複匯入。

sqoop import --connect jdbc:oracle:thin:@192.168.1.107:1521:ORCL \--username SCOTT --password tiger \--table EMP --columns "EMPNO,ENAME,JOB,SAL,COMM" \--fields-terminated-by ‘\t‘ --lines-terminated-by ‘\n‘ \--hive-drop-import-delims --hive-overwrite --hive-import --hive-table emp_column \-m 3;

 

註:--hive-overwrite指定覆蓋表裡已經存在的記錄,99%都是要使用overwrite的,避免重跑時產生重複資料。

 

匯入表的指定欄位到hive分區表

建立hive分區表:

create table emp_partition(empno int,ename string,job string,mgr int,hiredate string,sal double,comm double,deptno int)partitioned by (pt string)row format delimited fields terminated by ‘\t‘ lines terminated by ‘\n‘ stored as textfile;

匯入pt=‘2013-08-01‘

sqoop import --connect jdbc:oracle:thin:@192.168.1.107:1521:ORCL \--username SCOTT --password tiger \--table EMP --columns "EMPNO,ENAME,JOB,SAL,COMM" \--hive-overwrite --hive-import  --hive-table emp_partition \--fields-terminated-by ‘\t‘ --lines-terminated-by ‘\n‘ \--hive-drop-import-delims --hive-partition-key ‘pt‘ --hive-partition-value ‘2013-08-01‘ \-m 3;

 

匯入pt=‘2013-08-02‘

sqoop import --connect jdbc:oracle:thin:@192.168.1.107:1521:ORCL \--username SCOTT --password tiger \--table EMP --columns "EMPNO,ENAME,JOB,SAL,COMM" \--hive-overwrite --hive-import  --hive-table emp_partition \--fields-terminated-by ‘\t‘ --lines-terminated-by ‘\n‘ \--hive-drop-import-delims  --hive-partition-key ‘pt‘ --hive-partition-value ‘2013-08-02‘ \-m 3;

 

查詢:

select * from emp_partition where pt=‘2013-08-01‘;select * from emp_partition where pt=‘2013-08-02‘;

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.