Hbase學習第 4 天:HBase 命令行操作(上)

  • HBase 爲用戶提供了一個非常方便的命令行使用方式——HBase Shell。

  • HBase Shell 提供了大多數的 HBase 命令,通過 HBase Shell,用戶可以方便地創建、刪除及修改表,還可以向表中添加數據。

列出表中的相關信息等。本節介紹一些常用的命令和具體操作

進入 HBase 命令行

HBase shell

在這裏插入圖片描述
在 HBase 命令行中輸入 help 查看幫助命令

help

顯示如下:

在這裏插入圖片描述

一、基本命令行操作

HBase 中常規命令包括 processlist, status, table_help, version, whoami等

1、查看當前運行的任務列表

hbase(main):026:0> processlist
0 tasks as of: 2019-11-18 15:49:46
No general tasks currently running.

2、查看當前安裝的 HBase 版本信息

hbase(main):015:0* version

1.4.9, rd625b212e46d01cb17db9ac2e9e927fdb201afa1, Wed Dec  5 11:54:10 PST 2018

3、查看 HBase 狀態信息

hbase(main):017:0> status

1 active master, 0 backup masters, 3 servers, 0 dead, 18.3333 average load

4、表操作幫助命令

hbase(main):022:0> table_help
Help for table-reference commands.

You can either create a table via 'create' and then manipulate the table via commands like 'put', 'get', etc.
See the standard help information for how to use each of these commands.

However, as of 0.96, you can also get a reference to a table, on which you can invoke commands.
For instance, you can get create a table and keep around a reference to it via:

   hbase> t = create 't', 'cf'   # 表示 HBase 創建表

Or, if you have already created the table, you can get a reference to it:

   hbase> t = get_table 't'  # 獲取表數據

You can do things like call 'put' on the table:

  hbase> t.put 'r', 'cf:q', 'v'   # 向表中插入數據

which puts a row 'r' with column family 'cf', qualifier 'q' and value 'v' into table t.

To read the data out, you can scan the table:

  hbase> t.scan

which will read all the rows in table 't'.

Essentially, any command that takes a table name can also be done via table reference.
Other commands include things like: get, delete, deleteall,
get_all_columns, get_counter, count, incr. These functions, along with
the standard JRuby object methods are also available via tab completion.

For more information on how to use each of these commands, you can also just type:

   hbase> t.help 'scan'  

which will output more information on how to use that command.

You can also do general admin actions directly on a table; things like enable, disable,
flush and drop just by typing:

   hbase> t.enable
   hbase> t.flush
   hbase> t.disable
   hbase> t.drop

Note that after dropping a table, your reference to it becomes useless and further usage
is undefined (and not recommended).

基本表操作命令裏詳細揭示了 HBase 表操作的詳細解釋

5、查看當前角色(用戶名)

hbase(main):024:0* whoami
root (auth:SIMPLE)
    groups: root

二、DDL 數據定義語言命令行解釋

主要針對表爲對象進行操作,如創建表、修改表、刪除表等。
HBase 主要是針對表爲對象進行操作,比如創建表、修改表、刪除表等,主要語句有

 alter, alter_async, alter_status, create, describe, disable, disable_all, drop, drop_all, enable, enable_all, exists, get_table, is_disabled, is_enabled, list, list_regions, locate_region, show_filters

1、創建表 test1,列族名 info

hbase(main):032:0> create 'test1','info'
0 row(s) in 1.3100 seconds

=> Hbase::Table - test1

2、查看所有創建的表

hbase(main):033:0> list
TABLE                                                                                                            
pre_job_all                                 
test1                                                    
18 row(s) in 0.0140 seconds

=>["pre_job_all", "test1"]
hbase(main):034:0>

3、查看錶結構信息

hbase(main):037:0> describe 'test1'
Table test1 is ENABLED                                 
test1                                           
COLUMN FAMILIES DESCRIPTION                                                            
{NAME => 'info', BLOOMFILTER => 'ROW', VERSIONS => '1', IN_MEMORY => 'false', KEEP_DELETED_CELLS => 'FALSE', DATA_BLOCK_ENCODING => 'NONE', TTL => 'FOREVER', COMPRESSION => 'NONE', MIN_VERSIONS => '0', BLOCK
CACHE => 'true', BLOCKSIZE => '65536', REPLICATION_SCOPE => '0'}                                                                                                                                               
1 row(s) in 0.0130 seconds

Table test1 is ENABLED :表示 ‘test1’ 這個表正在使用中

4、設置表不可用使用命令 disable 或者 disable_all

disable:單表設置表不可用,當要刪除表或者表數據時需要先設置表不可用
再使用刪除命令

hbase(main):002:0> disable 'test1'
0 row(s) in 2.2530 seconds

