HBase Shell操作
HBase Shell是HBase的命令行工具,提供了对HBase的管理操作,可以对HBase中的表进行数据的增、删、改、查等操作,也可以对表进行快照生成,通过生成快照进行复制表的操作,同时还可以对表中的region进行split的操作等等。
基本操作
1.进入HBase Shell命令
在Linux下进行命令输入
hbase shell
便可进入到hbase shell命令行中
HBase Shell
Use "help" to get list of supported commands.
Use "exit" to quit this interactive shell.
For Reference, please visit: http://hbase.apache.org/2.0/book.html#shell
Version 2.1.0-cdh6.3.2, rUnknown, Fri Nov 8 05:44:07 PST 2019
Took 0.0010 seconds
Ignoring eventmachine-1.2.7 because its extensions are not built. Try: gem pristine eventmachine --version 1.2.7
Ignoring executable-hooks-1.6.1 because its extensions are not built. Try: gem pristine executable-hooks --version 1.6.1
Ignoring gem-wrappers-1.4.0 because its extensions are not built. Try: gem pristine gem-wrappers --version 1.4.0
Ignoring json-1.8.6 because its extensions are not built. Try: gem pristine json --version 1.8.6
Ignoring thin-1.5.1 because its extensions are not built. Try: gem pristine thin --version 1.5.1
2.查看帮助
通常进入命令行中,不知道可以进行什么操作时,可以输入“help”进行查看,HBase Shell也提供帮助查看
在HBase Shell命令行中进行输入
help
可以看到
HBase Shell, version 2.1.0-cdh6.3.2, rUnknown, Fri Nov 8 05:44:07 PST 2019
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, loca
te_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_cac
he, 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_exclude_namespaces, append_peer_exclude_tableCFs, 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_exclude_namespaces, remove_peer_exclude_tableCFs, 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
Group name: snapshots
Commands: clone_snapshot, delete_all_snapshot, delete_snapshot, delete_table_snapshots, list_snapshots, list_table_snapshots, restore_snapshot, snapshot
Group name: configuration
Commands: update_all_config, update_config
Group name: quotas
Commands: list_quota_snapshots, list_quota_table_sizes, list_quotas, list_snapshot_sizes, set_quota
Group name: security
Commands: grant, list_security_capabilities, revoke, user_permission
Group name: procedures
Commands: list_locks, list_procedures
Group name: visibility labels
Commands: add_labels, clear_auths, get_auths, list_labels, set_auths, set_visibility
Group name: rsgroup
Commands: add_rsgroup, balance_rsgroup, get_rsgroup, get_server_rsgroup, get_table_rsgroup, list_rsgroups, move_namespaces_rsgroup, move_servers_namespaces_rsgroup, move_servers_rsgroup, move_servers_tables_r
sgroup, move_tables_rsgroup, remove_rsgroup, remove_servers_rsgroup
SHELL USAGE:
Quote all names in HBase Shell such as table and column names. Commas delimit
command parameters. Type <RETURN> after entering a command to run it.
Dictionaries of configuration used in the creation and alteration of tables are
Ruby Hashes. They look like this:
{'key1' => 'value1', 'key2' => 'value2', ...}
and are opened and closed with curley-braces. Key/values are delimited by the
'=>' character combination. Usually keys are predefined constants such as
NAME, VERSIONS, COMPRESSION, etc. Constants do not need to be quoted. Type
'Object.constants' to see a (messy) list of all constants in the environment.
If you are using binary keys or values and need to enter them in the shell, use
double-quote'd hexadecimal representation. For example:
hbase> get 't1', "key\x03\x3f\xcd"
hbase> get 't1', "key\003\023\011"
hbase> put 't1', "test\xef\xff", 'f1:', "\x01\x33\x40"
The HBase shell is the (J)Ruby IRB with the above HBase-specific commands added.
For more on the HBase Shell, see http://hbase.apache.org/book.html
3.查看表操作
list
类似于sql中的show tables
表的基本操作
1.创建表
create 'tableName', 'family'
例,创建一个记录学生信息的表,表名为student,列族为info
create 'student', 'info'
返回
Created table student
Took 2.5063 seconds
=> Hbase::Table - student
创建成功
2.增加数据
现在可以往表中插入新数据
put 'tableName', 'rowkey', 'family:column', 'value'
例,新增一名学生tony,年龄为18
put 'student', 'tony', 'info:age', '18'
返回
Took 0.1874 seconds
3.查询数据
对以插入的数据进行查询,在HBase中有两种查询方式,一种为精确查询-get,一种为模糊查询scan
3.1.精确查询
最简单的精确查询方式为知道表名与rowkey就可以进行精确查询,同时也可以进一步精确查询到family与column
get 'tableName', 'rowkey'
get 'tableName', 'rowkey', 'family:column'
例,查询名为tony学生的信息
get 'student', 'tony'
返回
COLUMN CELL
info:age timestamp=1675147857865, value=18
1 row(s)
Took 0.0393 seconds
如果现在加入tony的身高、体重信息进去
put 'student', 'tony', 'info:height', '180'
put 'student', 'tony', 'info:weight', '150'
再查询的tony的信息
get 'student', 'tony'
就会发现tony的信息变多了
COLUMN CELL
info:age timestamp=1675147857865, value=18
info:height timestamp=1675148520086, value=180
info:weight timestamp=1675148553814, value=150
1 row(s)
Took 0.0056 seconds
但是现在需要查询tony的年龄信息,就可以加入family与column进行更精确的查询
get 'student', 'tony', 'info:age'
返回
COLUMN CELL
info:age timestamp=1675147857865, value=18
1 row(s)
Took 0.0116 seconds
便可以查询更精确的信息
3.2.模糊查询
最简单的模糊查询方式为知道表名就可以进行精确查询,为全表模糊查询。同时也可以在scan中加入条件,进行有条件的筛选。
scan 'tableName'
例,对学生表加入学生kim的信息
put 'student', 'kim', 'info:age', '17'
put 'student', 'kim', 'info:height', '183'
put 'student', 'kim', 'info:weight', '160'
现在对学生表进行全表查询
scan 'student'
返回
ROW COLUMN+CELL
kim column=info:age, timestamp=1675148878890, value=17
kim column=info:height, timestamp=1675148878913, value=183
kim column=info:weight, timestamp=1675148879721, value=160
tony column=info:age, timestamp=1675147857865, value=18
tony column=info:height, timestamp=1675148520086, value=180
tony column=info:weight, timestamp=1675148553814, value=150
2 row(s)
Took 0.0200 seconds
可以看到两个学生信息都返回。
4.修改数据
HBase是没有改修数据的命令的,但是根据HBase的特性,在同一张表中,相同rowkey,family,column下,所对应的值,可以进行覆盖操作。利用这一特性,进行修改数据的操作
例,tony的年龄为20岁,但是发现在学生表中为18岁,现在要对其进行修改。
查看tony年纪
get 'student', 'tony', 'info:age'
返回
COLUMN CELL
info:age timestamp=1675147857865, value=18
1 row(s)
Took 0.0068 seconds
修改tony年龄为20
put 'student', 'tony', 'info:age', '20'
再次查看tony年龄
get 'student', 'tony', 'info:age'
返回
COLUMN CELL
info:age timestamp=1675149552233, value=20
1 row(s)
Took 0.0107 seconds
可见,修改为20岁了
3.删除数据
删除数据分为两种与get查询类似,一种为对于一个rowkey全部删除,或对于一个rowkey下的一个family与cloumn进行删除。
deleteall 'tableName', 'rowkey'
delete 'tableName', 'rowkey', 'family:cloumn'
例,要把tony年龄进行删除
delete 'student', 'tony', 'info:age'
查看tony信息
get 'student', 'tony'
返回
COLUMN CELL
info:height timestamp=1675148520086, value=180
info:weight timestamp=1675148553814, value=150
1 row(s)
Took 0.0248 seconds
tony的年龄信息已经被删除掉了
现在要将tony这个学生的所有信息删除掉
deleteall 'student', 'tony'
查询tony信息
get 'student', 'tony'
返回
COLUMN CELL
0 row(s)
Took 0.0119 seconds
已经没有tony信息了
6.查看表结构
describe 'tableName'
例如,查看学生表信息
describe 'student'
返回
Table student is ENABLED
student
COLUMN FAMILIES DESCRIPTION
{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'}
1 row(s)
Took 0.1256 seconds
7.清空表
truncate 'tableName'
再用truncate用tab补全的时候可以发现还有会一个操作“truncate_preserve”
truncate会连带表的分区一起清空;
truncate_preserve只会清空数据,不会清空分区;
8.删除表
在删除表时,先要对删除的表进行disable的操作,要不然无法对表进行删除
disable 'tableName'
drop 'tableName'
例,将studen表进行删除
disable 'student'
drop 'student'