HiveSQL基础Day03

回顾总结

hive表的类型 :内部表和外部表

删除内部表会删除表的所有数据

删除外部表只会删除表的元数据,hdfs上的行数据会保留

表的分区和分桶

本质都是对表数据的拆分存储

分区的方式 是通过创建不同的目录来拆分数据 ,根据数据本身的内容最为目录名

分桶的方式 是通过创建不同的文件来拆分数据 文件名时hash取余的名字


数据拆分后可以提升数据的查询效率


分桶还有特殊使用场景

分桶关联多张表

分桶随机采样

序列化

本质就是对hdfs上的文件数据进行读取和写入

可以通过row format delimited fields terminated by ',' 指定如何读取和写入hdfs上的字段数据

CREATE  [EXTERNAL] TABLE [IF NOT EXISTS] [db_name.]table_name(col_name data_type  [COMMENT col_comment])[COMMENT table_comment]hive中的独有语法-- 分区字段指定,分区字段是不能表字段重复[PARTITIONED BY (col_name data_type [COMMENT col_comment], ...)]-- 分桶字段,需要指定表中存在的字段[CLUSTERED BY (col_name, col_name, ...) INTO num_buckets BUCKETS]            -- 指定分割符   默认的字段分割符\001[ROW FORMAT row_format] -- 指定表的存储目录位置  不指定默认是在对应的数据库目录下创建表目录[LOCATION hdfs_path]

一、内置函数

内置函数时hive中自带的函数方法,用来对不同类型的字段数据进行操作

字符串,数值,条件判断

字符串方法

length
concat
concat_ws
substr
split
regexp_replace
% 
round()
ceil()
floor()
if()
​
case when 条件1  then 结果when 条件2  then 结果when 条件3  then 结果else前面条件都不成返回的结果
end 字段名称
-- 查看hive中的所有内置函数
show functions;
-- 查看函数的具体用户
desc function extended 函数名;

1-1 日期类型操作

-- 获取当前日期
select `current_date`();
-- 获取当前日期时间
select `current_timestamp`();
-- 获取unix时间(时间戳) 从1970年1月1号0时0分0秒 到现在过去了多少秒
select unix_timestamp();
​
​
-- unix时间 和日期时间的转化
-- 日期时间转为unix
select unix_timestamp('2023-10-01 15:30:28');
-- 将unix时间转为日期时间
select from_unixtime(12390886789);
​
-- 年月日的取值
select year('2023-10-01 15:30:28');
select month('2023-10-01 15:30:28');
select day('2023-10-01 15:30:28');
select dayofmonth('2023-10-12 15:30:28');
select dayofweek('2023-10-12 15:30:28');
select hour('2023-10-12 15:30:28');
select minute('2023-10-12 15:30:28');
select second('2023-10-12 15:30:28');
​
-- 时间加减
select date_add('2023-10-12 15:30:28',5);
select date_add('2023-10-12 15:30:28',-5);
​
-- 比较时间相差多少天
select datediff(`current_date`(),'2023-10-12');

1-2 类型转化

-- 字段类型不符合计算需求,可以进行类型转化
-- 隐式转化  hive会自动判断进行转化数据然后计算
select '123'+'456';
-- 手动指定转化
select cast('123' as int) + cast('456' as int);
​
select * from itcast.tb_hero;
desc itcast.tb_hero;
-- 转化只是在计算时进行,并不会改变字段本身类型
select cast(blood as bigint) from itcast.tb_hero;

1-3 字符串数据转json,array,map操作

  • josn字符串操作

    • 数据是一个 "{key:value}" 格式

    • 使用方法取值value

create table tb_order_detail(json_field string
);
​
​
select * from tb_order_detail;
-- 对字段中的json字符串数据进行取值,按照key取value值
-- 方法一  get_json_object 每次只能取一个字段数据  ,可以向下一直取值
selectget_json_object(json_field,'$.orderid') as orderid,get_json_object(json_field,'$.total_price') as total_price,get_json_object(json_field,'$.total_num') as total_num,get_json_object(json_field,'$.goods') as goods,get_json_object(json_field,'$.goods[0]') as good1,get_json_object(json_field,'$.goods[0].name') as good1_name,get_json_object(json_field,'$.goods[1]') as good2
from tb_order_detail;
​
-- json_tuple 一次取多个字段值,不能对嵌套数据往下取值
select json_tuple(json_field,'orderid','total_price','total_num','goods') as(orderid,total_price,total_num,goods) from tb_order_detail
​

