Hadoop基础教程-第10章 HBase:Hadoop数据库(10.5 HBase Shell)(草稿)

来源:互联网 发布:知轩藏书论坛 编辑:程序博客网 时间:2024/05/24 05:20

第10章 HBase:Hadoop数据库

10.5 HBase Shell


10.5.1 官方快速入门教程

http://hbase.apache.org/book.html#quickstart

Procedure: Use HBase For the First Time
Connect to HBase

这里写图片描述

10.5.2 连接HBase

进入HBase Shell

[root@node1 ~]# hbase shellSLF4J: Class path contains multiple SLF4J bindings.SLF4J: Found binding in [jar:file:/opt/hbase-1.2.6/lib/slf4j-log4j12-1.7.5.jar!/org/slf4j/impl/StaticLoggerBinder.class]SLF4J: Found binding in [jar:file:/opt/hadoop-2.7.3/share/hadoop/common/lib/slf4j-log4j12-1.7.10.jar!/org/slf4j/impl/StaticLoggerBinder.class]SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.SLF4J: Actual binding is of type [org.slf4j.impl.Log4jLoggerFactory]HBase Shell; enter 'help<RETURN>' for list of supported commands.Type "exit<RETURN>" to leave the HBase ShellVersion 1.2.6, rUnknown, Mon May 29 02:25:32 CDT 2017hbase(main):001:0>

查看版本

hbase(main):001:0> version1.2.6, rUnknown, Mon May 29 02:25:32 CDT 2017hbase(main):002:0>

查看状态

hbase(main):002:0> status1 active master, 1 backup masters, 3 servers, 0 dead, 0.6667 average loadhbase(main):003:0> 

查看当前用户

hbase(main):003:0> whoamiroot (auth:SIMPLE)    groups: roothbase(main):004:0>

查看帮助

hbase(main):004:0> helpHBase Shell, version 1.2.6, rUnknown, Mon May 29 02:25:32 CDT 2017Type 'help "COMMAND"', (e.g. 'help "get"' -- the quotes are necessary) for help on a specific command.Commands are grouped. Type 'help "COMMAND_GROUP"', (e.g. 'help "general"') for help on a command group.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, locate_region, show_filters  Group name: namespace  Commands: alter_namespace, create_namespace, describe_namespace, drop_namespace, list_namespace, list_namespace_tables  Group name: dml  Commands: append, count, delete, deleteall, get, get_counter, get_splits, incr, put, scan, truncate, truncate_preserve  Group name: tools  Commands: assign, balance_switch, balancer, balancer_enabled, catalogjanitor_enabled, catalogjanitor_run, catalogjanitor_switch, close_region, compact, compact_rs, flush, major_compact, merge_region, move, normalize, normalizer_enabled, normalizer_switch, split, trace, unassign, wal_roll, zk_dump  Group name: replication  Commands: add_peer, append_peer_tableCFs, disable_peer, disable_table_replication, enable_peer, enable_table_replication, list_peers, list_replicated_tables, remove_peer, remove_peer_tableCFs, set_peer_tableCFs, show_peer_tableCFs  Group name: snapshots  Commands: clone_snapshot, delete_all_snapshot, delete_snapshot, list_snapshots, restore_snapshot, snapshot  Group name: configuration  Commands: update_all_config, update_config  Group name: quotas  Commands: list_quotas, set_quota  Group name: security  Commands: grant, list_security_capabilities, revoke, user_permission  Group name: procedures  Commands: abort_procedure, list_procedures  Group name: visibility labels  Commands: add_labels, clear_auths, get_auths, list_labels, set_auths, set_visibilitySHELL USAGE:Quote all names in HBase Shell such as table and column names.  Commas delimitcommand parameters.  Type <RETURN> after entering a command to run it.Dictionaries of configuration used in the creation and alteration of tables areRuby Hashes. They look like this:  {'key1' => 'value1', 'key2' => 'value2', ...}and are opened and closed with curley-braces.  Key/values are delimited by the'=>' character combination.  Usually keys are predefined constants such asNAME, VERSIONS, COMPRESSION, etc.  Constants do not need to be quoted.  Type'Object.constants' to see a (messy) list of all constants in the environment.If you are using binary keys or values and need to enter them in the shell, usedouble-quote'd hexadecimal representation. For example:  hbase> get 't1', "key\x03\x3f\xcd"  hbase> get 't1', "key\003\023\011"  hbase> put 't1', "test\xef\xff", 'f1:', "\x01\x33\x40"The HBase shell is the (J)Ruby IRB with the above HBase-specific commands added.For more on the HBase Shell, see http://hbase.apache.org/book.htmlhbase(main):005:0> 

