Hbase中 shell简单操作

由于Hbase是NoSql数据库,里面不仅仅是SQL,操作命令会有不同,经常使用Mysql数据库的同学请注意

首先启动Hbase的进程服务:

[hadoop@Master hbase-2.1.0]$ bin/start-hbase.sh

然后启动使用shell操作的客户端:

[hadoop@Master hbase-2.1.0]$ bin/hbase shell

看见如下界面之后,我们输入help查看Hbase  shell命令使用的帮助文档:

hbase(main):002:0> help
HBase Shell, version 2.1.0, re1673bb0bbfea21d6e5dba73e013b09b8b49b89b, Tue Jul 10 17:26:48 CST 2018
Type 'help "COMMAND"', (e.g. 'help "get"' -- the quotes are necessary) for help on a specific command.
Commands are grouped. Type 'help "COMMAND_GROUP"', (e.g. 'help "general"') for help on a command group.

COMMAND GROUPS:
  Group name: general
  Commands: processlist, status, table_help, version, whoami

  Group name: ddl
  Commands: alter, alter_async, alter_status, clone_table_schema, 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

  Group name: namespace
  Commands: alter_namespace, create_namespace, describe_namespace, drop_namespace, list_namespace, list_namespace_tables

  Group name: dml
  Commands: append, count, delete, deleteall, get, get_counter, get_splits, incr, put, scan, truncate, truncate_preserve

  Group name: tools
  Commands: assign, balance_switch, balancer, balancer_enabled, catalogjanitor_enabled, catalogjanitor_run, catalogjanitor_switch, cleaner_chore_enabled, cleaner_chore_run, cleaner_chore_switch, clear_block_cache, clear_compaction_queues, clear_deadservers, close_region, compact, compact_rs, compaction_state, flush, is_in_maintenance_mode, list_deadservers, major_compact, merge_region, move, normalize, normalizer_enabled, normalizer_switch, split, splitormerge_enabled, splitormerge_switch, stop_master, stop_regionserver, trace, unassign, wal_roll, zk_dump

  Group name: replication
  Commands: add_peer, append_peer_namespaces, append_peer_tableCFs, disable_peer, disable_table_replication, enable_peer, enable_table_replication, get_peer_config, list_peer_configs, list_peers, list_replicated_tables, remove_peer, remove_peer_namespaces, remove_peer_tableCFs, set_peer_bandwidth, set_peer_exclude_namespaces, set_peer_exclude_tableCFs, set_peer_namespaces, set_peer_replicate_all, set_peer_serial, set_peer_tableCFs, show_peer_tableCFs, update_peer_config

查看数据库:list

hbase(main):003:0> list
TABLE                                                                                                                                                                                                             
SYSTEM.CATALOG                                                                                                                                                                                                    
student                                                                                                                                                                                                           
student2                                                                                                                                                                                                          
3 row(s)
Took 0.0702 seconds                                                                                                                                                                                               
=> ["SYSTEM.CATALOG", "student", "student2"]

创建表和列族:

hbase(main):009:0> create 'student','info','address' //student:表名    列族:info  address 
Created table student
Took 1.2751 seconds                                                                                                                                                                                               
=> Hbase::Table - student

查看表的描述:describe

hbase(main):011:0> describe 'student'
Table student is ENABLED                                                                                                                                                                                          
student                                                                                                                                                                                                           
COLUMN FAMILIES DESCRIPTION                                                                                                                                                                                       
{NAME => 'address', VERSIONS => '1', EVICT_BLOCKS_ON_CLOSE => 'false', NEW_VERSION_BEHAVIOR => 'false', KEEP_DELETED_CELLS => 'FALSE', CACHE_DATA_ON_WRITE => 'false', DATA_BLOCK_ENCODING => 'NONE', TTL => 'FORE
VER', MIN_VERSIONS => '0', REPLICATION_SCOPE => '0', BLOOMFILTER => 'ROW', CACHE_INDEX_ON_WRITE => 'false', IN_MEMORY => 'false', CACHE_BLOOMS_ON_WRITE => 'false', PREFETCH_BLOCKS_ON_OPEN => 'false', COMPRESSIO
N => 'NONE', BLOCKCACHE => 'true', BLOCKSIZE => '65536'}                                                                                                                                                          
{NAME => 'info', VERSIONS => '1', EVICT_BLOCKS_ON_CLOSE => 'false', NEW_VERSION_BEHAVIOR => 'false', KEEP_DELETED_CELLS => 'FALSE', CACHE_DATA_ON_WRITE => 'false', DATA_BLOCK_ENCODING => 'NONE', TTL => 'FOREVER
', MIN_VERSIONS => '0', REPLICATION_SCOPE => '0', BLOOMFILTER => 'ROW', CACHE_INDEX_ON_WRITE => 'false', IN_MEMORY => 'false', CACHE_BLOOMS_ON_WRITE => 'false', PREFETCH_BLOCKS_ON_OPEN => 'false', COMPRESSION =
> 'NONE', BLOCKCACHE => 'true', BLOCKSIZE => '65536'}                                                                                                                                                             
2 row(s)
Took 0.1581 seconds 

查看表的描述:desc

