从关系库导入数据到hive-hbase表中

8 篇文章 0 订阅

[Author]: kwu

从关系库导入数据到hive-hbase表中,关系库到hbase中,可以直接由sqoop来导入,但是我们要对rowkey做特殊处理并加入更新时间,则操作步骤如下:

1、创建hive与hbase的表

1)创建hbase表

命令行输入 hbase shell 进入hbase的终端:

[plain]  view plain copy
  1. create 'location','cf1'  

查询hbase创建的表

[plain]  view plain copy
  1. list  

如需删除表

[plain]  view plain copy
  1. disable 'location'  
  2. drop 'location'  



2)创建hive的外表

[plain]  view plain copy
  1. hive -e"  
  2. drop TABLE ods.hbase_location;  
  3. CREATE EXTERNAL  TABLE ods.hbase_location(key string ,  
  4.         ID int comment '唯一ID',  
  5.         location1 string  comment '国家代号' ,  
  6.         location2 string  comment '省份代号' ,  
  7.         location3 string  comment '城市代号',  
  8.         country string  comment '国家(中文)',  
  9.         cn string  comment '省份(中文)',  
  10.         cn_city string  comment '城市(中文)',  
  11.         cn_district string  comment '城市所属地区(中文)',  
  12.         py string  comment '省份缩略(拼音)',  
  13.         en_city string comment '城市(英文)',  
  14.         en_district string comment '城市所属地区(英文)',  
  15.         en string comment '省份(英文)',  
  16.         geoid INT comment '行政区划代码',  
  17.         updatetime  string comment '更新时间'  
  18. )  
  19. STORED BY 'org.apache.hadoop.hive.hbase.HBaseStorageHandler'     
  20. WITH SERDEPROPERTIES (\"hbase.columns.mapping\" = \":key,  
  21. cf1:ID,  
  22. cf1:location1,  
  23. cf1:location2,  
  24. cf1:location3,  
  25. cf1:country,  
  26. cf1:cn,  
  27. cf1:cn_city,   
  28. cf1:cn_district,  
  29. cf1:py,  
  30. cf1:en_city,  
  31. cf1:en_district,  
  32. cf1:en,  
  33. cf1:geoid,  
  34. cf1:updatetime\")     
  35. TBLPROPERTIES(\"hbase.table.name\" = \"location\"); "  

2、自动化的shell脚本

[plain]  view plain copy
  1. #!/bin/sh    
  2. # upload logs to hdfs    
  3.   
  4. today=`date --date='0 days ago' +%Y-%m-%d`  
  5. hive -e"  
  6. drop table stage.location_tmp;  
  7. CREATE TABLE  
  8.     stage.location_tmp  
  9.     (  
  10.         ID INT comment '唯一ID',  
  11.         location1 string  comment '国家代号' ,  
  12.         location2 string  comment '省份代号' ,  
  13.         location3 string  comment '城市代号',  
  14.         country string  comment '国家(中文)',  
  15.         cn string  comment '省份(中文)',  
  16.         cn_city string  comment '城市(中文)',  
  17.         cn_district string  comment '城市所属地区(中文)',  
  18.         py string  comment '省份缩略(拼音)',  
  19.         en_city string comment '城市(英文)',  
  20.         en_district string comment '城市所属地区(英文)',  
  21.         en string comment '省份(英文)',  
  22.         geoid INT comment '行政区划代码'  
  23.     )  
  24. ROW FORMAT DELIMITED fields terminated by '\001'  
  25. STORED AS TEXTFILE;  
  26. "  
  27.   
  28. sqoop import --connect jdbc:mysql://10.130.2.6:3306/bdc_test --username lvwenjuan --password Abcd1234  --table location --where "1=1" --columns "ID ,location1 ,location2 ,location3 ,country ,cn ,cn_city , cn_district ,py , en_city , en_district ,en , geoid " --fields-terminated-by '\001' --hive-import --hive-drop-import-delims --hive-table stage.location_tmp -m 1  
  29.   
  30.   
  31. hive -e"  
  32. insert into table ods.hbase_location select reverse(ID) ,  
  33. ID ,  
  34. location1 ,  
  35. location2  ,  
  36. location3 ,  
  37. country,  
  38. cn,  
  39. cn_city,  
  40. cn_district,  
  41. py,  
  42. en_city,  
  43. en_district,  
  44. en string,  
  45. geoid ,  
  46. udf_getbfhourstime(0) from stage.location_tmp;"  
  47.   
  48.   
  49. hive -e "drop TABLE stage.location_tmp;"  


3、说明

1) stage.location_tmp为临时中转表,本次ETL完后即删除。

2)--where "1=1" 可设置关系库的查询语句

3)reverse(ID) 对hbase的rowkey自动逆序处理

4)insert into 到hbase中自动根据rowkey来去重

5)udf_getbfhourstime(0) 自定义函数,取的是当前时间

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值