Fork me on GitHub

HBase笔记之namespace

 

一、什么是namespace

在RDBMS中有database的概念,用来对table进行分组,那么在HBase中当表比较多的时候如何对表分组呢,就是namespace,可以简单的把namespace理解为RDBMS中的database。

 

二、namespace相关操作

内置的namespace

HBase有两个内置的namespace:

hbase(main):009:0> list_namespace
NAMESPACE 
default 
hbase 
2 row(s)
Took 0.0683 seconds 

hbase:用来存放系统相关的一些元数据等,相当于mysql中的mysql数据库:

hbase(main):025:0> list_namespace_tables "hbase""
TABLE 
meta
namespace 
2 row(s)
Took 0.0174 seconds 
=> ["meta", "namespace"]

default:创建表时未指定命名空间的话默认挂在default下。

 

创建namespace

创建namespace:

hbase(main):010:0> create_namespace "test"
Took 0.2781 seconds

在创建namespace的时候还可以添加一些说明信息:

hbase(main):018:0> create_namespace "test002", {"author"=>"CC11001100", "create_time"=>"2018-11-4 17:51:53"}
Took 0.2262 seconds    

 

查看namespace信息

上面创建namespace时添加了一些附加信息,如何查看这些附加信息呢:

hbase(main):019:0> describe_namespace "test002""
DESCRIPTION 
{NAME => 'test002', author => 'CC11001100', create_time => '2018-11-4 17:51:53'}
Took 0.0042 seconds 
=> 1

 

修改namespace

查看附加信息发现作者写错了,需要修改:

hbase(main):014:0> alter_namespace "test002", {METHOD=>"set", "author"=>"ChenEr"}
Took 0.2458 seconds 
hbase(main):015:0> describe_namespace "test002"
DESCRIPTION 
{NAME => 'test002', author => 'ChenEr', create_time => '2018-11-4 17:51:53'}
Took 0.0056 seconds 
=> 1

改完发现还漏了一项,需要追加自己的联系方式:

hbase(main):016:0> alter_namespace "test002", {METHOD=>"set", "email"=>"CC11001100@qq.com"}
Took 0.2313 seconds 
hbase(main):017:0> describe_namespace "test002"
DESCRIPTION 
{NAME => 'test002', author => 'ChenEr', create_time => '2018-11-4 17:51:53', email => 'CC11001100@qq.com'}
Took 0.0038 seconds 
=> 1

想了下放上去自己的联系方式搞不好会被不断骚扰,还是去掉此项属性:

hbase(main):010:0> alter_namespace "test002", {METHOD=>"unset", NAME=>"email"}
Took 0.2419 seconds 
hbase(main):011:0> describe_namespace "test002"
DESCRIPTION 
{NAME => 'test002', author => 'ChenEr', create_time => '2018-11-4 17:51:53'}
Took 0.0056 seconds 
=> 1

总结:

添加或修改属性:

alter_namespace 'ns1', {METHOD => 'set', 'PROPERTY_NAME' => 'PROPERTY_VALUE'}

删除属性:

alter_namespace 'ns1', {METHOD => 'unset', NAME=>'PROPERTY_NAME'}

 

查看已创建的所有namespace

查看当前已经创建的namespace,相当于RDBMS中的show databases:

hbase(main):019:0> list_namespace
NAMESPACE 
default 
hbase 
test
test002 
4 row(s)
Took 0.0122 seconds

 

删除namespace

删除namespace:

hbase(main):019:0> list_namespace
NAMESPACE 
default 
hbase 
test
test002 
4 row(s)
Took 0.0122 seconds 
hbase(main):020:0> drop_namespace "test002"
Took 0.2321 seconds 
hbase(main):021:0> list_namespace
NAMESPACE 
default 
hbase 
test
3 row(s)
Took 0.0111 seconds

注意要删除的namespace必须是空的,其下没有表,否则会删除失败:

hbase(main):051:0> create_namespace "test_drop_namespace"
Took 0.2376 seconds 
hbase(main):052:0> create "test_drop_namespace:t1", "cf1"
Created table test_drop_namespace:t1
Took 2.2312 seconds 
=> Hbase::Table - test_drop_namespace:t1
hbase(main):053:0> drop_namespace "test_drop_namespace"

ERROR: org.apache.hadoop.hbase.constraint.ConstraintException: Only empty namespaces can be removed. Namespace test_drop_namespace has 1 tables
	at org.apache.hadoop.hbase.master.procedure.DeleteNamespaceProcedure.prepareDelete(DeleteNamespaceProcedure.java:217)
	at org.apache.hadoop.hbase.master.procedure.DeleteNamespaceProcedure.executeFromState(DeleteNamespaceProcedure.java:78)
	at org.apache.hadoop.hbase.master.procedure.DeleteNamespaceProcedure.executeFromState(DeleteNamespaceProcedure.java:45)
	at org.apache.hadoop.hbase.procedure2.StateMachineProcedure.execute(StateMachineProcedure.java:189)
	at org.apache.hadoop.hbase.procedure2.Procedure.doExecute(Procedure.java:850)
	at org.apache.hadoop.hbase.procedure2.ProcedureExecutor.execProcedure(ProcedureExecutor.java:1473)
	at org.apache.hadoop.hbase.procedure2.ProcedureExecutor.executeProcedure(ProcedureExecutor.java:1241)
	at org.apache.hadoop.hbase.procedure2.ProcedureExecutor.access$800(ProcedureExecutor.java:75)
	at org.apache.hadoop.hbase.procedure2.ProcedureExecutor$WorkerThread.run(ProcedureExecutor.java:1761)

For usage try 'help "drop_namespace"'

Took 0.1217 seconds 
hbase(main):054:0> disable "test_drop_namespace:t1"
Took 0.4357 seconds 
hbase(main):055:0> drop "test_drop_namespace:t1"
Took 0.2351 seconds 
hbase(main):056:0> drop_namespace "test_drop_namespace"
Took 0.2172 seconds

 

创建表时指定namespace

创建表时指定表所在的namespace,使用namespace-name:tale-name的格式指定:

hbase(main):022:0> create "test:user", "userInfo"
Created table test:user
Took 2.3992 seconds 
=> Hbase::Table - test:user

 

查看namespace下的表

查看namespace下都有哪些表:

hbase(main):024:0> list_namespace_tables "test"
TABLE 
user
1 row(s)
Took 0.0252 seconds 
=> ["user"]

 

.

posted @ 2018-11-05 21:37  CC11001100  阅读(3703)  评论(0编辑  收藏  举报