HBase Shell操作

HBase Shell是HBase的命令行工具,提供了对HBase的管理操作,可以对HBase中的表进行数据的增、删、改、查等操作,也可以对表进行快照生成,通过生成快照进行复制表的操作,同时还可以对表中的region进行split的操作等等。

基本操作

1.进入HBase Shell命令

在Linux下进行命令输入

hbase shell

便可进入到hbase shell命令行中

HBase Shell
Use "help" to get list of supported commands.
Use "exit" to quit this interactive shell.
For Reference, please visit: http://hbase.apache.org/2.0/book.html#shell
Version 2.1.0-cdh6.3.2, rUnknown, Fri Nov  8 05:44:07 PST 2019
Took 0.0010 seconds                                                                                                                                                                                               
Ignoring eventmachine-1.2.7 because its extensions are not built.  Try: gem pristine eventmachine --version 1.2.7
Ignoring executable-hooks-1.6.1 because its extensions are not built.  Try: gem pristine executable-hooks --version 1.6.1
Ignoring gem-wrappers-1.4.0 because its extensions are not built.  Try: gem pristine gem-wrappers --version 1.4.0
Ignoring json-1.8.6 because its extensions are not built.  Try: gem pristine json --version 1.8.6
Ignoring thin-1.5.1 because its extensions are not built.  Try: gem pristine thin --version 1.5.1

2.查看帮助

通常进入命令行中,不知道可以进行什么操作时,可以输入“help”进行查看,HBase Shell也提供帮助查看

在HBase Shell命令行中进行输入

help

可以看到

HBase Shell, version 2.1.0-cdh6.3.2, rUnknown, Fri Nov  8 05:44:07 PST 2019
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: processlist, status, table_help, version, whoami

  Group name: ddl
  Commands: alter, alter_async, alter_status, clone_table_schema, create, describe, disable, disable_all, drop, drop_all, enable, enable_all, exists, get_table, is_disabled, is_enabled, list, list_regions, loca
te_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, cleaner_chore_enabled, cleaner_chore_run, cleaner_chore_switch, clear_block_cac
he, clear_compaction_queues, clear_deadservers, close_region, compact, compact_rs, compaction_state, flush, is_in_maintenance_mode, list_deadservers, major_compact, merge_region, move, normalize, normalizer_enabled, normalizer_switch, split, splitormerge_enabled, splitormerge_switch, stop_master, stop_regionserver, trace, unassign, wal_roll, zk_dump
  Group name: replication
  Commands: add_peer, append_peer_exclude_namespaces, append_peer_exclude_tableCFs, append_peer_namespaces, append_peer_tableCFs, disable_peer, disable_table_replication, enable_peer, enable_table_replication, 
get_peer_config, list_peer_configs, list_peers, list_replicated_tables, remove_peer, remove_peer_exclude_namespaces, remove_peer_exclude_tableCFs, remove_peer_namespaces, remove_peer_tableCFs, set_peer_bandwidth, set_peer_exclude_namespaces, set_peer_exclude_tableCFs, set_peer_namespaces, set_peer_replicate_all, set_peer_serial, set_peer_tableCFs, show_peer_tableCFs, update_peer_config
  Group name: snapshots
  Commands: clone_snapshot, delete_all_snapshot, delete_snapshot, delete_table_snapshots, list_snapshots, list_table_snapshots, restore_snapshot, snapshot

  Group name: configuration
  Commands: update_all_config, update_config

  Group name: quotas
  Commands: list_quota_snapshots, list_quota_table_sizes, list_quotas, list_snapshot_sizes, set_quota

  Group name: security
  Commands: grant, list_security_capabilities, revoke, user_permission

  Group name: procedures
  Commands: list_locks, list_procedures

  Group name: visibility labels
  Commands: add_labels, clear_auths, get_auths, list_labels, set_auths, set_visibility

  Group name: rsgroup
  Commands: add_rsgroup, balance_rsgroup, get_rsgroup, get_server_rsgroup, get_table_rsgroup, list_rsgroups, move_namespaces_rsgroup, move_servers_namespaces_rsgroup, move_servers_rsgroup, move_servers_tables_r
