Hive基础知识

1.数仓概念的理解

数据仓库,英文名叫data warehouse. 主要使用来存储历史数据的。
数仓一般按主题划分,整合了不通数据源的所有数据,存储的一般是历史数据,基本不会有什么变化, 数据量会随着时间增量变化。

与传统业务数据库不同,传统数据库主要是面向事务,数仓主要是面向分析的,也就是主要有利于数据分析,就是可取的方向。

数据仓库的分层大致为数据源,ODS(细节数据), DW(数仓) , DA(数据应用)。

ETL将数据源数据抽取,转换,加载到数据仓库,用于数据分析。

2. Hive的理解

Hive是构建在hadoop之上的数据仓库框架。
Hive的核心是通过将HFDS上文件,映射成一张表,并提供SQL查询的功能。
Hive的本质是将SQL转换为MapReduce任务,在hadoop上执行。
可以简单理解为Hive是MapReduce的一个客户端。

2.1 Hive架构

Hive架构主要包括

  • Hive客户端
    比如Hive Cli, Beeline,java, python客户端
  • metastore
    主要用于存储元数据,一般存储在mysql
  • driver
    1. SQL Parser
    2. Physical Plan
    3. Query Optimizer
    4. Execution
  • 存储和计算
    1. 主要存储在HDFS
    2. 使用MapReduce计算, 也可以使用spark和tez
2.2 Hive的安装

推荐使用远程模式启动。 本教程使用2.1.0

  • metastore服务
  • hiveserver2
  • mysql
    (略)

启动方式1 hive-cli:

hive   # 进入hive
hive -e "show databases;" # 执行sql 
hive -f ./hive.sql # 执行sql文件

启动方式2 beeline:

beeline 
2.3 数据库操作
create database if not exists test01;
desc database test01;
drop database test01;
drop database test01 cascade;
-------------------------------------------
use myhive;

-- 创建外部表-teacher表
create external table teacher (tid string,tname string)
row format delimited fields terminated by '\t';

-- 创建外部表-student表
create external table student
(
    sid    string,
    sname  string,
    sbirth string,
    ssex   string
)
    row format delimited fields terminated by '\t';

-- 从本地文件系统向表中加载数据,就是将本地的文件复制一份到HDFS的表目录下
/*
  Load命令:
    方式1:从本地加载
    load data local inpath '/export/data/datas/student.txt' [overwrite] | into table student [partition (partcol1=val1,…)];
    方式2:从HDFS加载
    load data  inpath '/export/data/datas/student.txt' [overwrite] | into table student [partition (partcol1=val1,…)];
  */
-- 向student表加载数据,就是将本地的文件复制一份到HDFS的表目录下
load data local inpath '/export/data/hivedata/student.txt' into table student;
-- 向student表加载数据,并覆盖原来数据
load data local inpath '/export/data/hivedata/student.txt' overwrite into table student;
-- 查询student表
select * from student;

-- 删除表
drop table student;


-- 从HDFS向teacher表加载数据 ,本质上做的是剪切,将原来的文件移到到表目录文件
 -- 首先需要将teacher.txt上传到HDFS(/hivedatas)
load data inpath '/hivedatas/teacher.txt' into table  teacher;

select  * from teacher;

drop table teacher;


-- 加载weblog数据
drop table if exists ods_weblog_origin;
create table weblog_origin(
    valid string,
    remote_addr string,
    remote_user string,
    time_local string,
    request string,
    status string,
    body_bytes_sent string,
    http_referer string,
    http_user_agent string
)
row format delimited fields terminated by '\t';


load data local inpath '/export/data/hivedata/part-r-00000' into table  weblog_origin;

select * from weblog_origin;

-- 演示三张表共享一份数据
drop table if exists weblog_origin1;
create external table weblog_origin1
(
    valid           string,
    remote_addr     string,
    remote_user     string,
    time_local      string,
    request         string,
    status          string,
    body_bytes_sent string,
    http_referer    string,
    http_user_agent string
)
row format delimited fields terminated by '\t'
location '/hivedatas/weblog_origin';


drop table if exists weblog_origin2;
create external table weblog_origin2
(
    valid           string,
    remote_addr     string,
    remote_user     string,
    time_local      string,
    request         string,
    status          string,
    body_bytes_sent string,
    http_referer    string,
    http_user_agent string
)
    row format delimited fields terminated by '\t'
    location '/hivedatas/weblog_origin';


drop table if exists weblog_origin3;
create external table weblog_origin3
(
    valid           string,
    remote_addr     string,
    remote_user     string,
    time_local      string,
    request         string,
    status          string,
    body_bytes_sent string,
    http_referer    string,
    http_user_agent string
)
    row format delimited fields terminated by '\t'
    location '/hivedatas/weblog_origin';


