HBase的shell命令
前提:
一定要启动HBase
进入HBase shell
执行:
# 执行
hbase shell
# 如果没有配置环境变量
/export/server/hbase-2.1.0/bin/hbase shell
查看HBase集群状态
# hbase shell内输入status
hbase(main):001:0> status
1 active master, 0 backup masters, 3 servers, 0 dead, 0.6667 average load
Took 0.5295 seconds
查看HBase shell的帮组
# hbase shell内输入help
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
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: abort_procedure, 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_rsgroup, 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', "keyx03x3fxcd"
hbase> get 't1', "key 03 23 11"
hbase> put 't1', "testxefxff", 'f1:', "x01x33x40"
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
基础操作API
简单创建表
创建一个表,名字叫做user,包含2个列族,分别是,info和data
hbase(main):003:0> create 'user', 'info', 'data'
Created table user
Took 1.3863 seconds
=> Hbase::Table - user
# user是表名
# info是列族1的名字
# data 是列族2的名字
查看表信息
# 通过desc '表名称'查看
hbase(main):004:0> desc 'user'
Table user is ENABLED
user
COLUMN FAMILIES DESCRIPTION
{NAME => 'data', 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', BLOC
KSIZE => '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', BLOC
KSIZE => '65536'}
2 row(s)
Took 0.1656 seconds
插入数据
向刚刚创建的表,
插入:
-
rowkey:rk001,列族info 二级列name,值是zhangsan
-
rowkey:rk001,列族data 二级列addr,值是beijing
-
rowkey:rk002,列族info二级列name,值是wangwu
-
rowkey:rk002,列族data 二级列addr,值是深圳
# 语法
put '表名', 'rowkey', '列族:二级列', '具体的值'
hbase(main):005:0> put 'user', 'rk001', 'info:name', 'zhangsan'
Took 0.1305 seconds
hbase(main):006:0> put 'user', 'rk001', 'data:addr', 'beijing'
Took 0.0181 seconds
hbase(main):007:0> put 'user', 'rk002', 'info:name', 'wangwu'
Took 0.0151 seconds
hbase(main):008:0> put 'user', 'rk002', 'data:addr', '深圳'
Took 0.0073 seconds
全表扫描
# 语法
scan '表名'
hbase(main):009:0> scan 'user'
ROW COLUMN+CELL
rk001 column=data:addr, timestamp=1654104961553, value=beijing
rk001 column=info:name, timestamp=1654104946778, value=zhangsan
rk002 column=data:addr, timestamp=1654105096732, value=xE6xB7xB1xE5x9CxB3
rk002 column=info:name, timestamp=1654105082043, value=wangwu
2 row(s)
Took 0.0346 seconds中文默认转成Unicode显示
显示中文
scan '表名', {FORMATTER => 'toString'}
针对rowkey进行查询
# 语法
get '表名', 'rowkey'
hbase(main):011:0> get 'user', 'rk001'
COLUMN CELL
data:addr timestamp=1654104961553, value=beijing
info:name timestamp=1654104946778, value=zhangsan
1 row(s)
Took 0.0133 seconds
针对列族查询
get '表名', 'rowkey', '列族:二级列'
get 'user', 'rk001', 'info:name' # 查询rk001的info列族的二级列name的数据
get 'user', 'rk001', 'info' # 查询rk001的info列族的全部二级列数据
练习作业
1. 创建一个表,名称为test,有2个列族,分别是:info和data
info列版本设置为5,data列版本设置为3
create 'test', {NAME=>'info', VERSIONS=>5}, {NAME=>'data', VERSIONS=>3}
2. 向test表插入5条数据,rowkey以rk001开头
向info列族插入2个列,分别是name和age
向data列族插入2个列,分别是address和tel
put 'test', 'rk003', 'info:name', 'zhaoliu'
put 'test', 'rk003', 'info:age','11'
put 'test', 'rk003', 'data:address', '广州'
put 'test', 'rk003', 'data:tel', '123'
put 'test', 'rk004', 'info:name', '王力宏'
put 'test', 'rk004', 'info:age','11'
put 'test', 'rk004', 'data:address', '广州'
put 'test', 'rk004', 'data:tel', '123'
put 'test', 'rk005', 'info:name', '周杰伦'
put 'test', 'rk005', 'info:age','11'
put 'test', 'rk005', 'data:address', '深圳'
put 'test', 'rk005', 'data:tel', '123'
3. 扫描test表的全部数据以及info列族的数据(要求中文正常显示)
scan 'test', {FORMATTER => 'toString', COLUMNS => ['info']}
4. 并计算test表有多少条数据
count 'test'
5. 删除rk003的数据
deleteall 'test', 'rk003'
6. 清空、关闭并删除表
truncate 'test'
disable 'test'
drop 'test'
# 其中 4 5 6 这三个步骤,大家预习解决
更多大数据相关免费课程,可点击链接查看:
发表评论