sgroup, move_tables_rsgroup, remove_rsgroup, remove_servers_rsgroup
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

3.查看表操作

list

类似于sql中的show tables

表的基本操作

1.创建表

create 'tableName', 'family'

例,创建一个记录学生信息的表,表名为student,列族为info

create 'student', 'info'

返回

Created table student
Took 2.5063 seconds                                                                                                                                                                                               
 => Hbase::Table - student

创建成功

2.增加数据

现在可以往表中插入新数据

put 'tableName', 'rowkey', 'family:column', 'value'

例,新增一名学生tony,年龄为18

put 'student', 'tony', 'info:age', '18'

返回

Took 0.1874 seconds

3.查询数据

对以插入的数据进行查询,在HBase中有两种查询方式,一种为精确查询-get,一种为模糊查询scan

3.1.精确查询

最简单的精确查询方式为知道表名与rowkey就可以进行精确查询,同时也可以进一步精确查询到family与column

get 'tableName', 'rowkey'
get 'tableName', 'rowkey', 'family:column'

例,查询名为tony学生的信息

get 'student', 'tony'

返回

COLUMN                                                CELL                                                                       
 info:age                                             timestamp=1675147857865, value=18                                         
1 row(s)
Took 0.0393 seconds

如果现在加入tony的身高、体重信息进去

put 'student', 'tony', 'info:height', '180'

put 'student', 'tony', 'info:weight', '150'

再查询的tony的信息

get 'student', 'tony'

就会发现tony的信息变多了

COLUMN                                                CELL                                                                      
 info:age                                             timestamp=1675147857865, value=18                                        
 info:height                                          timestamp=1675148520086, value=180                                         
 info:weight                                          timestamp=1675148553814, value=150                                        
1 row(s)
Took 0.0056 seconds

但是现在需要查询tony的年龄信息,就可以加入family与column进行更精确的查询

get 'student', 'tony', 'info:age'

返回

COLUMN                                                CELL                                                                       
 info:age                                             timestamp=1675147857865, value=18                                        
1 row(s)
Took 0.0116 seconds

便可以查询更精确的信息

3.2.模糊查询

最简单的模糊查询方式为知道表名就可以进行精确查询,为全表模糊查询。同时也可以在scan中加入条件,进行有条件的筛选。

scan 'tableName'

例,对学生表加入学生kim的信息

put 'student', 'kim', 'info:age', '17'

put 'student', 'kim', 'info:height', '183'

put 'student', 'kim', 'info:weight', '160'

现在对学生表进行全表查询

scan 'student'

返回

ROW                                                   COLUMN+CELL                                                               
 kim                                                  column=info:age, timestamp=1675148878890, value=17                         
 kim                                                  column=info:height, timestamp=1675148878913, value=183                     
 kim                                                  column=info:weight, timestamp=1675148879721, value=160                     
 tony                                                 column=info:age, timestamp=1675147857865, value=18                         
 tony                                                 column=info:height, timestamp=1675148520086, value=180                     
 tony                                                 column=info:weight, timestamp=1675148553814, value=150                     
2 row(s)
Took 0.0200 seconds

可以看到两个学生信息都返回。

4.修改数据

HBase是没有改修数据的命令的,但是根据HBase的特性,在同一张表中,相同rowkey,family,column下,所对应的值,可以进行覆盖操作。利用这一特性,进行修改数据的操作

例,tony的年龄为20岁,但是发现在学生表中为18岁,现在要对其进行修改。

查看tony年纪

get 'student', 'tony', 'info:age'

返回

COLUMN                                                CELL                                                                       
 info:age                                             timestamp=1675147857865, value=18                                         
