Hive表【汇总】

news2024/9/22 19:29:04

提前必备

1、内部表和外部表的区别

概念讲解

外部表:
	1、存放他人给予自己的数据
	2、当我们删除表操作时,会将表的元数据删除,保留数据文件
内部表:
	1、存放已有的数据
	2、当我们删除表操作时,会将表的元数据以及数据文件都删除掉

2、公共查询语句

with:一次查询内可无数次调用
temporary table:一次会话内可无数次调用【临时】
view与table:何时都可无数次调用【永久】

一:内部表

概念

  • 内部表是由 Hive 管理数据和元数据的一种表类型,通常包含表的名称、列定义、存储格式等信息。

  • 【默认创建的表】就是【内部表】

基本形式

create table if not exists inner_table_employee(
	name string,
	places array<string>,
	info struct<gender:string,age:int>,
	scores map<string,int>,
	dept_pos map<string,string>
)
row format delimited
fields terminated by '|'
collection items terminated by ','
map keys terminated by ':'
lines terminated by '\n'
stored as textfile;

二:外部表

概念

  • 创建表时,带有【external】关键字的表即为【外部表】。

  • 外部表允许在 Hive 中定义一个表结构,并对外部存储系统中的数据进行查询和分析,而不会对数据本身进行移动或修改。

基本形式

数据准备

{"name":"henry","age":22,"gender":"male","phone":"18014499655"}
{"name":"pola","age":18,"gender":"female","phone":"18014499656"}

外部表创建

create external table if not exists hive_ext_json_family(
	name string,
	age int,
	gender string,
	phone string
)
row format serde 'org.apache.hive.hcatalog.data.JsonSerDe'
stored as textfile
location '/hive_data/hive_cha01/json';

三:分区表

1、概念

将一份大的文件拆分成多份,每一份存储在hdfs上表空间内的一个专门文件夹内

文件夹的命名包含了字段名和字段值,如:year=2012 形式。

注意:year可以作为字段来使用,但本质上year不是原始表字段,是分区字段。

2、优势

hive进行查询时就是文件流读文件,即使读取一条数据也需要加载整个文件。因此分区表将文件切割成更小的粒度,当需要针对局部数据进行检索、聚合等处理时只需要加载对应的粒度即可,从而提高了处理的效率。

3、建立分区要素

1、字段频繁出现于:
	一:where... , on...
	二:group by... , distribute by ... , cluster by...
	此时,就要考虑将此字段来建立分区
2、数据的容量(大),需要考虑建立分区

4、基本语法形式

create [external] table TABLE_NAME(FIELD_NAME TYPE,...)
partitioned by(PAR_FIELD_NAME TYPE,...)row format delimited | serde 'SERDE_CLASS'
....

5、实际操作

一:分区的建立

创建一级分区
drop table if exists test1w_partitioned_by_year;
create external table if not exists test1w_partitioned_by_year(
	user_id int,
	user_gender string,
	order_time timestamp,
	order_amount decimal(6,2)
)
partitioned by (year int)row format delimited
fields terminated by ';'
stored as textfile;
创建多级分区【以二级分区为例】
drop table if exists test1w_partitioned_by_year_month;
create table if not exists test1w_partitioned_by_year_month(
    user_id int,
    user_gender string,
    order_time timestamp,
    order_amount decimal(6,2)
)
partitioned by(year int,month int)row format delimited
fields terminated by ';'
stored as textfile;

二:数据的操作

静态分区

主要用处:客户按分区级别改|插入数据

1.筛选原文件:
	一级分区:
        cat test1w.log | awk '/2012-/{print $0}'>test2012.log
        cat test1w.log | awk '/2013-/{print $0}'>test2013.log
	多级分区【以二级分区为例】:
		cat test1w.log | awk '/2012-7/{print $0}'>test20127.log
		cat test1w.log | awk '/2012-8/{print $0}'>test20128.log
		
2.装到两分区内:
	一级分区:
		test2012.log:
			load data local inpath '/root/file/test2012.log' 
			overwrite into table hive_internal_par_regex_test1w partition(year=2012);
										
		test2013.log:
			load data local inpath '/root/file/test2013.log' 
			overwrite into table hive_internal_par_regex_test1w partition(year=2013);
								
	多级分区【以二级分区为例】:
		test20127.log:
			load data local inpath '/root/file/test20127.log'
			overwrite into table zhou.test1w_partitioned_by_year_month partition(year=2012,month=7);
									
		test20128.log:
			load data local inpath '/root/file/test20128.log'
			overwrite into table zhou.test1w_partitioned_by_year_month partition(year=2012,month=8);