select * from weblog_origin1;
select * from weblog_origin2;
select * from weblog_origin3;


drop table weblog_origin1; -- 外部表删除,只删除元数据
drop table weblog_origin2;
drop table weblog_origin3;

select count(*) from weblog_origin1; -- 11893   23786



-- -- -- -- -- -- -- -- -- -- -- -- -- 复杂类型-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
/*
 zhangsan	beijing,shanghai,tianjin,hangzhou
 wangwu	changchun,chengdu,wuhan,beijing
 */
-- 1、Array类型
create external table hive_array
(
    name           string,
    work_locations array<string>
)
row format delimited fields terminated by '\t'
collection items terminated by ',';

load data local inpath '/export/data/hivedata/array.txt' into table hive_array;

select * from hive_array;

-- 查询
-- 查询所有数据
select * from hive_array;
-- 查询work_locations数组中第一个元素
select name, work_locations[0] location from hive_array;
-- 查询location数组中元素的个数
select name, size(work_locations) location_size from hive_array;
-- 查询location数组中包含tianjin的信息
select * from hive_array where array_contains(work_locations,'tianjin');

-- 2、Map类型
/*
1,zhangsan,father:xiaoming#mother:xiaohuang#brother:xiaoxu,28
2,lisi,father:mayun#mother:huangyi#brother:guanyu,22
3,wangwu,father:wangjianlin#mother:ruhua#sister:jingtian,29
4,mayun,father:mayongzhen#mother:angelababy,26
 */
create table hive_map
(
    id      int,
    name    string,
    members map<string,string>,
    age     int
)
row format delimited
fields terminated by ','
collection items terminated by '#'
map keys terminated by ':';

load data local inpath '/export/data/hivedata/map.txt' into table hive_map;

select * from hive_map;

select * from hive_map;
-- 根据键找对应的值
select id, name, members['father'] father, members['mother'] mother, age from hive_map;

-- 获取所有的键
select id, name, map_keys(members) as relation from hive_map;
-- 获取所有的值
select id, name, map_values(members) as relation from hive_map;
-- 获取键值对个数
select id,name,size(members) num from hive_map;
-- 获取有指定key的数据
select * from hive_map where array_contains(map_keys(members), 'brother');

-- 查找包含brother这个键的数据,并获取brother键对应的值
select id, name, members['brother'] brother
from hive_map
where array_contains(map_keys(members), 'brother');

-- 3、Struct类型
/*
 192.168.1.1#zhangsan:40
192.168.1.2#lisi:50
192.168.1.3#wangwu:60
192.168.1.4#zhaoliu:70

 */
create table hive_struct
(
    ip   string,
    info struct<name:string, age:int>
)
row format delimited
fields terminated by '#'
collection items terminated by ':';

load data local inpath '/export/data/hivedata/struct.txt' into table hive_struct;

select * from hive_struct;
--根据struct来获取指定的成员的值
select ip, info.name from hive_struct;
select ip, info.name ,info.age from hive_struct;


-- #################分区表#############
-- 分区就是分文件夹
-- month表示分区字段,以后文件夹的名字就是: month=xxx
create table score
(
    sid    string,
    cid    string,
    sscore int
) partitioned by (month string) row format delimited fields terminated by '\t';

-- 分区表在加载数据时,必须制定你要将这些数据放在哪个文件夹下
load data local inpath '/export/data/hivedata/score.txt' overwrite into table score partition(month='202101');
load data local inpath '/export/data/hivedata/score.txt' overwrite into table score partition(month='202102');


select * from score;

select * from score where month = '202102';

desc score;


-- 创建多级分区表
create table score2
(
    sid    string,
    cid    string,
    sscore int
) partitioned by (year string,month string,day string) row format delimited fields terminated by '\t';

load data local inpath '/export/data/hivedata/score.txt'
     into table score2 partition(year='2021',month='01',day='01');

load data local inpath '/export/data/hivedata/score.txt'
    into table score2 partition(year='2021',month='01',day='02');


load data local inpath '/export/data/hivedata/score.txt'
    into table score2 partition(year='2021',month='02',day='01');

load data local inpath '/export/data/hivedata/score.txt'
    into table score2 partition(year='2022',month='01',day='01');

select * from score2;

desc score2;

select * from score2 where year = '2021' and month = '01' and day='01';



-- 查看表的所有分区
show  partitions  score;

show  partitions  score2;


-- 添加分区
alter table score add partition(month='202008');
-- 同时添加多个分区
alter table score add partition(month='202009') partition(month = '202010');

-- 删除分区
alter table score drop partition(month = '202010');

