Hive之——HQL 数据定义

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

转载请注明出处:https://blog.csdn.net/l1028386804/article/details/82469740

一、创建表

1、创建Hive管理表

create table student(
name string,
age int,
cource array<string>,
body map<string, float>,
address struct<street:string,city:string,state:string>
)
row format delimited
fields terminated by '\001'
collection items terminated by '\002'
map keys terminated by '\003'
lines terminated by '\n'
stored as textfile;
  • row format delimited fields terminated by 字句指定列分隔符;
  • collection items terminated by 指定了集合元素间的分隔符
  • map keys terminated by 指定了map字段的键值对分隔符
  • lines terminated by 指定了行分隔符
  • stored as 指定了存储的文件格式
create table if not exists test.student(
name string comment 'student.name',
age int comment 'student.age',
cource array<string>,
body map<string, float>,
address struct<streent:string,city:string,state:string>
)
comment 'the info of student'
row format delimited
fields terminated by '\001'
collection items terminated by '\002'
map keys terminated by '\003'
lines terminated by '\n'
stored as textfile
location '/user/hive/warehouse/test.db/student';

2、创建外部表

create external table if not exists test.student(
name string comment 'student.name',
age int comment 'student.age',
cource array<string>,
body map<string, float>,
address struct<streent:string,city:string,state:string>
)
location '/user/test/x'

关键字external指明了该表为外部表,location指明了数据放在HDFS的/user/test/x目录下。
当删除外部表时,Hive会认为没有完全拥有这份数据,所以Hive只会删除该外部表的元数据而不会删除该表的数据。

3、创建分区管理表

create table student_info(
student_id string,
name string,
age int,
sex string,
father_name string,
mother_name string
)
partitioned by(province string, city string);

注意:定义分区的字段不能和定义表的字段重合。对于直接命中分区的查询,Hive不会执行MapReduce查询
通过show partitions sutdent_info来显示student_info的分区情况,通过describe extended student_info来查看分区表的详细信息。

4、创建外部分区表

create external table student_info(
student_id string,
name string,
age int,
sex string,
father_name string,
mother_name string
)
partitioned by(province string, city string);

和普通外部表不同的是,在建表时并没有指定表的存储路径,所以在创建完外部分区表后,如果执行查询语句是查不到任何数据的。这时候需要单独为外部表的分区键指定值和存储位置:

alter table student_info add partition (province = sichuan, city = chengdu) location 'hdfs://192.168.209.121:9000/student/sichuan/chengdu'

从上面可以看出,外部表的目录结构可以完全由自己指定。同其他外部表一样,即使外部分区表被删除,数据也不会被删除。无论是管理表还是外部表,一旦该表存在分区,那么在数据加载时必须加载进入指定的分区中。如下:

load data inpath '/user/hadoop/data' into student_info partition(province = 'sichuan', city = 'chengdu')

二、删除表

drop table if exists test;

三、修改表

alter table意味着改子句仅仅修改表的元数据,而不会修改本身的数据。
1、表重命名

alter table test rename to test2

2、增加、修改、删除分区

增加分区(通常是外部表):

alter table test add partition(x=x1, y=y2) location '/user/test/x1/y1'

修改分区

alter table test add partition(x=x1, y=y2) set location '/user/test/x1/y1'

该命令修改已存在的分区路径

删除分区

alter table test add drop partition(x=x1, y=y2)

3、修改列信息

用户可以对某个字段(列)进行重命名,并修改其数据类型、注释、在表中的位置:

alter table test
change column id uid int
comment 'the unique id'
after name;

本例子是将test表中的id字段重命名为uid, 并指定其类型为int(即使类型和原来一样,也需要重新指定),并注释为the unique id,最后在将该字段移动到name字段会后。

4、增加列

alter table test add columns (new_col int, new_col2 string);

5、删除或者替换列

alter table test replace columns (new_col int, new_col2 string);

改命令删除了test表的所有列并重新定义了字段,由于只是改变了元数据,表数据并不会因此而丢失或改变。
注意:alter table只是修改了表的元数据,所以一定要保证表的数据和修改后的元数据模式要匹配,否则数据将会变得不可用。

猜你喜欢

转载自blog.csdn.net/l1028386804/article/details/82469740