1.建表语法
建表
create [external] table [if not exists] table_name
[(col_name data_type [comment col_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]
[row format row_format] row format delimited fields terminated by “分隔符”
[stored AS file_format]
[location hdfs_path]
例子:
1,zs,12.f
2,ls,14,m
3,ww,16,f
create external table tb_user3 (id String ,
name String,
age int,
gender String
)
row format delimited fields terminated by ','
location 'hdfs://linux01:8020/data/log/' ;
2.内部表和外部表
1) external 外部表
2) 默认的 内部表 管理表 managertable
问? 什么是内部表 什么是外部表 ? 什么时候使用内部表 什么时候使用外部表
1 公共/原始数据 使用外部表
2 业务有关的业务报表使用管理表(内部表)
内部表和外部表的区别:
create table tb_log2(
id int ,
name string
)
row format delimited fields terminated by ","
location '/data/log/' ; -- 管理表 ,路径默认本地为hdfs://linux01:8020drop table tb_log2 ; -- 内部表:指定的路径下的数据会被删除
select * from tb_log ; --没有数据create external table tb_log2( --外部表
id int ,
name string
)
row format delimited fields terminated by ","
location '/data/log/' ;drop table tb_log2 ; -- 外部表:指定的路径下的数据不会被删除
select * from tb_log ; --有数据 没有收别的表影响