使用Talend Open Studio將數據分步從oracle導入到hive中


先使用Tos建立模型,將Oracle中的數據導入到本地:

image

build job后,形成獨立可以運行的程序:

image

image

將生成的zip文件,上傳到hadoop集群上,有hive環境的機器上:

[hive@h1 work]$ ls
file.zip  jobInfo.properties  join  lib
[hive@h1 work]$ cd join/
[hive@h1 join]$ ls
bigdatademo  items  join_0_1.jar  join_run.bat  join_run.sh  src  user_activity2
[hive@h1 join]$ pwd
/home/work/join
[hive@h1 join]$ ls
bigdatademo  items  join_0_1.jar  join_run.bat  join_run.sh  src  user_activity2
[hive@h1 join]$ pwd
/home/work/join
[hive@h1 join]$ ./join_run.sh > user_activity2 2>&1 &

這樣就得到了SQL語句執行的結果,存放在user_activity2中。

hive建表語句:

image

 

hive> show create table user_activity2;
OK
CREATE TABLE `user_activity2`(
  `user_id` string, 
  `user_name` string, 
  `sex` string, 
  `age` string, 
  `reg_hosp` string, 
  `reg_community` string, 
  `type` string, 
  `disease_code` string, 
  `disease` string, 
  `doctor` string, 
  `hosp_name` string, 
  `service_id` string, 
  `drug_id` string, 
  `drug_name` string, 
  `antibiotic` string, 
  `hormone` string, 
  `source` string, 
  `base_drug` string, 
  `community` string, 
  `date` string)
ROW FORMAT DELIMITED 
  FIELDS TERMINATED BY '|' 
STORED AS INPUTFORMAT 
  'org.apache.hadoop.mapred.TextInputFormat' 
OUTPUTFORMAT 
  'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat'
LOCATION
  'hdfs://h1:8020/apps/hive/warehouse/cyw.db/user_activity2'
TBLPROPERTIES (
  'transient_lastDdlTime'='1435547544')
Time taken: 0.288 seconds, Fetched: 31 row(s)

將數據導入到hive表中:load data local inpath './user_activity2' into table user_activity2;

hive> show tables;                                                         
OK
Time taken: 0.794 seconds
hive> use cyw;
OK
Time taken: 0.256 seconds
hive> show tables;
OK
user_activity
user_activity2
Time taken: 0.136 seconds, Fetched: 2 row(s)
hive> load data local inpath './user_activity2' into table  user_activity2;
Loading data to table cyw.user_activity2
Table cyw.user_activity2 stats: [numFiles=1, totalSize=216927483]
OK
Time taken: 10.898 seconds
hive> select * from user_activity2;
OK
F805418B-335F-4CA3-A209-7C9655148146    余澤英  2       47      成都高新區合作社區衛生服務中心  合作    1               急性支氣管炎    譚萬龍  成都高新區合作社區衛生服務中心  1E972231-C65A-4CE3-9233-8EA1B18058DE  滅菌注射用水    d875aacf-4723-4777-91ec-12d63732b58f    0       0       其他            合作    2014-02-27
F805418B-335F-4CA3-A209-7C9655148146    余澤英  2       47      成都高新區合作社區衛生服務中心  合作

查詢語句:

select a.個人id,
       b.姓名,
       b.性別,
       round((sysdate - b.出生日期) / 365) as fage,
       b.建檔單位,
       replace(replace(replace(b.建檔單位, '高新區'), '社區衛生服務中心'),
               '成都') 建檔社區,
       1 as ftype,
       a.問題編碼,
       a.問題名稱,
       a.處理醫生,
       c.機構名,
       a.服務記錄id,
       f.名稱,
       f.id 葯品ID ,
       f.抗生素, 
       f.激素類葯,
       case when f.葯品來源 is null then '其他' else f.葯品來源 end 葯品來源,       
       f.基葯分類,
       replace(replace(replace(c.機構名, '高新區'), '社區衛生服務中心'),'成都') 診療社區, 
       to_char(a.發現日期, 
'yyyy-mm-dd') 診療日期
  from ZLCHS.個人問題列表 a,
       ZLCHS.個人信息 b,
       ZLCHS.服務活動記錄 c,
       (select d.事件id, e.名稱, e.id, h.葯品來源, h.基葯分類, g.抗生素, g.激素類葯
          from ZLCHS.個人費用記錄 d, ZLCHS.收費項目目錄 e, ZLCHS.葯品規格 h, ZLCHS.葯品特性 g
         where d.收費項目id = e.id
           and d.收據費目 in ('西葯費', '中草葯費', '中成葯費')
           and h.葯品id(+) = e.id
           and h.葯名id = g.葯名id) f                 
 where a.個人id = b.id(+)
   and a.服務記錄id = c.id(+)
   and a.服務記錄id = f.事件id(+)

 

加入分區字段:

CREATE TABLE `user_activity`(
  `user_id` string, 
  `user_name` string, 
  `sex` string, 
  `age` string, 
  `reg_hosp` string, 
  `reg_community` string, 
  `type` string, 
  `disease_code` string, 
  `disease` string, 
  `doctor` string, 
  `hosp_name` string, 
  `service_id` string, 
  `drug_id` string, 
  `drug_name` string, 
  `antibiotic` string, 
  `hormone` string, 
  `source` string, 
  `base_drug` string, 
  `community` string, 
  `date` string)
PARTITIONED BY ( 
  `dt` string)
ROW FORMAT DELIMITED 
  FIELDS TERMINATED BY '|' 
STORED AS INPUTFORMAT 
  'org.apache.hadoop.mapred.TextInputFormat' 
OUTPUTFORMAT 
  'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat'
LOCATION
  'hdfs://h1:8020/apps/hive/warehouse/cyw.db/user_activity'
TBLPROPERTIES (
  'transient_lastDdlTime'='1435559269')
Time taken: 0.252 seconds, Fetched: 33 row(s)

 

默認的字段分隔符為ascii碼的控制符\001,建表的時候用fields terminated by '\001',如果要測試的話,造數據在vi 打開文件里面,用ctrl+v然后再ctrl+a可以輸入這個控制符\001。按順序,\002的輸入方式為ctrl+v,ctrl+b。以此類推。

image

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM