HBase Shell 练习

96
_和_
2016.12.30 07:15* 字数 1079

1. 环境准备

成功启动HBase,假如你还没有成功地部署HBase,可以先看下:HBase部署入门指南

2. HBase Shell 练习

2.1 基本操作

1)成功启动HBase之后,想要使用HBase Shell ,得输入命令行:hbase shell 并且控制台成功打印以下信息:

HBase Shell 进入成功

备注:写错 HBase Shell 命令时将光标移动到你要删除的字符上,按下‘Backspace’或者‘Delete’删除

2)输入help命令查看 ‘COMMAND’ :hbase(main):001:0> help

HBase Shell help.png

3)输入 version 查看HBase 版本

hbase(main):003:0> version
0.98.23-hadoop2, r44c724b56dc1431209f561cb997fce805f9f45f9, Wed Oct 5 01:05:05 UTC 2016

2.2 DDL 操作

1)创建和删除表格

创建
create 'student','info','address','score'
删除表格之前先要disabled
disabled 'student'
drop 'student'

2)查看表格描述

describe 'student'

如下:

hbase(main):015:0> describe 'student'
Table student is ENABLED
student
COLUMN FAMILIES DESCRIPTION
{NAME => 'address', DATA_BLOCK_ENCODING => 'NONE', BLOOMFILTER => 'ROW', REPLICATION_SCOPE => '0', VERSIONS => '1', COMPRESSION => 'NONE', MIN_VERSIONS => '0', TTL => 'FOREVER', KEEP_DELETE
D_CELLS => 'FALSE', BLOCKSIZE => '65536', IN_MEMORY => 'false', BLOCKCACHE => 'true'}
{NAME => 'info', 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 => 'score', 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'}
3 row(s) in 0.0900 seconds

3)修改表格(schema)

alter 'student',NAME=>'info',VERSIONS => 5

查询结果如下:

hbase(main):012:0> describe 'student'
Table student is ENABLED
student
COLUMN FAMILIES DESCRIPTION
{NAME => 'address', DATA_BLOCK_ENCODING => 'NONE', BLOOMFILTER => 'ROW', REPLICATION_SCOPE => '0', COMPRESSION => 'NONE', VERSIONS => '1', TTL => 'FOREVER', MIN_VERSIONS => '0', KEEP_DELETE
D_CELLS => 'FALSE', BLOCKSIZE => '65536', IN_MEMORY => 'false', BLOCKCACHE => 'true'}
{NAME => 'info', DATA_BLOCK_ENCODING => 'NONE', BLOOMFILTER => 'ROW', REPLICATION_SCOPE => '0', COMPRESSION => 'NONE', VERSIONS => '5', TTL => 'FOREVER', MIN_VERSIONS => '0', KEEP_DELETED_C
ELLS => 'FALSE', BLOCKSIZE => '65536', IN_MEMORY => 'false', BLOCKCACHE => 'true'}
{NAME => 'score', DATA_BLOCK_ENCODING => 'NONE', BLOOMFILTER => 'ROW', REPLICATION_SCOPE => '0', COMPRESSION => 'NONE', VERSIONS => '1', TTL => 'FOREVER', MIN_VERSIONS => '0', KEEP_DELETED_
CELLS => 'FALSE', BLOCKSIZE => '65536', IN_MEMORY => 'false', BLOCKCACHE => 'true'}
3 row(s) in 0.0740 seconds

注意上下两个查询中黑体字的对比

2.3 DML操作

1)插入数据:put

put 'student','xiaoming','info:age',18
put 'student','xiaoming','info:birthday','1990-12-12'
put 'student','xiaoming','info:school','beijingdaxue'
put 'student','xiaoming','address:country','china'
put 'student','xiaoming','address:province','guangdong'

put 'student','xiaoming','address:city','shenzhen'

2)全盘扫描:scan

2.1)获得整个表格的数据

scan 'student'

scan 结果.png

2.2)查询某一列的数据:scan

scan 'student' {COLUMNS => 'info:birthday'}

scan某列数据.png

注意:如果你不知道scan还有那些方法可以使用,你可以输入 scan help,然后系统就会告诉你出错,并且告诉你正确的多重输入指令

3)get:获得数据
3.1)获得某一行数据

get 'student','xiaoming'

获得某行数据.png

3.2)获得某行某列族数据

get 'student','xiaoming','info'

获得某行某列族数据.png

3.3)获得某行某列族某列数据

get 'student','xiaoming','info:age'

获得某行某列族某列数据.png

3.4)获得某行某列族某列的多个版本数据
先插入数据:

put 'student','xiaoming','info:age',100,123456789
put 'student','xiaoming','info:age',158,12313213132212321

再查询数据:

get 'student','xiaoming',{COLUMN => 'info:age',TIMERANGE =>[0,123132131322123211],VERSIONS =>5}

结果如下:

查询多版本结果