10.5.3 创建数据表

(1)查看create帮助

hbase(main):005:0> createERROR: wrong number of arguments (0 for 1)Here is some help for this command:Creates a table. Pass a table name, and a set of column familyspecifications (at least one), and, optionally, table configuration.Column specification can be a simple string (name), or a dictionary(dictionaries are described below in main help output), necessarily including NAME attribute. Examples:Create a table with namespace=ns1 and table qualifier=t1  hbase> create 'ns1:t1', {NAME => 'f1', VERSIONS => 5}Create a table with namespace=default and table qualifier=t1  hbase> create 't1', {NAME => 'f1'}, {NAME => 'f2'}, {NAME => 'f3'}  hbase> # The above in shorthand would be the following:  hbase> create 't1', 'f1', 'f2', 'f3'  hbase> create 't1', {NAME => 'f1', VERSIONS => 1, TTL => 2592000, BLOCKCACHE => true}  hbase> create 't1', {NAME => 'f1', CONFIGURATION => {'hbase.hstore.blockingStoreFiles' => '10'}}Table configuration options can be put at the end.Examples:  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'}}  hbase> create 't1', {NAME => 'f1', DFS_REPLICATION => 1}You can also keep around a reference to the created table:  hbase> t1 = create 't1', 'f1'Which gives you a reference to the table named 't1', on which you can thencall methods.hbase(main):006:0>

(2)创建表
语法:create ‘表名’, ‘列簇’

hbase(main):007:0> create 't1' ,'cf1','cf2'0 row(s) in 4.5550 seconds=> Hbase::Table - t1hbase(main):008:0>

(3)列举表

hbase(main):008:0> listTABLE                                                                                                                                                                                        t1                                                                                                                                                                                           1 row(s) in 0.0570 seconds=> ["t1"]hbase(main):009:0>

(4)查看表结构

hbase(main):009:0> desc 't1'Table t1 is ENABLED                                                                                                                                                                          t1                                                                                                                                                                                           COLUMN FAMILIES DESCRIPTION                                                                                                                                                                  {NAME => 'cf1', BLOOMFILTER => 'ROW', VERSIONS => '1', IN_MEMORY => 'false', KEEP_DELETED_CELLS => 'FALSE', DATA_BLOCK_ENCODING => 'NONE', TTL => 'FOREVER', COMPRESSION => 'NONE', MIN_VERSIONS => '0', BLOCKCACHE => 'true', BLOCKSIZE => '65536', REPLICATION_SCOPE => '0'}                                                                                                            {NAME => 'cf2', BLOOMFILTER => 'ROW', VERSIONS => '1', IN_MEMORY => 'false', KEEP_DELETED_CELLS => 'FALSE', DATA_BLOCK_ENCODING => 'NONE', TTL => 'FOREVER', COMPRESSION => 'NONE', MIN_VERSIONS => '0', BLOCKCACHE => 'true', BLOCKSIZE => '65536', REPLICATION_SCOPE => '0'}                                                                                                            2 row(s) in 0.3030 secondshbase(main):010:0> 

(5)表是否存在

hbase(main):010:0> exists 't1'Table t1 does exist                                                                                                                                                                          0 row(s) in 0.5200 secondshbase(main):011:0> 

10.5.4 操作数据表

(1)添加数据
添加4行

hbase(main):010:0> put 't1','r1','cf1:name','aa'0 row(s) in 0.1800 secondshbase(main):011:0> put 't1','r2','cf2:age','22'0 row(s) in 0.0620 secondshbase(main):012:0> put 't1','r3','cf1:name','bb'0 row(s) in 0.0230 secondshbase(main):013:0> put 't1','r4','cf2:age','23'0 row(s) in 0.0190 secondshbase(main):014:0> 

添加1行

hbase(main):014:0> put 't1','rk1','cf1:name','bb'0 row(s) in 0.0210 secondshbase(main):015:0> put 't1','rk1','cf1:age','23'0 row(s) in 0.0220 secondshbase(main):016:0> put 't1','rk1','cf2:cid','1001'0 row(s) in 0.0220 secondshbase(main):017:0> put 't1','rk1','cf2:dept','bigdata'0 row(s) in 0.0210 secondshbase(main):018:0> 

(2)查询

