网站数据库丢失,wordpress 主机销售,关键词优化的技巧,软件外包行业一般在查询hbase的数据的时候我们可以直接使用hbase的命令行或者是api进行查询就行了#xff0c;但是在日常的计算过程中我们一般都不是为了查询#xff0c;都是在查询的基础上进行二次计算#xff0c;所以使用hbase的命令是没有办法进行数据计算的#xff0c;并且对于hbas…一般在查询hbase的数据的时候我们可以直接使用hbase的命令行或者是api进行查询就行了但是在日常的计算过程中我们一般都不是为了查询都是在查询的基础上进行二次计算所以使用hbase的命令是没有办法进行数据计算的并且对于hbase的压力也会增加很多hbase的本身并没有提供任何的计算逻辑所以我们要依赖于mapreducer进行计算这个代码上面我们已经实现过了但是后续开发过程中很少有人会直接开发mr程序这个代码的复杂程度比较高并且会非常大的拖慢我们的开发速度所以一般我们都会使用hive以外表的形式操作hbase中的数据进行多表的管理查询计算或者是进行数据的导入和导出。
首先在hive中增加hbase的链接信息。
修改hive-site.xml中的值。
propertynamehive.zookeeper.quorum/namevaluehadoop106,hadoop107,hadoop108/value
/property
propertynamehive.zookeeper.client.port/namevalue2181/value
/property
在自己的hadoop目录下的mapred-site.xml文件修改
propertynamehive.zookeeper.quorum/namevaluehadoop106,hadoop107,hadoop108/value
/property
propertynamehive.zookeeper.client.port/namevalue2181/value
/property
在hive/conf目录中增加log4j.properties文件输入日志级别设置
log4j.rootLoggererror,consolelog4j.appender.consoleorg.apache.log4j.ConsoleAppender
log4j.appender.console.targetSystem.out
log4j.appender.console.layoutorg.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern%d{yy/MM/dd HH:mm:ss} %p %c %M(): %m%n
然后启动hive就可以直接连接hbase了
1. 创建hive的内部表
hive的内部表hive会不仅会管理元数据信息也会管理整个表的其他所有数据。当在hive创建该表时将会在hbase创建映射表。
create table student_hive(id int,name string,age int)
STORED BY org.apache.hadoop.hive.hbase.HBaseStorageHandler
WITH SERDEPROPERTIES (hbase.columns.mapping
:key,info:name,info:age)
TBLPROPERTIES (hbase.table.name student_hbase1); # 删除hive中的表
drop table student_hive;
# 内部表在删除的时候hbase的表也会被删除 可以看到hbase中的映射表被删除。
2. 创建外部表
有的时候在hbase中已经存在一个表并且其中存在数据我们需要使用hive进行分析那么我们就需要创建一个外部表进行映射。
# 首先在hbase中创建表
create student_hbase,info
# 增加数据
put student_hbase,1,info:name,zhangsan
put student_hbase,1,info:age,20
put student_hbase,2,info:name,lisi
put student_hbase,2,info:age,30
# 这个时候就需要创建外部表进行映射
create external table student_hive(id int,name string,age int)
STORED BY org.apache.hadoop.hive.hbase.HBaseStorageHandler
WITH SERDEPROPERTIES (hbase.columns.mapping
:key,info:name,info:age)
TBLPROPERTIES (hbase.table.name student_hbase);
hbase:012:0 create student_hbase,info
Created table student_hbase
Took 1.2293 seconds Hbase::Table - student_hbase
hbase:013:0 list
TABLE
student_hbase
hainiu:advance_split_region
hainiu:info
hainiu:stu
hainiu:student
5 row(s)
Took 0.0148 seconds [student_hbase, hainiu:advance_split_region, hainiu:info, hainiu:stu, hainiu:student]
hbase:014:0 put student_hbase,1,info:name,zhangsan
Took 0.3755 seconds
hbase:015:0 put student_hbase,1,info:age,20
Took 0.0229 seconds
hbase:016:0 put student_hbase,2,info:name,lisi
Took 0.0219 seconds
hbase:017:0 put student_hbase,2,info:age,30
Took 0.0128 seconds
hbase:018:0 scan student_hbase;
ROW COLUMNCELL 1 columninfo:age, timestamp2024-11-13T22:35:58.531, value20 1 columninfo:name, timestamp2024-11-13T22:35:58.471, valuezhangsan 2 columninfo:age, timestamp2024-11-13T22:36:05.765, value30 2 columninfo:name, timestamp2024-11-13T22:35:58.604, valuelisi
2 row(s)
Took 0.1260 seconds 删除表因为hive对应的是外部表所以hbase的表不会被删除掉。
drop table student_hive;
3. 关联计算表的值
hbase中创建工资表
#创建salary工资表
create salary,info
put salary,001,info:id,1
put salary,002,info:id,1
put salary,003,info:id,1
put salary,004,info:id,2
put salary,005,info:id,2
put salary,006,info:id,2put salary,001,info:salary,1000
put salary,002,info:salary,2000
put salary,003,info:salary,3000
put salary,004,info:salary,4000
put salary,005,info:salary,5000
put salary,006,info:salary,6000#创建hive的表映射
create external table salary_hive(salary_id string,id int,salary int)
STORED BY org.apache.hadoop.hive.hbase.HBaseStorageHandler
WITH SERDEPROPERTIES (hbase.columns.mapping
:key,info:id,info:salary)
TBLPROPERTIES (hbase.table.name salary);
现在实现关联查询每个用户的平均工资是多少以及人名。
select a.name,avg(b.salary) as avg
from student_hive a join salary_hive b
on a.id b.id
group by a.name
可以根据计算得出最终结果。
4. hbase的数据导入导出
hbase的数据导出
# 使用hive的导出命令可以直接导出数据
insert overwrite local directory /home/hadoop/salary.txt select * from salary_hive;
可以通过外表的形式直接将数据导出到文件夹中。 结果数据查看。。
导入数据
不能用hive的load方式直接将数据导入到hbase中但是可以通过中间表的形式导入进行。
# 首先在本地创建teacher.txt 输入以下内容
1,yeniu,20
2,xinniu,30
3,qingniu,35
# 在hive中创建临时表
create table teacher_tmp(id int,name string,age int)
row format delimited fields terminated by ,;
# 将数据加载到临时表中
load data local inpath /home/hadoop/teacher.txt into table teacher_tmp;
# 创建和hbase的外部映射表
create table teacher_hive(id int,name string,age int)
STORED BY org.apache.hadoop.hive.hbase.HBaseStorageHandler
WITH SERDEPROPERTIES (hbase.columns.mapping
:key,info:name,info:age)
TBLPROPERTIES (hbase.table.name teacher_hbase);#从临时表使用mr将数据导入到hbase中insert into teacher_hive select * from teacher_tmp;
5. hbase的bulkload
在大数据的场景计算中有时候我们会遇见将大量数据一次性导入到hbase的情况但是这个时候hbase是不能够容纳的因为插入的数据首先会进入到memstore中如果大量插入数据会造成memstore的内存压力急剧增大这个时候机器的其他进程是没有办法执行的并且还会出现非常严重的问题比如hbase在大量插入数据的时候首先这个region会急剧增加后续region会按照拆分策略进行region拆分当前region下线插入程序会直接卡死造成hbase宕机等严重问题为了解决这个问题hbase给用户提供了一种新的插入数据的方式bulkload方式这个方式中会跳过hbase本身的过程首先在使用hbase的提供的mapreduce程序按照插入数据的格式和hbase的表格式生成hfile文件然后我们将hfile文件一次性插入到hbase对应的hdfs的文件夹中这种方式是最快捷并且对于hbase的压力最小的方式。
过程如下
# 首先在本地创建文件a.txt 输入以下内容
1,zhangsan,20
2,lisi,30
3,wangwu,40
5 zhaosi,50
# 然后将数据上传到hdfs中
hdfs dfs -put a.txt /
# 在hbase中创建表
create t,info
# 然后将id当成是rowkeyinfo:name存放名称 info:age存放年龄
执行importTSV方法产生hfile文件
-Dimporttsv.separator 指定分隔符-Dimporttsv.columns 指定列映射 HBASE_ROW_KEY强制要求写 cf:pk指定rowkey字段 其他字段与hive表中对应-Dimporttsv.skip.bad.lines是否跳过无效行-Dimporttsv.bulk.outputhfile输出路径hbase表名hdfs://worker-1:8020/data/hainiu/t2 用于生成hfile文件的输入目录
具体执行命令如下
hbase org.apache.hadoop.hbase.mapreduce.ImportTsv \
-Dimporttsv.separator, \
-Dimporttsv.columnsHBASE_ROW_KEY,info:name,info:age \
-Dimporttsv.skip.bad.linesfalse \
-Dimporttsv.bulk.output/t \
default:t hdfs://ns1/a.txt
查看hdfs文件发现hfile文件已经生成然后我们将数据导入到hdfs对应的目录中。
hbase org.apache.hadoop.hbase.mapreduce.LoadIncrementalHFiles /t default:t