动态分区

主要用处:项目初期导入数据

准备工作[动态配置]

set hive.exec.dynamic.partition=true;				-- 1、会话
set hive.exec.dynamic.partition.mode=nonstrict;
hive-site.xml										-- 2、个性化配置
hive-default.xml									-- 3、为所有配置项提供默认配置

具体代码

一级分区:
	insert overwrite table test1w_partitioned_by_year partition (`year`)
	select *,year(order_time) from test1w;
	
多级分区【以二级分区为例】:
	insert overwrite table zhou.test1w_partitioned_by_year_month partition (`year`,`month`)
	select * ,year(order_time),month(order_time) from test1w where year(order_time)<=2012;

三:分区的其他操作【查删改】

– 查看分区信息
show partitions 表名;
– 手动添加分区
一级分区:
	alter table zhou.test1w_partitioned_by_year add partition (year=2014);
多级分区【以二级分区为例】:
	alter table zhou.test1w_partitioned_by_year_month add partition (year=2012,month=7);
– 手动删除分区
一级分区:
	alter table zhou.test1w_partitioned_by_year drop partition (year=2014);
多级分区【以二级分区为例】:
	alter table zhou.test1w_partitioned_by_year_month drop partition (year=2012,month=7);

四:分桶表

1、概念

分桶表时将一个表或分区内的数据,拆分成更小的文件片段,使抽样更加高效。

2、必知点

1、分桶字段必须是表中已存在的原始字段
2、默认采用:原始字段值的hashcode%分桶数列 => 决定当前行数据会被拆分到几号桶
3、优势:数据采样
4、采样率:10% -> 桶数定义为10
5、一般是在 【分区】 的基础上进行 【分桶】,更好地优化查询性能。

3、实际应用场景

1.抽样【数据采样】

在开发中,数据量大的情况下,我们为了针对开发做测试,就可以采用分桶来进行数据采样,采样得到的结果是一个具有代表性的查询结果,可以达到快速开发的目的。

2.拉链表【便于修改】

修改某行数据时,无需将整个文件都读取出来,只需将小份文件导出进行修改即可。

4、实际操作

一:创建分桶表

在根据year分区的基础上,对每个year内部进行了分桶,分为4份数据,便于抽样|修改

drop table if exists test1w_partitioned_SeparateBarrel;
create table if not exists test1w_partitioned_SeparateBarrel(
    user_id int,
    user_gender string,
    order_time timestamp,
    order_amount decimal(6,2)
)
partitioned by(year int)
clustered by(order_time) into 4 buckets	✔	=> 此时采样率:25%
row format delimited
fields terminated by ';'
stored as textfile;

二:数据的操作

准备工作[动态配置]
set hive.exec.dynamic.partition=true;				-- 1、会话
set hive.exec.dynamic.partition.mode=nonstrict;
hive-site.xml										-- 2、个性化配置
hive-default.xml									-- 3、为所有配置项提供默认配置
具体代码
insert into table zhou.test1w_partitioned_SeparateBarrel partition (year)
select *,year(order_time) from test1w;

三:实际应用【数据采集】

–随机抽样【基于整行数据】

基本解释

  • 取每个桶中 四分之三的数据【很少用】
  • 进行随机抽样,不考虑数据的顺序或时间等因素,可以使用类似 bucket 3 out of 4 on rand()形式,这样每次抽样的结果可能会有所不同,适合需要随机性的分析或实验。
select * from test1w_partitioned_SeparateBarrel
tablesample(bucket 3 out of 4 on rand())s;
–分桶字段抽样【基于指定列】✔

基本解释

  • 取每个桶中 四分之一的数据[桶]【随机】 => 推荐使用,使用分桶列更高效

  • 从【有序的数据】中抽样,例如按照时间排序的订单数据,可以使用类似于 bucket 3 out of 4 on order_time形式,这样可以保证抽样数据具有一定的顺序性和连续性。

最终结果分析:最后获取的数据是在每个分区【文件夹】内随机抽取指定数量【如:四分之一]的数据[桶]】=> 抽到的数据[桶]是具有随机性的。

select year,count(*) as order_count from test1w_partitioned_SeparateBarrel
tablesample ( bucket 1 out of 4 on order_time)s
group by year;

五:临时表(temporary)

1、概念

  • 一次链接(会话session)内临时创建的表格,会话结束后自动删除
默认hdfs路径:/tmp/hive/root 内根据时间寻找临时表		
idea中:show tables; 才可看到临时表。

