當(dāng)前位置:首頁 > IT技術(shù) > 系統(tǒng)服務(wù) > 正文

hbase shell學(xué)習(xí)-2
2021-08-10 17:40:14

一個學(xué)生成績表的例子來演示hbase的用法。
?

name grade course
math english
Tom 5 97 87
Jim 4 89 80

表的創(chuàng)建:語法:create '表名稱','列簇名稱1','列簇名稱2'........

create 'student','name','grade','course'

?

desc 'student'

結(jié)果:

{
    NAME=>'course',
    DATA_BLOCK_ENCODING=>'NONE',
    BLOOMFILTER=>'ROW',
    REPLICATION_SCOPE=>'0',
    VERSIONS=>'1',
    COMPRESSION=>'NONE',
    MIN_VERSIONS=>'0',
    TTL=>'FOREVER',
    KEEP_DELETED_CELLS=>'FALSE',
    BLOCKSIZE=>'65536',
    IN_MEMORY=>'false',
    BLOCKCACHE=>'true'
}{
    NAME=>'grade',
    DATA_BLOCK_ENCODING=>'NONE',
    BLOOMFILTER=>'ROW',
    REPLICATION_SCOPE=>'0',
    VERSIONS=>'1',
    COMPRESSION=>'NONE',
    MIN_VERSIONS=>'0',
    TTL=>'FOREVER',
    KEEP_DELETED_CELLS=>'FALSE',
    BLOCKSIZE=>'65536',
    IN_MEMORY=>'false',
    BLOCKCACHE=>'true'
}{
    NAME=>'name',
    DATA_BLOCK_ENCODING=>'NONE',
    BLOOMFILTER=>'ROW',
    REPLICATION_SCOPE=>'0',
    VERSIONS=>'1',
    COMPRESSION=>'NONE',
    MIN_VERSIONS=>'0',
    TTL=>'FOREVER',
    KEEP_DELETED_CELLS=>'FALSE',
    BLOCKSIZE=>'65536',
    IN_MEMORY=>'false',
    BLOCKCACHE=>'true'
}

新增列簇:

alter '表名稱',NAME='列簇名稱'

hbase(main):068:0> alter 'student',NAME=>'age'
Updating all regions with the new schema...
1/1 regions updated.

刪除列簇:

alter '表名稱',NAME=>'列簇名稱',METHOD=>'delete'

?alter 'student',NAME=>'test',METHOD=>'delete'

刪除一個表:之前,必須先將該表disable掉。

disable 'student'

drop 'student'

給表添加記錄:

put '表名稱','rowkey','列簇名稱:列名稱','值'

put 'student','001201509011001','name','Tom'

結(jié)果:

hbase(main):085:0> scan 'student'
ROW                           COLUMN+CELL
 001201509011001              column=name:, timestamp=1447766388162, value=Tom
1 row(s) in 0.0090 seconds

繼續(xù)執(zhí)行:name列簇的value='jim',但是rowkey 不變,

hbase(main):086:0> put 'student','001201509011001','name','Jim'

結(jié)果還是一條數(shù)據(jù),001201509011001的rowkey,被第二條數(shù)據(jù)覆蓋。

?

scan 'student'
ROW                           COLUMN+CELL
 001201509011001              column=name:, timestamp=1447766492893, value=Jim
put 'student','001201509011001','course:math','100'
put 'student','001201509011001','course:english','100'
hbase(main):096:0> scan 'student'
ROW                           COLUMN+CELL
 001201509011001              column=course:english, timestamp=1447766828720, value=100
 001201509011001              column=course:math, timestamp=1447766813289, value=100
 001201509011001              column=grade:, timestamp=1447766751652, value=2
 001201509011001              column=name:, timestamp=1447766492893, value=Jim
 001201509011002              column=name:, timestamp=1447766547713, value=Tom


加了很多rowKey進行測試:如下

?

