2023.11.13 hive数据仓库之分区表与分桶表操作,与复杂类型的运用

 

目录

 

0.hadoop hive的文档

1.一级分区表

2.一级分区表练习2

 3.创建多级分区表

4.分区表操作

5.分桶表

6. 分桶表进行排序

7.分桶的原理

 8.hive的复杂类型

9.array类型: 又叫数组类型,存储同类型的单数据的集合

 10.struct类型: 又叫结构类型,可以存储不同类型单数据的集合

 11.map类型: 又叫映射类型,存储键值对数据的映射(根据key找value)


0.hadoop hive的文档

hive文档: https://cwiki.apache.org/confluence/display/Hive/Configuration+Properties
hdfs文档: https://hadoop.apache.org/docs/stable/hadoop-project-dist/hadoop-hdfs/hdfs-default.xml
yarn文档: https://hadoop.apache.org/docs/stable/hadoop-yarn/hadoop-yarn-common/yarn-default.xml
mr文档: https://hadoop.apache.org/docs/stable/hadoop-mapreduce-client/hadoop-mapreduce-client-core/mapred-default.xml

1.一级分区表

创建分区表: create [external] table [if not exists] 表名(字段名 字段类型 , 字段名 字段类型 , ... )partitioned by (分区字段名 分区字段类型)... ; 

自动生成分区目录并插入数据: load data [local] inpath '文件路径' into table 分区表名 partition (分区字段名='值'); 

注意: 如果加local后面文件路径应该是linux本地路径,如果没有加那么就是hdfs文件路径

建表

create database hive3;
use hive3;
--练习1 创建一级分区表
-- 创建分区表: create [external] table [if not exists]
-- 表名(字段名 字段类型 , 字段名 字段类型 , ... )
-- partitioned by (分区字段名 分区字段类型) ;

-- 自动生成分区目录并插入数据: load data [local] inpath
-- '文件路径' into table 分区表名 partition (分区字段名='值');

--建表
create table if not exists
score (name string,subject string,grade int)
partitioned by (dt string)
row format delimited
fields terminated by '\t'
;

此为score.txt文件 

--在hdfs的网页中手动上传score.txt到目录,下面每一次load data都会把这个文件移动
-- 加载数据
load data inpath '/score.txt' into table score partition (dt='2022');
load data inpath '/score.txt' into table score partition (dt='2023');
load data inpath '/score.txt' into table score partition (dt='2024');

--查询数据
select  * from score;--此时dt的三个年份都存在了这个表里
--如,查询2023年的数据,效率提升
select * from score where dt = '2023';
select * from score where dt = '2022';
select * from score where dt = '2024';

 

可以直接根据年份作为条件来查询表的内容,结果如下

 

2.一级分区表练习2

 1.建表

--练习2建一个新表,-- 创建分区表: create [external] table [if not exists]
-- 表名(字段名 字段类型 , 字段名 字段类型 , ... )
-- partitioned by (分区字段名 分区字段类型) ;
--建表
create table one_part_order(
    oid string,
    name string,
    price double,
    num int
)partitioned by (year string)
row format delimited
fields terminated by ' ';

2.加载数据

四个order.txt文件如下

--加载数据,现在hdfs中准备好文件,再使用load加载数据到分区表中
load data inpath '/itcast/order202251.txt'
    into table one_part_order partition (year='2022');

load data inpath '/itcast/order2023415.txt'
    into table one_part_order partition (year='2023');

load data inpath '/itcast/order202351.txt'
    into table one_part_order partition (year='2023');

load data inpath '/itcast/order202352.txt'
    into table one_part_order partition (year='2023');

 3.验证数据

select * from one_part_order ;
select * from one_part_order where year = '2023';

 

 3.创建多级分区表

创建分区表: create [external] table [if not exists] 表名(字段名 字段类型 , 字段名 字段类型 , ... )partitioned by (一级分区字段名 分区字段类型, 二级分区字段名 分区字段类型 , ...) ; 

自动生成分区目录并插入数据: load data [local] inpath '文件路径' into table 分区表名 partition (一级分区字段名='值',二级分区字段名='值' , ...);

注意: 如果加local后面文件路径应该是linux本地路径,如果没有加那么就是hdfs文件路径

 1.建表


--删表
drop table more_part_order;
truncate table multi_part_order;
--建表
create table multi_part_order(
    oid string,
    pname string,
    price double,
    num int)partitioned by (year string,month string,day string)
row format delimited
fields terminated by ' ';

或者