将字符串数据切割转为数组数据

create table tb_user(id int,name string,hobby string
)row format delimited fields terminated by ',';
​
select id,name,split(hobby,'-') from tb_user;

将字符串数据切割转为map数据

-- 使用map方法
select `map`('name','张三','age',29);
​
create table tb_hero(id int,name string,blood int,skin string
)row format delimited fields terminated by ',';
-- 西部大镖客:288-大圣娶亲:888-全息碎片:0-至尊宝:888-地狱火:1688 --> {'西部大镖客':288,'大圣娶亲':88}
select id,name,blood,map(split(split(skin,'-')[0],":")[0], cast(split(split(skin,'-')[0],":")[1] as int),split(split(skin,'-')[1],":")[0],cast(split(split(skin,'-')[1],":")[1] as int))from tb_hero;

mysql中的内置函数操作

​
select concat(name,sex) from member;
select concat_ws(',',name,sex) from member;
select substr(s_birth,1,4) from student;
select replace(s_birth,'-','/') from student;
select round(3.1421,2);
select round(3.123);
select pow(2,3);
​
​
​
select current_timestamp;
select current_date;
select unix_timestamp();
select unix_timestamp('2023-10-10 10:10:10');
select from_unixtime(127381923);
​
select date_add('2023-10-10 10:10:10',interval 1 day);
select date_add('2023-10-10 10:10:10',interval 1 month );
select date_add('2023-10-10 10:10:10',interval 1 year );
select date_add('2023-10-10 10:10:10',interval -1 year );
select date_add('2023-10-10 10:10:10',interval -1 hour );
​
select timestampdiff(year ,'2020-02-02','2023-02-02');
select timestampdiff(month ,'2020-02-02','2023-02-02');
select timestampdiff(day ,'2020-02-02','2023-02-02');
select timestampdiff(hour ,'2020-02-02 16:23:12','2023-02-02 15:12:12');
​
​
​
​
select if(s_sex='男',1,2),s_sex from student;
select *,casewhen year(s_birth) between 1980 and 1989 then '80后'when year(s_birth) between 1990 and 1999 then '90后'end
from student;

二、DQL的查询计算(掌握)

对表进行查询计算

select 字段  from 表;
​
select 字段1,字段2,字段3,常量值,内置函数计算 from tb

2-1 单表查询计算

where 的条件过滤
select 字段1,字段2,字段3,常量值,内置函数计算 from tb where 过滤条件

过滤条件,条件成立的将对的行数据返回

  • 比较大小

    • 字段 = 数值 判断字段和数值是否相等

    • 字段 > 数值

    • 字段 < 数值

    • 字段 >= 数值

    • 字段 <= 数值

    • 字段 != 数值

-- 大小比较
-- 年龄大于19岁
select * from tb_stu where age >19;
-- 查询性别为女性的学生信息
select * from tb_stu where gender='女';
-- 查询学科不是IS的学生信息
select * from tb_stu where cls !='IS';

  • 判断空值

    • 字段 is null 字段为空

    • 字段 is not null

-- 空值判断
insert into tb_stu values(9023,null,'男',20,'MA');
select * from tb_stu where name is not null;
select * from tb_stu where name is null;
​
select * from tb_stu where name !=''; -- 空字符过滤是会将null值一起过滤掉
select * from tb_stu where name =''; -- 相等判断是,空字符是不会过滤出null值的

  • 范围判断

    • 字段 between 数值1 and 数值2

      • 字段 >=数值 and 字段 <=数值

    • 字段 in (数值1,数值2....) 字段的值等于任意一个值就返回结果

-- 范围判断
select * from tb_stu where age between 20 and 25;
select * from tb_stu where age in(19,22);
select * from tb_stu where age not in(19,22);

  • 模糊查询

    • 字段 like '% _ 数据' % 可以匹配任意多个 _ 匹配任意一个字符

    • 字段 rlink '正则表达式'

