Hive 之 分区表 外部分区表 关联查询

转自:http://blog.csdn.net/u014726937/article/details/51984867


1.查看mysql中metastore数据存储结构

Metastore中只保存了表的描述信息(名字,列,类型,对应目录

使用SQLYog连接itcast05 的MySQL数据库 
这里写图片描述 
查看Hive数据库的表结构: 
这里写图片描述

2.建表(默认是内部表(先建表,后有数据))

(建表时必须指定列的分隔符)

create table trade_detail(
id bigint, 
account string, 
income double, 
expenses double, 
time string) 
row format delimited fields terminated by '\t';

3.Hive状态下执行Hadoop hdfs命令

在使用hive shell 的时候,我们有时候需要操作hdfs 
Hive为我们提供了在hive命令行下hdfs的shell: 
,例如:

dfs  -ls  /; 
dfs -mkdir  /data;
dfs  -put  /root/student.txt;

用法和hdfs下是一样的,只是细微的差别 
Hadoop命令稍微有些差别,前面是dfs开头,后面以“;”结尾

4.创建–外部表(先有数据,后建表)

先上传数据文件 a.txt b.txt 到hdfs:/data目录下, 
a.txt 和 b.txt 中的内容都是: 
这里写图片描述 
后执行创建表的命令:

create external table ext_student (
id int,
name string) 
row format delimited fields terminated by '\t' 
location '/data';

创建完成后使用命令:select * from ext_student; 查看表中内容: 
这里写图片描述

再次上传数据文件 pep.avi  
这里写图片描述

到 hdfs:/data 目录下,后执行全表扫描:select * from ext_student; 
这里写图片描述

说明只要将这个数据放到 hdfs:/data 表所指定的目录下,hive就能将这个表中的数据读取出来(内部表和外部表都支持,但也存在特殊情况读不出)

为什么把文件丢到对应目录下就能把数据读出来?

答:因为metastore记录了这张表和数据的映射关系

SDS表中的内容: 
这里写图片描述

5.创建–分区表

建分区表是为了提高数据的查询效率,按照省份、年份、月份等分区

创建一个外部分区表(External Table ): 
(表名:beauties 指向文件:beauty)

create external table beauties (
id bigint, 
name string, 
size double) 
partitioned by (nation string) 
row format delimited fields terminated by ‘\t’ 
location ‘/beauty’ ;

show create table beauties;  

执行完成之后发现hdfs根目录下有beauty文件夹。

准备好3个数据文件: b.c b.j b.a 
这里写图片描述 
这里写图片描述 
这里写图片描述 
载入数据文件,同时指定分区:

load data local inpath '/root/b.c' into table beauties partition (nation='China');

查看表中是否成功load数据: 
这里写图片描述 
突发奇想:能否像平常使用外部表一样,在 hdfs:/beauty 目录下创建一个文件夹 nation=Japan ,然后将b.j 文件上传到这个目录下,数据就可以查出来了?

答:不行! 因为在载入数据的时候,metastore是不知道你将这个文件放到 /beauty/nation=Japan/ 目录下的。

拯救方法:通知hive在元数据库中添加一个beauties表的分区记录

alter table beauties add partition (nation=’Japan’) location “/beauty/nation=Japan/”

添加分区后,metastore中SDS表多了一条 记录:

这里写图片描述

再次查询beauties表,发现b.j中的数据也能查询出来了:

分区表的使用优势:

select * from beauties where nation=’China’;

在数据量很大的时候,建分区表可以提高查询效率,就不需要将整张表数据筛选对比之后再输出,因为数据在hdfs中直接是以分区存储的,所以使用类似”nation”等分区字段是可以直接把数据取出的

删除分区:

alter table beauties drop if exists partition (nation ='Japan') ;

注:这里的 if exists 字段呢,是一个检查分区是否存在的字段,存在则删除,不存在也不会报错说分区不存在啦

建内部分区表(Managed Table)

create table td_part(
id bigint, 
account string, 
income double, 
expenses double, 
time string) 
partitioned by (logdate string) 
row format delimited fields terminated by '\t';

普通表和分区表区别:有大量数据增加的需要建分区表

create table book (
id bigint, 
name string) 
partitioned by (pubdate string) 
row format delimited fields terminated by '\t'; 

分区表加载数据 
(hive自己的语法)

load data local inpath './book.txt' 
overwrite into table book 
partition (pubdate='2010-08-22');

local inpath –>从本地磁盘加载,不是hdfs

overwrite –>以覆盖的方式将数据写入book表中

以下创建表的方式少了“overwrite”,则是以追加方式将数据加载到hive表中:

load data local inpath '/root/data.am' 
into table beauty 
partition (nation="USA");

使用分区字段查询表中的数据

select nation, avg(size) from beauties group by nation order by avg(size);

6. 表关联查询

查询举例: 
需求: 
  对 trade_detail 按照账户进行分组,求出每个账户的总支出总结余,然后和 user_info 进行表关联,取出名称。

mysql中一条查询语句就能完成关联查询:

select t.account,u.name,t.income, t.expenses, t.surplus 
from user_info u join (
    select account,sum(income) as income,sum(expenses) as expenses,sum(income-expenses) as surplus 
    from trade_detail group by account 
) t 
on u.account = t.account

但是数据量一大,这个查询过程将变得极其漫长

所以我们使用hive来完成:

a) 首先要将2张表中的数据导入hdfs中,同样,我们也可以将mysql中的数据直接导入到hive表里面:

Mysql中的表: 
trade_detail表: 
这里写图片描述

user_info表: 
这里写图片描述

b) 在hive中创建表

trade_detail表:

create table trade_detail (
id bigint,
account string,
income string,
expenses string ,
times string) 
row format delimited fields terminated by ‘\t’;

user_info表:

create table user_info (
id int,
account string,
name string,
age int) 
row format delimited fields terminated by ‘\t’;

c) 使用Sqoop 将mysql中trade_detail的数据导入hive中

./sqoop import 
--connect jdbc:mysql://192.168.1.102:3306/itcast 
--username root 
--password 123 
--table trade_detail 
--hive-import 
--hive-overwrite 
--hive-table trade_detail 
--fields-terminated-by '\t';

可能会出现如下的错误:

这里写图片描述

原因是没有将hive添加到环境变量: 
解决: 
1)编辑 /etc/profile 文件,添加HIVE_HOME: 
这里写图片描述

2)source /etc/profile 刷新配置

3)使用 which 命令查看是否添加成功: 
这里写图片描述 
ok 
4)再次执行sqoop命令,发现sqoop导入正在执行,可以看到map-reduce工作正在执行,在web浏览器上查看执行完成之后的结果文件: 
这里写图片描述

Sqoop导入执行成功!

d) 使用Sqoop 将mysql中user_info的数据导入hive的user_info中

./sqoop import 
--connect jdbc:mysql://192.168.1. 102:3306/itcast 
--username root 
--password 123 
--table user_info 
--hive-import 
--hive-overwrite 
--hive-table user_info 
--fields-terminated-by '\t';

e) hive执行关联查询语句之后的结果:

select t.account,u.name,t.income, t.expenses, t.surplus 
from user_info u join (
    select account,sum(income) as income,sum(expenses) as expenses,sum(income-expenses) as surplus 
    from trade_detail group by account 
) t 
on u.account = t.account;

这里写图片描述

经验证,这样的查询结果和在mysql中执行的结果是相同的

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Hive SQL中的分区表是指根据特定的字段值将数据存储在不同的文件夹或子文件夹中的表。分区表可以提高查询性能和数据管理的效率。分区表可以分为静态分区和动态分区两种类型。在创建分区表时,可以通过指定分区字段来实现数据的分区存储。分区字段不能是已有的字段,也不能重复。分区字段的值可以手动指定(静态分区)或根据查询结果位置自动推断(动态分区)。 动态分区是一种根据查询结果自动推断分区字段值的方式。在Hive中启用动态分区需要设置两个参数,即hive.exec.dynamic.partition为true和hive.exec.dynamic.partition.mode为nonstrict。动态分区可以使用insert select语法来实现,通过该语法可以根据查询结果动态地将数据插入分区表中。 Hive还支持多重分区,即在分区表的基础上继续进行分区。多重分区可以通过指定多个分区字段来实现,不同分区字段之间具有递进关系,可以理解为在前一个分区的基础上再进行分区,划分更加细的粒度。从HDFS的角度来看,多重分区就是在文件夹下继续划分子文件夹。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [HiveSQL 分区表](https://blog.csdn.net/weixin_53570636/article/details/127240576)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值