分布式数据库——HBase基本操作

启动HBase:

1.启动hadoop,进入hadoop的sbin中

bash 复制代码
cd /opt/hadoop/sbin/

2.初始化namenode

bash 复制代码
hdfs namenode -format

3.启动hdfs

bash 复制代码
./start-all.sh

4.启动hbase

bash 复制代码
cd /opt/hbase/bin
./start-hbase.sh

5.使用jps查看进程

bash 复制代码
jps

以下图片则是hbase启动成功~

运行HBase

bash 复制代码
./hbase shell

接下来就可以开始建表啦~

shell操作:

HBase创建数据库建表:

语法:

bash 复制代码
create '表名','列族名1','列族名2',...,'列族名N'

查看所有数据库:

bash 复制代码
list

查看表结构:

bash 复制代码
discribe '表名'

计算表中所有记录的数量:

bash 复制代码
count '表名'

HBase数据库表数据的增、删、查、改

(1)HBase增加数据的语法格式:

bash 复制代码
put '表名','rowKey','列族:列','值'

(2)HBase查询数据的语法格式:

scan查询所有表记录

bash 复制代码
scan '表名'

get查询某个rowKey的所有记录

bash 复制代码
get '表名','rowKey'

get查询某个rowKey列族的记录

bash 复制代码
get '表名','rowKey','列族'

get查询rowKey列族的某个列记录

bash 复制代码
get '表名','rowKey','列族:列'

(2)HBase 删除数据:

删除表的所有记录:(drop)

bash 复制代码
disable '表名'
drop '表名'

删除表的某一条记录:(delete)

cpp 复制代码
delete '表名','行名','列族:列'

删除表的整行记录

bash 复制代码
deleteall '表名','rowKey'

清空表的所有记录

bash 复制代码
truncate '表名'

(4)HBase更新数据

用put重新写一遍,可以覆盖

下面以建如下表结构为例

1.创建一个名为student的表,字段包括stuInfo和grades

bash 复制代码
creat 'student','stuInfo','grades'

2.向表中插入数据

bash 复制代码
put 'student','001','stuInfo:name','alice' 
put 'student','001','stuInfo:age','18' 
put 'student','001','stuInfo:sex','female'
put 'student','001','grades:English','80'
put 'student','001','grades:math','90'
put 'student','002','stuInfo:name','nancy'
put 'student','002','stuInfo:sex','male'
put 'student','002','stuInfo:class','1802'  
put 'student','002','grades:English','85'
put 'student','002','grades:math','78'
put 'student','002','grades:bigdata','88'
put 'student','003','stuInfo:name','harry' 
put 'student','003','stuInfo:age','19' 
put 'student','003','stuInfo:sex','male'
put 'student','003','grades:English','90'
put 'student','003','grades:bigdata','90'

过滤操作:

1.行键过滤器(RowFilter、KeyOnlyFilter、FirstKeyOnlyFilter等)

格式:scan '表名',{FILTER=>"过滤器(比较运算符,'比较器')"}

(1)RowFilter:针对行键进行过滤

例1: 显示行键前缀0开头的键值对
bash 复制代码
scan 'student',{FILTER=>"RowFilter(=,'substring:001')"}
例2:显示行键字节顺序大于002的键值对
bash 复制代码
scan 'student',FILTER=>"RowFilter(>,'binary:002')"

(2)PrefixFilter:行键前缀过滤器

例1:扫描前缀为001的行键
bash 复制代码
scan 'student',FILTER=>"PrefixFilter('001')"

(3)FirstKeyOnlyFilter:扫描全表,显示每个逻辑行的第一个键值对

bash 复制代码
scan 'student',FILTER=>"FirstKeyOnlyFilter()"

(4)InclusiveStopFilter:替代EndRow返回终止条件行

例:扫描显示行键001道002的范围内的键值对
bash 复制代码
scan 'student',{STARTROW=>'001',FILTER=>"InclusiveStopFilter('002')"}

等同于:

bash 复制代码
scan 'student',{STARTROW=>'001',ENDROW=>'003'}

2.列族与过滤器