​
create table tb_stu2(id int,name string,gender string,age int,cls string,email string
)row format delimited fields terminated by ',';
​
select * from tb_stu2;
-- like的模糊查询
-- 查询姓名为刘的学生
select * from tb_stu where name like '刘%'; -- % 代表任意多个字符
-- 查询姓名为刘的学生 名字个数时2个字的
select * from tb_stu where name like '刘_';
select * from tb_stu where name like '刘__'; -- 查询三个字的
​
-- rlike 的正则表达式
-- 表的是就是通过不同的符号来表示不同的数据进行匹配
-- \\d 匹配数据的表达式   \\w  匹配字符字母  \\s 匹配空格
select * from tb_stu2;
-- ^ 表是什么开头
select * from tb_stu2 where email rlike '^\\d'; -- 表是以数字开头
select * from tb_stu2 where email rlike '^\\w';
select * from tb_stu2 where email rlike '^\\S';
​
-- ^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$
​
select email,split(email,'@')[1] from tb_stu2;
select email,split(split(email,'@')[1],'\\.')[0] from tb_stu2;
  • 与或非

    • 条件1 and 条件2 and 条件3 ... 多个条件都成立,返回对应的行数据

    • 条件1 or 条件2 or 条件3 ... 多个条件满足任意一个,返回对应的行数据

-- 与 多个条件都成立
select * from  tb_stu;
-- 查询性别为男性,学科是is的
select * from  tb_stu where gender='男' and cls = 'IS';
-- 查询性别为男性或学科是is的
select * from  tb_stu where gender='男' or cls = 'IS';
聚合计算 sum,count
select * from tb_stu;
select sum(age) from tb_stu2;
select count(*) from tb_stu where name is not null;
select avg(age) from tb_stu2;
select max(age) from tb_stu;
select min(age) from tb_stu;
分组聚合 group by
select sum(age) from tb_stu group by gender;
select sum(age),gender from tb_stu group by gender;

注意分组后,select 中不能出现非分组字段

分组后过滤 having
select sum(age),gender from tb_stu group by gender having sum(age)> 200;
排序

order by 全局排序

select * from tb_stu order by age; -- 默认是升序 从小到大
select * from tb_stu order by age desc ; -- 降序 从大到小
分页 limit
-- 分页
select * from tb_stu limit 5;
select * from tb_stu limit 10,5; -- 页数 m  每页数量是n   (m-1)*n,n

2-2 多表关联查询

join的列关联
  • 内关联

    • 找关联字段相同的数据

  • 左关联

    • 展示保留左边表的所有数据,右边表有相同数据显示,没有相同数据则为null

  • 右关联

    • 展示保留右边表的所有数据,左边表有相同数据显示,没有相同数据则为null

-- table1: 员工表
CREATE TABLE employee(id int,name string,deg string,salary int,dept string) row format delimited
fields terminated by ',';
​
-- table2:员工家庭住址信息表
CREATE TABLE employee_address (id int,hno string,street string,city string
) row format delimited
fields terminated by ',';
​
-- table3:员工联系方式信息表
CREATE TABLE employee_connection (id int,phno string,email string
) row format delimited
fields terminated by ',';
-- on 当成where使用,进行条件顾虑
select * from employee t1 join  employee_address t2  on  t1.id = t2.id and salary> 30000;
select * from employee t1 left join  employee_address t2  on  t1.id = t2.id;
select * from employee t1 right join  employee_address t2  on  t1.id = t2.id;
-- 实现内关联的效果
select * from employee,employee_address where employee.id = employee_address.id;
union的行关联

将select查询计算后的结果表合并

-- union合并
select 'tb_stu',count(*) from tb_stu where name is not null
union
select 'tb_stu2', count(*) from tb_stu2 where name is not null;-- 保留重复数据
select id,name from tb_stu
union all
select id,name from tb_stu2;

三、窗口聚合

默认没有窗口函数进行计算时全表数据获取计算

根据指定的窗口范围计算数据,将计算结果单独呈现一列展示,不会因为聚合改变行数

聚合使用,取值函数,排序函数 over(partition by 分组字段  order by 排序字段  rows between 起始行 and 结束行)
rows 指定计算的行范围

3-1 聚合窗口

-- 按照性别分组统计年龄和
select sid,sname,age,gander,province,tuition,sum(age) over(partition by gander) from stu;
select sid,sname,age,gander,province,tuition,sum(age) over(partition by province,gander) from stu;
-- order by 排序后会将前面的数据进行累加
select sid,sname,age,gander,province,tuition,sum(tuition) over(partition by gander order by age) from stu;
select sid,sname,age,gander,province,tuition,avg(tuition) over(partition by gander order by age) from stu;

3-2 取值窗口