2、实际操作

create temporary table if not exists test1w_gender as
select user_gender,count(*) as gender_cnt from zhou.test1w group by user_gender;

六:视图(view)

1、概念

  • 本质:一条较为复杂的共用的查询语句

2、实际操作

create view if not exists hive_view_test1w_Girl as
select * from test1w where user_gender = "女";

七:拉链表(zip tables)

1、发展流程

hive发展

  • hive 0.14就已经有这一逻辑模型,名为slowly changing dimension。
  • hive 2.6.0 支持merge语法,运用了 事务管理

拉链表由来

原来采用分区表,用户分区存储历史增量数据,缺点是重复数据太多
目前运用拉链表来解决这一问题

2、含义

用于解决持续增长且存在一定时间范围内重复的数据,即:合并有一定重复性【较小时间范围内】的数据。

3、优点

  • 节约空间(一份订单只有一条数据)

4、应用场景

【数据规模庞大】,新数据【在有限区间(时间…)内】存在多种状态变化

5、准备工作[动态配置]

set hive.support.concurrency=true;
set hive.enforce.bucketing=true;
set hive.exec.dynamic.partition.mode=nonstrict;
set hive.txn.manager=org.apache.hadoop.hive.ql.lockmgr.DbTxnManager;
set hive.compactor.initiator.on=true; -- 表合并开启
set hive.compactor.worker.threads=1; -- 表合并线程必须为一
set hive.auto.convert.join=false; -- 关闭 mapjoin
set hive.merge.cardinality.check=false; -- 关闭检查数据列的基数(列值的差异性)
set mapreduce.job.reduces=4;

6、具体代码

drop table if exists zhou.hive_zipper_order;
create table zhou.hive_zipper_order(
	order_id bigint,
	user_id bigint,
	order_modify_dt timestamp,
	order_money decimal(10,2),
	current_status int
)
row format delimited 
fields terminated by ',';
--导入f F的数据至普通表hive_zipper_order中
load data local inpath '/root/file/log/order_record.log'
overwrite into table zhou.hive_zipper_order;

--创建拉链表hive_zipper_pc_order	✔
drop table if exists zhou.hive_zipper_pc_order;
create table zhou.hive_zipper_pc_order(
	order_id bigint,
	user_id bigint,
	order_create_dt timestamp,
	order_modify_dt timestamp,
	order_money decimal(10,2),
	current_status int
)
partitioned by(year int,month int,day int)
clustered by(order_create_dt) into 4 buckets
row format delimited
	fields terminated by ','
stored as orc
tblproperties("transactional"="true");

--操作历史全量数据用动态分区	✔
set hive.support.concurrency=true;
set hive.enforce.bucketing=true;
set hive.exec.dynamic.partition.mode=nonstrict;
set hive.txn.manager=org.apache.hadoop.hive.ql.lockmgr.DbTxnManager;
set hive.compactor.initiator.on=true;
set hive.compactor.worker.threads=1;
set hive.auto.convert.join=false;
set hive.merge.cardinality.check=false;
set mapreduce.job.reduces=4;

--开启动态分区,一次性挂载至拉链表hive_zipper_pc_order中	✔
with zip_src as (
	select order_id,user_id,order_money,
		min(order_modify_dt) as order_create_dt,
		max(order_modify_dt) as order_modify_dt,
		max(current_status) as current_status
	from zhou.hive_zipper_order
	group by order_id,user_id,order_money
)
insert overwrite table zhou.hive_zipper_pc_order partition(year,month,day)
select
	order_id,
	user_id,
	order_create_dt,
	order_modify_dt,
	order_money,
	current_status,
	year(order_create_dt) as year,
	month(order_create_dt) as month,
	day(order_create_dt) as day
from zip_src;

-- 拉链表查询	✔
set hive.txn.manager=org.apache.hadoop.hive.ql.lockmgr.DbTxnManager;
set hive.support.concurrency=true;
select * from zhou.hive_zipper_pc_order
where to_date(order_modify_dt)='2021-02-04'
order by order_modify_dt desc;

--之后每天,增量添加【在原表处新增】
load data local inpath '/root/file/log/order_record_2021_02_05.log'
overwrite into table zhou.hive_zipper_order;