--建表
create table multi_part1_order(
    oid string,
    pname string,
    price double,
    num int)partitioned by (year string,month string,day string)
row format delimited
fields terminated by ' ';

 

2.加载数据

--加载数据
load data inpath '/itcast/order202251.txt' into table
    multi_part_order partition (year='2022',month='05',day='01');

load data inpath '/itcast/order2023415.txt' into table
    multi_part_order partition (year='2023',month='04',day='15');

load data inpath '/itcast/order202351.txt' into table
    multi_part_order partition (year='2023',month='05',day='01');

load data inpath '/itcast/order202352.txt' into table
    multi_part_order partition (year='2023',month='05',day='02');

或者

 

--加载数据
load data inpath '/itcast/order202251.txt' into table
    multi_part1_order partition (year='2022',month='2022-05',day='2022-05-01');

load data inpath '/itcast/order2023415.txt' into table
    multi_part1_order partition (year='2023',month='2023-04',day='2023-04-15');

load data inpath '/itcast/order202351.txt' into table
    multi_part1_order partition (year='2023',month='2023-05',day='2023-05-01');

load data inpath '/itcast/order202352.txt' into table
    multi_part1_order partition (year='2023',month='2023-05',day='2023-05-02');

 3.验证数据

--验证数据
select * from multi_part1_order;
select * from multi_part1_order where day = '2023-05-01';

 需求1:查询日期为2023年5月1日的商品

 需求2:统计日期2023年5月1日的商品销售额

--统计2023年5月1日,商品的销售额
select sum(price*num)  as money from multi_part_order
                      where year='2023'and month='05'and day='01';

 

4.分区表操作

 添加,删除

--------------------------------分区表操作------------------------------
--添加分区:alter table 分区表名 add partition (分区字段名='值',....);
select * from multi_part1_order;
alter table multi_part1_order add partition (year='2024',month='5',day='01');
--删除分区:alter table 分区表名 drop partition(分区字段名='值',....);
alter table multi_part1_order drop partition (year='2024');
alter table multi_part1_order drop partition (year='2024',month='5',day='01');
alter table multi_part1_order drop partition (year='2024',month='5');

 修改

--修改分区:alter table 分区表名 partition  (分区字段名='旧值' , ...)rename to partition (分区字段名='新值' , ...);
alter table multi_part1_order partition (year='2024',month='5',day='01')
    rename to partition (year='2030',month='5',day='01');
--本质上是改了原本day01,被移动.并新增了year=2024的目录

查看

-- 查看所有分区: show partitons 分区表名;
show partitions multi_part1_order;
-- 同步/修复分区: msck repair table 分区表名;
msck repair table multi_part1_order;

5.分桶表

创建基础分桶表:  
create [external] table [if not exists] 表名(字段名 字段类型 )clustered by (分桶字段名) 

into 桶数量 buckets ;

 1.建表

- 创建基础分桶表:
-- create [external] table [if not exists] 表名(字段名 字段类型)clustered by (分桶字段名)into 桶数量 buckets ;
--建表
create table course_base(
    cid int,
    cname string,
    sname string
)clustered by (cid) into 3 buckets
row format delimited fields terminated by '\t';

2.加载数据

--加载数据
load data inpath '/itcast/course.txt'into table course_base;

3.验证数据

--验证数据
select * from course_base;
--取余数:12/3余0, 9/3余0 , 6/3余0 , 3/3余0  ,1/3余1 ,13/3余1....

6. 分桶表进行排序

--创建基础分桶表,要求分3个桶,桶内根据cid排序
-- 创建基础分桶表,然后桶内排序:
-- create [external] table [if not exists] 表名(字段名 字段类型)
-- clustered by (分桶字段名)sorted by(排序字段名 asc|desc)   # 注意:asc升序(默认) desc降序
-- into 桶数量 buckets ;

1.创建表

--创建分桶表
truncate table course_sort;
create table course_sort(
    cid int,
    cname string,
    sname string
)clustered by (cid) sorted by (cid desc )into 3 buckets
row format delimited fields terminated by '\t';

2.加载数据

--加载数据
load data inpath '/input/course.txt'into table course_sort;
 

3. 验证数据

--验证数据
select * from course_sort ;
--生成的三个文件,000000_0   ,    000001_0,     000002_0
--验证余数:12/3=0 9/3=0 6/3=0 3/3=0
-- 13/3=1 10/3=1 7/3=1 4/3=1 1/3的结果是0,余1
-- 14/3=2 11/3=2 8/3=2 5/3=2

 