hbase(main):018:0> get 't1','rk1'COLUMN                                           CELL                                                                                                                                         cf1:age                                         timestamp=1501251921190, value=23                                                                                                            cf1:name                                        timestamp=1501251900543, value=bb                                                                                                            cf2:cid                                         timestamp=1501251958895, value=1001                                                                                                          cf2:dept                                        timestamp=1501252040732, value=bigdata                                                                                                      4 row(s) in 0.1210 secondshbase(main):019:0> get 't1','rk1','cf1:name'COLUMN                                           CELL                                                                                                                                         cf1:name                                        timestamp=1501251900543, value=bb                                                                                                           1 row(s) in 0.0440 secondshbase(main):020:0> get 't1','rk1','cf2:dept'COLUMN                                           CELL                                                                                                                                         cf2:dept                                        timestamp=1501252040732, value=bigdata                                                                                                      1 row(s) in 0.0200 secondshbase(main):021:0> 

(3)扫描表

hbase(main):021:0> scan 't1'ROW                                              COLUMN+CELL                                                                                                                                  r1                                              column=cf1:name, timestamp=1501251794870, value=aa                                                                                           r2                                              column=cf2:age, timestamp=1501251806541, value=22                                                                                            r3                                              column=cf1:name, timestamp=1501251815736, value=bb                                                                                           r4                                              column=cf2:age, timestamp=1501251824433, value=23                                                                                            rk1                                             column=cf1:age, timestamp=1501251921190, value=23                                                                                            rk1                                             column=cf1:name, timestamp=1501251900543, value=bb                                                                                           rk1                                             column=cf2:cid, timestamp=1501251958895, value=1001                                                                                          rk1                                             column=cf2:dept, timestamp=1501252040732, value=bigdata                                                                                     5 row(s) in 0.0900 secondshbase(main):022:0> 

(4)删除数据

删除整行数据

hbase(main):014:0> deleteall 't1','r1'0 row(s) in 0.1620 secondshbase(main):015:0> scan 't1'ROW                                              COLUMN+CELL                                                                                                                                  r2                                              column=cf2:age, timestamp=1501251806541, value=22                                                                                            r3                                              column=cf1:name, timestamp=1501251815736, value=bb                                                                                           r4                                              column=cf2:age, timestamp=1501251824433, value=23                                                                                            rk1                                             column=cf1:age, timestamp=1501251921190, value=23                                                                                            rk1                                             column=cf1:name, timestamp=1501251900543, value=bb                                                                                           rk1                                             column=cf2:cid, timestamp=1501251958895, value=1001                                                                                          rk1                                             column=cf2:dept, timestamp=1501252040732, value=bigdata                                                                                     4 row(s) in 0.2190 secondshbase(main):016:0>

删除指定行的列

hbase(main):016:0> delete 't1','rk1','cf2:dept'0 row(s) in 0.0260 secondshbase(main):017:0> get 't1','rk1'COLUMN                                           CELL                                                                                                                                         cf1:age                                         timestamp=1501251921190, value=23                                                                                                            cf1:name                                        timestamp=1501251900543, value=bb                                                                                                            cf2:cid                                         timestamp=1501251958895, value=1001                                                                                                         3 row(s) in 0.0840 secondshbase(main):018:0>

(5)删除表

hbase(main):018:0> create 't2','info'0 row(s) in 2.4840 seconds=> Hbase::Table - t2hbase(main):019:0> listTABLE                                                                                                                                                                                        t1                                                                                                                                                                                           t2                                                                                                                                                                                           2 row(s) in 0.0250 seconds=> ["t1", "t2"]hbase(main):020:0>

分两步,首先disable,然后drop

hbase(main):020:0> disable 't2'0 row(s) in 2.2960 secondshbase(main):021:0> drop 't2'0 row(s) in 1.3080 secondshbase(main):022:0> listTABLE                                                                                                                                                                                        t1                                                                                                                                                                                           1 row(s) in 0.0130 seconds=> ["t1"]hbase(main):023:0>

(6)截断表

hbase(main):023:0> count 't1'4 row(s) in 0.0670 seconds=> 4hbase(main):024:0> truncate 't1'Truncating 't1' table (it may take a while): - Disabling table... - Truncating table...0 row(s) in 4.2030 secondshbase(main):025:0> count 't1'0 row(s) in 0.1620 seconds=> 0hbase(main):026:0> 

(7)修改表结构

先disable后enable

hbase(main):026:0> disable 't1'0 row(s) in 2.3110 secondshbase(main):027:0> alter 't1',READONLYUpdating all regions with the new schema...1/1 regions updated.Done.0 row(s) in 1.9830 secondshbase(main):028:0> enable 't1'0 row(s) in 1.3060 secondshbase(main):029:0> 

