Hive
1. 数据仓库
1.1 数仓需求简介
-
基于分析决策需求,构建数仓平台
-
OLTP——>OLAP
1.2 数仓主要特征
ETL:抽取,转换,加载
1.3 SQL
- DDL:数据定义语言 (外部结构操作)
- DML:数据操纵语言 (内部数据操作)
2.Hive
Hive利用HDFS存储数据,利用MapReduce查询分析数据
2.1 Hive介绍
-
Hive能将数据文件映射成为一张表
- 映射指文件和表之间的对应关系**(映射信息),也就是元数据**(记录数据的数据)
-
映射信息记录
-
表对应那个文件(位置信息)
-
表对应着文件的那一个字段(顺序信息)
-
文件字段之间的分隔符是什么(eg:竖线,逗号,etc)
-
-
功能职责
- 将HQL语法解析编译成MapReduce并提交到YARN(Hadoop)集群上运行
- 开发人员干什么
- SQL编写
- 结果返回显示
- 建表时将元数据/映射信息写好:对应什么文件,分隔符是什么
2.2 Hive架构图
- Hive默认使用MR执行数据引擎进行数据计算
- 可以支持其他引擎,eg:内存计算引擎spark
2.3 Hive配置
3.Docker-Hive搭建
直接下载使用镜像即可
#镜像:
#创建容器
docker run -itd \
-p 8088:8088 \
-p 9000:9000 \
-p 50070:50070 \
-p 9870:9870 \
-p 10000:10000 \
-p 10002:10002 \
--name hive --privileged xiaoluhive:arm64
#进入容器
docker exec -it hivetest bash
#开启ssh
sudo service ssh start
service postgresql start
bash ~/hive-start.sh
#检测ssh是否开启
sudo service ssh status
#开启postgresql
service postgresql start
#启动hive
bash ~/hive-start.sh
#重启hive
service postgresql restart
#hive使用
hive
#beeline登陆
beeline -u jdbc:hive2://localhost:10000 -n hive -p hive
#元数据使用PostgreSQL,
用户: root
密码: root
#HIVE数据库
用户: hive
密码: hive
#hive远程连接无法创建表问题
#查看hdfs-site.xml配置文件permission下是否为false
<property>
<name>dfs.permissions</name>
<value>false</value>
<description>
#hive注释中文乱码问题 修改hive-site.xml
<property>
<name>javax.jdo.option.ConnectionURL</name>
<value>jdbc:mysql://hadoop102:3306/metastore?useSSL=false&useUnicode=true&characterEncoding=UTF-8</value>
</property>
注意:
hadoop2.x访问端口:50070
hadoop3.x访问端口:9870
#三条命令一键开启(直接复制三条)
sudo service ssh start
service postgresql start
bash ~/hive-start.sh
#开启后直接用DataGrip连接即可
4.postgreSQL
#登录postgreSQL
sudo -u postgres psql
#查看用户
\du
#查看库
\l
#切换库
\c hive
#查看表
\d
5.Hive操作
5.1 DDL
5.1.1 库操作
-
查看库
show databases;
-
创建库
-
create database [if not exists] ifnxs [comment "库描述"] [with dbproperties ('createdBy'='dr34m')];
-
with dbproperties 用于指定一些数据库的属性配置
-
location 可以指定数据库在HDFS存储位置,默认/user/hive/warehouse/dbname.db (一般都采用默认路径)
-
例
create database test;
-
-
使用库
use rtmc; #切换到rtmc数据库下
-
删除库
drop database [if exists] test [cascade];
- cascade表示强制删除,默认为restrict,这意味着仅在数据库为空时才删除它
5.1.2 表操作
-
查看表
show tables [in xxx库];
-
创建表
- 库默认都是在 :/user/hive/warehouse/hive_exam.db 下
create table [if not exists] [xxx.]zzz (col_name data_type [comment "字段描述"], ...) [comment "表描述"] #指定分隔符:将结构化文件映射成表 [row format delimited ...];
- 分隔符(最常见为第一种)
-
数据类型
-
例子 先建表
create table hive_exam.t_archer( id int comment "ID编号", name string comment "英雄名称", hp_max int comment "最大生命", mp_max int comment "最大法力", attack_max int comment "最高物攻", defense_max int comment "最大物防", attack_range string comment "攻击范围", role_main string comment "主要定位", role_assist string comment "次要定位" ) row format delimited fields terminated by "\t"; --字段之间的分割符是tab键 制表符
-
将射手的数据传到当前表目录下面:/user/hive/warehouse/hive_exam.db/t_archer
-
从select创建表
create table xxx as select id,name from ifnxs.t_user;
默认分隔符为:/001
-
查看表的元数据信息