目录
对比
整合
需求一
将hive分析结果的数据,保存到HBase当中
步骤
一、将HBase的五个jar包拷贝到Hive的lib目录下
HBase的jar包在/export/servers/hbase-1.2.0-cdh5.14.0/lib
,我们要用的五个jar包
hbase-client-1.2.0-cdh5.14.0.jar
hbase-hadoop2-compat-1.2.0-cdh5.14.0.jar
hbase-hadoop-compat-1.2.0-cdh5.14.0.jar
hbase-it-1.2.0-cdh5.14.0.jar
hbase-server-1.2.0-cdh5.14.0.jar
可以选择创建软连接,也可以直接拷贝,我这里选择的是软连接
ln -s /export/servers/hbase-1.2.0-cdh5.14.0/lib/hbase-client-1.2.0-cdh5.14.0.jar /export/servers/hive-1.1.0-cdh5.14.0/lib/hbase-client-1.2.0-cdh5.14.0.jar
ln -s /export/servers/hbase-1.2.0-cdh5.14.0/lib/hbase-hadoop2-compat-1.2.0-cdh5.14.0.jar /export/servers/hive-1.1.0-cdh5.14.0/lib/hbase-hadoop2-compat-1.2.0-cdh5.14.0.jar
ln -s /export/servers/hbase-1.2.0-cdh5.14.0/lib/hbase-hadoop-compat-1.2.0-cdh5.14.0.jar /export/servers/hive-1.1.0-cdh5.14.0/lib/hbase-hadoop-compat-1.2.0-cdh5.14.0.jar
ln -s /export/servers/hbase-1.2.0-cdh5.14.0/lib/hbase-it-1.2.0-cdh5.14.0.jar /export/servers/hive-1.1.0-cdh5.14.0/lib/hbase-it-1.2.0-cdh5.14.0.jar
ln -s /export/servers/hbase-1.2.0-cdh5.14.0/lib/hbase-server-1.2.0-cdh5.14.0.jar /export/servers/hive-1.1.0-cdh5.14.0/lib/hbase-server-1.2.0-cdh5.14.0.jar
二、修改hive的配置文件
把第三台服务器上的hive配置文件hive-site.xml
和hive-env.sh
cd /export/servers/hive-1.1.0-cdh5.14.0/conf
vim hive-site.xml
<property>
<name>hive.zookeeper.quorum</name>
<value>node01,node02,node03</value>
</property>
<property>
<name>hbase.zookeeper.quorum</name>
<value>node01,node02,node03</value>
</property>
cd /export/servers/hive-1.1.0-cdh5.14.0/conf
vim hive-env.sh
export HADOOP_HOME=/export/servers/hadoop-2.6.0-cdh5.14.0
export HBASE_HOME=/export/servers/hbase-1.2.0-cdh5.14.0
export HIVE_CONF_DIR=/export/servers/hive-1.1.0-cdh5.14.0/conf
三、在Hive中建表
进入hive客户端
cd /export/servers/hive-1.1.0-cdh5.14.0/
bin/hive
创建hive数据库与hive对应的数据库表
CREATE DATABASE course;
USE course;
CREATE external TABLE IF NOT EXISTS course.score(
id INT,cname string,score INT)
ROW FORMAT DELIMITED FIELDS TERMINATED BY ' '
STORED AS textfile;
在/export
目录下创建需要加载的数据
cd /export/
vim hive-hbase.txt
中间要用
分割
1 zhangsan 80
2 lisi 60
3 wangwu 30
4 zhaoliu 70
加载数据
hive (course)> load data local inpath '/export/hive-hbase.txt' into table score;
hive (course)> select * from score;
四、创建hive管理表与HBase映射
hive当中创建内部表
CREATE TABLE course.hbase_score(
id INT,cname STRING,score INT)
STORED BY 'org.apache.hadoop.hive.hbase.HBaseStorageHandler'
WITH serdeproperties("hbase.columns.mapping" = "cf:name,cf:score")
tblproperties("hbase.table.name" = "hbase_score");
向内部表中插入数据
INSERT OVERWRITE TABLE course.hbase_score
SELECT id,cname,score FROM course.score;
五、在HBase中查看hbase_score
需求二
创建hive外部表,映射HBase当中已有的表模型,直接通过
步骤
一、在HBase中创建表并插入数据
create 'hbase_hive_score',{ NAME =>'cf'}
put 'hbase_hive_score','1','cf:name','zhangsan'
put 'hbase_hive_score','1','cf:score', '95'
put 'hbase_hive_score','2','cf:name','lisi'
put 'hbase_hive_score','2','cf:score', '96'
put 'hbase_hive_score','3','cf:name','wangwu'
put 'hbase_hive_score','3','cf:score', '97'
二、建立hive外部表,映射HBase中的表及字段
CREATE external TABLE course.hbase2hive(
id int, name string, score int)
STORED BY'org.apache.hadoop.hive.hbase.HBaseStorageHandler'
WITH SERDEPROPERTIES ("hbase.columns.mapping" = ":key,cf:name,cf:score")
TBLPROPERTIES("hbase.table.name" ="hbase_hive_score");