base(main):127:0> scan 'student'
ROW                           COLUMN+CELL
 001201509011001              column=course:english, timestamp=1447766828720, value=100
 001201509011001              column=course:math, timestamp=1447766813289, value=100
 001201509011001              column=grade:, timestamp=1447766751652, value=2
 001201509011001              column=name:, timestamp=1447766492893, value=Jim
 001201509011002              column=course:english, timestamp=1447766987607, value=95
 001201509011002              column=course:math, timestamp=1447767003501, value=80
 001201509011002              column=grade:, timestamp=1447767073299, value=6
 001201509011002              column=name:, timestamp=1447766547713, value=Tom
 001201509011003              column=grade:, timestamp=1447767130750, value=5
 001201509011004              column=grade:, timestamp=1447767139371, value=3
 001201509011005              column=grade:, timestamp=1447767146338, value=3
 001201509011006              column=course:math, timestamp=1447767489278, value=30
 001201509011006              column=grade:, timestamp=1447767153088, value=2
 001201509011007              column=course:math, timestamp=1447767474245, value=87
 001201509011007              column=grade:, timestamp=1447767173296, value=2
 001201509011008              column=grade:, timestamp=1447767181639, value=3
 001201509011008              column=name:, timestamp=1447767278902, value=lucy
 001201509011009              column=grade:, timestamp=1447767190450, value=10
 001201509011009              column=name:, timestamp=1447767257259, value=Mike
 001201509011010              column=grade:, timestamp=1447767198644, value=11
 001201509011010              column=name:, timestamp=1447767236548, value=Peter

根據(jù)rowkey查看對應(yīng)列的數(shù)據(jù):

get '表名稱','rowkey','列簇名稱:列名稱'

 get 'student','001201509011001','name'
COLUMN                        CELL
 name:                        timestamp=1447766492893, value=Jim

?

查看表中的記錄數(shù):根據(jù)列簇來統(tǒng)計:

hbase(main):133:0* count 'student'

結(jié)果:10

?

查詢表中指定列的所有記錄:

語法:scan '表名',{COLUMNS =>'列簇'}

?

hbase(main):134:0> scan 'student',{COLUMNS=>'name'}
ROW                           COLUMN+CELL
 001201509011001              column=name:, timestamp=1447766492893, value=Jim
 001201509011002              column=name:, timestamp=1447766547713, value=Tom
 001201509011008              column=name:, timestamp=1447767278902, value=lucy
 001201509011009              column=name:, timestamp=1447767257259, value=Mike
 001201509011010              column=name:, timestamp=1447767236548, value=Peter
hbase(main):135:0> scan 'student',{COLUMNS=>'grade'}
ROW                           COLUMN+CELL
 001201509011001              column=grade:, timestamp=1447766751652, value=2
 001201509011002              column=grade:, timestamp=1447767073299, value=6
 001201509011003              column=grade:, timestamp=1447767130750, value=5
 001201509011004              column=grade:, timestamp=1447767139371, value=3
 001201509011005              column=grade:, timestamp=1447767146338, value=3
 001201509011006              column=grade:, timestamp=1447767153088, value=2
 001201509011007              column=grade:, timestamp=1447767173296, value=2
 001201509011008              column=grade:, timestamp=1447767181639, value=3
 001201509011009              column=grade:, timestamp=1447767190450, value=10
 001201509011010              column=grade:, timestamp=1447767198644, value=11
10 row(s) in 0.0220 seconds
hbase(main):136:0> scan 'student',{COLUMNS=>'course'}
ROW                           COLUMN+CELL
 001201509011001              column=course:english, timestamp=1447766828720, value=100
 001201509011001              column=course:math, timestamp=1447766813289, value=100
 001201509011002              column=course:english, timestamp=1447766987607, value=95
 001201509011002              column=course:math, timestamp=1447767003501, value=80
 001201509011006              column=course:math, timestamp=1447767489278, value=30
 001201509011007              column=course:math, timestamp=1447767474245, value=87
4 row(s) in 0.0130 seconds

?

查詢表中指定區(qū)間的所有記錄數(shù):

也可以指定一些修飾詞:TIMERANGE, FILTER, LIMIT, STARTROW, STOPROW, TIMESTAMP, MAXLENGTH,or COLUMNS。沒任何修飾詞,就是上邊例句,就會顯示所有數(shù)據(jù)行。

語法:scan '表名',{COLUMNS =>'列簇',LIMIT =>記錄數(shù),STARTROW=>'開始rowkey',STOPROW=>'結(jié)束rowkey'}

取出name列族,前3條記錄