hbase(main):003:0> describe 'test1'
Table test1 is DISABLED                 # 表示表不可用                                                  
test1                                                                                     
COLUMN FAMILIES DESCRIPTION                                                               
{NAME => 'info', BLOOMFILTER => 'ROW', VERSIONS => '1', IN_MEMORY => 'false', KEEP_DELETED_CELLS => 'FALSE', DATA_BLOCK_ENCODING => 'NONE', TTL => 'FOREVER', COMPRESSION => 'NONE', MIN_VERSIONS => '0', BLOCK
CACHE => 'true', BLOCKSIZE => '65536', REPLICATION_SCOPE => '0'}                                                                                                                                               
1 row(s) in 0.0120 seconds

hbase(main):004:0> d
NameError: undefined local variable or method `d' for #<Object:0x2313db84>

hbase(main):005:0> drop 'test1'
0 row(s) in 1.2290 seconds

hbase(main):006:0> list
TABLE                                                                                                            
pre_job_all                                                                                
17 row(s) in 0.0170 seconds
=> ["1_ETL_LOAD", "2_top10_skill", "3_1_top10_affect_money_indicator", "3_2_top10_affect_money_indicator", "4_hadoop_hot_map", "5_employee_profile", "BINZANG", "EXAMPLE", "SYSTEM.CATALOG", "SYSTEM.FUNCTION", "SYSTEM.LOG", "SYSTEM.MUTEX", "SYSTEM.SEQUENCE", "SYSTEM.STATS", "US_POPULATION", "WEB_STAT", "pre_job_all"]
hbase(main):007:0>


如果表不設置不可用則不能直接刪除表:
刪除表
hbase(main):009:0> drop 'test1'

ERROR: Table test is enabled. Disable it first.

Here is some help for this command:
Drop the named table. Table must first be disabled:
  hbase> drop 't1'
  hbase> drop 'ns1:t1'
  

5、查詢表是否可用

hbase(main):014:0> is_disabled 'test1'
false                             


hbase(main):015:0> is_enabled 'test1'
true                                                                                                                                                                                                           
0 row(s) in 0.0080 seconds

6、查詢表是否存在

hbase(main):017:0> exists 'test1'
Table test1 does exist                                     
0 row(s) in 0.0090 seconds

#表示表存在

7、刪除列族

#刪除列族,指定表和列族名
hbase(main):032:0> alter 'test1', NAME => 'INFO', METHOD => 'delete'
Updating all regions with the new schema...
1/1 regions updated.
Done.
0 row(s) in 1.8720 seconds

三、Namespace 命令行操作

HBase 命名空間是對 HBase 中表的邏輯劃分,將不同的表歸與不同的命名空間方便對HBase表中數據進行操作,關於 HBase 命名空間後面做詳細介紹,今天主要介紹命名空間的基本操作命令 ,具體的命令有
alter_namespace, create_namespace, describe_namespace, drop_namespace, list_namespace, list_namespace_tables;詳細操作如下:

1、創建命名空間

hbase(main):035:0* create_namespace 'my_namespace'
0 row(s) in 0.8760 seconds

2、查看命名空間詳細信息

hbase(main):036:0> describe_namespace 'my_namespace'
DESCRIPTION                                                    
{NAME => 'my_namespace'}                                       
1 row(s) in 0.0190 seconds

3、羅列出所有的命名空間

hbase(main):037:0> list_namespace
NAMESPACE                           
default        //用戶建表未指定命名空間時的默認命名空間                         
hbase          //系統內建表,包括namespace和meta表                          
my_namespace                                                  
3 row(s) in 0.0160 seconds

4、列出命名空間下的表

hbase(main):038:0> list_namespace_tables 'hbase'
TABLE                                                      
meta                             
namespace                           
2 row(s) in 0.0190 seconds

5、在命名空間站創建表

hbase(main):041:0> create 'my_namespace:test','info'
0 row(s) in 1.2320 seconds

=> Hbase::Table - my_namespace:test


hbase(main):044:0> list_namespace_tables 'my_namespace' # 查看剛剛創建的表
TABLE                            
test                          
1 row(s) in 0.0070 seconds

6、查看所有表

hbase(main):046:0> list
TABLE                            
my_namespace:test                   my_namespace:test            
pre_job_all                         
test                          
test1                           
20 row(s) in 0.0080 seconds

=> ["1_ETL_LOAD", "2_top10_skill", "3_1_top10_affect_money_indicator", "3_2_top10_affect_money_indicator", "4_hadoop_hot_map", "5_employee_profile", "BINZANG", "EXAMPLE", "SYSTEM.CATALOG", "SYSTEM.FUNCTION", "SYSTEM.LOG", "SYSTEM.MUTEX", "SYSTEM.SEQUENCE", "SYSTEM.STATS", "US_POPULATION", "WEB_STAT", "my_namespace:test", "pre_job_all", "test", "test1"]
hbase(main):047:0>

注意:my_namespace:test —> 表示哪個命名空間下的某個表

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章