7.分桶的原理

分桶原理: 
如果是数值类型分桶字段: 直接使用数值对桶数量取模   
如果是字符串类型分桶字段: 底层会使用hash算法计算出一个数字然后再对桶数量取模

Hash: Hash是一种数据加密算法,其原理我们不去详细讨论,我们只需要知道其主要特征:同样的值被Hash加密后的结果是一致的
举例: 字符串“binzi”被Hash后的结果是93742710(仅作为示意),那么无论计算多少次,字符串“binzi”的结果都会是93742710。
计算余数: hash('binzi')%3==0  
注意: 同样的数据得到的结果一致,如’binzi’ hash取模结果是0,无论计算多少次,它的取模结果都是0

 8.hive的复杂类型

---------------------------复杂类型建表格式------------------------
-- 复杂类型建表格式:
        [row format delimited] # hive的serde机制
        [fields terminated by '字段分隔符'] # 自定义字段分隔符固定格式
        [collection ITEMS terminated by '集合分隔符'] # 自定义array同类型集合和struct不同类型集合
        [map KEYS terminated by '键值对分隔符'] # 自定义map映射kv类型
        [lines terminated by '\n'] # # 默认即可
        hive复杂类型:   array  struct  map

9.array类型: 又叫数组类型,存储同类型的单数据的集合

-- array类型: 又叫数组类型,存储同类型的单数据的集合
--      建表指定类型:  array<数据类型>
--      取值: 字段名[索引]   注意: 索引从0开始
--      获取长度: size(字段名)
--      判断是否包含某个数据: array_contains(字段名,某数据)

 需求: 已知data_for_array_type.txt文件,存储了学生以及居住过的城市信息,要求建hive表把对应的数据存储起

1.创建表

----建表,
create table test_array_1(
   name string,
   location array<string>
)row format delimited
fields terminated by '\t'
collection items terminated by ',';

2.加载数据

--加载数据
load data inpath '/itcast/data_for_array_type.txt' into table test_array_1;

 3.验证数据

--验证数据
select * from test_array_1;
--zhangsan,"[""beijing"",""shanghai"",""tianjin"",""hangzhou""]"
--wangwu,"[""changchun"",""chengdu"",""wuhan"",""beijin""]"

4.需求:查询张三是否在天津住过?

select array_contains(location,'tianjin')from test_array_1 where name = 'zhangsan';
--结果:true

5. 需求:查询张三的地址有几个?

select size(location)from test_array_1 where name = 'zhangsan';
--结果:4

6.需求:查询王五的第二个地址?

select location[1] from test_array_1 where name = 'wangwu';
--结果:chengdu

 10.struct类型: 又叫结构类型,可以存储不同类型单数据的集合

--   建表指定类型: struct<子字段名1:数据类型1, 子字段名2:数据类型2 , ...>
--      取值: 字段名.子字段名n

1.建表

-- 建表
create table test_struct_1(
    id int,
    name_info struct<name:string,age:int>
)row format delimited fields terminated by '#'
collection items terminated by ':';

2.加载数据

--加载数据
load data inpath '/itcast/data_for_struct_type.txt' into table test_struct_1;

3.验证数据

--验证数据
select * from test_struct_1;

 

需求1:查询所有用户姓名

select name_info.name from test_struct_1;

需求2:查询所有的用户年龄

select name_info.age from test_struct_1;

需求3:查询所有用户的平均年龄

 

 11.map类型: 又叫映射类型,存储键值对数据的映射(根据key找value)

--  建表指定类型: map<key类型,value类型>
--     取值: 字段名[key]
--     获取长度: size(字段名)
--     获取所有key: map_keys()
--     获取所有value: map_values()

1.创建表

--创建表
create table test_map_1(
    id int,
    name string,
    members map<string,string>,
    age int
)row format delimited
fields terminated by ','
collection items terminated by '#'
map keys terminated by ':';

2.加载数据

-- 加载数据
load data inpath '/itcast/data_for_map_type.txt'into table test_map_1;

3.验证数据

--验证数据
select * from test_map_1;
-- 1,林杰均,"{""father"":""林大明"",""mother"":""小甜甜"",""brother"":""小甜""}",28
-- 2,周杰伦,"{""father"":""马小云"",""mother"":""黄大奕"",""brother"":""小天""}",22
-- 3,王葱,"{""father"":""王林"",""mother"":""如花"",""sister"":""潇潇""}",29
-- 4,马大云,"{""father"":""周街轮"",""mother"":""美美""}",26

 需求1:查询每个学生的家庭成员关系(就是所有的key)

