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

第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 shell
SLF4J: 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 Shell
Version 1.2.6, rUnknown, Mon May 29 02:25:32 CDT 2017

hbase(main):001:0>

查看版本

hbase(main):001:0> version
1.2.6, rUnknown, Mon May 29 02:25:32 CDT 2017

hbase(main):002:0>

查看状态

hbase(main):002:0> status
1 active master, 1 backup masters, 3 servers, 0 dead, 0.6667 average load

hbase(main):003:0> 

查看当前用户

hbase(main):003:0> whoami
root (auth:SIMPLE)
    groups: root

hbase(main):004:0>

查看帮助

hbase(main):004:0> help
HBase Shell, version 1.2.6, rUnknown, Mon May 29 02:25:32 CDT 2017
Type '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_visibility

SHELL USAGE:
Quote all names in HBase Shell such as table and column names.  Commas delimit
command parameters.  Type <RETURN> after entering a command to run it.
Dictionaries of configuration used in the creation and alteration of tables are
Ruby 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 as
NAME, 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, use
double-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.html
hbase(main):005:0> 

10.5.3 创建数据表

(1)查看create帮助

hbase(main):005:0> create

ERROR: 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 family
specifications (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 then
call methods.


hbase(main):006:0>

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

hbase(main):007:0> create 't1' ,'cf1','cf2'
0 row(s) in 4.5550 seconds

=> Hbase::Table - t1
hbase(main):008:0>

(3)列举表

hbase(main):008:0> list
TABLE                                                                                                                                                                                        
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_VERSI
ONS => '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_VERSI
ONS => '0', BLOCKCACHE => 'true', BLOCKSIZE => '65536', REPLICATION_SCOPE => '0'}                                                                                                            
2 row(s) in 0.3030 seconds

hbase(main):010:0> 

(5)表是否存在

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

hbase(main):011:0> 

10.5.4 操作数据表

(1)添加数据 添加4行

hbase(main):010:0> put 't1','r1','cf1:name','aa'
0 row(s) in 0.1800 seconds

hbase(main):011:0> put 't1','r2','cf2:age','22'
0 row(s) in 0.0620 seconds

hbase(main):012:0> put 't1','r3','cf1:name','bb'
0 row(s) in 0.0230 seconds

hbase(main):013:0> put 't1','r4','cf2:age','23'
0 row(s) in 0.0190 seconds

hbase(main):014:0> 

添加1行

hbase(main):014:0> put 't1','rk1','cf1:name','bb'
0 row(s) in 0.0210 seconds

hbase(main):015:0> put 't1','rk1','cf1:age','23'
0 row(s) in 0.0220 seconds

hbase(main):016:0> put 't1','rk1','cf2:cid','1001'
0 row(s) in 0.0220 seconds

hbase(main):017:0> put 't1','rk1','cf2:dept','bigdata'
0 row(s) in 0.0210 seconds

