一.DDL数据库定义操作
--创建数据库
create database [if not exit] database_name
[comment database_comment]--针对当前库的描述信息(也就是库的注释)
[location hfds_path]--指定当前库在hdfs中对应的位置
[with dbproperties(property_name=property_value)]--针对当前库加一些结构化的描述信息
--1.创建数据库
create table stu;
create table stu2
comment "mystutable"
location '/stu'
wirh dbproperties('author'='hahaha','createtime'='2022.04.14');
2.查看数据库详情
desc database extended stu2;
3.切换库
use stu2;
4.修改库
alter database stu2 set dbproperties("author"="niuniuzi");
5.删除库
drop database stu2;(空库)
drop database stu2 cascade;(删除非空库)
二.表的相关操作
--建表的语法结构
create [external(创建外部表)] table student;
[comment table_comment]--指定当前表的描述信息
[partitioned by ]--指定分区字段
[clustered by]--指定分桶字段
[sorted by]-- 指定排序字段(一般情况不加,效率很差)
[row format delimited fields terminated by "xxx"]--指定集合数据类型中的元素之间的分隔符
[collection items terminated by "xxx"]--指定集合数据类型中的元素之间的分割符
[map keys terminated by "xxx"]--指定map结构中的key和value之间的分割符
[lines terminated by "\n"]--行与行之间的分隔符
[stored as file_foemat]--指定当前表的存储文件格式,默认textfile
[localtion hdfs_path]-- 指定当前表在hdfs中的对应的位置
[tableproperties ....]--针对当前表加一些结构化的描述信息
[as select_statement]--根据一个查询结果建表,这种情况就不用自定义字段,直接使用查询结果中的字段
三.练习
--1.建一个内部表
create table coolboy(name string,score int)
row format delimited fields terminated by "\t";
--2.创建外部表
create external table pretygirl(name string, score int)
row foramt delimited fields terminated by "\t";
--3.查看表详情
desc coolboy;
desc formatted student;
--4.内部表和外部表的互相转化
alter table student set tblproperties('external'='true');(内部表转化成外部表)
alter table student set tblproperties('external'='false');(外部表转化成内部表)
4.修改表
4.1修改表名
alter table student rename to stu;
4.1.1针对表的列的修改
--新增列
alter table stu add cloums(age int,num int score int);
--修改列
alter table stu change colums num num1 bigint;
--替换列
alter table stu replace columns(ids int,name1 string);
--删除表
drop table stu;
二.DML数据操作
--导入
1.load数据导入
1.1(追加导入)
load data local inpath '/opt/module/hive/datas/student.txt'
into table student;
1.2(覆盖导入)
load data local inpath '/opt/module/hive/datas/student.txt'
overwrite into table student;
1.3导入数据(不加local)
local data inpath "/student2.txt"
overwrite into table student;
1.4其他导入方式
--基本模式插入(根据单张表查询结果导入)
load data local inpath '/opt/module/hive/datas/student.txt'
into table student;
select * from student1;
--覆盖导入
insert overwrite table student
select * from student1;
--追加导入
insert into table student
select * from student1;
--查询语句中创建表并加载数据(as select)
create table student2
as select * from student;
--创建表时通过location指定加载数据路径
create table student3(id int,name string)row format delimited fields terminated by '\t'
location '/student3';
--导出
1. Insert导出
-- 语法结构
1.1 将查询结果导出到本地指定的目录
insert overwrite local directory '/opt/module/hive-3.1.2/datas/export/student'
select * from student;
1.2 将查询的结果格式化导出到本地
insert overwrite local directory '/opt/module/hive-3.1.2/datas/export/student'
row format delimited fields terminated by "\t"
select * from student;
1.3 将查询的结果导出到HDFS上(没有local)
insert overwrite directory '/export/student'
row format delimited fields terminated by "\t"
select * from student;
2. Export导出到HDFS上
export table student1 to '/export/student1';
3. Import数据导入到指定Hive表中
import table bucunzai from '/export/student1';