hbase(main):012:0> scan 'student',{COLUMNS=>['name'],LIMIT=>3}
ROW                           COLUMN+CELL
 001201509011001              column=name:, timestamp=1447766492893, value=Jim
 001201509011002              column=name:, timestamp=1447766547713, value=Tom
 001201509011008              column=name:, timestamp=1447767278902, value=lucy

取出name列族,前3條記錄 rowkey[001201509011001,001201509011008) 左邊閉合,右邊開的數(shù)據(jù)。

hbase(main):014:0> scan 'student',{COLUMNS=>['name'],LIMIT=>3,STARTROW=>'001201509011001',STOPROW=>'001201509011008'}
ROW                           COLUMN+CELL
 001201509011001              column=name:, timestamp=1447766492893, value=Jim
 001201509011002              column=name:, timestamp=1447766547713, value=Tom

指定兩列:name,grade

hbase(main):018:0> scan 'student',{COLUMNS=>['name','grade'],STARTROW=>'001201509011001',STOPROW=>'001201509011010'}
ROW                           COLUMN+CELL
 001201509011001              column=grade:, timestamp=1447766751652, value=2
 001201509011001              column=name:, timestamp=1447766492893, value=Jim
 001201509011002              column=grade:, timestamp=1447767073299, value=6
 001201509011002              column=name:, timestamp=1447766547713, value=Tom
 001201509011003              column=grade:, timestamp=1447767130750, value=5
 001201509011004              column=grade:, timestamp=1447767139371, value=3
 001201509011005              column=grade:, timestamp=1447767146338, value=3
 001201509011006              column=grade:, timestamp=1447767153088, value=2
 001201509011007              column=grade:, timestamp=1447767173296, value=2
 001201509011008              column=grade:, timestamp=1447767181639, value=3
 001201509011008              column=name:, timestamp=1447767278902, value=lucy
 001201509011009              column=grade:, timestamp=1447767190450, value=10
 001201509011009              column=name:, timestamp=1447767257259, value=Mike

?

可以根據(jù) TIMERANGE查詢:

hbase(main):020:0> scan 'student',{COLUMNS=>['grade'],LIMIT => 3,TIMERANGE=>[1447766751652,1447767257259]}
ROW                           COLUMN+CELL
 001201509011001              column=grade:, timestamp=1447766751652, value=2
 001201509011002              column=grade:, timestamp=1447767073299, value=6
 001201509011003              column=grade:, timestamp=1447767130750, value=5

?

刪除數(shù)據(jù):

rowkey=001201509011002 的數(shù)據(jù)有4列

hbase(main):024:0> get 'student','001201509011002'
COLUMN                        CELL
 course:english               timestamp=1447766987607, value=95
 course:math                  timestamp=1447767003501, value=80
 grade:                       timestamp=1447767073299, value=6
 name:                        timestamp=1447766547713, value=Tom

刪除一行數(shù)據(jù):

hbase(main):027:0> delete 'student','001201509011002','grade'
hbase(main):028:0> get 'student','001201509011002'
COLUMN                        CELL
 course:english               timestamp=1447766987607, value=95
 course:math                  timestamp=1447767003501, value=80
 name:                        timestamp=1447766547713, value=Tom

查看那一行的數(shù)據(jù):發(fā)現(xiàn)沒有了。

hbase(main):033:0> scan 'student',{COLUMNS=>['grade'],STARTROW=>'001201509011002',STOPROW=>'001201509011003'}
ROW                           COLUMN+CELL
0 row(s) in 0.0080 seconds

?

?

?http://www.cnblogs.com/ggjucheng/p/3379607.html? 參考:HBase shell的基本用法

http://blog.csdn.net/u010967382/article/category/2387735?參考?HBase基本數(shù)據(jù)操作詳解【完整版,絕對精品】

?
?
?
?
Face your past without regret. Handle your present with confidence.Prepare for future without fear. keep the faith and drop the fear. 面對過去無怨無悔,把握現(xiàn)在充滿信心,備戰(zhàn)未來無所畏懼。保持信念,克服恐懼!一點一滴的積累,一點一滴的沉淀,學(xué)技術(shù)需要不斷的積淀!

本文摘自 :https://blog.51cto.com/u

開通會員,享受整站包年服務(wù)立即開通 >