当前位置: 首页 > news >正文

Hive建表高阶语句

CTAS -as select方式建表

CREATE TABLE ctas_employee as SELECT * FROM employee;

CTE (CTAS with Common Table Expression)

CREATE TABLE cte_employee AS
WITH
r1 AS  (SELECT name FROM r2 WHERE name = 'Michael'),
r2 AS  (SELECT name FROM employee WHERE gender= 'Male'),
r3 AS  (SELECT name FROM employee  WHERE gender = 'Female')
SELECT * FROM r1 UNION ALL SELECT * FROM r3;

LIKE

CREATE TABLE employee_like LIKE employee;

创建临时表--temporary

临时表是应用程序自动管理在复杂查询期间生成的中间数据的方法

表只对当前session有效,session退出后自动删除

表空间位于/tmp/hive-<user_name>(安全考虑)

如果创建的临时表表名已存在,实际用的是临时表

CREATE TEMPORARY TABLE tmp_table_name1 (c1 string);
CREATE TEMPORARY TABLE tmp_table_name2 AS..
CREATE TEMPORARY TABLE tmp_table_name3 LIKE..

清空表数据--truncate

TRUNCATE TABLE employee; 

创建动态分区

---动态分区需设定属性
set hive.exec.dynamic.partition=true; ---是否开启动态分区
set hive.exec.dynamic.partition.mode=nonstrict;
---建表
create table employee_dt(name         string,workplace    array<string>,skills_score map<string,int>,depart_title map<string,string>
) partitioned by (gender string,age int )row format delimited fields terminated by ','collection items terminated by '-'map keys terminated by ':'lines terminated by "\n";

动态表插入数据

/*将employeept中的数据导入到employee_dt表中*/
insert into table employee_dt partition (age,gender)
select name, workplace, skills_score, depart_title,gender,agefrom employee2;

查询分区

show partitions employee_dt;

添加分区

alter table employee_dtadd partition (gender = "Male",age = 28);

删除分区

alter table employee_dtdrop partition (gender = "Male",age = 28);

修改表(alter针对元数据)

改名

alter table employee rename to new_employee;

修正表文件格式

alter table employee set fileformat rcfile; 

修改列名

alter table employee change name  employee_name string; 

添加列

alter table employee add columns (work string);

替换列

alter table employee replace columns (name string);

Hive视图(view)

视图:通过隐藏子查询、连接和函数来简化查询的逻辑结构;只保存定义,不存储数据;如果删除或更改基础表,则查询视图将失败;视图是只读的,不能插入或装载数据

创建视图

create view view_name as select statement;

查看视图定义

show create table view_name;

删除视图

drop view_name;

更改视图属性

alter view view_name set tblproperties ('comment' = 'This is a view');

更改视图定义

alter view view_name as select statement;

Hive侧视图(Lateral View)

select name,wps,gender_age.gender, gender_age.age,skill,score,depart,title
from employeelateral view explode(workplace) work_place as wpslateral view explode(skills_score) sks as skill, scorelateral view explode(depart_title) ga as depart, title;

http://www.lryc.cn/news/34955.html

相关文章:

  • 面向新时代,海泰方圆战略升级!“1465”隆重发布!
  • 带你感受一次JVM调优实战
  • ALG和STUN
  • 原生HTML放大镜
  • C++——模板
  • Chapter2.1:线性表基础
  • Spring源码解析-Spring 循环依赖
  • 从零开始学架构——架构设计的目的
  • Python 异步: 异步生成器(16)
  • .net6 web api使用EF Core,根据model类自动生成表
  • 计算机科学导论笔记(五)
  • 通过命令打Java可执行jar包
  • java基础系列(九) 接口和抽象类
  • Docker启动问题docker is starting…
  • Django/Vue实现在线考试系统-03-开发环境搭建-MySQL安装
  • python实现波士顿房价预测
  • Pinia不酸,保甜
  • uniapp生命周期
  • 经典卷积模型回顾11—Xception实现图像分类(matlab)
  • 移动App性能测试包含哪些内容?App性能测试工具有哪些?
  • AI测试的迷思
  • [ 红队知识库 ] 一些常用bat文件集合
  • Qt广告机服务器(上位机)
  • SOA架构的理解
  • 如何选择一款数据库?
  • week2
  • JavaScript的学习
  • 用gin写简单的crud后端API接口
  • CF大陆斗C战士(三)
  • TTS | 语音合成论文概述