desc 'student'
Table student is ENABLED                                                                                                                                                                                          
student                                                                                                                                                                                                           
COLUMN FAMILIES DESCRIPTION                                                                                                                                                                                       
{NAME => 'address', VERSIONS => '1', EVICT_BLOCKS_ON_CLOSE => 'false', NEW_VERSION_BEHAVIOR => 'false', KEEP_DELETED_CELLS => 'FALSE', CACHE_DATA_ON_WRITE => 'false', DATA_BLOCK_ENCODING => 'NONE', TTL => 'FORE
VER', MIN_VERSIONS => '0', REPLICATION_SCOPE => '0', BLOOMFILTER => 'ROW', CACHE_INDEX_ON_WRITE => 'false', IN_MEMORY => 'false', CACHE_BLOOMS_ON_WRITE => 'false', PREFETCH_BLOCKS_ON_OPEN => 'false', COMPRESSIO
N => 'NONE', BLOCKCACHE => 'true', BLOCKSIZE => '65536'}                                                                                                                                                          
{NAME => 'info', VERSIONS => '1', EVICT_BLOCKS_ON_CLOSE => 'false', NEW_VERSION_BEHAVIOR => 'false', KEEP_DELETED_CELLS => 'FALSE', CACHE_DATA_ON_WRITE => 'false', DATA_BLOCK_ENCODING => 'NONE', TTL => 'FOREVER
', MIN_VERSIONS => '0', REPLICATION_SCOPE => '0', BLOOMFILTER => 'ROW', CACHE_INDEX_ON_WRITE => 'false', IN_MEMORY => 'false', CACHE_BLOOMS_ON_WRITE => 'false', PREFETCH_BLOCKS_ON_OPEN => 'false', COMPRESSION =
> 'NONE', BLOCKCACHE => 'true', BLOCKSIZE => '65536'}                                                                                                                                                             
2 row(s)
Took 0.0500 seconds                               

判断某表是否存在:exists

hbase(main):013:0> exists 'student'
Table student does exist                                                                                                                                                                                          
Took 0.0185 seconds                                                                                                                                                                                               
=> true

判断某表是否被禁用:is_disabled

hbase(main):014:0> is_disabled 'student'
false         //fales:没有被禁用                                                                                                                                                                                                    
Took 0.0346 seconds                                                                                                                                                                                               
=> 1

判断某表是否在启用:is_enabled

hbase(main):015:0> is_enabled 'student'
true        //true :正在启用状态                                                                                                                                                                                                      
Took 0.0098 seconds                                                                                                                                                                                               
=> true

禁用表:disable

hbase(main):017:0> disable 'student'//禁用student表
Took 0.8065 seconds                                                                                                                                                                                               
hbase(main):018:0> is_disabled 'student'//查看是否被禁用
true     //已被禁用                                                                                                                                                                                                         
Took 0.0124 seconds                                                                                                                                                                                               
=> 1

启用表:enable

hbase(main):024:0> enable 'student'
Took 0.8293 seconds      

删除表:drop

删除表这里要说一下,只有被禁用的表,才能被drop删除,所以说我们先禁用再删除

hbase(main):019:0> drop 'student'
Took 0.4638 seconds 

删除列族:alter 表,delete=>列族

hbase(main):028:0> alter 'student','delete'=>'address'
Updating all regions with the new schema...
1/1 regions updated.
Done.
Took 2.0847 seconds 

新增列族:alter 表, NAME=>列族

alter 'student,'NAME'=>'address'

删除多个表(首先要禁用多个表):

删除多个表:	disable_all		drop_all
首先全部禁用
disable_all 'stu.*'
drop_all 'stu.*'	//删除以stu开头的所有表

添加数据:put

hbase(main):004:0> put 'student','1001','info:name','zhangsna'
    //1001:是rowkey
Took 0.5393 seconds 

hbase(main):007:0> put 'student','1002','info:name','lisi'
Took 0.0104 seconds                                                                                                                                                                                               
                           

查看数据:scan

scan 'student'
ROW                                                   COLUMN+CELL                                                                                                                                                 
 1001                                                 column=info:name, timestamp=1537408041183, value=zhangsna                                                                                                   
1 row(s)
Took 0.0767 seconds                          

修改表中数据:

put 'student','1002','info:name','wangwu'
Took 0.0134 seconds                                                                                                                                                                                               
hbase(main):010:0> scan 'student'
ROW                                                   COLUMN+CELL                                                                                                                                                 
 1001                                                 column=info:name, timestamp=1537408041183, value=zhangsna                                                                                                   
 1002                                                 column=info:name, timestamp=1537408278285, value=wangwu     

如上图我们可以发现,添加既是修改,是属于同一语句

查询数据:get

get 'student','1002'
COLUMN                                                CELL                                                                                                                                                        
 info:name                                            timestamp=1537408278285, value=wangwu                                                                                                                       
1 row(s)
Took 0.0212 seconds                                

查询扫描整个表:

hbase(main):032:0> scan 'student'
ROW                                                   COLUMN+CELL                                                                                                                                                 
 1001                                                 column=address:phone, timestamp=1537408682430, value=10086                                                                                                  
 1001                                                 column=info:name, timestamp=1537408041183, value=zhangsna                                                                                                   
 1002                                                 column=info:name, timestamp=1537408278285, value=wangwu                                                                                                     
2 row(s)
Took 0.0124 seconds     

删除数据:delete

delete 'student','1001','address:phone'

删除整行数据:

deleteall 'student','1002'

查看表中总记录数:

hbase(main):046:0> count 'student'
1 row(s)
Took 0.2269 seconds                                                                                                                                                                                               
=> 1

清空表中数据:

hbase(main):047:0> truncate 'student'
Truncating 'student' table (it may take a while):
Disabling table...
Truncating table...
Took 2.3399 seconds               

以上就是使用xshell对Hbase进行简单的命令操作,如果是类似于SQL中条件查询,在这里操作的话需要导入Java包,所以我们不会再使用命令操作,比如使用JavaAPI进行操作。

猜你喜欢

转载自blog.csdn.net/qq_32941881/article/details/82781148
今日推荐