(1)Family:针对列族进行比较和过滤

例1:显示列族前缀为stu开头的键值对

bash 复制代码
scan 'student',FILTER=>"FamilyFilter(=,'substring:stu')"

scan 'student',FILTER=>"FamilyFilter(=,'binary:stu')"

(2)QualifierFilter:列标识过滤器

例:显示列名为name的记录

cpp 复制代码
scan 'student',FILTER=>"QualifierFilter(=,'substring:name')"

等价于

(3)ColumnPrefixFilter:对列名前缀进行过滤

例:显示列名为name的记录

bash 复制代码
 scan 'student',FILTER=>"ColumnPrefixFilter('name')"

等价于

bash 复制代码
scan 'student',FILTER=>"QualifierFilter(=,'substring:name')"

(4)MultipleColumnPrefixFilter:可以指定多个前缀

例:显示列名为name和age的记录

bash 复制代码
 scan 'student',FILTER=>"ColumnPrefixFilter('name')"scan 'student',FILTER=>"MultipleColumnPrefixFilter('name','age')"

(5)ColumnRangeFilter:设置范围按字典序对列名进行过滤

例:

bash 复制代码
scan 'student',FILTER=>"ColumnRangeFilter('bi',true,'na',true)"

3.值过滤器

(1)ValueFilter:值过滤器

例:查询等于19的所有键值对

bash 复制代码
scan 'student',FILTER=>"ValueFilter(=,'binary:19') "
scan 'student',FILTER=>"ValueFilter(=,'substring:19')"

(2)SingleColumnValueFilter:在指定的列族和列中进行值过滤器

例:查询studiofo列族age列中值等于19的所有键值对

bash 复制代码
scan 'student',{COLUMN=>'stuInfo:age',FILTER=>"SingleColumnValueFilter('stuInfo','age',=,'binary:19')"}

4.其他过滤器

(1)ColumnCountGetFilter:限制每个逻辑行返回的键值对数

例:返回行键位001的前3个键值对

bash 复制代码
get 'student','001',FILTER=>"ColumnCountGetFilter(3)"

(2)PageFilter:基于行的分页过滤器,设置返回的行数

例:显示1行

bash 复制代码
scan 'student',FILTER=>"PageFilter(1)"

(3)ColumnPaginationFilter:基于列的进行分页过滤器,需要设置偏移量与返回数量

例:显示每行第1列之后的第二个键值对

bash 复制代码
scan 'student',FILTER=>"ColumnPaginationFilter(2,1)"
相关推荐
Lyra_Infra7 分钟前
从 MySQL 到达梦:一次信创隔离环境里的数据库迁移踩坑实录
数据库·后端·mysql
数据工匠老o7 分钟前
sysbench/TPC-C/自定义脚本:数据库压测工具对比与实战流程
数据库·测试
数安3000天40 分钟前
数据分类分级产品有哪四类技术路线?
大数据·数据库
泡茶喝茶写代码43 分钟前
量化数据开发实战系列(第 25 篇):基金基础接口实战:基金列表、ETF-LOF 分类、基金概况、净值数据
java·数据库·人工智能·python·mysql
张小姐的猫1 小时前
【AI大模型接入SDK】 —— 数据管理 & 与Session模块进行联动
数据结构·数据库·c++·人工智能·python·chatgpt
IvorySQL1 小时前
PostgreSQL 日报|大模型破解在线校验和(9 月 15 日)
数据库·人工智能·postgresql
Leon-Ning Liu2 小时前
Oracle 19c ASMB 进程 PGA 内存泄漏排查实战 (Bug 38610635)
数据库·oracle
步行cgn2 小时前
Spring 注入 null 和空字符串详解
数据库·spring·oracle
Allen_LVyingbo2 小时前
医疗人工智能项目全生命周期管理系统:监管知识建模、工程实现与实证评估(下)
大数据·数据库·人工智能·python·自然语言处理·自动化
草莓熊Lotso2 小时前
【Redis 进阶】主从复制深度解析:从配置落地到 PSYNC 同步原理
linux·开发语言·网络·数据库·redis·缓存·php