hive---DDL

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/poppy_rain/article/details/86547797

表操作语句---DDL(建表、修改表等语句)


  • 创建表

create [external] table [if not exists] table_name          {external:外部表}

[(col_name data_type [comment col_comment],...)]           {comment:说明} {每列}

[comment table_comment]           

[partitioned by (col_name data_type [comment col_comment],...)]           {分区表:分区的字段和类型}

[clustered by (col_name,col_name,...) [sorted by (col_name [asc|desc],...)] into num_buckets buckets]          

 {桶表:指定几个桶,按照哪个字段进行hash分桶,按照哪个字段排序}

[row format row_format]           

{一行的格式:例如/n是一行,或者使用内置的格式,delimited默认的行分隔符,

fields terminated by ‘,’  字段分隔符逗号,}

[stored as file_format]          {文件格式,默认textfile}

扫描二维码关注公众号,回复: 5025065 查看本文章

[location hdfs_path]           {创建外部表时用,指明当前外部表的路径}

 

data_type的集合类型:array、map、struct

例子

1. create table student (id int, name string, age int)

row format delimited fields terminated by ',';

执行后生成一个student文件夹( /user/hive/warehouse/db1.db/表名(例如student)/文件 ),添加数据后可生成文件例子:

1,张三,24

2,李四,25

3,王五,22

2. create table student (id int, name string, age int)

partitioned by (country string) 

row format delimited fields terminated by ',';

执行后在表名文件夹后面生成分区文件夹( /user/hive/warehouse/db1.db/表名(例如student)/分区名(例如country=china)/文件 )

3. create table student (id int, name string, age int)

partitioned by (country string) 

clustered by (id) sorted by (age) into 2 buckets 

row format delimited fields terminated by ',';

桶的分配:id除以2取余,余数为0放到一个桶,余数为1放到另一个桶

4. create table b like a;          {创建一个表b,和a表的字段一致,无数据}


  • 修改表:alter table table_name

1. 增加分区:

alter table table_name    add [if not exists] partition_spec [ location 'location1' ] partition_spec [ location 'location2' ] ;

partition_spec:partition (partition_col = partition_col_value, partition_col = partiton_col_value, ...)

例子:alter table student add partition(country='China') partition(country='UK');

           alter table student add partition(country='China') location '/user/hive/warehouse/student' partition(country='UK');

2. 删除分区:

alter table table_name    drop partition_spec, partition_spec,...;

例子:alter table student drop partition(country='China'), partition(country='UK');

3. 重命名表:

alter table table_name    rename to new_table_name;

4. 增加列:

alter table table_name    add columns (col_name data_type [comment col_comment],...)     {增加列}

alter table table_name    replace columns (col_name data_type [comment col_comment],...)     {替换表中所有列}

alter table table_name    change [column] col_old_name col_new_name column_type [comment col_comment] [first | after column_name]     {修改字段和类型}

例子:alter table student add columns (city string);     增加列city

            alter table student change column city school string;     修改字段city为school


  • 其它一些命令:

show databases;     use db1;     show tables;   

show partitions table_name;     show functions;   

create database db2;     drop table table_name;     

truncate table table_name;   清理表

desc table_name;   表字段描述

desc extended table_name;   附带表的详细信息

desc formatted table_name;   将表的详细信息格式为表格形式

!ls;   前面加‘!’,可以和linux本机交互

dfs -ls /;   前面加‘dfs -’,可以和hdfs交互

猜你喜欢

转载自blog.csdn.net/poppy_rain/article/details/86547797