1,如何运行HBase
1,如何查找hbase的安装目录
whereis用来查找程序的安装目录、帮助文档等等,如下:
whereis hbase
结果如下,目录下一级包含bin的就是hbase的安装目录。
[hadoop@cdh002 bin]$ whereis hbase
hbase: /usr/bin/hbase /etc/hbase /usr/lib/hbase
2,如何运行hbase命令
首先切换到hbase的安装目录
cd /usr/lib/hbase/bin/
执行hbase shell
hbase shell
3 如何查找当前用户
使用whoami命令可查看当前用户
hbase(main):001:0> whoami
hadoop (auth:SIMPLE)
groups: hadoop
4 HBase如何删除字符
需要同时按住Ctrl + Backspace 才能删除之前输入的字符。
2,如何操作HBase表结构
1,查看有哪些表
list
2,如何创建HBase表
创建HBase表不需要指定列,只需要指定列簇就行了
语法:create <table>, {NAME => <family>, VERSIONS => <VERSIONS>}
比如创建一个表,保存用户的个人信息infos和工作经历jobs
(1)
hbase(main):007:0> create "user",{ NAME => "infos",VERSIONS => 1},{NAME => "jobs"}
0 row(s) in 4.6440 seconds
=> Hbase::Table - user
注意:NAME要大写,否则报错提示未定义的本地变量
NameError: undefined local variable or method `name' for #<Object:0x79972a95>
(2)简单的写法:表名,列簇
create "user","infos","jobs"
3,如何查看HBase的表结构
desc "user" 或者 describe "user"
hbase(main):008:0> describe "user"
Table user is ENABLED
user
COLUMN FAMILIES DESCRIPTION
{NAME => 'infos', DATA_BLOCK_ENCODING => 'NONE', BLOOMFILTER => 'ROW', REPLICATION_SCOPE => '0'
, VERSIONS => '1', COMPRESSION => 'NONE', MIN_VERSIONS => '0', TTL => 'FOREVER', KEEP_DELETED_C
ELLS => 'FALSE', BLOCKSIZE => '65536', IN_MEMORY => 'false', BLOCKCACHE => 'true'}
{NAME => 'jobs', DATA_BLOCK_ENCODING => 'NONE', BLOOMFILTER => 'ROW', REPLICATION_SCOPE => '0',
VERSIONS => '1', COMPRESSION => 'NONE', MIN_VERSIONS => '0', TTL => 'FOREVER', KEEP_DELETED_CE
LLS => 'FALSE', BLOCKSIZE => '65536', IN_MEMORY => 'false', BLOCKCACHE => 'true'}
2 row(s) in 0.0400 seconds
表结构信息显示有两个列簇
4,如何修改HBase的表结构
(1)首先要禁用表
disable "user"
(2)
删除字段指定METHOD => "delete"
因为有3台机器,因此更新了3台机器 Updating all regions with the new schema
hbase(main):002:0> alter "user",{NAME => "infos"},{NAME => "jobs",METHOD => "delete"},{NAME => "educations"}
Updating all regions with the new schema...
1/1 regions updated.
Done.
Updating all regions with the new schema...
1/1 regions updated.
Done.
Updating all regions with the new schema...
1/1 regions updated.
Done.
0 row(s) in 8.0190 seconds
(3)然后启用表
enable 'user'
5,如何删除HBase表
要先禁用表
hbase(main):004:0> disable "user"
0 row(s) in 0.0410 seconds
再drop表
hbase(main):005:0> drop "user"
0 row(s) in 1.3550 seconds
3,如何操作HBase表数据
1,hbase如何插入数据
语法:put <table>,<rowkey>,<family:column>,<value>
put 表名,列簇:列,值
hbase(main):020:0> put "user","rowno001","infos:name","zhangsan"
0 row(s) in 0.1400 seconds
hbase(main):021:0> put "user","rowno001","infos:age","18"
0 row(s) in 0.0100 seconds
注意:
(1)一次只能插入一列的值;同一行的多列,要分开列插入
2,查询记录
(1)查看所有记录
scan "user"
数据量很大时,千万不要用这个命令
(2) 查找前2条数据
hbase(main):028:0> scan "user",{LIMIT => 2}
ROW COLUMN+CELL
rowno001 column=infos:age, timestamp=1528170157838, value=18
rowno001 column=infos:name, timestamp=1528170127697, value=zhangsan
rowno002 column=infos:age, timestamp=1528170336721, value=19
(3)查找某范围内的记录
hbase(main):032:0> scan "user",{ STARTROW => "rowno002", ENDROW => "rowno004"}
ROW COLUMN+CELL
rowno002 column=infos:age, timestamp=1528170336721, value=19
rowno003 column=infos:name, timestamp=1528171140729, value=xiaosan
注意:ENDROW是不包含的。
(4)查找某一行记录
hbase(main):034:0> get "user","rowno001"
COLUMN CELL
infos:age timestamp=1528170157838, value=18
infos:name timestamp=1528170127697, value=zhangsan
2 row(s) in 0.0250 seconds
可以指定列名
hbase(main):035:0> get "user","rowno001","infos:name"
COLUMN CELL
infos:name timestamp=1528170127697, value=zhangsan
3,统计记录
(1)统计总共多少行
hbase(main):033:0> count "user"
3 row(s) in 0.0290 seconds
=> 3
4,修改一条数据
和添加数据的命令是一致的,修改一下值就行,会直接覆盖
put "user","rowno003","infos:name","san"
5,删除数据
删除某一列:
delete "user","rowno003","infos:name"
删除某一行:
注意删除某一行,用的是deleteall
deleteall "user","rowno002"