设为首页 收藏本站
查看: 1252|回复: 0

[经验分享] 26.Hive整合Hbase

[复制链接]

尚未签到

发表于 2017-11-22 16:30:41 | 显示全部楼层 |阅读模式
0.系统版本信息



OS:Debian-7.9
Jdk:1.8.0_131
hadoop:2.8.1
zookeeper:3.4.9
hbase:1.3.1
hive:2.1.1
  主机信息



192.168.74.128  master
192.168.74.132  slave-2
192.168.74.133  slave-3
192.168.74.134  slave-4
1.修改hive配置文件,需要hadoop,zookeeper,hbase全都启动,创建相关的测试表
  A:hive-site.xml文件添加配置



<property>  
<name>hbase.zookeeper.quorum</name>  
<value>master,slave-2,slave-3,slave-4</value>  
<description></description>  
</property>
  B:在hive安装目录bin下执行./hive



cd /home/hadoop/opt/hive-2.1.1/bin
./hive
#或者执行
/home/hadoop/opt/hive-2.1.1/bin/hive
  C:创建hbase识别的表



hive>
CREATE TABLE hbase_table_1(key int, value string)      
STORED BY 'org.apache.hadoop.hive.hbase.HBaseStorageHandler'   
WITH SERDEPROPERTIES ("hbase.columns.mapping" = ":key,cf1:val")     
TBLPROPERTIES ("hbase.table.name" = "table_1");  
DSC0000.png

  D:hbase shell:list发现有新创建的table_1
DSC0001.png


2.hbase表不存在,然后通过hive创建关联表,插入数据进行测试
  主要有两种方式
  1:通过hive插入数据,在hbase中可以查到相关数据
  2:通过hbase插入数据,在hive中可以查询到
  A:在hive中创建临时表导入数据



hive> create table ccc(foo int,bar string) row format delimited fields terminated by '\t' lines terminated by '\n' stored as textfile;
DSC0002.png

  B:创建数据文件data.txt



touch /home/hadoop/software/data.txt
vim data.txt
1   zhangsan
2   lisi
3   wangwu
  C:将数据导入临时的hive表中,数据字段之间是tab键分隔,数据条数之间使用换行符分隔



hive>load data local inpath '/home/hadoop/software/data.txt' overwrite into table ccc;
DSC0003.png

  D:将临时表中的数据导入到hbase中



hive> insert overwrite table hbase_table_1 select * from ccc where foo=1;
DSC0004.png

  E:在hbas-shell中查看数据是否插入成功
DSC0005.png

  这样就说明了插入通过hive插入的数据实际上最后保存到了habse的表中去了
  下面证明的通过hbase插入的记录也可以通过hive查询出来,在hbase-shell中插入数据



hbase>put 'table_1','4','cf1:val','zhaoliu'
hbase>scan 'table_1'
DSC0006.png




hive>select * from hbase_table_1;
DSC0007.png


3.hbase表存在,然后通过hive创建关联表,插入数据进行测试
  A:在hbase-shell中创建新的表,并插入新的数据



hbase>create 'student','info'
hbase>put "student",'1','info:name','tom'
hbase>put "student",'2','info:name','lily'
hbase>put "student",'3','info:name','wwn'
hbase>scan 'student'
DSC0008.png

  B:创建hive关联关系表读取hbase中的数据



hive>CREATE EXTERNAL TABLE hbase_table_2(key int, value string)      
STORED BY 'org.apache.hadoop.hive.hbase.HBaseStorageHandler'   
WITH SERDEPROPERTIES ("hbase.columns.mapping" = "info:name")     
TBLPROPERTIES("hbase.table.name" = "student");
DSC0009.png


4.hive和hbase多列多族的问题
  下面在hbase中创建这样一张表customer

rowkeyaddressinfocontact
provincecitycountryagecompanyphone
zhangsanhubeiwuhanchina24douyu101
lisiguangdongguangzhouchina24netease102



hbase>create 'customer','address','info', 'contact'
hbase>put 'customer','zhangsan','contact:phone','101'
hbase>put 'customer','zhangsan','address:province','hubie'
hbase>put 'customer','zhangsan','address:city','wuhan'
hbase>put 'customer','zhangsan','address:country','china'
hbase>put 'customer','zhangsan','info:age','24'
hbase>put 'customer','zhangsan','info:company','douyu'
hbase>put 'customer','lisi','contact:phone','102'
hbase>put 'customer','lisi','address:province','guangdong'
hbase>put 'customer','lisi','address:city','guangzhou'
hbase>put 'customer','lisi','address:country','china'
hbase>put 'customer','lisi','info:age','24'
hbase>put 'customer','lisi','info:company','netease'
DSC00010.png




hive>CREATE EXTERNAL TABLE hbase_table_3(key string, province string, city string, country string, age int, company string, phone string)      
STORED BY 'org.apache.hadoop.hive.hbase.HBaseStorageHandler'   
WITH SERDEPROPERTIES ("hbase.columns.mapping" = ":key,address:province, address:city, address:country, info:age, info:company,contact:phone")     
TBLPROPERTIES("hbase.table.name" = "customer");
DSC00011.png

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-409559-1-1.html 上篇帖子: Linux+树莓派3开发总结——树莓派远程文件共享winows 下篇帖子: 哪款Linux桌面环境是你的最佳选择?
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表