Hbase学习记录(2)| Shell操作

时间:2022-11-07 00:11:39

查看表结构 describe ‘表名’

查看版本 get ‘表名’,’zhangsan’{COLUMN=>’info:age’,VERSIONS=>3}

删除整行 deleteall ‘表名’

清空表 truncate ‘表名’

关闭表 disable ‘表名’

开启表 enbale ‘表名’

添加列族 alter ‘表名’,{NAME=>’f2’,VERSIONS=>3} //添加前需要先关闭表,完成操作后再开启

hbase(main):001:0> alter 'log1',{NAME => 'info',VERSIONS => 3}

Updating all regions with the new schema...

1/1 regions updated.

Done.

0 row(s) in 1.7290 seconds

删除列族 alter ‘表名’,{NAME => ‘info’,METHOD => ‘delete’} (先关闭表再删除)

hbase(main):007:0> alter 'log1',{NAME => 'info',METHOD => 'delete'}

Updating all regions with the new schema...

1/1 regions updated.

Done.

0 row(s) in 1.1610 seconds

判断表是否存在 exists ‘表名’

hbase(main):010:0> exists 'log1'

Table log1 does exist #表存在

0 row(s) in 0.0430 seconds

hbase(main):011:0> exists 'log'

Table log does not exist #表不存在

0 row(s) in 0.0990 seconds

判断表是否开启 is_enabled ‘log1’

hbase(main):012:0> is_enabled 'log1'

true

0 row(s) in 0.0090 seconds

查看服务器状态 status

hbase(main):006:0> status

3 servers, 0 dead, 1.0000 average load

查看服务器版本

hbase(main):005:0> version

0.94.7, r1471806, Wed Apr 24 18:44:36 PDT 2013

put

插入数据实例:

hbase(main):001:0> put 'log1','row2:1.1.1.2 ','cf :ip ','1.1.1.2 '

0 row(s) in 1.4540 seconds

hbase(main):002:0> put 'log1','row3:1.1.1.3','cf:ip','1.1.1.3'

0 row(s) in 0.0100 seconds

hbase(main):003:0> scan 'log1'

ROW COLUMN+CELL

row1:1.1.1.1 column=cf:ip, timestamp=1394459831377, value=1.1.1.1

row2:1.1.1.2 column=cf:ip, timestamp=1394459903492, value=1.1.1.2

row3:1.1.1.3 column=cf:ip, timestamp=1394459932581, value=1.1.1.3

3 row(s) in 0.0860 seconds

get

查看数据实例

hbase(main):011:0> get 'log1','row1:1.1.1.1'

COLUMN CELL

cf:ip timestamp=1394460344525, value=1.1.1.2

scan

查看表中所有信息

hbase(main):012:0> scan 'log1'

ROW COLUMN+CELL

row1:1.1.1.1 column=cf:ip, timestamp=1394460344525, value=1.1.1.2

row2:1.1.1.2 column=cf:ip, timestamp=1394459903492, value=1.1.1.2

row3:1.1.1.3 column=cf:ip, timestamp=1394459932581, value=1.1.1.3

查看表中所有cf列族的信息

hbase(main):020:0> scan 'log1',{COLUMNS => 'cf'}

ROW COLUMN+CELL

2014-1-1:1.1.1.1 column=cf:age, timestamp=1394678729716, value=\x00\x00\x00\x00\x00\x00\x00\x03

2014-1-1:1.1.1.1 column=cf:city, timestamp=1394677780371, value=hunan

2014-1-1:1.1.1.1 column=cf:host, timestamp=1394677584102, value=222.123.42.13

2014-1-1:1.1.1.1 column=cf:tmp, timestamp=1394678565621, value=\x00\x00\x00\x00\x00\x00\x00\x01

2014-1-1:1.1.1.1 column=cf:url, timestamp=1394677483830, value=www.everbuys.com

row2:1.1.1.2 column=cf:ip, timestamp=1394459903492, value=1.1.1.2

row3:1.1.1.3 column=cf:ip, timestamp=1394459932581, value=1.1.1.3

row4:1.1.1.43 column=cf:info, timestamp=1394676106631, value=www.baidu.com

row4:1.1.1.43 column=cf:ip, timestamp=1394676368581, value=1.1.1.43

row4:1.1.1.45 column=cf:ip, timestamp=1394676152264, value=1.1.1.43

5 row(s) in 0.0350 seconds

查看表中cf列族的city列所有信息

hbase(main):021:0> scan 'log1',{COLUMNS => 'cf:city'}

ROW COLUMN+CELL

2014-1-1:1.1.1.1 column=cf:city, timestamp=1394677780371, value=hunan

1 row(s) in 0.0180 seconds

删除表

先disable再drop表

hbase(main):014:0> disable 'log2'

0 row(s) in 2.3600 seconds

hbase(main):015:0> drop 'log2'

0 row(s) in 1.1570 seconds

delete

删除一个字段

hbase(main):030:0> delete 'log1','2014-1-1:1.1.1.1','cf:method'

0 row(s) in 0.0350 seconds

deleteall

删除整行

hbase(main):031:0> deleteall 'log1','row1:1.1.1.1'

0 row(s) in 0.0140 seconds

count

统计表中的行数

hbase(main):033:0> count 'log1'

5 row(s) in 0.0550 seconds

incr

指定行添加字段

hbase(main):003:0> incr 'log1','2014-1-1:1.1.1.1','cf:tmp'

COUNTER VALUE = 1

get_counter

获取当前count值

hbase(main):012:0> get_counter 'log1','2014-1-1:1.1.1.1','cf:age'

COUNTER VALUE = 3

退出shell

hbase(main):016:0> exit

[root@h1 ~]#

关闭hbase集群

[root@h1 ~]# stop-hbase.sh

stopping hbase..........

Log时间转换

hbase(main):001:0> import java.text.SimpleDateFormat

=> Java::JavaText::SimpleDateFormat

hbase(main):003:0> import java.text.ParsePosition

=> Java::JavaText::ParsePosition

hbase(main):004:0> import java.util.Date

file:/usr/local/hbase/lib/jruby-complete-1.6.5.jar!/builtin/javasupport/core_ext/object.rb:99 warning: already initialized constant Date

=> Java::JavaUtil::Date

Debug

开启debug

hbase(main):009:0* debug

Debug mode is ON

可以在进入shell模式时添加 –d的选项运行debug模式

[root@h1 bin]# hbase shell -d

Setting DEBUG log level...

HBase Shell; enter 'help<RETURN>' for list of supported commands.

Type "exit<RETURN>" to leave the HBase Shell

Version 0.94.7, r1471806, Wed Apr 24 18:44:36 PDT 2013

hbase(main):001:0>