--拉链处理增量数据(新增新数据,修改旧数据)	✔
merge into zhou.hive_zipper_pc_order as O
using (
	select 
		order_id,
		user_id,
		order_create_dt,
		order_modify_dt,
		order_money,
		current_status,
		year(order_create_dt) as year,
		month(order_create_dt) as month,
		day(order_create_dt) as day
	from (
		select order_id,user_id,order_money,
			min(order_modify_dt) as order_create_dt,
			max(order_modify_dt) as order_modify_dt,
			max(current_status) as current_status
		from zhou.hive_zipper_order
		--where to_date(order_modify_dt)='2021-02-05'
		group by order_id,user_id,order_money
	)T
) as H
on O.order_id=H.order_id
when matched then 
update set order_modify_dt=H.order_modify_dt,current_status=H.current_status
when not matched then 
insert values(H.order_id,H.user_id,H.order_create_dt,H.order_modify_dt,H.order_money,H.current_status,H.year,H.month,H.day);

--验证拉链结果	✔
select * from zhou.hive_zipper_pc_order
where to_date(order_modify_dt)>to_date(order_create_dt);

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1925139.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

LeetCode Day8|● 344.反转字符串(原地) ● 541. 反转字符串II(i可以大步跨越) ● 卡码网:54.替换数字(ACM模式多熟悉熟悉)

字符串part01 day8-1 ● 344.反转字符串整体思路代码实现总结 day8-2 ● 541. 反转字符串II整体思路代码实现总结 day8-3 ● 卡码网&#xff1a;54.替换数字题目解题思路代码实现总结 day8-1 ● 344.反转字符串 整体思路 字符串和数组的思路差不多 原地操作 代码实现 class…

递归解决换零钱问题--代码实现

