Hbase中 shell簡單操作
阿新 • • 發佈:2018-12-10
由於Hbase是NoSql資料庫,裡面不僅僅是SQL,操作命令會有不同,經常使用Mysql資料庫的同學請注意
首先啟動Hbase的程序服務:
[[email protected] hbase-2.1.0]$ bin/start-hbase.sh
然後啟動使用shell操作的客戶端:
[[email protected] 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進行操作。