10.5.5 命名空间

关系数据库系统中,命名空间namespace是表的逻辑分组,同一组中的表有类似的用途。

hbase的表也有命名空间的管理方式,命名空间的概念为即将到来的多租户特性打下基础:

  • 配额管理( Quota Management (HBASE-8410)):限制一个namespace可以使用的资源,资源包括region和table等;
  • 命名空间安全管理( Namespace Security Administration (HBASE-9206)):提供了另一个层面的多租户安全管理;
  • Region服务器组(Region server groups (HBASE-6721)):一个命名空间或一张表,可以被固定到一组 regionservers上,从而保证了数据隔离性。

(1)缺省命名空间
HBase系统默认定义了两个缺省的namespace

  • hbase:系统内建表,包括namespace和meta表
  • default:用户建表时未指定namespace的表都创建在此

可以通过list_namespace命令查看命名空间

hbase(main):029:0> list_namespaceNAMESPACE                                                                                                                                                                                    default                                                                                                                                                                                      hbase                                                                                                                                                                                        2 row(s) in 0.2170 secondshbase(main):030:0>

(2)创建namespace

hbase(main):030:0> create_namespace 'mydb'0 row(s) in 0.1070 secondshbase(main):031:0> list_namespaceNAMESPACE                                                                                                                                                                                    default                                                                                                                                                                                      hbase                                                                                                                                                                                        mydb                                                                                                                                                                                         3 row(s) in 0.0400 secondshbase(main):032:0> 

(3)查看namespace结构

hbase(main):032:0> describe_namespace 'mydb'DESCRIPTION                                                                                                                                                                                  {NAME => 'mydb'}                                                                                                                                                                             1 row(s) in 0.0330 secondshbase(main):033:0> 

(4)在指定namespace下创建表

hbase(main):033:0> create 'mydb:test',{NAME => 'f1', VERSIONS => 5}0 row(s) in 1.2490 seconds=> Hbase::Table - mydb:testhbase(main):034:0>
hbase(main):034:0> listTABLE                                                                                                                                                                                        mydb:test                                                                                                                                                                                    t1                                                                                                                                                                                           2 row(s) in 0.0390 seconds=> ["mydb:test", "t1"]hbase(main):035:0>

(5)添加数据

hbase(main):035:0> put 'mydb:test','rk1','f1:name','abc'0 row(s) in 0.1310 secondshbase(main):036:0> put 'mydb:test','rk1','f1:age','2'0 row(s) in 0.0220 secondshbase(main):037:0> scan 'mydb:test'ROW                                              COLUMN+CELL                                                                                                                                  rk1                                             column=f1:age, timestamp=1501335244278, value=2                                                                                              rk1                                             column=f1:name, timestamp=1501335234503, value=abc                                                                                          1 row(s) in 0.0970 secondshbase(main):038:0>
阅读全文
0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 打呼吵得睡不着怎么办 打呼太吵睡不着怎么办 睡觉时舍友说话怎么办 夜晚醒了睡不着怎么办 夜晚怕黑睡不着怎么办 宝宝不愿意盖被子怎么办 白天睡觉晚上睡不着怎么办 晚上睡觉不安神怎么办 晚上经常睡不着觉怎么办 老是睡不着觉怎么办啊 小孩睡觉不安稳怎么办 睡觉时动不了怎么办 特别累还睡不着怎么办 又累又睡不着怎么办 干活累的睡不着怎么办 狗一有动静就叫怎么办 楼上天天闹动静怎么办 喝了奶茶失眠怎么办 失眠一宿第二天怎么办 睡觉外面噪音大怎么办 怀孕早期晚上睡不着怎么办 短发发尾翘怎么办 很累就是睡不着怎么办 人累但是睡不着怎么办 如果晚上睡不着该怎么办 晚上睡不着觉该怎么办 晚上睡不着该怎么办呢 晚上失眠睡不着该怎么办 晚上一直睡不着该怎么办 怀孕晚上睡不着该怎么办 运动太累睡不着怎么办 运动完睡不着觉怎么办 晚上冷得睡不着怎么办 晚上脚冷睡不着怎么办 短发头发有点乱怎么办 不想让别人睡觉怎么办 15岁晚上睡不着怎么办 16岁青少年失眠怎么办 好累又睡不着怎么办 造口患者拉肚子怎么办? 起床后头发乱怎么办