HBase之表空間

一、介紹
在HBase中,namespace命名空間指對一組表的邏輯分組,相似RDBMS中的database,方便對錶在業務上劃分。Apache HBase從0.98.0, 0.95.2兩個版本開始支持namespace級別的受權操做,HBase全局管理員能夠建立、修改和回收namespace的受權。

二、namespace
HBase系統默認定義了兩個缺省的namespace
hbase:系統內建表,包括namespace和meta表
default:用戶建表時未指定namespace的表都建立在此

建立namespace:
hbase(main):010:0* create_namespace 'datamanage'


查看namespace:
hbase(main):011:0> describe_namespace 'datamanage'
  DESCRIPTION               
   {NAME =>'datamanage'}                                                                                                                 
列出全部namespace:
hbase(main):012:0> list_namespace
	NAMESPACE
	 datamanage                                                                                                                        
	 default   
	 hbase    

在namespace下建立表:        
hbase(main):013:0> create 'datamanage:testtable','colfam01'
    => Hbase::Table - datamanage:testtable
查看namespace下的表
hbase(main):015:0> list_namespace_tables 'datamanage'
	TABLE                 
    testtable    

刪除namespace:

hbase(main):016:0> drop_namespace 'datamanage'

ERROR: org.apache.hadoop.hbase.constraint.ConstraintException: Only empty namespaces can be removed. Namespace datamanage has 1 tables
        at org.apache.hadoop.hbase.master.TableNamespaceManager.remove(TableNamespaceManager.java:200)
        at org.apache.hadoop.hbase.master.HMaster.deleteNamespace(HMaster.java:2488)
        at org.apache.hadoop.hbase.master.MasterRpcServices.deleteNamespace(MasterRpcServices.java:489)
        at org.apache.hadoop.hbase.protobuf.generated.MasterProtos$MasterService$2.callBlockingMethod(MasterProtos.java:55730)
        at org.apache.hadoop.hbase.ipc.RpcServer.call(RpcServer.java:2178)
        at org.apache.hadoop.hbase.ipc.CallRunner.run(CallRunner.java:112)
        at org.apache.hadoop.hbase.ipc.RpcExecutor.consumerLoop(RpcExecutor.java:133)
        at org.apache.hadoop.hbase.ipc.RpcExecutor$1.run(RpcExecutor.java:108)
        at java.lang.Thread.run(Thread.java:745)

Here is some help for this command:
Drop the named namespace. The namespace must be empty.
#刪除表
hbase(main):017:0> disable 'datamanage:testtable'
hbase(main):018:0> drop 'datamanage:testtable'
#刪除datamanage命名空間
hbase(main):019:0> drop_namespace 'datamanage'

 java API:java

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.util.Bytes;

import java.io.IOException;


/**
 * Hbase namespace
 */
public class NameSpaceInHbase {
    public static void main(String[] args) throws IOException {

        Configuration conf = HBaseConfiguration.create();
        Connection connection = ConnectionFactory.createConnection(conf);
        //管理員對象
        Admin admin = connection.getAdmin();
        //建立命名空間
        NamespaceDescriptor namespaceDescriptor = NamespaceDescriptor.create("datastrom3").build();
        admin.createNamespace(namespaceDescriptor);
        //表名
        TableName tableName = TableName.valueOf("datastrom3","testtable3");
        //表描述
        HTableDescriptor desc = new HTableDescriptor(tableName);
        //列族描述datastrom
        HColumnDescriptor coldef = new HColumnDescriptor(Bytes.toBytes("colfam3"));
        //表加入列族
        desc.addFamily(coldef);
        //建立表
        admin.createTable(desc);
        //校驗表是否可用
        boolean avail = admin.isTableAvailable(tableName);
        System.out.println("Table available: "+avail);
    }
}

/**
 preHell:
 ========
 nextShell:
 hbase(main):005:0> list
 TABLE
 testtable2
 user
 2 row(s) in 0.0070 seconds

java output:
 =====
 Table available: true

shell:
 =====
 hbase(main):022:0> list
 TABLE
 datastrom3:testtable3
 ...
  **/
相關文章
相關標籤/搜索