select name,map_keys(members) from test_map_1;

需求2:查询每个学生的家庭成员姓名(就是所有的value)

select name ,map_values(members) from test_map_1;

需求3:查询每个学生和对应的父亲名字

select name,members['father'] as father from test_map_1;

需求4:查询马大云是否有兄弟

select name,array_contains(map_keys(members),'brother') from test_map_1 where name ='马大云';

 

-- 需求5:查询每个学生的对应brother姓名,没有brother的学生null补全

-- 需求6:查询每个学生的对应brother姓名,没有brother的学生直接不显示

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

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

【函数讲解】pygmo中的函数 fast_non_dominated_sorting() + 利用支配关系,学习一个SVM分类器,将解分为两类

这个函数是用来执行非支配排序的&#xff0c;可以分层构建Pareto&#xff0c;并返回每一层的解以及每个解支配其他解的索引、解被其他解支配的次数、解所在的非支配层级。这个函数对这些解进行非支配排序&#xff0c;并返回四个数组&#xff1a;ndf, dl, dc, 和 ndr。 ndf (Non…

CentOS7、CentOS8 如何修改ip信息(修改网络信息)(无图形界面)(亲测可用)

文章目录 CentOS 7方法一&#xff1a;使用 nmcli 命令方法二&#xff1a;编辑配置文件&#xff08;我的CentOS7是使用这种方法&#xff0c;亲测可用&#xff09; CentOS 8方法一&#xff1a;使用 nmcli 命令方法二&#xff1a;编辑配置文件 在 CentOS 系统中&#xff0c;如果你…

【论文精读】DMVSNet

今天读的是一篇发表在ICCV 2023上的文章&#xff0c;作者来自华中科技大学。 文章地址&#xff1a;点击前往 项目地址&#xff1a;Github 文章目录 Abstract1 Introduction2 Relative Work3 Motivation3.1 Estimated bias and interpolated bias3.2 One-sided V.S. Saddle-shap…

怎么做到高性能网络IO?

为什么要做高性能网络IO。主要是解决c10&#xff0c;c10M问题 最开始的时候我们走的内核协议栈&#xff0c;走内核协议栈其实性能比较低&#xff0c;因为我们之前介绍的时候需要拷贝两次 但是我们采用用户态协议栈可以少拷贝一次&#xff0c;可以大大提高效率&#xff0c; 步骤…

基于粒子群算法优化概率神经网络PNN的分类预测 - 附代码

基于粒子群算法优化概率神经网络PNN的分类预测 - 附代码 文章目录 基于粒子群算法优化概率神经网络PNN的分类预测 - 附代码1.PNN网络概述2.变压器故障诊街系统相关背景2.1 模型建立 3.基于粒子群优化的PNN网络5.测试结果6.参考文献7.Matlab代码 摘要&#xff1a;针对PNN神经网络…

职业迷茫,我该如何做好职业规划

案例25岁男&#xff0c;入职2月&#xff0c;感觉自己在混日子&#xff0c;怕能力没有提升&#xff0c;怕以后薪资也提不起来。完全不知道应该往哪个方向进修&#xff0c;感觉也没有自己特别喜欢的。感觉自己特别容易多想&#xff0c;想多年的以后一事无成的样子。 我觉得这个案…

Tektronix(泰克)示波器TBS1102B测试电压

对于 Tektronix TBS1102B 示波器来说&#xff0c;测试电压的步骤基本如下&#xff1a; 连接测量点&#xff1a; 将被测电路的测量点连接到示波器的输入通道。使用正确的探头并确保连接的极性正确。 选择通道&#xff1a; 选择示波器上的通道&#xff0c;你想要测量的电压可能连…

20231112_DNS详解

DNS是实现域名与IP地址的映射。 1.映射图2.DNS查找顺序图3.DNS分类和地址4.如何清除缓存 1.映射图 图片来源于http://egonlin.com/。林海峰老师课件 2.DNS查找顺序图 3.DNS分类和地址 4.如何清除缓存

单链表按位序与指定结点 删除

按位序删除(带头结点) #define NULL 0 #include<stdlib.h>typedef struct LNode {int data;struct LNode* next; }LNode, * LinkList;//按位序删除&#xff08;带头结点&#xff09; bool ListInsert(LinkList& L, int i, int& e) {if (i < 1)return false;L…

【Spring Cloud】声明性REST客户端:Feign