hbase(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 seconds

hbase(main):019:0> get 't1','rk1','cf1:name'
COLUMN                                           CELL                                                                                                                                        
 cf1:name                                        timestamp=1501251900543, value=bb                                                                                                           
1 row(s) in 0.0440 seconds

hbase(main):020:0> get 't1','rk1','cf2:dept'
COLUMN                                           CELL                                                                                                                                        
 cf2:dept                                        timestamp=1501252040732, value=bigdata                                                                                                      
1 row(s) in 0.0200 seconds

hbase(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 seconds

hbase(main):022:0> 

(4)删除数据

删除整行数据

hbase(main):014:0> deleteall 't1','r1'
0 row(s) in 0.1620 seconds

hbase(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 seconds

hbase(main):016:0>

删除指定行的列

hbase(main):016:0> delete 't1','rk1','cf2:dept'
0 row(s) in 0.0260 seconds

hbase(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 seconds

hbase(main):018:0>

(5)删除表

hbase(main):018:0> create 't2','info'
0 row(s) in 2.4840 seconds

=> Hbase::Table - t2
hbase(main):019:0> list
TABLE                                                                                                                                                                                        
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 seconds

hbase(main):021:0> drop 't2'
0 row(s) in 1.3080 seconds

hbase(main):022:0> list
TABLE                                                                                                                                                                                        
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

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

hbase(main):025:0> count 't1'
0 row(s) in 0.1620 seconds

=> 0
hbase(main):026:0> 

(7)修改表结构

先disable后enable

hbase(main):026:0> disable 't1'
0 row(s) in 2.3110 seconds

hbase(main):027:0> alter 't1',READONLY
Updating all regions with the new schema...
1/1 regions updated.
Done.
0 row(s) in 1.9830 seconds

hbase(main):028:0> enable 't1'
0 row(s) in 1.3060 seconds

hbase(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_namespace
NAMESPACE                                                                                                                                                                                    
default                                                                                                                                                                                      
hbase                                                                                                                                                                                        
2 row(s) in 0.2170 seconds

hbase(main):030:0>

(2)创建namespace

hbase(main):030:0> create_namespace 'mydb'
0 row(s) in 0.1070 seconds

hbase(main):031:0> list_namespace
NAMESPACE                                                                                                                                                                                    
default                                                                                                                                                                                      
hbase                                                                                                                                                                                        
mydb                                                                                                                                                                                         
3 row(s) in 0.0400 seconds

hbase(main):032:0> 

(3)查看namespace结构

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

hbase(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:test
hbase(main):034:0>
hbase(main):034:0> list
TABLE                                                                                                                                                                                        
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 seconds

hbase(main):036:0> put 'mydb:test','rk1','f1:age','2'
0 row(s) in 0.0220 seconds

hbase(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 seconds

hbase(main):038:0>

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏VMCloud

【解析向】腾讯云的Windows Server日志配置收集工具是个什么鬼?(3)

回顾上篇,解释了场景“2”中的四个标签,也介绍了对应着Windows Server中的四个功能在日常运维中究竟起到什么作用以及如何去驾驭他们。

27213
来自专栏小俊博客

[测评]云迪Host US-LA-SSD KVM Mini_384M VPS测评

最近博主买了KYRAHOST的LA CN2 VPS这款,SSD硬盘,从测试看,硬盘I/O还可以,有500-800左右,据商家说是SSD阵列RAID10,一个月才...

641
来自专栏Clive的技术分享

修改CentOS服务器时间为北京时间

3004
来自专栏一英里广度一英寸深度的学习

HBase 分布式数据库

在5、6年前,我们就希望能用分布式存储和分布式数据库来替代集中存储,觉得分布式廉价,而且高可靠。

982
来自专栏杨建荣的学习笔记

数据迁移部分问题总结(r2第3天)

按照计划在周二开始了数据迁移,本来之前也做了不少的准备工作。但是还是在迁移的过程中出现了一些问题。简单做一个总结。 1.constraint导致的数据rej...

2757
来自专栏猫哥学前班

网络慢?看看路由器设置对不对

防蹭网 开启 UPnP QoS 与网络限速 设置正确的 MTU 值 使用路由器交换机模式 使用无线中继扩展 Wi-Fi 信号 1、防蹭网 这是最最首要的一条...

41016
来自专栏wym

socket---TCP服务器界面

#include<winsock2.h> #pragma comment(lib,"ws2_32.lib") #define DEFAULT_iPor...

612
来自专栏互联网开发者交流社区

AvalonJS前端开发源码

913
来自专栏向治洪

studio多渠道打包

由于国内Android市场众多渠道,为了统计每个渠道的下载及其它数据统计,就需要我们针对每个渠道单独打包,如果让你打几十个市场的包岂不烦死了,不过有了Grad...

1808
来自专栏北京马哥教育

Linux 上的基础网络设备详解

抽象网络设备的原理及使用 网络虚拟化是 Cloud 中的一个重要部分。作为基础知识,本文详细讲述 Linux 抽象出来的各种网络设备的原理、用法、数据流向。您通...

3526

扫码关注云+社区