-- 向上一行取值
select empno,ename,lag(ename) over() from emp;
-- 向上两行行取值
select empno,ename,lag(ename,2) over() from emp;
-- 去不到值给默认值
select empno,ename,lag(ename,2,'itcast') over() from emp;
-- 向下一行取值
select empno,ename,lead(ename) over() from emp;
-- 向下两行行取值
select empno,ename,lead(ename,2) over() from emp;
-- 去不到值给默认值
select empno,ename,lead(ename,2,'itcast') over() from emp;-- 分组后只进行组内查找
select *,lag(ename) over(partition by job) from emp;-- 取第一个值
select *,first_value(ename)over(partition by job order by sal desc ) from emp;

3-3 排序窗口

按照指定字段排序后生成序号

-- 排序
select *,rank() over (order by sal)  as rnk, -- 如果有并列生成序号是不连续的dense_rank()  over (order by sal)  as dernk, -- 生成连续序号row_number() over (order by sal) as rw -- 生成行号
from emp;-- 查找薪资前三
select * from (
select *,dense_rank()  over (order by sal desc )  as dernk -- 生成连续序号
from emp) tb1
where dernk <=3;

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

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

相关文章

K8S哲学 - 常见的资源类型

资源类型 namespace kubectl apply 和 kubectl create kubectl apply是声明式的 和 kubectl create是命令式的对吗 deployment 和 job的区别 k8s 的 lable 的意义

【YUNBEE云贝-进阶课】MySQL8.0性能优化实战培训

众多已经学习过MySQL 8.0 OCP认证专家的课程的同学们对 MySQL 8.0 的安装部署、体系结构、配置监控、用户管理、主从复制、系统运维、MGR等基础操作和动手实验有了一定的学习基础.很多学员反馈希望更进一步提升技术能力、解决工作中碰到的性能问题。 针对MySQL8.0的数据库性能优…

DMA的认识

DMA介绍 Q:什么是DMA&#xff1f; DMA( Direct Memory Access&#xff0c;直接存储器访问 ) 提供在 外设与内存 、 存储器和存储器 、 外设 与外设 之间的 高速数据传输 使用。它允许不同速度的硬件装置来沟通&#xff0c;而不需要依赖于 CPU &#xff0c;在这个时间中&am…

go的标准化error处理

go的标准化error处理 文章目录 go的标准化error处理1、建议这样写错误处理2、怎么优化代码让其不再堆积 1、建议这样写错误处理 // 1 func autn() {var err errorif err ! nil {// handle err}//do stuff }// 2 func a(r *http.Request) error {//err : r.ParseForm()//if er…

算法打卡day34

今日任务&#xff1a; 1&#xff09;62.不同路径 2&#xff09;63.不同路径 II 3&#xff09;复习day10 62.不同路径 题目链接&#xff1a;62. 不同路径 - 力扣&#xff08;LeetCode&#xff09; 一个机器人位于一个 m x n 网格的左上角 &#xff08;起始点在下图中标记为 “S…

数据中心的网络架构设计,打造高效、安全的数字底座

数据中心的网络架构设计 一、数据中心网络架构设计原则 网络,作为数据中心的核心支柱,其结构精妙,由众多二层接入设备与少量三层设备共同编织而成。过去,数据中心网络规模有限,仅凭数十台设备的简单互连便能实现信息的畅通无阻。然而,随着技术与应用需求的飞速增长,数据…

Missing artifact org.opencv:opencv:jar:4.10.0 [opencv-4.10.0.jar]

Missing artifact org.opencv:opencv:jar:4.10.0 [opencv-4.10.0.jar] https://mvnrepository.com/artifact/org.opencv/opencv 根本就没有 找了个旧项目的opencv-410.jar修改下opencv-4.10.0.jar放到目录下面就好了 D:\localRepository\org\opencv\opencv\4.10.0 OpenCV-C…

[HDFS 相关Shell命令]

目录 HDFS 相关Shell命令: 相关文件操作命令: HDFS 相关Shell命令: 注意&#xff0c;下述命令中的<path>代表文件或目录的路径&#xff0c;<local_path>代表本地文件系统的路径&#xff0c;而<hdfs_path>代表HDFS上的路径。使用这些命令时&#xff0c;需要…

类的六个默认成员函数(上)

目录 构造函数 析构函数 拷贝构造函数 对于日期&#xff08;Date&#xff09;类&#xff0c;可以通过 Init 公有方法给对象设置日期&#xff0c;但如果每次创建对象时都调用该方法设置信息&#xff0c;未免有点麻烦&#xff0c;那能否在对象创建时&#xff0c;就将信息设置进…

鸿蒙OS开发学习:【第三方库调用】