在上一篇中, 经过深入分析, 已经得出一个能够递归的形式化的结果, 现在则准备给出一个具体实现. 结果回顾 前述结果如下: caseOfChange(amount, cashList) { // base caseif (amount.isNegative()) { // 负数 return 0; } if (amount.isZero()) { // 0元 return 1; }if (cas…

vscode终端(控制台打印乱码)

乱码出现的两种可能&#xff08;重点是下面标题2&#xff09; 1、文件中的汉字本来就是乱码&#xff0c;输出到控制台(终端)那就当然是乱码 在vscode中设置文件的编码格式为UTF-8&#xff0c; 2、输出到控制台(终端)之前的汉字不是乱码&#xff0c;针对此种情况如下设置 原因…

MySQL卸载 - Windows版

MySQL卸载 - Windows版 1. 停止MySQL服务 winR 打开运行&#xff0c;输入 services.msc 点击 “确定” 调出系统服务。 2. 卸载MySQL相关组件 打开控制面板 —> 卸载程序 —> 卸载MySQL相关所有组件 3. 删除MySQL安装目录 4. 删除MySQL数据目录 数据存放目录是在 …

C++从入门到起飞之——缺省参数/函数重载/引用全方位剖析!

目录 1.缺省参数 2. 函数重载 3.引⽤ 3.1 引⽤的概念和定义 3.2 引⽤的特性 3.3 引⽤的使⽤ 3.4 const引⽤ 3.5 指针和引⽤的关系 4.完结散花 个人主页&#xff1a;秋风起&#xff0c;再归来~ C从入门到起飞 个人格言&#xff1a;悟已往之不谏…

端到端自动驾驶系列(一):自动驾驶综述解析

端到端自动驾驶系列(一)&#xff1a;自动驾驶综述解析 End-to-end-Autonomous-Driving Abstract Abstract—The autonomous driving community has witnessed a rapid growth in approaches that embrace an end-to-end algorithm framework, utilizing raw sensor input to …

免费的ssh工具

1.Quickstart - kitty 2 Download Termius for Windows 3. MobaXterm Xserver with SSH, telnet, RDP, VNC and X11 - Download

基于Android平台开发,备忘录记事本

相关视频教程在某站上面(&#x1f50d;浩宇软件开发) 1. 项目功能思维导图 2. 项目涉及到的技术点 使用CountDownTimer实现开屏页倒计时使用SQLite数据库存储数据使用BottomNavigationView实现底部导航栏使用ActivityFragment实现底部导航栏页面切换使用RecyclerViewadapter实…

【人工智能】线性回归

目录 一、使用正规化方法计算下列样本的预测函数 1. 没有归一化之前 2. 归一化之后 二、读取ex1data2.txt中的数据&#xff0c;建立样本集&#xff0c;使用正规化法获取&#xff08;房屋面积&#xff0c;房间数量&#xff09;与房屋价格间的预测函数 1. 读取数据&#xff…

【OpenCV】BGR三色通道的提取与合并--超详细解读

在OpenCV中&#xff0c;处理图像时经常需要提取或合并图像的RGB&#xff08;红、绿、蓝&#xff09;三色通道。OpenCV默认使用BGR&#xff08;蓝、绿、红&#xff09;顺序来存储图像的颜色通道&#xff0c;这一点与很多图像处理库&#xff08;如PIL/Pillow&#xff09;不同&…

【项目计划】软件项目计划(Word)

项目开发计划包括项目描述、项目组织、成本预算、人力资源估算、设备资源计划、沟通计划、采购计划、风险计划、项目过程定义及项目的进度安排和里程碑、质量计划、数据管理计划、度量和分析计划、监控计划和培训计划等。 软件资料清单列表部分文档&#xff1a; 工作安排任务书…

Renesas R7FA8D1BH (Cortex®-M85) 读取芯片内部温度值

目录 概述 1 软硬件 1.1 软硬件环境信息 1.2 开发板信息 1.3 调试器信息 2 FSP和KEIL配置ADC 2.1 ADC硬件接口 2.2 FSP配置ADC 3 软件功能实现 3.1 FSP生成项目 3.2 FSP ADC模块库函数介绍 3.2.1 库函数列表 3.2.2 函数介绍 4 读Temperature sensor 4.1 初始化ADC…

Matlab-Simulink模型保存为图片的方法

有好多种办法将模型保存为图片&#xff0c;这里直接说经常用的 而且贴到Word文档中清晰、操作简单。 simulink自带有截图功能&#xff0c;这两种方法都可以保存模型图片。选择后直接就复制到截切板上了。直接去文档中粘贴就完事了。 这两个格式效果不太一样&#xff0c;第一种清…

麒麟系统开发笔记(十四):在国产麒麟系统上编译libmodbus库、搭建基础开发环境和移植测试Demo

若该文为原创文章&#xff0c;转载请注明原文出处 本文章博客地址&#xff1a;https://hpzwl.blog.csdn.net/article/details/140387947 长沙红胖子Qt&#xff08;长沙创微智科&#xff09;博文大全&#xff1a;开发技术集合&#xff08;包含Qt实用技术、树莓派、三维、OpenCV…

多数元素-哈希表

169. 多数元素 - 力扣&#xff08;LeetCode&#xff09; 哈希表来找出这个数出现几次 class Solution { public:int majorityElement(vector<int>& nums) {unordered_map<int,int> counts;int cnt 0, majority 0;for(int num : nums){counts[num];if(counts…

Python写api程序批量自动抓取商品评论数据演示

要实现一个Python程序批量自动抓取商品评论数据&#xff0c;你可以使用requests库来发送HTTP请求&#xff0c;并使用BeautifulSoup库来解析HTML页面。以下是一个简单的示例&#xff1a; 首先&#xff0c;确保已经安装了所需的库&#xff1a; pip install requests pip instal…

【Python学习笔记】Optuna + Transformer B站视频实践

【Python学习笔记】Optuna Transformer 实践 背景前摇&#xff08;省流可不看&#xff09;&#xff1a; 之前以泰坦尼克号数据集为案例&#xff0c;学习了Optuna的基本操作&#xff0c;为了进一步巩固知识和便于包装简历&#xff0c;决定找个唬人一点的项目练练手。 ————…

《揭秘深度强化学习》:一本揭示AI前沿技术的必读书籍

在人工智能&#xff08;AI&#xff09;领域飞速发展的今天&#xff0c;深度强化学习作为一种革命性技术&#xff0c;正在改变我们的世界。今天要向大家推荐的是《揭秘深度强化学习》这本书&#xff0c;它不仅为读者提供了深度强化学习的全面指南&#xff0c;还揭示了这一技术的…

常用I/O复用模型 --> 一、单线程Accept(无IO复用)

文章目录 一、前言二、I/O复用中最基础的知识点1、流2、I/O操作3、阻塞等待4、非阻塞&#xff0c;忙轮询5、多路I/O复用 三、单线程Accept(无IO复用)1、服务端2、客户端 一、前言 单线程Accept(无IO复用)是网络最基础的模型&#xff0c;常供学习使用。 下面是我的GitHub仓库&…

韦东山嵌入式linux系列-驱动设计的思想(面向对象/分层/分离)

1 面向对象 字符设备驱动程序抽象出一个 file_operations 结构体&#xff1b; 我们写的程序针对硬件部分抽象出 led_operations 结构体。 2 分层 上下分层&#xff0c;比如我们前面写的 LED 驱动程序就分为 2 层&#xff1a; ① 上层实现硬件无关的操作&#xff0c;比如注册…