hive创建 关联hbase表有2种形式:
一、建立hive内表,指向hbase:(数据是存在hbase中的)
通过下面的方式创建hive的内表,这种情况是hbase本来没有这张表,创建后会在hbase中同样创建一张表,将来数据也是存放在hbase中的;hdfs的hive表目录有hive文件夹,但是里面没有数据。
create tablehive_hbase_test(id string,name string, age int) stored by'org.apache.hadoop.hive.hbase.HBaseStorageHandler' with serdeproperties("hbase.columns.mapping" = ":key,cf:name,cf:age")tblproperties ("hbase.table.name" = "hive_hbase_test");
1、创建hive-hbase表:
hive> createtable hive_hbase_test(id string,name string, age int)
> stored by'org.apache.hadoop.hive.hbase.HBaseStorageHandler'
> with serdeproperties("hbase.columns.mapping" = ":key,cf:name,cf:age")
> tblproperties("hbase.table.name" = "hive_hbase_test");
1)这时,如果使用load data 命令导入数据会报错,因为该表是non-native table。
hive> load datalocal inpath '/home/qytt/test' into table hive_habse_test;
FAILED:SemanticException [Error 10101]: A non-native table cannot be used as targetfor LOAD
2)创建普通hive内表,加载数据:
hive> createtable test_liu (id string,name string, age int)
> ROW FORMAT DELIMITED
> FIELDS TERMINATED BY '\t'
> STORED AS TEXTFILE;
hive> load datalocal inpath '/home/qytt/test' into table test_liu;
Copying data from file:/home/qytt/test
Copying file: file:/home/qytt/test
Loading data totable qytt.test_liu
OK
查看test_liu表在hdfs中查看数据文件:
$ hadoop fs -ls -R/hive/warehouse/qytt.db/test_liu
-rwxr-xr-x 3 qytt qytt 22 2017-04-09 11:33/hive/warehouse/qytt.db/test_liu/test
3)导入数据到hive-hbase表:
hive> insertoverwrite table hive_hbase_test select * from test_liu;
Total jobs = 1
…
Time taken: 26.199seconds
查询数据:
hive> select *from hive_hbase_test;
OK
1 test 2
2 liuxiao 34
Time taken: 0.138seconds, Fetched: 2 row(s)
通过show create tablehive_hbase_test命令查看到hive_hbase_test表在hdfs上的目录,然后在hdfs中查看数据文件,结果并没有这样的数据文件,这是因为hive_hbase_test表中的数据是在hbase中存放的。
$hadoop fs -ls -R /hive/warehouse/qytt.db/hive_hbase_test
4)当hive使用overwrite关键字进行插入数据时,原本数据不会被删除,有同样的行健会被更新覆盖。因为数据是存在hbase中的,遵守hbase插入数据的规则。
2、在hbase中查询数据:
hbase(main):009:0>get 'hive_hbase_test',1
COLUMN CELL
cf:age timestamp=1491708915228, value=2
cf:name timestamp=1491708915228, value=test
2 row(s) in 0.1620seconds
3、其他:
- 当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表了。
二、建立hive外表(external),引用hbase中一张已经存在的表:
这种情况是hbase里面已经有这张表了,创建一个hive表去管理这hbase表,方便使用sql来操作hbase中的数据(不用手工写mapreduce任务了)。建立这样的外表,数据也是在hbase中存放,hive会在hdfs中创建目录,但没有数据文件。
1、创建hive外表,引用hbase中的表hive_hbase_test:
hive> createexternal table ext_hive_hbase(id string,name string ,ct string)
> stored by'org.apache.hadoop.hive.hbase.HBaseStorageHandler'
> with serdeproperties("hbase.columns.mapping" = ":key,cf:name,cf:ct")
> tblproperties("hbase.table.name" = "hive_hbase_test");
注意:在创建hive的外表时,可以引用hbase中不存在列(例如:hive_hbase_test表中不存在cf:ct列)
1)查询:
hive> select *from ext_hive_hbase;
OK
1 test NULL
2 liuxiao NULL
Time taken: 0.134seconds, Fetched: 2 row(s)
通过hadoop 只能看到对应的目录,没有数据文件:
$ hadoop fs -ls -R/hive/warehouse/qytt.db/ext_hive_hbase
2)同样,不能使用load data命令给外表加载数据,因为该表使用HBaseStorageHandler创建的:
hive> load datalocal inpath '/home/qytt/test' into table ext_hive_hbase;
FAILED:SemanticException [Error 10101]: A non-native table cannot be used as targetfor LOAD
2、导入数据:
1)查询一张hive本身的内表:
hive> select *from test_liu;
OK
1 test 20
2 liuxiao 30
3 xiaoli 29
2)导入数据到hive外表:
hive> insertoverwrite table ext_hive_hbase select * from test_liu;
Total jobs = 1
...
Time taken: 22.107seconds
3)查询hive外表:
hive> select *from ext_hive_hbase;
OK
1 test 20
2 liuxiao 30
3 xiaoli 29
Time taken: 0.117seconds, Fetched: 3 row(s)
4)查询hbase中的表:
hbase(main):015:0>scan 'hive_hbase_test'
ROW COLUMN+CELL
1 column=cf:age, timestamp=1491708915228, value=2
1 column=cf:ct, timestamp=1491713128051, value=20
1 column=cf:name, timestamp=1491713128051, value=test
2 column=cf:age, timestamp=1491708915228, value=34
2 column=cf:ct, timestamp=1491713128051, value=30
2 column=cf:name, timestamp=1491713128051, value=liuxiao
3 column=cf:ct, timestamp=1491713128051, value=29
3 column=cf:name, timestamp=1491713128051, value=xiaoli
3 row(s) in 0.0190seconds
可以发现,在hbase中的数据也发生了改变。
3、其他:
- 删除hive表对hbase没有影响;
- 但是先删除hbase表hive就会报TableNotFoundException;
总结:
hive+hbase这种方式,无论哪种,数据都是在hbase中存放的,hive只会在hdfs上创建目录,不会生成真正的数据文件;