更多关于get的命令如下,大家有时间可以多敲敲

  常用get命令格式:
  hbase> get 'ns1:t1', 'r1'
  hbase> get 't1', 'r1'
  hbase> get 't1', 'r1', {TIMERANGE => [ts1, ts2]}
  hbase> get 't1', 'r1', {COLUMN => 'c1'}
  hbase> get 't1', 'r1', {COLUMN => ['c1', 'c2', 'c3']}
  hbase> get 't1', 'r1', {COLUMN => 'c1', TIMESTAMP => ts1}
  hbase> get 't1', 'r1', {COLUMN => 'c1', TIMERANGE => [ts1, ts2], VERSIONS => 4}
  hbase> get 't1', 'r1', {COLUMN => 'c1', TIMESTAMP => ts1, VERSIONS => 4}
  hbase> get 't1', 'r1', {FILTER => "ValueFilter(=, 'binary:abc')"}
  hbase> get 't1', 'r1', 'c1'
  hbase> get 't1', 'r1', 'c1', 'c2'
  hbase> get 't1', 'r1', ['c1', 'c2']
  hbase> get 't1','r1', {COLUMN => 'c1', ATTRIBUTES => {'mykey'=>'myvalue'}}
  hbase> get 't1','r1', {COLUMN => 'c1', AUTHORIZATIONS => ['PRIVATE','SECRET']}

4)更新数据

put 'student','xiaoming','info:age',100

再查询info:age

get 'student','xiaoming','info:age'

更新数据后查询.png

更多关于put的命令如下,大家有时间多敲敲

  hbase> put 'ns1:t1', 'r1', 'c1', 'value'
  hbase> put 't1', 'r1', 'c1', 'value'
  hbase> put 't1', 'r1', 'c1', 'value', ts1
  hbase> put 't1', 'r1', 'c1', 'value', {ATTRIBUTES=>{'mykey'=>'myvalue'}}
  hbase> put 't1', 'r1', 'c1', 'value', ts1, {ATTRIBUTES=>{'mykey'=>'myvalue'}}
  hbase> put 't1', 'r1', 'c1', 'value', ts1, {VISIBILITY=>'PRIVATE|SECRET'}

5)统计:统计有多少行

count 'student'

统计行数.png

6)删除数据
6.1)删除student表中'xiaoming'行中的info:school中的值

delete 'student','xiaoming','info:school'

再查询:

get 'student','xiaoming','info:school'

删除后查询.png

6.2)删除整行

deleteall 'student','xiaoming'

再查询:

scan 'student'

删除整行后查询.png

更多删除全部命令如下:

  hbase> deleteall 'ns1:t1', 'r1'
  hbase> deleteall 't1', 'r1'
  hbase> deleteall 't1', 'r1', 'c1'
  hbase> deleteall 't1', 'r1', 'c1', ts1
  hbase> deleteall 't1', 'r1', 'c1', ts1, {VISIBILITY=>'PRIVATE|SECRET'}

7)创建 “对象”

  hbase> t = get_table 't'
  hbase> t.scan

get 的命令:

  hbase> t.get 'r1'
  hbase> t.get 'r1', {TIMERANGE => [ts1, ts2]}
  hbase> t.get 'r1', {COLUMN => 'c1'}
  hbase> t.get 'r1', {COLUMN => ['c1', 'c2', 'c3']}
  hbase> t.get 'r1', {COLUMN => 'c1', TIMESTAMP => ts1}
  hbase> t.get 'r1', {COLUMN => 'c1', TIMERANGE => [ts1, ts2], VERSIONS => 4}
  hbase> t.get 'r1', {COLUMN => 'c1', TIMESTAMP => ts1, VERSIONS => 4}
  hbase> t.get 'r1', {FILTER => "ValueFilter(=, 'binary:abc')"}
  hbase> t.get 'r1', 'c1'
  hbase> t.get 'r1', 'c1', 'c2'
  hbase> t.get 'r1', ['c1', 'c2']

例子:

t.get 'xiaoming', {COLUMNS => 'info:age'}

t.get查询info_age.png

8)分区

hbase(main):002:0> create 'people','info',SPLITS=>['10','20','30','40']
0 row(s) in 0.4970 seconds
=> Hbase::Table - people

分区结果
  hbase> create 'ns1:t1', 'f1', SPLITS => ['10', '20', '30', '40']
  hbase> create 't1', 'f1', SPLITS => ['10', '20', '30', '40']
  hbase> create 't1', 'f1', SPLITS_FILE => 'splits.txt', OWNER => 'johndoe'
  hbase> create 't1', {NAME => 'f1', VERSIONS => 5}, METADATA => { 'mykey' => 'myvalue' }
  hbase> # Optionally pre-split the table into NUMREGIONS, using
  hbase> # SPLITALGO ("HexStringSplit", "UniformSplit" or classname)
  hbase> create 't1', 'f1', {NUMREGIONS => 15, SPLITALGO => 'HexStringSplit'}
  hbase> create 't1', 'f1', {NUMREGIONS => 15, SPLITALGO => 'HexStringSplit', REGION_REPLICATION => 2, CONFIGURATION => {'hbase.hregion.scan.loadColumnFamiliesOnDemand' => 'true'}}

其他命令的格式都跟get的一样,请自行修改即可

HBase
Web note ad 1