HIVE学习(hive基础)

一、HIVE简介

  1. 什么是hive?
    HIVE是一种基于hadoop的一个数据仓库工具,可以将结构化的数据文件映射为一张数据库表,并提供SQL查询功能
    本质:SQL转换为MAPREDUCE程序
    主要用途:用来做离线数据分析,比直接用MapReduce开发效率更高
  2. hive与数据库的区别
    ① 查询语句
    HQLSQL
    ② 数据存储
    HDFS // RAW DEVICE OR LOCAL FS
    ③ 执行器
    MAPREDUCE / EXECUTOR
    ④ 数据插入
    支持批量导入与单挑插入//支持单条或批量导入
    ⑤ 数据操作
    覆盖追加//行级更新删除
    ⑥数据规模
    大/小
    ⑦执行延迟
    高/低
    3.HIVE不支持数据更新与删除是因为hive存储在HDFS中,删除为物理删除,代价较高,只支持覆盖和追加
  3. hive扩展性好是因为可以在多个集群的服务器上做应用开发
  4. hive的读时模块快,是指hive加载数据到表中时不会做数据校验,在读取数据时才校验,它的查询延迟主要浪费在资源调度上,进行任务划分然后进行计算任务的申请

二、hive的数据类型

1、基本数据类型

boolean true/false true
tinyint 1字节的有符号整数 1
smallint 2字节的有符号整数 1
int 4字节的有符号整数 1
bigint 8字节的有符号整数 1
float 4字节单精度浮点数 1.0
double 8字节单精度浮点数 1.0
string 字符串 “abc”
varchar 字符串 “abc”
timestamp 时间戳 1563157873
date 日期 20190715

2、复合数据类型

类型名称 描述 举例
array 字段类型相同的有序字段 array(1,2,3)
map 无序的键值对map(k1,v2,k2,v2) map(‘a’,‘1’,‘b’,‘2’)
struct 一组命名的字段,字段类型可以不同struct(元素1,元素2) struct(‘a’,1,2,0)
select map_key(''),map_values('') from user;
create table complex(
    col1 array<int>,
    col2 map<string,int>,
    col3 struct<a:string,b:int,c:int>
)

三、HIVE的DDL操作

show database;
show database like 'db_hive*';
# 显示数据库详细信息
desc database extended db_hive;
# 切换当前数据库
use db_hive;
# 删除数据库
drop database if exists db_hive;
# 强制删除
drop database if exists bd_hive cascade;

四、创建一个表

1. 建表语句

create(external) table (if not exists) table_name(
 col_name data_type comment "中文名"
)
row format delimited fields terminated by'\t'[指定每一行中字段的分隔符]
stored as orc[指定存储文件类型(sequencefile 二进制序列文件、textfile 文本、rcfile 列式存储格式文件,不指定就默认为文本文件]

(1)查询建表法
通过AS语句,将查询的子结果存在新表里

create table if not exists student1 as select;

like建表法

create table if not exists student2 like select;

(2)分区表的创建
一级分区

create table student_partition1(
  id int,
  name string,
  age int
)
partitioned by (dt string)
row format delimited fields terminated by '\t';

二级分区

create table student_partition1(
  id int,
  name string,
  age int
)
partitioned by (dt string,day string)
row format delimited fields terminated by '\t';

五、修改表结构

1.修改表名

alter tablestudent——partition1 rename to student—p1

2. 列修改或增加

增加列

alter table student add columns(字段名,字段类型)

修改列

alter table student change columns 字段名 更改的类型;

替换列

alter table student replace columns(deptno string,dname string,loc string)
替换表中所有有字段

3. 修改分区

# 添加单个分区
alter table student add partition(dt='20230402');
# 添加多个分区
alter table student add partition(dt='20230402',dt='20230402');
# 删除分区
alter table student drop partition (dt= '20200401'

五、常见函数

六、一对一关联

left join左关联

将左边的表A作为主表,以A表为外循环对右表进行匹配,如果右表没有匹配,就将右表项值为空

right join 右关联

内连接

选择两个表同时出现的项

全连接

选择所有出现的项

查询只有A表的数据

select * from A 
left join B A.ID=B.ID
WHERE B.ID =NULL

相关推荐

  1. HIVE学习hive基础

    2023-12-10 16:24:03       39 阅读
  2. Hive-基础介绍

    2023-12-10 16:24:03       37 阅读

最近更新

  1. TCP协议是安全的吗?

    2023-12-10 16:24:03       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-10 16:24:03       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-10 16:24:03       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-10 16:24:03       20 阅读

热门阅读

  1. React富文本编辑器wangEditor

    2023-12-10 16:24:03       40 阅读
  2. 12.9每日一题(备战蓝桥杯循环结构)

    2023-12-10 16:24:03       27 阅读
  3. 力扣-151. 反转字符串中的单词

    2023-12-10 16:24:03       44 阅读
  4. 聊聊spring.mvc.servlet.load-on-startup

    2023-12-10 16:24:03       34 阅读
  5. HarmonyOS--ArkTS(1)--基本语法(2)

    2023-12-10 16:24:03       40 阅读
  6. K8S学习指南(3)-minikube的安装

    2023-12-10 16:24:03       33 阅读
  7. 从零开始搭建链上dex自动化价差套利程序(10)

    2023-12-10 16:24:03       36 阅读