介绍 本篇Codelab主要向开发者展示了在Stage模型中&#xff0c;如何调用已经上架到[三方库中心]的社区库和项目内创建的本地库。效果图如下&#xff1a; 相关概念 [Navigation]&#xff1a;一般作为Page页面的根容器&#xff0c;通过属性设置来展示页面的标题、工具栏、菜单。…

使用低空无人机图像对树种进行实例分割

在这项试点研究中,利用低空无人机图像开发了一种针对当地树种的机器学习实例分割模型,用于生态调查目的。实例分割包括个体树冠描绘和物种分类。 20 种树种及其相关学名已通过无人机图像进行了训练和收集,用于机器学习过程。为了评估 ML 模型的准确性,半监督分割图像将与已…

政安晨:【Keras机器学习实践要点】(二十七)—— 使用感知器进行图像分类

目录 简介 设置 准备数据 配置超参数 使用数据增强 实施前馈网络&#xff08;FFN&#xff09; 将创建修补程序作为一个层 实施补丁编码层 建立感知器模型 变换器模块 感知器模型 编译、培训和评估模式 政安晨的个人主页&#xff1a;政安晨 欢迎 &#x1f44d;点赞✍…

Lua语法(五)——垃圾回收

参考链接: 系列链接: Lua语法(一) 系列链接: Lua语法(二)——闭包/日期和时间 系列链接: Lua语法(三)——元表与元方法 系列链接: Lua语法(四)——协程 系列链接: Lua语法(五)——垃圾回收 系列链接: Lua语法(六)——面相对象编程 Lua语法 五——垃圾回收 垃圾回收弱引用表__m…

并查集(Union-Find)介绍

并查集是一种树型的数据结构&#xff0c;用于处理一些不相交集合&#xff08;Disjoint Sets&#xff09;的合并及查询问题。它常常在某些图论算法中被使用&#xff0c;比如求解最小生成树的Kruskal算法中就需要使用并查集来判断加入某条边后是否会形成环。在实际应用中&#xf…

2024届数字IC秋招-华为机试-数字芯片-笔试真题和答案(五)(含2022年和2023年)

文章目录 前言1、多比特信号A,时钟域clk_a存在从4’d11到4’d12的变化过程中,若时钟域clk_b直接采用D触发器采样,可能采样到数据是2、Bod由1变成0,Arb会如何变化3、减少片外DRAM的访问,而代之以片内SRAM访问,这样可以降低访问功耗,降低片外DRAM,同时加大片内SRAM能节省…

Spring Boot集成Graphql快速入门Demo

1.Graphql介绍 GraphQL 是一个用于 API 的查询语言&#xff0c;是一个使用基于类型系统来执行查询的服务端运行时&#xff08;类型系统由你的数据定义&#xff09;。GraphQL 并没有和任何特定数据库或者存储引擎绑定&#xff0c;而是依靠你现有的代码和数据支撑。 优势 GraphQL…

【Android】【root remount】adb su如何添加密码校验

前言 客户想在user版本添加su 权限&#xff0c;并实现user版本的root remount功能。 当前思路时执行su时添加密码&#xff0c;如果密码正确设置 sys.变量为true。adb root时判断sys变量为true时&#xff0c;执行root动作。 su 添加密码实现 su.cpp 添加密码部分&#xff1a;…

npm install 报 ERESOLVE unable to resolve dependency tree 异常解决方法

问题 在安装项目依赖时&#xff0c;很大可能会遇到安装不成功的问题&#xff0c;其中有一个很大的原因&#xff0c;可能就是因为你的npm版本导致的。 1.npm ERR! code ERESOLVE npm ERR! ERESOLVE unable to resolve dependency tree 2.ERESOLVE unable to resolve dependenc…

【C++之queue的应用及模拟实现】

C学习笔记---014 C之queue的应用及模拟实现1、queue的简单介绍2、queue的简单接口应用3、queue的模拟实现3.1、queue的结构一般的构建3.2、queue的适配器模式构建3.3、queue的主要接口函数 4、queue的模拟实现完整代码4.1、一般方式4.2、泛型模式 5、queue巩固练习题5.1、最小栈…

VSCode中 task.json 和 launch.json 的作用和参数解释以及配置教程

前言 由于 VS Code 并不是一个传统意义上的 IDE&#xff0c;所以初学者可能在使用过程中会有很多的疑惑&#xff0c;其中比较常见的一个问题就是 tasks.json和 launch.json两个文件分别有什么作用以及如何配置 tasks.json VSCode 官网提供的 tasks.json 配置教程 使用不同的…