分布式非关系型数据库HBase快速入门
一、HBase介绍
HBase是一个分布式的、面向列的开源数据库,该技术来源于 Fay Chang 所撰写的Google论文“Bigtable:一个结构化数据的分布式存储系统”。就像Bigtable利用了Google文件系统(File System)所提供的分布式数据存储一样,HBase在Hadoop之上提供了类似于Bigtable的能力。HBase是Apache的Hadoop项目的子项目。HBase不同于一般的关系数据库,它是一个适合于非结构化数据存储的数据库。另一个不同的是HBase基于列的而不是基于行的模式。
1. HBase 数据模型
1)HBase可以有多个Name Space(命名空间),类似于关系型数据库的 DatabBase 概念,每个命名空间下有多个表。HBase有两个自带的命名空间,分别是 hbase 和 default,hbase 中存放的是 HBase 内置的表,default 表是用户默认使用的命名空间
2)逻辑上,HBase 表的数据模型同关系型数据库很类似,数据存储在一张表中,有行有列
3)HBase的列是以Column Family(列族)区分的,并以列族为物理存储的依据,每个列族里有一个或多个列(Column Qualifier),列可以动态指定增加
4)HBase每行都有一个Row Key(RK),并且RK唯一,类似于java 的Map中的key,当key已存在时,会覆盖之前的数据,RK也是物理存储的依据
5)当数据量很大时,横向上,连续的(字典排序)若干行会被分隔成若干Region(切片);而纵向上会被分成若干列族。这样整个表会被切分成若干个Store,每个Store就是HBase物理存储的一个单元
2.HBase的物理存储结构
1)HBase以每个Store为单元进行存储
2)每个单元在存储时会被转化为一个类似于关系型数据库的二维表,这个"二维表"每一行的核心为原Region中的一个Cell对应的值,其他都为描述这个value的属性值
3)“二维表”中除了RK、列族名、列名和value之外,还会有一个TimeStamp(时间戳)与一个Type(操作类型)
4)一条修改指令下达之后,HBase中的数据不会马上按指令进行修改,而是增加一条数据,该数据中标记时间戳与操作类型,查询时会根据时间戳查询最新的一条数据,获取响应的值。例如t3手机号为131***,t4修改为177***,这两条数据在一定时间内会同时存在,查询时会取t4时间的数据。
5) RowKey/列族名/列名/时间戳 共同确定一个Cell,cell中的数据以字节码形式存储
3.HBase的基本架构
1)HBase集群本身分为Master与RegionSever
1.1)Master负责表结构相关的操作,create/delete/alter等table级ddl(数据定义)操作,并负责管理RegionSever
1.2)RegionServer负责数据的具体操作,get/put/delete等data级dml(数据操纵)操作,并负责region的切分与合并
2)Hadoop集群中,HBase的运行依赖于Zookeeper与HDFS
2.1)HBase 通过 Zookeeper 来做 Master 的高可用、RegionServer 的监控、元数据的入口以及集群配置的维护等工作
2.2)HDFS负责具体的数据存储
4.HBase的完整架构
1)HBASE依赖于两大组件–Zookeeper与HDFS
2.1)如果是dml操作,name客户端请求过来时首先访问zk,获取系统命名空间hbase下的meta表所在的regionsever
- 查看zk上的/hbase/meta-region-server
[zk: localhost:2181(CONNECTED) 3] get /hbase/meta-region-server
�regionserver:16020At�r�F�OPBUF
hadoop103�}�����/
- 查看hbase命名空间下meta表的信息
hbase(main):001:0> scan 'hbase:meta'
ROW COLUMN+CELL
…省略…
stu,,1649124000807.41f1089de1 column=info:server, timestamp=1649124001468, value=hadoop103:16020
d69147feb992583365b0c7.
…省略…
2 row(s) in 0.3060 seconds
2.2)这一步如果是ddl操作,会由HMaster请求ZK后直接调用相关的regionsever执行
3)获取到regionsever之后,regionserver会进行具体的执行与存储
4)该请求中的操作会首先被写到一个HLog中(也就是wal,类似于hdfs的edits文件),然后再被加载到内存中
5)一个regionserver可维护多个HRegion,每个HRegion根据列族分为多个Store,Store与Store的存储时相互隔离的
6)内存中的数据在逻辑上被分为若干Mem Store,当执行flush操作时会被持久化,形成一个一个的StoreFile,Store File文件过多时会有compact的合并过程。StoreFile以HFile规定的文件格式存储。
7)flush的过程中,regionsever会调用hdfs的客户端,将StoreFile保存到HDFS(dataNode的磁盘)中。
二、Hbase的工作流程
1.HBase写数据流程
1)client首先访问zk,获取hbase系统命名空间下的meta表所在的regionserver(图中的hadoop102)
2)然后访问meta表所在的regionserver(hadoop102),获取当前请求关联的region所在的regionserver(图中的hadoop103)
3)table 的 region 信息以及 meta 表的位置信息缓存在客户端的 meta cache
4)向region所在的regionserver(图中的hadoop103)发送put请求,进行数据的写入
5)写数据时先写到HLog(wal,预写入日志),再加载到内存中(Mem Store)
6)flush时内存中的数据会持久化为StoreFile
2.Flush流程
1)在一个RegionSever里可以有多个Region,一个Region可以有多个Store,每个Store有自己的内存区,即MemStore
2)在达到一定条件时执行flush,每个MemStore会持久化到HDFS形成一个StoreFile文件
3)不同Store的StoreFile隔离存储在不同的HDFS目录中
- 查看storefile文件:
[atguigu@hadoop104 bin]$ hdfs dfs -ls /HBase/data/default/stu/41f1089de1d69147feb992583365b0c7/office_info/
Found 1 items
-rw-r--r-- 3 atguigu supergroup 5263 2022-04-05 11:23 /HBase/data/default/stu/41f1089de1d69147feb992583365b0c7/office_info/51587257d4e04740b08fea5e7f11d5c4
- flush的触发设置:
- regionserver级的触发
<property>
<name>hbase.regionserver.global.memstore.size</name>
<value></value>
<description>regionServer的全局memstore的大小,超过该大小会触发flush到磁盘的操作,默认是堆大小的40%,而且regionserver级别的flush会阻塞客户端读写</description>
</property>
<property>
<name>hbase.regionserver.global.memstore.size.lower.limit</name>
<value></value>
<description>可以理解为一个安全的设置,有时候集群的“写负载”非常高,写入量一直超过flush的量,这时,我们就希望memstore不要超过一定的安全设置。在这种情况下,写操作就要被阻塞一直到memstore恢复到一个“可管理”的大小, 这个大小就是默认值是堆大小 * 0.4 * 0.95,也就是当regionserver级别的flush操作发送后,会阻塞客户端写,一直阻塞到整个regionserver级别的memstore的大小为 堆大小 * 0.4 *0.95为止</description>
</property>
<property>
<name>hbase.regionserver.optionalcacheflushinterval</name>
<value>3600000</value>
<description>内存中的文件在自动刷新之前的最后一条数据能够存活的最长时间,默认是1h </description>
</property>
- region级的触发
<property>
<name>hbase.hregion.memstore.flush.size</name>
<value>134217728</value>
<description>单个region里memstore的缓存大小,超过那么整个HRegion就会flush,默认128M </description>
</property>
3.HBase读数据流程
1)client首先访问zk,获取hbase系统命名空间下的meta表所在的regionserver(图中的hadoop102)
2)然后访问meta表所在的regionserver(hadoop102),获取当前请求关联的region所在的regionserver(图中的hadoop103)
3)table 的 region 信息以及 meta 表的位置信息缓存在客户端的 meta cache
4)读流程比写流程多了一个Block Cache,用于storefile中的数据缓存
5)分别在 Block Cache(读缓存),MemStore 和 Store File(HFile)中查询目标数据,并将查到的所有数据进行合并。此处所有数据是指同一条数据的不同版本(time stamp)或者不同的类型(Put/Delete)。
6)将从Store File文件中查询到的数据块(Block,HFile 数据存储单元,默认大小为 64KB)缓存到Block Cache
7)将合并后的最终结果按时间戳最大返回给客户端
8)由于读流程会访问磁盘空间,所以比写流程慢
4.HBase的合并流程–Compact
两种compact的区别
- compact(MinorCompaction):简单合并,合并小文件,保留数据的版本。将临近的若干个较小的 HFile 合并成一个较大的 HFile,但不会清理过期和删除的数据
- major_compact:合并所有文件,并只保留新版本。将一个 Store 下的所有的 HFile 合并成一个大 HFile,并且会清理掉过期和删除的数据。
有关compact的配置
<property>
<name>hbase.hregion.majorcompaction</name>
<value>604800000</value>
<description>一个region进行 major compaction合并的周期,在这个点的时候, 这个region下的所有hfile会进行合并,默认是7天,major compaction非常耗资源,建议生产关闭(设置为0),在应用空闲时间手动触发 </description>
</property>
<property>
<name>hbase.hstore.compactionThreshold</name>
<value>3</value>
<description>一个store里面允许存的hfile的个数,超过这个个数会被写到新的一个hfile里面 也即是每个region的每个列族对应的memstore在fulsh为hfile的时候,默认情况下当达到3个hfile的时候就会对这些文件进行合并重写为一个新文件,设置个数越大可以减少触发合并的时间,但是每次合并的时间就会越长</description>
</property>
5. 旧版本数据的删除
1)flush:针对内存中的数据,在flush的时候会被flush为一个storefile,这时内存中的数据会将旧版本中的数据删除,只保留最新时间戳的数据
2)major_compact:针对磁盘中的数据,在大合并的时候,将多个storefile合并为一个,此时也会删除旧版本的数据
6. HBase的拆分流程–Split
当1个region中的某个Store下所有StoreFile的总大小超过Min(R^2 * “hbase.hregion.memstore.flush.size”," hbase.hregion.max.filesize ") 就会拆分,其中R为当前RegionServer中属于该table的region个数
<property>
<name>hbase.hregion.memstore.flush.size</name>
<value>134217728</value>
<description>单个region里memstore的缓存大小,超过那么整个HRegion就会flush,默认128M</description>
</property>
<property>
<name>hbase.hregion.max.filesize</name>
<value>10737418240</value>
<description>HStoreFile最大的大小,当某个region的某个列族超过这个大小会进行region拆分</description>
</property>
三、zookeeper集群安装
规划三台机器hadoop102,hadoop103,hadoop104组成zk集群
zk安装包下载地址 https://zookeeper.apache.org/
1.集群节点安装
(所有节点都要执行)
拷贝 apache-zookeeper-3.5.7-bin.tar.gz 安装包到 Linux 系统下(三台机器hadoop102,hadoop103,hadoop104),
解压并修改名称
[atguigu@hadoop102 software]$ tar -xvf apache-zookeeper-3.5.7-bin.tar.gz -C /opt/module/
[atguigu@hadoop102 module]$ cd ../module/
[atguigu@hadoop102 module]$ pwd
/opt/module
[atguigu@hadoop102 module]$ mv apache-zookeeper-3.5.7-bin/ zookeeper-3.5.7
修改配置:
# zoo_sample.cfg重命名为 zoo.cfg
[atguigu@hadoop102 module]$ cd /opt/module/zookeeper-3.5.7/conf
[atguigu@hadoop102 conf]$ mv zoo_sample.cfg zoo.cfg
# 修改如下值 :dataDir=/opt/module/zookeeper-3.5.7/zkData
[atguigu@hadoop102 conf]$ vi zoo.cfg
[atguigu@hadoop102 zookeeper-3.5.7]$ pwd
/opt/module/zookeeper-3.5.7
# 创建zkData目录
[atguigu@hadoop102 zookeeper-3.5.7]$ mkdir zkData
启动zk与查看状态(可选):
[atguigu@hadoop102 zookeeper-3.5.7]$ ./bin/zkServer.sh start
[atguigu@hadoop102 zookeeper-3.5.7]$ bin/zkServer.sh status
进入客户端(可选):
[atguigu@hadoop102 zookeeper-3.5.7]$ ./bin/zkCli.sh
[zk: localhost:2181(CONNECTED) 0]
2.集群搭建
1)在hadoop102、hadoop103、hadoop104的/opt/module/zookeeper-3.5.7/zkData 目录下分别创建 myid 的文件,值分别为2/3/4
[atguigu@hadoop102 zkData]$ echo 2 > myid
[atguigu@hadoop102 zkData]$ pwd
/opt/module/zookeeper-3.5.7/zkData
[atguigu@hadoop103 zkData]$ echo 3 > myid
[atguigu@hadoop103 zkData]$ pwd
/opt/module/zookeeper-3.5.7/zkData
[atguigu@hadoop104 zkData]$ pwd
/opt/module/zookeeper-3.5.7/zkData
[atguigu@hadoop104 zkData]$ echo 4 > myid
2)zoo.cfg中增加配置(所有机器)
#######################cluster##########################
server.2=hadoop102:2888:3888
server.3=hadoop103:2888:3888
server.4=hadoop104:2888:388
3)分别重启zk,查看状态
[root@hadoop102 bin]# pwd
/opt/module/zookeeper-3.5.7/bin
[root@hadoop102 bin]# ./zkServer.sh restart
[atguigu@hadoop102 bin]$ ./zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /opt/module/zookeeper-3.5.7/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost.
Mode: leader
[root@hadoop103 bin]# pwd
/opt/module/zookeeper-3.5.7/bin
[root@hadoop103 bin]# ./zkServer.sh restart
[root@hadoop103 bin]# ./zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /opt/module/zookeeper-3.5.7/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost.
Mode: follower
[root@hadoop104 bin]# pwd
/opt/module/zookeeper-3.5.7/bin
[root@hadoop104 bin]# ./zkServer.sh restart
[root@hadoop104 bin]# ./zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /opt/module/zookeeper-3.5.7/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost.
Mode: follower
四、HBase集群安装
拷贝 hbase-1.3.1-bin.tar.gz 安装包到 Linux 系统下(三台机器hadoop102,hadoop103,hadoop104),解压
[atguigu@hadoop103 software]$ tar xvf apache-zookeeper-3.5.7-bin.tar.gz -C /opt/module/
配置hbase-site.xml:
[atguigu@hadoop103 software]$ cd /opt/module/hbase-1.3.1/conf
[atguigu@hadoop103 conf]$ vi hbase-site.xml
<configuration>
<property>
<name>hbase.rootdir</name>
<!--注意端口号要与hadoop中core-site.xml中fs.defaultFS配置一致-->
<value>hdfs://hadoop102:8020/HBase</value>
</property>
<property>
<name>hbase.cluster.distributed</name>
<value>true</value>
</property>
<property>
<name>hbase.master.port</name>
<value>16000</value>
</property>
<property>
<name>hbase.master.info.port</name>
<value>16010</value>
</property>
<property>
<name>hbase.zookeeper.quorum</name>
<value>hadoop102,hadoop103,hadoop104</value>
</property>
<property>
<name>hbase.zookeeper.property.dataDir</name>
<value>/opt/module/zookeeper-3.5.7/zkData</value>
</property>
</configuration>
配置hbase-env.sh:
[atguigu@hadoop103 conf]$ vi hbase-env.sh
[atguigu@hadoop103 conf]$ tail -2 hbase-env.sh
export JAVA_HOME=/opt/module/jdk1.8.0_212
export HBASE_MANAGES_ZK=false
配置regionservers:
[atguigu@hadoop103 conf]$ vi regionservers
[atguigu@hadoop103 conf]$ cat regionservers
hadoop102
hadoop103
hadoop104
创建软连接
[atguigu@hadoop103 module]$ ln -s /opt/module/hadoop3.1.3/etc/hadoop/core-site.xml /opt/module/hbase-1.3.1/conf/coresite.xml
[atguigu@hadoop103 module]$ ln -s /opt/module/hadoop3.1.3/etc/hadoop/hdfs-site.xml /opt/module/hbase-1.3.1/conf/hdfssite.xml
启动与关闭
[atguigu@hadoop102 bin]$ pwd
/opt/module/hbase-1.3.1/bin
# 单启动master
[atguigu@hadoop102 bin]$ ./hbase-daemon.sh start master
starting master, logging to /opt/module/hbase-1.3.1/bin/../logs/hbase-atguigu-master-hadoop102.out
# 单启动regionserver
[atguigu@hadoop102 bin]$ ./hbase-daemon.sh start regionserver
starting regionserver, logging to /opt/module/hbase-1.3.1/bin/../logs/hbase-atguigu-regionserver-hadoop102.out
# 停止hbase集群
[atguigu@hadoop102 bin]$ ./stop-hbase.sh
---------- hbase 停止 ------------
stopping hbase...............
# 启动hbase集群
[atguigu@hadoop102 bin]$ ./start-hbase.sh
---------- hbase 启动 ------------
starting master, logging to /opt/module/hbase-1.3.1/bin/../logs/hbase-atguigu-master-hadoop102.out
hadoop102: starting regionserver, logging to /opt/module/hbase-1.3.1/bin/../logs/hbase-atguigu-regionserver-hadoop102.out
hadoop103: starting regionserver, logging to /opt/module/hbase-1.3.1/bin/../logs/hbase-atguigu-regionserver-hadoop103.out
hadoop104: starting regionserver, logging to /opt/module/hbase-1.3.1/bin/../logs/hbase-atguigu-regionserver-hadoop104.out
五、HBase命令行操作
1.进入与退出hbase命令行模式
[atguigu@hadoop102 bin]$ pwd
/opt/module/hbase-1.3.1/bin
[atguigu@hadoop102 bin]$ ./hbase shell
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/opt/module/hbase-1.3.1/lib/slf4j-log4j12-1.7.5.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/opt/module/hadoop-3.1.3/share/hadoop/common/lib/slf4j-log4j12-1.7.25.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.3.1, r930b9a55528fe45d8edce7af42fef2d35e77677a, Thu Apr 6 19:36:54 PDT 2017
hbase(main):001:0>quit
[atguigu@hadoop102 bin]$
2.help
hbase(main):009:0> help
HBase Shell, version 1.3.1, r930b9a55528fe45d8edce7af42fef2d35e77677a, Thu Apr 6 19:36:54 PDT 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, splitormerge_enabled, splitormerge_switch, 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, get_peer_config, list_peer_configs, 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, 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_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
3.常用命令
- list 查看用户表
hbase(main):019:0> list
TABLE
student
1 row(s) in 0.0110 seconds
=> ["student"]
- create 创建表
【create ‘表名’, ‘列族’, ‘列族’, …】
hbase(main):021:0> create 'stu','personal_info','office_info'
0 row(s) in 1.2300 seconds
=> Hbase::Table - stu
hbase(main):022:0> list
TABLE
stu
student
2 row(s) in 0.0120 seconds
=> ["stu", "student"]
- put 插入数据到表,或个更新表数据
【put ‘表名’,‘行键’,‘列族名:列名’, ‘列值’】
hbase(main):023:0> put 'stu','1001','personal_info:age','18'
0 row(s) in 0.1910 seconds
hbase(main):027:0> put 'stu','1002','office_info:address','beijing'
0 row(s) in 0.0380 seconds
- scan 查看数据
【scan ‘表名’】
hbase(main):028:0> scan 'stu'
ROW COLUMN+CELL
1001 column=personal_info:age, timestamp=1649124545538, value=18
1002 column=office_info:address, timestamp=1649125119860, value=beijing
2 row(s) in 0.0100 seconds
根据rowkey的范围查看
【scan ‘表名’, {STARTROW => ‘rowkeyNo1’ [,STOPROW => ‘rowkeyNo2’}]】
hbase(main):001:0> scan 'stu',{STARTROW => '1001' ,STOPROW => '1001'}
ROW COLUMN+CELL
1001 column=personal_info:age, timestamp=1649124545538, value=18
1 row(s) in 0.2880 seconds
hbase(main):002:0> scan 'stu',{STARTROW => '1001'}
ROW COLUMN+CELL
1001 column=personal_info:age, timestamp=1649124545538, value=18
1002 column=office_info:address, timestamp=1649125404588, value=shanghai
2 row(s) in 0.0160 seconds
- describe 查看表结构
hbase(main):003:0> describe 'stu'
Table stu is ENABLED
stu
COLUMN FAMILIES DESCRIPTION
{NAME => 'office_info', BLOOMFILTER => 'ROW', VERSIONS => '1', IN_MEMORY => 'false', KEEP_DELETED_CELLS => 'FALSE', DA
TA_BLOCK_ENCODING => 'NONE', TTL => 'FOREVER', COMPRESSION => 'NONE', MIN_VERSIONS => '0', BLOCKCACHE => 'true', BLOCK
SIZE => '65536', REPLICATION_SCOPE => '0'}
{NAME => 'personal_info', BLOOMFILTER => 'ROW', VERSIONS => '1', IN_MEMORY => 'false', KEEP_DELETED_CELLS => 'FALSE',
DATA_BLOCK_ENCODING => 'NONE', TTL => 'FOREVER', COMPRESSION => 'NONE', MIN_VERSIONS => '0', BLOCKCACHE => 'true', BLO
CKSIZE => '65536', REPLICATION_SCOPE => '0'}
2 row(s) in 0.1090 seconds
- get 查看数据
【get ‘表名’,‘rowkey’】查看一行数据
hbase(main):009:0> get 'stu','1003'
COLUMN CELL
office_info:Tel timestamp=1649125793323, value=17712345678
office_info:address timestamp=1649125923932, value=suzhou
1 row(s) in 0.0080 seconds
【get ‘表名’,‘rowkey’,‘列族:列名’】查看单个cell数据
hbase(main):010:0> get 'stu','1003','office_info:Tel'
COLUMN CELL
office_info:Tel timestamp=1649125793323, value=17712345678
1 row(s) in 0.0120 seconds
- count统计表行数
【count ‘表名’】
hbase(main):011:0> count 'stu'
3 row(s) in 0.0290 seconds
=> 3
- 删除数据
【deleteall ‘表名’,‘rowkey’】删除整行
hbase(main):013:0> deleteall 'stu','1001'
0 row(s) in 0.0270 seconds
【delete ‘表名’,‘rowkey’,‘列族名:列名’】delete删除一个cell数据
hbase(main):014:0> delete ‘stu’,‘1003’,‘office_info:address’
0 row(s) in 0.0180 seconds
【truncate ‘表名’】删除整张表的数据(自动disable)
hbase(main):015:0> truncate 'student'
Truncating 'student' table (it may take a while):
- Disabling table...
- Truncating table...
0 row(s) in 3.6790 seconds
【drop ‘表名’】删除表,需要手动disable
hbase(main):018:0> disable 'student'
0 row(s) in 2.2440 seconds
hbase(main):019:0> drop 'student'
0 row(s) in 1.2580 seconds
六、HBase的JavaAPI
1.前置准备
参考:HDFS–Hadoop分布式文件系统 中的四、HDFS的java Api使用 ----1.前置准备工作
配置maven依赖(pom.xml):
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-server</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>1.3.1</version>
</dependency>
创建HBaseClient类用于测试
2.API使用前资源与配置
package com.hbase;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.net.URISyntaxException;
public class HBaseClient {
Configuration configuration;
Connection connection;
Admin admin;
public HBaseClient() throws IOException {
}
@Before
public void init() throws URISyntaxException, IOException, InterruptedException {
configuration = HBaseConfiguration.create();
configuration.set("hbase.zookeeper.quorum","hadoop102,hadoop103,hadoop104");
configuration.set("hbase.zookeeper.property.clientPort" ,"2181"); //默认端口就是2181
connection = ConnectionFactory.createConnection(configuration);
admin = connection.getAdmin();
}
@Test
public void isTableExits() throws IOException {
TableName stu = TableName.valueOf("stu");
System.out.println(admin.tableExists(stu));
}
@After
public void closeResource() throws IOException {
admin.close();
connection.close();
}
}
2.DDL操作
后续补充
3.DML操作
后续补充
七、鸣谢
感谢尚硅谷,此文根据尚硅谷课程而写,bili连接 尚硅谷HBase教程