insert into  table score partition(month = '202011') values(1,1,1);
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Hive是一个基于Hadoop的数据仓库工具,它提供了一种类似SQL的查询语言,用于将结构化数据存储在Hadoop集群上,并进行查询和分析。下面是一些关于Hive基础知识的选择题: 1. Hive的主要特点是什么? a) 提供类似SQL的查询语言 b) 可以在Hadoop集群上进行数据存储和分析 c) 可以处理结构化和半结构化数据 d) 所有选项都正确 答案:d) 所有选项都正确 2. Hive将数据存储在哪里? a) HBase b) Hadoop Distributed File System (HDFS) c) Cassandra d) MySQL 答案:b) Hadoop Distributed File System (HDFS) 3. Hive中的表可以与以下哪种文件格式关联? a) CSV b) JSON c) Parquet d) 所有选项都正确 答案:d) 所有选项都正确 4. Hive使用什么来对数据进行分区和排序? a) HDFS b) Tez c) MapReduce d) Apache Spark 答案:c) MapReduce 5. Hive中的数据查询和分析通过什么来实现? a) Hive Query Language (HQL) b) Structured Query Language (SQL) c) Apache Hive d) Apache Hadoop 答案:a) Hive Query Language (HQL) 总之,Hive是一个基于Hadoop的数据仓库工具,具有类似SQL的查询语言,可以在Hadoop集群上存储和分析结构化和半结构化数据。它使用HDFS来存储数据,可以与多种文件格式关联,并使用MapReduce来进行数据分区和排序。数据查询和分析通过Hive Query Language (HQL)来实现。 ### 回答2: Hive是一款基于Hadoop的数据仓库工具,它提供了方便的数据查询和分析的功能。接下来我将回答一些关于Hive基础知识的选择题。 1. Hive中的表是如何定义的? 答案:C. 使用HiveQL语句创建表。 2. 在Hive中,数据是如何存储的? 答案:B. 在Hadoop的HDFS文件系统中。 3. Hive中的分区是用来做什么的? 答案:A. 对数据进行逻辑上的划分,便于查询优化和数据管理。 4. 在Hive中,可以使用哪种语言进行数据查询? 答案:D. HiveQL。 5. 在Hive中,用来处理复杂逻辑和数据运算的是什么? 答案:B. Hive的UDF(用户定义函数)和UDAF(用户定义聚合函数)。 6. Hive的数据存储格式有哪些? 答案:A. 文本文件(TextFile)、序列文件(SequenceFile)和Parquet等。 7. Hive表中的数据可以通过什么方式进行加载? 答案:C. 使用Hive的LOAD DATA语句。 8. 在Hive中,用来创建管理表结构的是什么? 答案:B. Hive的元数据存储。 9. Hive的优势是什么? 答案:C. 简化了对Hadoop数据的查询和分析。 10. 使用Hive时,可以通过什么方式进行数据的导入和导出? 答案:D. 使用Hive的导入和导出命令。 以上是关于Hive基础知识的一些选择题的答案。Hive是一个功能强大且易于使用的工具,可以帮助用户更好地处理和分析大数据。掌握Hive基础知识对于进行数据仓库的建设和数据分析工作非常重要。 ### 回答3: Hive是一个开源的数据仓库基础架构,运行在Hadoop集群上。以下是关于Hive基础知识选择题的回答: 1. Hive中的数据存储在哪里? 答:Hive中的数据存储在Hadoop分布式文件系统(HDFS)中。 2. Hive中的数据是如何组织的? 答:Hive中的数据是以表(Tables)的形式进行组织的。 3. Hive中的表的结构是如何定义的? 答:Hive中的表的结构是通过DDL语句来定义的,包括表的名称、列名称、数据类型等信息。 4. Hive中的查询语言是什么? 答:Hive中的查询语言类似于SQL,称为HiveQL或HQL。 5. Hive中的查询语句是如何转换为MapReduce作业的? 答:Hive将查询语句转换为逻辑查询计划,然后将逻辑查询计划转换为物理查询计划,最后将物理查询计划转换为MapReduce作业。 6. Hive中的分区表是什么? 答:Hive中的分区表是按照一个或多个列的值分成不同的目录,并且每个目录下存储相应分区的数据。 7. Hive中的桶是什么? 答:Hive中的桶是将数据分成固定数量的文件的一种方式,目的是提高查询性能。 8. Hive中的内部表和外部表有什么区别? 答:内部表的数据和元数据都由Hive管理,删除内部表时会删除表的数据;而外部表的数据和元数据存储在外部的文件系统中,删除外部表时只会删除元数据。 9. Hive中的UDF是什么? 答:UDF全称为用户定义函数(User-Defined Functions),是由用户自定义的用于特定数据处理操作的函数。 10. Hive中的压缩是如何实现的? 答:Hive中的压缩是通过执行MapReduce作业时将数据进行压缩,以减少数据的存储空间和提高查询性能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值