1 row(s)
Took 0.0068 seconds

修改tony年龄为20

put 'student', 'tony', 'info:age', '20'

再次查看tony年龄

get 'student', 'tony', 'info:age'

返回

COLUMN                                                CELL                                                                       
 info:age                                             timestamp=1675149552233, value=20                                         
1 row(s)
Took 0.0107 seconds

可见,修改为20岁了

3.删除数据

删除数据分为两种与get查询类似,一种为对于一个rowkey全部删除,或对于一个rowkey下的一个family与cloumn进行删除。

deleteall 'tableName', 'rowkey'

delete 'tableName', 'rowkey', 'family:cloumn'

例,要把tony年龄进行删除

delete 'student', 'tony', 'info:age'

查看tony信息

get 'student', 'tony'

返回

COLUMN                                                CELL                                                                      
 info:height                                          timestamp=1675148520086, value=180                                        
 info:weight                                          timestamp=1675148553814, value=150                                        
1 row(s)
Took 0.0248 seconds

tony的年龄信息已经被删除掉了

现在要将tony这个学生的所有信息删除掉

deleteall 'student', 'tony'

查询tony信息

get 'student', 'tony'

返回

COLUMN                                                CELL                                                                      
0 row(s)
Took 0.0119 seconds

已经没有tony信息了

6.查看表结构

describe 'tableName'

例如,查看学生表信息

describe 'student'

返回

Table student is ENABLED                                                                                                         
student                                                                                                                         
COLUMN FAMILIES DESCRIPTION                                                                                                     
{NAME => 'info', VERSIONS => '1', EVICT_BLOCKS_ON_CLOSE => 'false', NEW_VERSION_BEHAVIOR => 'false', KEEP_DELETED_CELLS => 'FALSE', CACHE_DATA_ON_WRITE => 'false', DATA_BLOCK_ENCODING => 'NONE', TTL => 'FOREVER
', MIN_VERSIONS => '0', REPLICATION_SCOPE => '0', BLOOMFILTER => 'ROW', CACHE_INDEX_ON_WRITE => 'false', IN_MEMORY => 'false', CACHE_BLOOMS_ON_WRITE => 'false', PREFETCH_BLOCKS_ON_OPEN => 'false', COMPRESSION =
> 'NONE', BLOCKCACHE => 'true', BLOCKSIZE => '65536'}                                                                           
1 row(s)
Took 0.1256 seconds

7.清空表

truncate 'tableName'

再用truncate用tab补全的时候可以发现还有会一个操作“truncate_preserve”

truncate会连带表的分区一起清空;

truncate_preserve只会清空数据,不会清空分区;

8.删除表

在删除表时,先要对删除的表进行disable的操作,要不然无法对表进行删除

disable 'tableName'

drop 'tableName'

例,将studen表进行删除

disable 'student'

drop 'student'
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,242评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,769评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,484评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,133评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,007评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,080评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,496评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,190评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,464评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,549评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,330评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,205评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,567评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,889评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,160评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,475评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,650评论 2 335

推荐阅读更多精彩内容

  • 基本操作 进入HBase客户端命令行 查看帮助命令 查看当前数据库中有哪些表 表的操作 创建表 插入数据到表 扫描...
    无来无去_A阅读 252评论 0 1
  • 注意HBase查询结果的排列顺序:All data model operations HBase return d...
    逸章阅读 494评论 0 0
  • HBase Shell 基本操作 进入HBase客户端命令行bin/hbase shell 查看帮助命令hbase...
    Rex_2013阅读 525评论 0 0
  • 基本操作 进入HBase客户端命令行[hadoop@hadoop-100 hbase]$ cd bin/hbase...
    ZFH__ZJ阅读 718评论 0 2
  • 语句功能进入hbase客户端bin/hbase shell查看帮助命令help查看数据库list_namespac...
    chongweiLin阅读 57评论 0 1