Spring Cloud Feign ——fallback 服务降级 1. Feign 简介2. Feign 的基础使用2.1 普通 HTTP 请求2.2 Feign 远程调用上传文件接口 1. Feign 简介 Feign 是一个声明式的 HTTP 客户端&#xff0c;它简化了编写基于 REST 的服务间通信代码的过程。在 Spring Cloud 中&#xff0c…

如何从零开始手写一个消息中间件(从宏观角度理解消息中间件的技术原理)

如何从零开始手写一个消息中间件&#xff08;从宏观角度理解消息中间件的技术原理&#xff09; 什么是消息中间件消息中间件的作用逐一拆解消息中间件的核心技术消息中间件核心技术总览IOBIONIOIO多路复用AIOIO多路复用详细分析selectpollepoll Java中的IO多路复用 协议序列化消…

【算法每日一练]-快速幂,倍增,滑动窗口(保姆级教程 篇1) #麦森数 #青蛙跳

之前是考试准备&#xff0c;所以有几天没更新&#xff0c;今天开始继续更新 目录 快速幂模板 题目&#xff1a;麦森数 思路&#xff1a; 题目&#xff1a;青蛙跳 思路&#xff1a; 快速幂模板 #include <bits/stdc.h> #define ll long long using namespa…

.net在使用存储过程中IN参数的拼接方案,使用Join()方法

有时候拼接SQL语句时&#xff0c;可能会需要将list中的元素都加上单引号&#xff0c;并以逗号分开&#xff0c;但是Join只能简单的分开&#xff0c;没有有单引号&#xff01; 1.第一种拼接方案 List<string> arrIds new List<string>(); arrIds.Add("aa&qu…

JavaScript_动态表格_删除功能

1、动态表格_删除功能 <!DOCTYPE html> <html lang"en"> <head><meta charset"UTF-8"><title>动态表格_添加和删除功能</title><style>table{border: 1px solid;margin: auto;width: 100%;}td,th{text-align: …

杂记 | 使用FRP搭建内网穿透服务(新版toml配置文件,搭配反向代理食用)

文章目录 01 需求与回顾02 下载程序包03 编辑.toml文件3.1 编辑frps.toml3.2 编辑frpc.toml 04 启动服务4.1 启动服务端4.2 启动客户端 05 配置反向代理&#xff08;可选&#xff09;06 windows设置为默认启动&#xff08;可选&#xff09;6.1 创建启动脚本6.2 设置为开机自启 …

【Java 进阶篇】Java与JQuery选择器:解锁前端开发的魔法大门

在前端开发的世界中&#xff0c;选择器是我们与HTML文档进行互动的钥匙&#xff0c;而Java和JQuery则为我们提供了强大的工具&#xff0c;使得前端开发不再是一个艰深的谜题。本篇博客将围绕Java与JQuery选择器展开&#xff0c;深入解析选择器的奥秘&#xff0c;为你打开前端开…

你一定要学会的Java语法 -- 【继承】

书接上回&#xff0c;我们已经学完了类和对象&#xff0c;今天内容可能有一点难&#xff0c;相信自己能跨过这道坎。 目录 一. 继承 1.什么是继承 2. 继承的概念 3. 继承的语法 4.父类成员访问 子类和父类成员变量同名 子类和父类成员方法同名 5.super关键字 6.子类构…

003、Nvidia Jetson Nano Developer KIT(b01)-深度学习环境配置

之——深度学习环境 杂谈 网上到处淘金&#xff0c;pytorch、opencv、torchvision。 正文 1.各种依赖库 1.1 pytorch的底层依赖库 sudo apt install build-essential make cmake cmake-curses-gui -ysudo apt install git g pkg-config curl -ysudo apt install libatlas-ba…

Java图像编程之:Graphics

一、概念介绍 1、Java图像编程的核心类 Java图像编程的核心类包括&#xff1a; BufferedImage&#xff1a;用于表示图像的类&#xff0c;可以进行像素级的操作。Image&#xff1a;表示图像的抽象类&#xff0c;是所有图像类的基类。ImageIcon&#xff1a;用于显示图像的类&a…

计算机中丢失msvcr120.dll文件怎么修复?找不到msvcr120.dll五种完美修复方案

今天我想和大家分享的是关于“msvcr120.dll丢失的问题的5个解决方法”。在我们日常的工作生活中&#xff0c;或许大家都曾遇到过这样的问题&#xff0c;那么&#xff0c;了解它的解决方法是非常必要的。 首先&#xff0c;让我们来了解一下msvcr120.dll是什么文件。简单来说&am…
最新文章