1、进入hbase命令行
[root@hadoop-server01 bin]# hbase shell
2、命令行帮助
COMMAND GROUPS:
Group name: general
Commands: status, table_help, version, whoami
Group name: ddl
Commands: alter, alter_async, alter_status, create, describe, disable, disable_all, drop, drop_all, enable, enable_all, exists, get_table, is_disabled, is_enabled, list, show_filters
Group name: namespace
Commands: alter_namespace, create_namespace, describe_namespace, drop_namespace, list_namespace, list_namespace_tables
Group name: dml
Commands: count, delete, deleteall, get, get_counter, incr, put, scan, truncate, truncate_preserve
Group name: tools
Commands: assign, balance_switch, balancer, catalogjanitor_enabled, catalogjanitor_run, catalogjanitor_switch, close_region, compact, flush, hlog_roll, major_compact, merge_region, move, split, trace, unassign, zk_dump
Group name: replication
Commands: add_peer, disable_peer, enable_peer, list_peers, list_replicated_tables, remove_peer
Group name: snapshot
Commands: clone_snapshot, delete_snapshot, list_snapshots, rename_snapshot, restore_snapshot, snapshot
Group name: security
Commands: grant, revoke, user_permission
3、简单命令
--查看有哪些表
hbase(main):003:0* list
TABLE
0 row(s) in 0.7800 seconds
=> []
--状态
hbase(main):004:0> status
3 servers, 0 dead, 0.6667 average load
4、创建表
hbase(main):001:0> create 'user_info',{NAME => 'base_info',VERSIONS => 3},{NAME => 'extra_info'}
hbase(main):004:0> list
TABLE
user_info
1 row(s) in 0.0310 seconds
=> ["user_info"]
说明:指定VERSIONS参数为n,代表存放多少份版本数据,3表示存放3份
通过hdfs文件系统查看创建表情况
5、插入数据
--第一行数据插入了4个值,base_info列族3个字段,extra_info一个字段
hbase(main):007:0> put 'user_info','rk0001','base_info:id','1'
0 row(s) in 0.0760 seconds
hbase(main):008:0> put 'user_info','rk0001','base_info:name','zhangsan'
0 row(s) in 0.0070 seconds
hbase(main):009:0> put 'user_info','rk0001','base_info:age','20'
0 row(s) in 0.0040 seconds
hbase(main):010:0> put 'user_info','rk0001','extra_info:addr','beijing'
0 row(s) in 0.0100 seconds
--第二行数据插入了4个值,base_info列族3个字段,extra_info没有插入值
hbase(main):011:0> put 'user_info','rk0002','base_info:id','2'
0 row(s) in 0.0060 seconds
hbase(main):012:0> put 'user_info','rk0002','base_info:name','lisi'
0 row(s) in 0.0060 seconds
hbase(main):013:0> put 'user_info','rk0002','base_info:sex','male'
0 row(s) in 0.0040 seconds
6、查询数据
scan命令
--全扫描
hbase(main):014:0> scan 'user_info'
ROW COLUMN+CELL
rk0001 column=base_info:age, timestamp=1530777071212, value=20
rk0001 column=base_info:id, timestamp=1530777007369, value=1
rk0001 column=base_info:name, timestamp=1530777057518, value=zhangsan
rk0001 column=extra_info:addr, timestamp=1530777108794, value=beijing
rk0002 column=base_info:id, timestamp=1530777246035, value=2
rk0002 column=base_info:name, timestamp=1530777259141, value=lisi
rk0002 column=base_info:sex, timestamp=1530777271117, value=male
2 row(s) in 0.0490 seconds
说明:hbase中,列族和kv都会都会自动排序,排序的贵州是按照列族名称和列名称的字典顺序
--范围查找
hbase(main):039:0> scan 'user_info', {COLUMNS => ['base_info','extra_info'], LIMIT => 10, STARTROW => 'rk0002'}
ROW COLUMN+CELL
rk0002 column=base_info:id, timestamp=1530777246035, value=2
rk0002 column=base_info:name, timestamp=1530777259141, value=lisi
rk0002 column=base_info:sex, timestamp=1530777271117, value=male
rk0003 column=base_info:id, timestamp=1530778028883, value=4
rk0005 column=base_info:sex, timestamp=1530778061222, value=female
rk0006 column=base_info:id, timestamp=1530778703899, value=12
rk0006 column=extra_info:addr, timestamp=1530778724940, value=shanghai
rk006 column=extra_info:code, timestamp=1530778095821, value=04442223
hbase(main):040:0> scan 'user_info', {COLUMNS => ['base_info'], LIMIT => 10, STARTROW => 'rk0002'}
ROW COLUMN+CELL
rk0002 column=base_info:id, timestamp=1530777246035, value=2
rk0002 column=base_info:name, timestamp=1530777259141, value=lisi
rk0002 column=base_info:sex, timestamp=1530777271117, value=male
rk0003 column=base_info:id, timestamp=1530778028883, value=4
rk0005 column=base_info:sex, timestamp=1530778061222, value=female
rk0006 column=base_info:id, timestamp=1530778703899, value=12
hbase(main):049:0> scan 'user_info', {COLUMNS => ['base_info','extra_info'], TIMERANGE => [1530777057518,1530778095821]}
ROW COLUMN+CELL
rk0001 column=base_info:age, timestamp=1530777071212, value=20
rk0001 column=base_info:name, timestamp=1530777057518, value=zhangsan
rk0001 column=extra_info:addr, timestamp=1530777108794, value=beijing
rk0002 column=base_info:id, timestamp=1530777246035, value=2
rk0002 column=base_info:name, timestamp=1530777259141, value=lisi
rk0002 column=base_info:sex, timestamp=1530777271117, value=male
rk0003 column=base_info:id, timestamp=1530778028883, value=4
rk0005 column=base_info:sex, timestamp=1530778061222, value=female
hbase(main):052:0> scan 'user_info', {FILTER => "(PrefixFilter ('rk0003'))"}
ROW COLUMN+CELL
rk0003 column=base_info:id, timestamp=1530778028883, value=4
1 row(s) in 0.0280 seconds
hbase(main):074:0> scan 'user_info', {FILTER => "((QualifierFilter (=, 'binary:id')))"}
ROW COLUMN+CELL
rk0001 column=base_info:id, timestamp=1530777007369, value=1
rk0002 column=base_info:id, timestamp=1530777246035, value=2
rk0003 column=base_info:id, timestamp=1530778028883, value=4
rk0006 column=base_info:id, timestamp=1530778703899, value=12
hbase(main):075:0> scan 'user_info', {FILTER => "((QualifierFilter (>=, 'binary:id')))"}
ROW COLUMN+CELL
rk0001 column=base_info:id, timestamp=1530777007369, value=1
rk0001 column=base_info:name, timestamp=1530777057518, value=zhangsan
rk0002 column=base_info:id, timestamp=1530777246035, value=2
rk0002 column=base_info:name, timestamp=1530777259141, value=lisi
rk0002 column=base_info:sex, timestamp=1530777271117, value=male
rk0003 column=base_info:id, timestamp=1530778028883, value=4
rk0005 column=base_info:sex, timestamp=1530778061222, value=female
rk0006 column=base_info:id, timestamp=1530778703899, value=12
hbase(main):015:0> scan 'user_info', {FILTER => "(PrefixFilter ('rk0003')) AND ((QualifierFilter (>=, 'binary:id'))))"}
ROW COLUMN+CELL
rk0003 column=base_info:id, timestamp=1530778028883, value=4
1 row(s) in 0.0110 seconds
get命令
hbase(main):017:0> get 'user_info','rk0001'
COLUMN CELL
base_info:age timestamp=1530777071212, value=20
base_info:id timestamp=1530777007369, value=1
base_info:name timestamp=1530777057518, value=zhangsan
extra_info:addr timestamp=1530777108794, value=beijing
4 row(s) in 0.0490 seconds
hbase(main):018:0> get 'user_info','rk0001',{COLUMN => 'base_info'}
COLUMN CELL
base_info:age timestamp=1530777071212, value=20
base_info:id timestamp=1530777007369, value=1
base_info:name timestamp=1530777057518, value=zhangsan
3 row(s) in 0.0160 seconds
--按版本查询
hbase(main):003:0> get 'user_info','rk0001', {COLUMN => 'base_info', VERSIONS => 4}
COLUMN CELL
base_info:age timestamp=1530777071212, value=20
base_info:id timestamp=1530777007369, value=1
base_info:name timestamp=1530780976365, value=qiqi
base_info:name timestamp=1530780971646, value=liuliu
base_info:name timestamp=1530777057518, value=zhangsan
hbase(main):002:0> get 'user_info','rk0001', {COLUMN => 'base_info:name', VERSIONS => 4}
COLUMN CELL
base_info:name timestamp=1530780976365, value=qiqi
base_info:name timestamp=1530780971646, value=liuliu
base_info:name timestamp=1530777057518, value=zhangsan
3 row(s) in 0.0490 seconds
6、管理名称空间
Hbase名称空间类似于关系型数据库的中的数据库
hbase(main):003:0* create_namespace 'orderdb'
--在名称空间创建表
hbase(main):005:0> create 'orderdb:t_order','f1','f2'
--查看名称空间
hbase(main):006:0> list_namespace
list_namespace list_namespace_tables
hbase(main):006:0> list_namespace
NAMESPACE
default
hbase
orderdb
--删除名称空间
需要先删除名称空间中表
删除表也需要先disable掉
示例:
hbase(main):010:0> disable 'orderdb:t_order'
0 row(s) in 1.2410 seconds
hbase(main):011:0> drop 'orderdb:t_order'
0 row(s) in 0.1630 seconds
hbase(main):012:0> drop_namespace 'orderdb'
0 row(s) in 0.0350 seconds