hive(DDL)数据定义

本文介绍了在Hive中创建和管理数据库及数据表的方法,包括创建数据库、查看数据库信息、删除数据库,以及创建内部表和外部表。此外,还讨论了SerDe在数据格式化中的作用,以及如何添加、更新和替换列。同时,提到了数据表的删除、内容清空、重命名以及查询等操作。
摘要由CSDN通过智能技术生成

1.创建数据库

格式

create database [if not exits] database_name [comment "说明内容"]

[location '指定路劲' ] [ with dbproperties( 'create_date'=' 2023-3-13')] #可以任意的

案例:

create database if not exist student location '/user/hive/datas/student';

查看数据信息

desc [extended|formatted] database_name;

删除数据库

语法: drop database database_name;

案例:

drop database student;

2.创建数据表

注:

temporary 内部表 ;external 管理表 \外部表

案例一

创建数据表 默认内部表

 

create table if not exits student(

id int comment '学生学号',

name string comment 'student姓名')

row format delimited files terminated by '\t'

location '/user/hive/student'

删除数据表

drop table student;

当删除内部表时 hadoop上hdfs的数据表路径和数据表不存在 一起删除

创建数据表 默认外部表

create external table if not exits student(

id int comment '学生学号',

name string comment 'student姓名')

row format delimited files terminated by '\t'

location '/user/hive/student' 当删除外部表时 hadoop上hdfs的数据表路径和数据表还存在

serde:

row format delimited[files terminated by '列分隔符']

[lines terminated by '行分隔符']

同时创建数据表和数据

案例01:

Create table student_001 As select * from student;

复制表结构和将查询的数据插入到新的数据表

案例02

Create table student_002 like student; (student是存在的数据表)

将查询的数据插入到新的数据表

insert into student_002 select * from student;

删除数据表

Drop table table_name;

清空数据表内容

truncate table table_name;

查询数据

selcet * from student;

修改数据表表名

** Alter table** student_001 rename to student001;

修改列信息

1.增加列

alter table table_name add columns (colu_name col_type ...)[comment '说明']

案例:

alter table student add columns(sex string);

2.更新列

alter table table_name change col_old_name col_new_name col_new_type

[first/after old_col_name]

案例:将hive中的强制转换类型关闭

set hive.metastore.disallow.incompatible.col.type.changes=false;

alter table student change sex sex varchar(16) after phone;

3.替换列(重新定义数据表的列)

alter table table_name replace columns (col_name col_type...) [comment '说明']

案例:

alter table student replace columns (name string,id int,address string, phone int);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小杨A

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值