参考网址(谢谢大佬的分享):
https://blog.csdn.net/vah101/article/details/22597341
https://blog.csdn.net/weixin_30773135/article/details/96920115
https://blog.csdn.net/vah101/article/details/22597341
案例:hive 2.1.1+hbase1.6.0
如何查看hive的版本?
如何查看hbase的版本
0.准备工作:
(1).将hbase的zk配置,添加到hive中
修改hive-site.xml文件,将以下配置信息,添加到最后
<configuration>
<property>
<name>hive.metastore.warehouse.dir</name>
<value>/user/hive_test/warehouse</value>
</property>
<property>
<name>javax.jdo.option.ConnectionURL</name>
<value>jdbc:mysql://c/hive_test?createDatabaseIfNotExist=true</value>
</property>
<property>
<name>javax.jdo.option.ConnectionDriverName</name>
<value>com.mysql.jdbc.Driver</value>
</property>
<property>
<name>javax.jdo.option.ConnectionUserName</name>
<value>root</value>
</property>
<property>
<name>javax.jdo.option.ConnectionPassword</name>
<value>root</value>
</property>
<property>
<name>hbase.zookeeper.quorum</name>
<value>c1:2181,c2:2181,c3:2181</value>
</property>
</configuration>
(2).将hbase的包导入到hive的环境变量中
修改hive-env.sh文件,将以下配置信息,添加到最后
# Set HADOOP_HOME to point to a specific hadoop install directory HADOOP_HOME=/usr/local/hadoop-2.7.1 # Hive Configuration Directory can be controlled by: export HIVE_CONF_DIR=/opt/hive/conf # Folder containing extra ibraries required for hive compilation/execution can be controlled by: # export HIVE_AUX_JARS_PATH= export HIVE_CLASSPATH=$HIVE_HOME/lib/*:/usr/local/hbase/lib/*
1.hbase表映射到hive表中
(1)创建hbase表
create 'hbase_test',{NAME=>'f1',VERSION=>1},{NAME=>'f2',VERSION=>1},{NAME=>'f3',VERSION=>1}
(2)向hbase表中添加数据
put 'hbase_test','r1','f1:name','zhangsan' put 'hbase_test','r1','f2:age','12' put 'hbase_test','r1','f3:sex','boy' put 'hbase_test','r2','f1:name','lisi' put 'hbase_test','r2','f2:age','12' put 'hbase_test','r2','f3:sex','boy' put 'hbase_test','r3','f1:name','wangwu' put 'hbase_test','r3','f2:age','12' put 'hbase_test','r3','f3:sex','girl'
(3)创建与hbase相关联的hive表
CREATE EXTERNAL TABLE hiveFromHbase( rowkey string, f1 map<STRING,STRING>, f2 map<STRING,STRING>, f3 map<STRING,STRING> ) STORED BY 'org.apache.hadoop.hive.hbase.HBaseStorageHandler' WITH SERDEPROPERTIES ("hbase.columns.mapping" = ":key,f1:,f2:,f3:") TBLPROPERTIES ("hbase.table.name" = "hbase_test");
创建的外部表,即使删除hive的外部表,也不会影响到hbase的表和数据,desc formatted hiveFromHbase查看表数据的hdfs的路径,只是创建了一个目录,并没有数据。
即,只是创建了一个hive的链接,可以查询hbase的数据。
- 删除hive表对hbase没有影响;
- 但是先删除hbase表hive就会报TableNotFoundException;
(4)查看hive表
2.hive表映射到hbase表中
(1)创建hive表,关联到hbase表,hbase表会新建
CREATE TABLE hive_test( id int, name string, age int, sex string ) STORED BY 'org.apache.hadoop.hive.hbase.HBaseStorageHandler' WITH SERDEPROPERTIES ("hbase.columns.mapping" = ":key,info:name,info:age,info:sex") TBLPROPERTIES ("hbase.table.name" = "hbaseFromHive");
创建的内部表,内部表指向hbase表。当hive表删除时,hbase的表也不存在了;当hbase的表删除时,hive表也会删除
- 当hive删除hive表时,hbase表也会删除。
- 当先删除hbase的时候,先disabled table,然后drop table;这时hbase表就被删除了,zookeeper里面也就删除了。但是hive里面还在,用show tables还能查出来。mysql中TBLS里面还有hive表的信息。但是用select * from hive 查询的时候报错,表不存在(TableNotFoundException)然后删除hive里面的表的时候会报错TableNotFoundException)。继续show tables时,发现表已经不在了。TBLS里面也没有hive表了
注意:不能直接使用load data local inpath '/opt/data.txt' into table hive_test;
会报错:
FAILED:SemanticException [Error 10101]: A non-native table cannot be used as targetfor LOAD
(2)创建一张本地hive表,将数据导入本地hive表
CREATE TABLE hive_test2( id int, name string, age int, sex string )row format delimited fields terminated by ' ' stored as textfile;
load data local inpath '/opt/data.txt' into table hive_test2;
(3)将本地hive表的数据,导入到hive表
insert into hive_test select * from hive_test2;
(4)查看hbase表
总结:
hive和hbase的整合,本质上是运用了org.apache.hadoop.hive.hbase.HBaseStorageHandler类,在运用时,需要注意以下几点
(1)hive和hbase的版本需要匹配
(2)hive需要修改的配置信息要修改正确
(3)hbase.columns.mapping表示hive和hbase的映射关系,第一个是rowkey值,后面是列族:列
例如:"hbase.columns.mapping" = ":key,info:name,info:age,info:sex"
(4)hbase.table.name表示hbase表的名字
注意:
在hive1.2.1 跟hbase 0.98整合时,需要添加:
"hbase.mapred.output.outputtable" = "hbaseFromhive" 表属性
TBLPROPERTIES ("hbase.table.name" = "hbaseFromhive","hbase.mapred.output.outputtable"="hbaseFromhive");
如果不添加会报错:Must specify table name
hive+hbase这种方式,无论哪种,数据都是在hbase中存放的,hive只会在hdfs上创建目录,不会生成真正的数据文件;